Lab Assignment-5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

EXPERIMENT-3

1. Try Linear Layout that contain 4 buttons as UI Components with following attribute
properties set –
Layout :android: divider , android : orientation , android: layout_width ,
android:layout_height
Button : android: layout_width , android:layout_height , android: inputType ,
android:marginTop ,android :text
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:divider="?android:attr/listDivider"
android:dividerPadding="12dip"
android:showDividers="middle">

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_gravity="center_horizontal"
android:text="@string/linear_layout_without_weight"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#009300"
android:text="Button 1"
android:textColor="@color/white"
android:inputType="text"
android:layout_marginTop="20dp"
tools:ignore="TextContrastCheck" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFEB3B"
android:text="Button 2"
android:textColor="@color/white"
android:inputType="text"
android:layout_marginTop="20dp"
tools:ignore="TextContrastCheck" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#0472f9"
android:text="Button 3"
android:textColor="@color/white"
android:inputType="text"
android:layout_marginTop="20dp"
tools:ignore="TextContrastCheck" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#e100d5"
android:text="Button 4"
android:inputType="text"
android:textColor="@color/white"
android:layout_marginTop="20dp"
tools:ignore="TextContrastCheck" />
</LinearLayout>

MainActivity.java
package com.example.linearlayout;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
2. Using Relative Layout Design a form that inputs first name , last name ,
Gender( using
radioButton ) and Date of Birth (Using Date Picker ) with following attribute properties
set –
Layout :android: layout_alignParentRight , android : layout_alignParentLeft, android:
layout_alignParentBottom, android: layout_alignParentTop

RadioButton :android: layout_width , android:layout_height , android: text ,


android:checked , android:textSize, android :textColor
EditText: android: layout_width , android:layout_height , android: editable ,
android:text ,
Android:background
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/form"
android:id="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_alignParentRight="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="128dp"
android:text="@string/firstname"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/firstName"
android:layout_margin="30dp"
android:text="@string/lastname"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />

<EditText
android:id="@+id/edt_firstName"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp"
android:inputType="text"
android:layout_toRightOf="@+id/firstName"
android:hint="@string/first_name"
android:layout_marginStart="20dp"
android:layout_toEndOf="@+id/firstName" />
<EditText
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_below="@+id/edt_firstName"
android:layout_centerVertical="true"
android:inputType="text"
android:layout_marginLeft="0dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@+id/lastName"
android:hint="@string/last_name"
android:layout_marginStart="0dp"
android:layout_toEndOf="@+id/lastName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lastName"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="8dp"
android:text="Gender:"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup

android:layout_width="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="18dp"
android:layout_height="wrap_content"
android:layout_below="@id/lastName"
>

<RadioButton
android:id="@+id/johnCena"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="female"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

<RadioButton
android:id="@+id/randyOrton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="male"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lastName"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="140dp"
android:text="Date of Birth:"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/date"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="@color/black"
android:hint="Select Date"
android:layout_marginTop="430dp"
android:padding="15dp"

android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_centerHorizontal="true"
android:layout_marginTop="500dp"
android:background="#03B424"
android:text="Submit"
android:textColor="#ffffff"
android:textStyle="bold" />

</RelativeLayout>

MainActivity.java
package com.example.relativelayout;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


EditText date;
DatePickerDialog datePickerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date = (EditText) findViewById(R.id.date);
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calender class's instance and get current date , month and year
from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
{
// set day of month , month and year value in the
edit text
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);

}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});

}
3. Try Table Layout and design following using EditText
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:stretchColumns="2"
>

<TableRow
android:layout_height="69dp"
android:padding="0dp">

<TextView
android:layout_height="66dp"
android:layout_marginBottom="0dp"
android:layout_span="3"
android:background="#2196F3"
android:gravity="center_horizontal"
android:text="This is Row1"
android:textColor="@color/white"
android:textSize="25sp"
android:textAlignment="center"
android:textStyle="bold" />

</TableRow>
<TableRow>

<EditText
android:layout_height="66dp"
android:layout_column="0"

android:hint="Row2 Column0"
android:background="@color/teal_200"
android:paddingRight="2dp"
android:padding="5dp"
android:textColorHint="@color/white"
/>

<EditText

android:layout_height="66dp"
android:layout_column="1"

android:background="@color/purple_500"
android:paddingLeft="2dp"
android:hint="Row2 Column1"
android:padding="5dp"
android:textColorHint="@color/white" />
<EditText

android:layout_height="66dp"
android:layout_column="2"

android:background="@color/teal_200"
android:hint="Row2 Column2"
android:padding="5dp"
android:paddingLeft="4dp"
android:textColorHint="@color/white" />
</TableRow>
<TableRow>

<EditText
android:layout_height="66dp"
android:layout_column="0"
android:layout_marginLeft="0dp"
android:hint="Row3 Column0"
android:background="@color/purple_200"
android:paddingRight="2dp"
android:padding="5dp"
android:textColorHint="@color/white"
/>

<EditText

android:layout_height="66dp"
android:layout_column="1"

android:background="@color/teal_200"
android:paddingLeft="2dp"
android:hint="Row3 Column1"
android:padding="5dp"
android:textColorHint="@color/white" />
<EditText

android:layout_height="66dp"
android:layout_column="2"

android:background="@color/purple_200"
android:hint="Row3 Column2"
android:padding="5dp"
android:paddingLeft="4dp"
android:textColorHint="@color/white" />
</TableRow>
<TableRow>

<EditText
android:layout_width="wrap_content"
android:layout_height="66dp"
android:layout_column="0"
android:layout_span="1"
android:background="@color/teal_200"
android:hint="Row4 Column0"
android:padding="5dp"
android:paddingRight="2dp"
android:textColorHint="@color/white" />

<EditText

android:layout_width="wrap_content"
android:layout_height="66dp"

android:layout_column="1"
android:layout_span="2"
android:background="@color/purple_500"
android:hint="Row4 Column1"
android:padding="5dp"
android:paddingStart="2dp"
android:paddingLeft="2dp"
android:textColorHint="@color/white" />

</TableRow>
<TableRow
android:layout_height="69dp"
android:padding="0dp">

<TextView
android:layout_height="66dp"
android:layout_marginBottom="0dp"
android:layout_span="3"
android:background="#2196F3"
android:gravity="center_horizontal"
android:text="This is Row5"
android:textColor="@color/white"
android:textSize="25sp"
android:textAlignment="center"
android:textStyle="bold" />

</TableRow>

</TableLayout>

MainActivity.java
package com.example.tablelayout;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
5. Create a Login page for Online Shopping using any learned Layouts
a. Create TextView for the title
b. Add two EditText for username and password
c. Add Login button
d. Create checkbox
e. Create Radio button
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF9800"
android:orientation="vertical"
android:stretchColumns="1">

<TableRow android:padding="5dip">

<TextView
android:layout_height="39dp"
android:layout_marginBottom="20dp"
android:layout_span="2"
android:background="@color/purple_200"
android:gravity="center_horizontal"
android:text="@string/loginForm"
android:textColor="#004D40"
android:textSize="25sp"
android:textStyle="bold" />
</TableRow>

<TableRow>

<TextView
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:text="@string/userName"
android:textColor="#fff"
android:textSize="16sp" />

<EditText
android:id="@+id/userName"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginLeft="10dp"
android:background="#fff"
android:hint="@string/userName"
android:padding="5dp"
android:textColor="#000"
tools:ignore="TouchTargetSizeCheck" />
</TableRow>

<TableRow>

<TextView
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="@string/password"
android:textColor="#fff"
android:textSize="16sp" />

<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="#fff"
android:hint="@string/password"
android:minHeight="48dp"
android:padding="5dp"
android:textColor="#000" />
</TableRow>

<TableRow android:layout_marginTop="20dp">

<Button
android:id="@+id/loginBtn"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_span="2"
android:background="#0ff"
android:text="@string/login"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>

MainActivity.java
package com.example.onlinelogin;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
}
Experiment-5
1. Create a calculator App with buttons for each operation
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relative1"
tools:context=".MainActivity">
<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/button4"
android:layout_alignRight="@+id/button4"
android:layout_below="@+id/edt1"
android:layout_marginTop="94dp"
android:text="1" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3"
android:text="2" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_centerHorizontal="true"
android:text="3" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_toLeftOf="@+id/button2"
android:text="4" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button4"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:text="5" />
<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_alignStart="@+id/button3"
android:layout_below="@+id/button3"
android:text="6" />
<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:layout_toLeftOf="@+id/button2"
android:text="7" />
<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_alignStart="@+id/button5"
android:layout_below="@+id/button5"
android:text="8" />
<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button6"
android:layout_alignStart="@+id/button6"
android:layout_below="@+id/button6"
android:text="9" />
<Button
android:id="@+id/buttonadd"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/edt1"
android:layout_alignRight="@+id/edt1"
android:layout_alignTop="@+id/button3"
android:layout_marginLeft="46dp"
android:layout_marginStart="46dp"
android:layout_toRightOf="@+id/button3"
android:text="+" />
<Button
android:id="@+id/buttonsub"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttonadd"
android:layout_alignLeft="@+id/buttonadd"
android:layout_alignRight="@+id/buttonadd"
android:layout_alignStart="@+id/buttonadd"
android:layout_below="@+id/buttonadd"
android:text="-" />
<Button
android:id="@+id/buttonmul"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonsub"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/buttonsub"
android:layout_below="@+id/buttonsub"
android:text="*" />
<Button
android:id="@+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button7"
android:layout_toLeftOf="@+id/button2"
android:text="." />
<Button
android:id="@+id/button0"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button8"
android:layout_alignStart="@+id/button8"
android:layout_below="@+id/button8"
android:text="0" />
<Button
android:id="@+id/buttonC"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button9"
android:layout_alignStart="@+id/button9"
android:layout_below="@+id/button9"
android:text="C" />
<Button
android:id="@+id/buttondiv"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttonmul"
android:layout_alignLeft="@+id/buttonmul"
android:layout_alignRight="@+id/buttonmul"
android:layout_alignStart="@+id/buttonmul"
android:layout_below="@+id/buttonmul"
android:text="/" />
<Button
android:id="@+id/buttoneql"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttondiv"
android:layout_alignLeft="@+id/button10"
android:layout_alignRight="@+id/buttondiv"
android:layout_alignStart="@+id/button10"
android:layout_below="@+id/button0"
android:layout_marginTop="37dp"
android:text="=" />

</RelativeLayout>

MainActivity.java
package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


Button button0, button1, button2, button3, button4, button5, button6,
button7, button8, button9, buttonAdd, buttonSub, buttonDivision,
buttonMul, button10, buttonC, buttonEqual;
EditText calculatorEditText;
float mValueOne, mValueTwo;
boolean calculatorAddition, mSubtract, calculatorMultiplication,
calculatorDivision;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
button10 = (Button) findViewById(R.id.button10);
buttonAdd = (Button) findViewById(R.id.buttonadd);
buttonSub = (Button) findViewById(R.id.buttonsub);
buttonMul = (Button) findViewById(R.id.buttonmul);
buttonDivision = (Button) findViewById(R.id.buttondiv);
buttonC = (Button) findViewById(R.id.buttonC);
buttonEqual = (Button) findViewById(R.id.buttoneql);
calculatorEditText = (EditText) findViewById(R.id.edt1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + "1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + "2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + "3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + "4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + "5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + "6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + "7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + "8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + "9");
}
});
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + "0");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (calculatorEditText == null) {
calculatorEditText.setText("");
} else {
mValueOne = Float.parseFloat(calculatorEditText.getText() +
"");
calculatorAddition = true;
calculatorEditText.setText(null);
}
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne = Float.parseFloat(calculatorEditText.getText() + "");
mSubtract = true;
calculatorEditText.setText(null);
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne = Float.parseFloat(calculatorEditText.getText() + "");
calculatorMultiplication = true;
calculatorEditText.setText(null);
}
});
buttonDivision.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne = Float.parseFloat(calculatorEditText.getText() + "");
calculatorDivision = true;
calculatorEditText.setText(null);
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueTwo = Float.parseFloat(calculatorEditText.getText() + "");
if (calculatorAddition == true) {
calculatorEditText.setText(mValueOne + mValueTwo + "");
calculatorAddition = false;
}
if (mSubtract == true) {
calculatorEditText.setText(mValueOne - mValueTwo + "");
mSubtract = false;
}
if (calculatorMultiplication == true) {
calculatorEditText.setText(mValueOne * mValueTwo + "");
calculatorMultiplication = false;
}
if (calculatorDivision == true) {
calculatorEditText.setText(mValueOne / mValueTwo + "");
calculatorDivision = false;
}
}
});
buttonC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText("");
}
});
button10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculatorEditText.setText(calculatorEditText.getText() + ".");
}
});

}
}
Experiment8 (Activities and Intents)
1. Create an application with 2 activities and study about <intent-
filter> in Manifest File
and alternatively make each activity main and launcher one by one at
a time.
2. Use a Button to make intent and move between various activities.
activity_mail.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="1dp"
android:paddingBottom="1dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="If you click on Explicit example we will navigate to second
activity within App and if you click on Implicit example android studio homepage
will open in Browser"
android:id="@+id/textView2"
android:clickable="false"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp"
android:background="#3e7d02"
android:textColor="#ffffff"
android:layout_alignParentLeft="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explicit Intent Example"
android:id="@+id/explicit_Intent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="147dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implicit Intent Example"
android:id="@+id/implicit_Intent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>

MainActivity.java
package com.example.intent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button explicit_btn, implicit_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
explicit_btn = (Button)findViewById(R.id.explicit_Intent);
implicit_btn = (Button) findViewById(R.id.implicit_Intent);
explicit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(getBaseContext(), SecondActivity.class);


startActivity(intent);

}
});
implicit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(Intent.ACTION_VIEW);


intent.setData(Uri.parse("https://www.googleadservices.com/pagead/
aclk?
sa=L&ai=DChcSEwim8aDohZf7AhXPmWYCHQmqB3AYABAAGgJzbQ&ohost=www.google.com&cid=CAESau
D2_rBqMt81PKytf8-S-
7fXxjKzijI9mcTyeYaHWq7MQuaLLIsZ8esgX5H30DRzEIsejMxeVlagEEDa_LdgsnR1hNDhjK9FzNb1WWKI
f2Z_BLReP1INGN_6YcS1UDC_Xk-Yy2eCC1C-d1I&sig=AOD64_1WmlbGi5-
qXRxVEYsxUuj0ugXTaA&q&adurl&ved=2ahUKEwig3JrohZf7AhVW-zgGHe7UCe8Q0Qx6BAgBEAE"));
startActivity(intent);
}
});

}
}

activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:background="#CCEEAA"
tools:context="com.example.intent.SecondActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="This is Second Activity"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

SecondActivity.java
package com.example.intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

Toast.makeText(getApplicationContext(), "We are moved to second


Activity",Toast.LENGTH_LONG).show();
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Intent"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity android:name=".SecondActivity" >

</activity>
</application>

</manifest>
3. Design a Login Page that forwards user to new activity using intent
on successful login.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9C27B0"
android:orientation="vertical"
android:stretchColumns="1">

<TableRow android:padding="5dip">

<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_span="2"
android:gravity="center_horizontal"
android:text="loginForm"
android:textColor="#0ff"
android:textSize="25sp"
android:textStyle="bold" />
</TableRow>

<TableRow>

<TextView
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:text="userName"
android:textColor="#fff"
android:textSize="16sp" />

<EditText
android:id="@+id/userName"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginLeft="10dp"
android:background="#fff"
android:hint="userName"
android:padding="5dp"
android:textColor="#E91E63"
android:textColorHint="#757575"
tools:ignore="TouchTargetSizeCheck" />
</TableRow>

<TableRow>

<TextView
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="password"
android:textColor="#fff"
android:textSize="16sp" />

<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="#fff"
android:hint="password"
android:minHeight="48dp"
android:padding="5dp"
android:textColor="#000"
android:textColorHint="#757575" />
</TableRow>

<TableRow android:layout_marginTop="20dp">

<Button
android:id="@+id/loginBtn"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_span="2"
android:background="#0ff"
android:text="login"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>

You might also like