Practical No 9 To ..
Practical No 9 To ..
Manifest code:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<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.AndriodDevelopment"
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>
</application>
</manifest>
Xml code:-
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:layout_marginTop="230dp"
android:layout_marginLeft="150dp"
android:id="@+id/tb1"
/>
</RelativeLayout>
Java code:-
package com.example.android;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb = (ToggleButton) findViewById(R.id.tb1);
blueAdp = BluetoothAdapter.getDefaultAdapter();
tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (tb.isChecked()) {
if(!blueAdp.isEnabled()){
blueAdp.enable();
Toast.makeText(getApplicationContext(), "Bluetooth is ON", Toast.LENGTH_SHORT).show();
}
} else {
blueAdp.disable();
Toast.makeText(getApplicationContext(), "Bluetooth is OFF", Toast.LENGTH_SHORT).show();
}
}
});
}}
Output:-
Practical No. 10: Develop a program to implement login window using above UI
controls
X. Exercise.
1. Write a program to create a login form for a social networking site.
Manifest code:-
<?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.AndriodDevelopment"
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>
</application>
</manifest>
Xml code:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:background="@color/white"
tools:context=".MainActivity">
<ImageView
android:layout_width="410dp"
android:layout_height="200dp"
android:layout_marginTop="100dp"
android:src="@drawable/fb"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:inputType="textPersonName"
android:hint="Email or Phone number"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:inputType="textPassword"
android:hint="Password"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Login"
android:id="@+id/btn"
android:backgroundTint="@color/material_dynamic_primary50"
/>
</LinearLayout>
Java code:-
package com.example.android;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Xml code:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Regestration"
android:layout_gravity="center"
android:textSize="50dp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_marginTop="40dp"
android:hint="Name"
android:inputType="textPersonName"
android:minHeight="48dp" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Surname"
android:inputType="textPersonName"
android:minHeight="48dp" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:minHeight="48dp" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Regester now"
android:id="@+id/btn"
/>
</LinearLayout>
Java code:-
package com.example.andrioddevelopment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Output:-
Practical No. 11 Develop a program to implement Checkbox.
X. Exercise.
1. Write a program to show five checkboxes and toast selected checkboxes.
Manifest code:-
<?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.AndriodDevelopment"
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>
</application>
</manifest>
Xml code:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="100dp"
android:text="Python"
android:onClick="onCheckboxClick"
android:id="@+id/cb1"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="C programming"
android:onClick="onCheckboxClick"
android:id="@+id/cb2"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="C++"
android:onClick="onCheckboxClick"
android:id="@+id/cb3"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Java"
android:onClick="onCheckboxClick"
android:id="@+id/cb4"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Android"
android:onClick="onCheckboxClick"
android:id="@+id/cb5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/btn"
android:layout_marginTop="10dp"
android:layout_marginLeft="150dp"
/>
</LinearLayout>
Java code:-
package com.example.andrioddevelopment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
}
public void onCheckboxClick(View view){
Boolean check = ((CheckBox)view).isChecked();
String select="";
switch (view.getId()){
case R.id.cb1:
select=check?"Python Selected":"Python deselected";
break;
case R.id.cb2:
select=check?"C programming Selected":"C programming deselected";
break;
case R.id.cb3:
select=check?"C++ Selected":"C++ deselected";
break;
case R.id.cb4:
select=check?"Java Selected":"Java deselected";
break;
case R.id.cb5:
select=check?"Android Selected":"Android deselected";
break;
}
Toast.makeText(getApplicationContext(), select, Toast.LENGTH_SHORT).show();
}
}
Output:-
Practical No. 12: Develop a program to implement Radio Button and Radio
Group.
X. Exercise.
1. Write the program to show the following output. First two radio buttons are without taking radio group and
next two are with using radio group. Note the changes between this two. Also toast which radio button has
been selected.
Manifest code:-
<?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.AndriodDevelopment"
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>
</application>
</manifest>
Xml code:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_marginTop="80dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="single radio button"
android:gravity="center"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio button1"
android:layout_marginLeft="80dp"
android:onClick="onButtonClick"
android:id="@+id/srb1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio button2"
android:layout_marginLeft="80dp"
android:onClick="onButtonClick"
android:id="@+id/srb2"
/>
<TextView
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="grouped radiio button"
android:gravity="center"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:onClick="onButtonClick"
android:id="@+id/male"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClick"
android:text="female"
android:id="@+id/female"
/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="Selected"
android:id="@+id/btn"
/>
</LinearLayout>
Java code:-
package com.example.andrioddevelopment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
Xml code:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="200dp" />
</LinearLayout>
Java code:-
package com.example.andrioddevelopment;
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);
}
}
Output:-
Xml code:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Start downloading file"
android:layout_gravity="center"
android:layout_marginTop="30dp"/>
</LinearLayout>
LayoutForProgress.xml:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/custom_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<ProgressBar
android:id="@+id/prog"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="13dp" />
<TextView
android:layout_marginTop="20dp"
android:id="@+id/percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00%"
android:textColor="@color/black"
app:layout_constraintEnd_toStartOf="@+id/percent2"
app:layout_constraintHorizontal_bias="0.175"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/prog" />
<TextView
android:layout_marginTop="20dp"
android:id="@+id/percent2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="28dp"
android:text="00/100"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/prog" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java code:-
package com.example.unitconverterapp;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
Button btn;
ProgressBar prgs;
TextView txt1,txt2;
private Handler hdlr = new Handler();
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflat=getLayoutInflater();
View
layout=inflat.inflate(R.layout.layoutforprogress,(ViewGroup)findViewById(R.id.custom_progress));
prgs=(ProgressBar) layout.findViewById(R.id.prog);
txt1=(TextView) layout.findViewById(R.id.percent);
txt2=(TextView) layout.findViewById(R.id.percent2);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Downloading...").setView(layout);
AlertDialog dialog=builder.create();
dialog.show();
i=prgs.getProgress();
new Thread(new Runnable() {
@Override
public void run() {
while (i<100){
i+=2;
hdlr.post(new Runnable() {
@Override
public void run() {
prgs.setProgress(i);
txt1.setText(i+"%");
txt2.setText(i+"/100");
}
});
try {
Thread.sleep(100);
}catch (Exception e){
e.printStackTrace();
}
}
dialog.dismiss();
}
}).start();
}
});
}
}
Output:-
Practical No. 14: Develop a program to implement List View, GridView, Image View and Scroll
View.
Exercise
(Use blank space provide for answers or attached more pages if needed)
1. Write a program to show the following output.
Use appropriate view for the same.
Manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.unitconverterapp">
<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.UnitConverterApp"
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>
</activity>
</application>
</manifest>
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="400dp"
/>
</LinearLayout>
MainActivity.java
package com.example.unitconverterapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
private ListView listview;
private ArrayAdapter array1;
private String[]
data={"android","java","php","hadoop","sap","python","ajax","C++","ruby","rails","nodejs"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview= (ListView) findViewById(R.id.listview);
array1=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);
listview.setAdapter(array1);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String selected1=(String) adapterView.getItemAtPosition(i);
Toast.makeText(getApplicationContext(),selected1, Toast.LENGTH_SHORT).show();
}
});
}
}
Output:
2. Write a program to display an image using Image View and a button as “Change Image”. Once you click on
button another image should get displayed.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.unitconverterapp">
<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.UnitConverterApp"
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>
</activity>
</application>
</manifest>
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@drawable/eren"
/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="30dp"
android:text="Click to change image"
/>
</LinearLayout>
MainActivity.java
package com.example.unitconverterapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button btn;
private ImageView imgV;
private int count=0;
private String[] imgName={"Gojo","Itachi","Luffy","Eren"};
private int[] imgs={R.drawable.gojo,R.drawable.itachi,R.drawable.luffy,R.drawable.eren};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgV=(ImageView)findViewById(R.id.imageview);
btn= (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
if(count==imgs.length){
count=0;
}
imgV.setImageResource(imgs[count]);
Toast.makeText(MainActivity.this, imgName[count]+" is goat🐐🔥",
Toast.LENGTH_SHORT).show();
count+=1;
}
});
}
}
Output:
3. Write a program to display 15 buttons using grid view.
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.unitconverterapp">
<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.UnitConverterApp"
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>
</activity>
</application>
</manifest>
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="110dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
buttonAdapter.class
package com.example.unitconverterapp;
import android.content.Context;
import android.widget.*;
import android.view.*;
public class buttonAdapter extends BaseAdapter{
private Context mContext;
public buttonAdapter(Context c){
mContext=c;
}
@Override
public int getCount() {
return btnName.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Button btn=new Button(mContext);
btn.setLayoutParams(new GridView.LayoutParams(280,200));
btn.setText(String.format("Button %s", btnName[i]));
btn.setPadding(8,3,3,8);
return btn;
}
public String[] btnName={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
}
MainActivity.class
package com.example.unitconverterapp;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
private GridView gridV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gv = (GridView) findViewById(R.id.gridview);
gv.setAdapter(new buttonAdapter(this));
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "Image Position: " + position, Toast.LENGTH_SHORT).show();
}
});
}}
Output
4. Write a program to display a text view using vertical scroll view.
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.unitconverterapp">
<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.UnitConverterApp"
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>
</activity>
</application>
</manifest>
acticity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fillViewport="false">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 1" />
<TextView android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 2" />
<TextView android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 3" />
<TextView android:id="@+id/t4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 4" />
<TextView android:id="@+id/t5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 5" />
<TextView android:id="@+id/t6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 6" />
<TextView android:id="@+id/t7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 7" />
<TextView android:id="@+id/t8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 8" />
<TextView android:id="@+id/t9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 9" />
<TextView android:id="@+id/t10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 10" />
<TextView android:id="@+id/t11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="Text view 11" />
</LinearLayout>>
</ScrollView>
MainActivity.java
package com.example.unitconverterapp;
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);
}}
Output:
Practical No. 15: Develop a program to implement Custom ToastAlert.
Exercise
1. Write a program to display following toast message.
Manifest.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"
package="com.example.unitconverterapp">
<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.UnitConverterApp"
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>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lay"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="Hello world, Toast example"/>
<Button android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="Show Toast"/>
</LinearLayout>
customtoast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_armin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"
android:background="@color/cardview_dark_background">
<TextView android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message for you:"
android:textSize="22dp"
android:textStyle="bold"
android:textColor="@color/white"/>
<TextView android:id="@+id/footer"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="You have got mail... 🙋♂📧📫"
android:textColor="@color/white"/>
</LinearLayout>
MainActivity.java
package com.example.unitconverterapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.btn);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
LayoutInflater inflat=getLayoutInflater();
View
layout=inflat.inflate(R.layout.customtoast,(ViewGroup)findViewById(R.id.custom_toast_armin));
Toast armin=new Toast(getApplicationContext());
armin.setGravity(Gravity.CENTER_VERTICAL,0,100);
armin.setDuration(Toast.LENGTH_LONG);
armin.setView(layout);
armin.show();
}
});
}
}
Output:
2. Write a program to display three checkboxes and one button named “Order” as shown below.
Once you click the button it should toast different selected checkboxes along with items individual and
total price.
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.unitconverterapp">
<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.UnitConverterApp"
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>
</activity>
</application>
</manifest>
activity_mian.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<CheckBox android:id="@+id/item1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="italic"
android:text="Pizza"
android:layout_gravity="center"/>
<CheckBox android:id="@+id/item2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="italic"
android:text="Coffee"
android:layout_gravity="center"/>
<CheckBox android:id="@+id/item3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="italic"
android:text="Burger"
android:layout_gravity="center"/>
<CheckBox android:id="@+id/item4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="italic"
android:text="Ramen"
android:layout_gravity="center"/>
<Button android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Order"
android:layout_marginTop="100dp"
android:layout_gravity="center"/>
</LinearLayout>
MainActivity.java
package com.example.unitconverterapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
Button btn1;
CheckBox ch1,ch2,ch3,ch4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.btn);
ch1=(CheckBox) findViewById(R.id.item1);
ch2=(CheckBox) findViewById(R.id.item2);
ch3=(CheckBox) findViewById(R.id.item3);
ch4=(CheckBox) findViewById(R.id.item4);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String message="Selected Items:";
int total=0;
if(ch1.isChecked()){
message+="\nPizza 275rs";
total+=275;
}
if(ch2.isChecked()){
message+="\nCoffee 50rs";
total+=50;
}
if(ch3.isChecked()){
message+="\nBurger 100rs";
total+=100;
}
if(ch4.isChecked()){
message+="\nRamen 425rs";
total+=425;
}
message+="\nTotal: "+total+"💸";
Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show();
}
});
}
}
Output:
Practical No. 16: Develop a program to implement Custom
ToastAlert.
Exercise
1. Write a program to display following output. Use TimePicker with Spinnermode.
Manifest.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"
package="com.example.unitconverterapp">
<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.UnitConverterApp"
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>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fillViewport="false">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TimePicker android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:timePickerMode="spinner"/>
<TimePicker android:id="@+id/time24"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:timePickerMode="spinner"/>
</LinearLayout>
</ScrollView>
MainActivity.java
package com.example.unitconverterapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
TimePicker tp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tp=(TimePicker)findViewById(R.id.time24) ;
tp.setIs24HourView(true);
}
}
Output:
2. Write a program to display following output. Select and display date and time onclick of “select
date”, “select time” buttons respectively.
Manifest.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"
package="com.example.unitconverterapp">
<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.prac16"
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>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/in_date"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="82dp"
android:layout_x="12dp"
android:layout_y="145dp" />
<Button
android:id="@+id/btn_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="229dp"
android:layout_y="146dp"
android:text="SELECT DATE" />
<EditText
android:id="@+id/in_time"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/in_date"
android:layout_x="10dp"
android:layout_y="79dp" />
<Button
android:id="@+id/btn_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="228dp"
android:layout_y="79dp"
android:text="SELECT TIME" />
</AbsoluteLayout>
MainActivity.java
package com.example.prac16;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.util.Calendar;
Output
Practical No. 17: Develop a program to create an activity
X. Exercise.
1. Write a program to create a HelloWorld Activity using all lifecycles methods to
display messages using Log.d.
Manifest file:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.unitconverterapp">
<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.prac16"
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>
</activity>
</application>
</manifest>
XML file:-
<?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"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="260dp"
android:text="Hello World"
/>
</RelativeLayout>
Java file:-
package com.example.andrioddevelopment;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Activity Cycle","onCrate Called");
}
@Override
protected void onStart() {
super.onStart();
Log.d("Activity Cycle", "onStart Called");
}
@Override
protected void onStop() {
super.onStop();
Log.d("Activity Cycle","onStop Called");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("Activity Cycle","onRestart Called");
}
@Override
protected void onResume() {
super.onResume();
Log.d("Activity Cycle","onResume Called");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("Activity Cycle","onDestroy Called");
}
@Override
protected void onPause() {
super.onPause();
Log.d("Activity Cycle","onPause Called");
}
}
Output:-
Practical No.18: Develop a program to implement new activity using explicit intent
and implicit intent.
X. Exercise.
1. Write a program to create a text field and a button “Navigate”. When you enter “www.google.com” and press
navigate button it should open google page.
Manifest file:-
<?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.MobileApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
XML file:-
<?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"
tools:context=".MainActivity">
<EditText
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="260dp"
android:ems="9"
/>
<Button
android:id="@+id/navigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate"
android:layout_centerHorizontal="true"
android:layout_marginTop="320dp"
/>
</RelativeLayout>
JAVA file:-
package com.example.mobileapp;
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;
import android.widget.EditText;
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
XML file:-
<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/startDial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Dialer"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp"
/>
</RelativeLayout>
Java file:-
package com.example.mobileapp;
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;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.startDial);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}
});
}
}
Output:-
3. Write a program to create two screens. First screen will take one number input from user. After click on
Factorial button, second screen will open and it should display factorial of the same number. Also specify which
type of intent you will use in this case.
Manifest File:-
Activity.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"
tools:context=".MainActivity">
<EditText
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="9"
android:layout_centerHorizontal="true"
android:layout_marginTop="250dp"
android:inputType="number"
/>
<Button
android:id="@+id/facto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Factorial"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp"
/>
</RelativeLayout>
Result.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:text="This result is displayed on another activity by using Explicit Intent"
android:textSize="15dp"
android:layout_marginTop="100dp"
/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:layout_marginLeft="100dp"
/>
</RelativeLayout>
MainActivity.java:-
package com.example.mobileapp;
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;
import android.widget.EditText;
Result.java:-
package com.example.mobileapp;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
Output:-