0% found this document useful (0 votes)
42 views46 pages

Android UI Components: AutoComplete & Toggle

Uploaded by

piyushkarnale68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views46 pages

Android UI Components: AutoComplete & Toggle

Uploaded by

piyushkarnale68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Develop a program to implement Auto Complete Text View

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView2"
android:layout_width="292dp"
android:layout_height="29dp"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="example_autocompletetextview"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="73dp" />

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_marginLeft="107dp"
android:layout_marginTop="32dp"
android:ems="10"
tools:layout_editor_absoluteX="82dp"
tools:layout_editor_absoluteY="126dp" />

</RelativeLayout>
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends Activity {


AutoCompleteTextView autocomplete;

String[] arr = { "Paries,France", "PA,United States","Parana,Brazil",


"Padua,Italy", "Pasadena,CA,United States"};

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

autocomplete = (AutoCompleteTextView)
findViewById([Link].autoCompleteTextView1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>


(this,[Link].select_dialog_item, arr);

[Link](2);
[Link](adapter);
}
}
2. Write a program to create a toggle button to display ON/OFF Bluetooth on
the display screen.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="@color/white"
tools:context=".MainActivity">

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="onToggleClick"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/toggleButton"
android:textStyle="bold"
android:textColor="@color/black"/>
</RelativeLayout>
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity


extends AppCompatActivity {

ToggleButton togglebutton;
TextView textview;
@Override
protected void onCreate(
Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);

togglebutton
= (ToggleButton)findViewById(
[Link]);

textview
= (TextView)findViewById(
[Link]);
}

public void onToggleClick(View view)


{
if ([Link]()) {
[Link]("Bluetooth is OFF");
}
else {
[Link]("Bluetooth is ON");
}
}
}
3. Write a program to create a login form for a social networking site.

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Login Form"
android:layout_gravity="center"/>
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
android:padding="8dp"
android:layout_marginTop="16dp"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="8dp"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="18sp"
android:layout_marginTop="16dp"/>
</LinearLayout>
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
EditText editTextUsername = findViewById([Link]);
EditText editTextPassword = findViewById([Link]);
Button buttonLogin = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String username = [Link]().toString();
String password = [Link]().toString();
// Perform authentication
if ([Link]("Ajinkya Patil") &&
[Link]("Social Password")) {
[Link](getApplicationContext(), "Login
successful", Toast.LENGTH_SHORT).show(); }
else {
[Link](getApplicationContext(), "Invalid username
or password", Toast.LENGTH_SHORT).show();
}

}
});
}
}
4. Write a program to show five checkboxes and toast selected checkboxes

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="123dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="145dp"
android:layout_marginTop="257dp"
android:layout_marginEnd="143dp"
android:layout_marginBottom="301dp"
android:text="MAD" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="123dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="235dp"
android:layout_marginTop="73dp"
android:layout_marginEnd="53dp"
android:layout_marginBottom="485dp"
android:text="MGT" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="123dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="237dp"
android:layout_marginTop="174dp"
android:layout_marginEnd="51dp"
android:layout_marginBottom="384dp"
android:text="ETI" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="123dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="52dp"
android:layout_marginTop="73dp"
android:layout_marginEnd="236dp"
android:layout_marginBottom="485dp"
android:text="PWP" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="123dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="53dp"
android:layout_marginTop="174dp"
android:layout_marginEnd="235dp"
android:layout_marginBottom="384dp"
android:text="WBP" />
</RelativeLayout>
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
CheckBox checkBox1, checkBox2, checkBox3, checkBox4, checkBox5;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
checkBox1=findViewById([Link].checkBox1);
checkBox2=findViewById([Link].checkBox2);
checkBox3=findViewById([Link].checkBox3);
checkBox4=findViewById([Link].checkBox4);
checkBox5=findViewById([Link].checkBox5);
[Link](new [Link]() {
@Override
public void onClick(View view) {
if([Link]()){
String getCBData=[Link]().toString();
[Link]([Link], "you clicked
:"+getCBData, Toast.LENGTH_SHORT).show();
}else if(![Link]())
{
String uncheckdata=[Link]().toString();
[Link]([Link], "you unclicked
:"+uncheckdata, Toast.LENGTH_SHORT).show();
}
}
});
[Link](new [Link]()
{
@Override
public void onClick(View view) {
if([Link]()){
String getCBData=[Link]().toString();
[Link]([Link], "you clicked
:"+getCBData, Toast.LENGTH_SHORT).show();
}else if(![Link]()){
String uncheckdata=[Link]().toString();
[Link]([Link], "you unclicked
:"+uncheckdata, Toast.LENGTH_SHORT).show();
}
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
if([Link]()){
String getCBData=[Link]().toString();
[Link]([Link], "you clicked
:"+getCBData, Toast.LENGTH_SHORT).show();
}
else if(![Link]()){
String uncheckdata=[Link]().toString();
[Link]([Link], "you unclicked
:"+uncheckdata, Toast.LENGTH_SHORT).show();
}
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
if([Link]()){
String getCBData=[Link]().toString();
[Link]([Link], "you clicked
:"+getCBData, Toast.LENGTH_SHORT).show(); }
else if(![Link]()){
String uncheckdata=[Link]().toString();
[Link]([Link], "you unclicked
:"+uncheckdata, Toast.LENGTH_SHORT).show(); } } });
[Link](new [Link]() {
@Override
public void onClick(View view) {
if([Link]()){
String getCBData=[Link]().toString();
[Link]([Link], "you clicked
:"+getCBData,
Toast.LENGTH_SHORT).show(); }
else if(![Link]()){
String uncheckdata=[Link]().toString();
[Link]([Link], "you unclicked
:"+uncheckdata, Toast.LENGTH_SHORT).show(); }}});}}
5. Develop a program to implement Radio Button and Radio Group

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#e0e0e0"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your favourite Food Culture :: "
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<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="true"
android:text="Indian"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

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

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

<RadioButton
android:id="@+id/sheamus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="Spanish"
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="Italian"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

</RadioGroup>

<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#0f0"
android:padding="10dp"
android:text="Submit"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />

</LinearLayout>
</LinearLayout>
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends Activity {

RadioButton johnCena, randyOrton, goldBerg, romanReigns, sheamus;


String selectedSuperStar;
Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
johnCena = (RadioButton) findViewById([Link]);
randyOrton = (RadioButton) findViewById([Link]);
goldBerg = (RadioButton) findViewById([Link]);
romanReigns = (RadioButton) findViewById([Link]);
sheamus = (RadioButton) findViewById([Link]);
submit = (Button) findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
if ([Link]()) {
selectedSuperStar = [Link]().toString();
} else if ([Link]()) {
selectedSuperStar = [Link]().toString();
} else if ([Link]()) {
selectedSuperStar = [Link]().toString();
} else if ([Link]()) {
selectedSuperStar = [Link]().toString();
} else if ([Link]()) {
selectedSuperStar = [Link]().toString();
}
[Link](getApplicationContext(),"You
selected"+selectedSuperStar, Toast.LENGTH_LONG).show(); // print the value of
selected super star
}
});
}
}
6. Develop a program to implement Progress Bar

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<!--on below line we are creating


a text for our app-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/idPBLoading"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:gravity="center"
android:padding="10dp"
android:text="Progress Bar in Android"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

<!--on below line we are creating a progress bar-->


<ProgressBar
android:id="@+id/idPBLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/idBtnDisplayProgress"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:visibility="gone" />

<!--on below line we are creating a button-->


<Button
android:id="@+id/idBtnDisplayProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:text="Show Progress Bar"
android:textAllCaps="false" />

</RelativeLayout>
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

// on below line we are creating a variable.


private Button showProgressBtn;
private ProgressBar loadingPB;
boolean isProgressVisible = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
showProgressBtn = findViewById([Link]);
loadingPB = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {

if (isProgressVisible) {

[Link]("Show Progress Bar");

[Link]([Link]);

isProgressVisible = false;
} else {
isProgressVisible = true;

[Link]("Hide Progress Bar");

[Link]([Link]);
}
}
});
}
}
Develop a program to implement List View and Grid View.

List View:
8. Write a program to display an image using Image View and a button named
as “Change Image”. Once you click on button another image should get
displayed

Take two images and add in res>drawable

<?xml version="1.0" encoding="utf-8"?>


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView"
android:layout_width="91dp"
android:layout_height="108dp"
android:layout_x="154dp"
android:layout_y="249dp"
tools:srcCompat="@tools:sample/avatars" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="152dp"
android:layout_y="465dp"
android:text="Change" />
</AbsoluteLayout>

</[Link]>
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


Button b1;
ImageView ig;
public int count=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
b1=findViewById([Link]);
ig=findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View view) {
if(count==0){
[Link]([Link]);
count=1;
}
else if (count==1) {
[Link]([Link]);
count=0;
}
}
});
}
}
9. Write a program to display three checkboxes and one button ’order’ , Once
you Clicked on button it should toast different selected checkboxes along
with items individual and total price.

<?xml version="1.0" encoding="utf-8"?>


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="68dp"
android:layout_x="160dp"
android:layout_y="143dp"
android:text="Pizza"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="28dp"
android:layout_x="160dp"
android:layout_y="230dp"
android:text="Coffee"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="28dp"
android:layout_x="160dp"
android:layout_y="312dp"
android:text="Burger"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox2" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="184dp"
android:layout_x="158dp"
android:layout_y="419dp"
android:text="Order"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox3" />

</AbsoluteLayout>

</[Link]>
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
CheckBox pizza,coffe,burger;
Button buttonOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
//Getting instance of CheckBoxes and Button from the activty_main.xml
file
pizza=(CheckBox)findViewById([Link]);
coffe=(CheckBox)findViewById([Link].checkBox2);
burger=(CheckBox)findViewById([Link].checkBox3);
buttonOrder=(Button)findViewById([Link]);

//Applying the Listener on the Button click


[Link](new [Link](){
@Override
public void onClick(View view) {
int totalamount=0;
StringBuilder result=new StringBuilder();
[Link]("Selected Items:");
if([Link]()){
[Link]("\nPizza 100Rs");
totalamount+=100;
}
if([Link]()){
[Link]("\nCoffe 50Rs");
totalamount+=50;
}
if([Link]()){
[Link]("\nBurger 120Rs");
totalamount+=120;
}
[Link]("\nTotal: "+totalamount+"Rs");
//Displaying the message on the toast
[Link](getApplicationContext(), [Link](),
Toast.LENGTH_LONG).show();
}

});
} }
[Link] a program to implement date and Time Picker
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<DatePicker
android:id="@+id/simpleDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#150"
android:datePickerMode="spinner" />

<Button
android:id="@+id/submitButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/simpleDatePicker"
android:layout_x="116dp"
android:layout_y="311dp"
android:text="submit"/>

<TimePicker
android:id="@+id/timePicker"
android:layout_width="316dp"
android:layout_height="305dp"
android:layout_marginTop="360dp"
android:layout_x="38dp"
android:layout_y="396dp" />

</AbsoluteLayout>

</[Link]>
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

DatePicker simpleDatePicker;
Button submit;
TimePicker t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// initiate the date picker and a button
simpleDatePicker = (DatePicker) findViewById([Link]);
submit = (Button) findViewById([Link]);
// perform click event on submit button
t1=(TimePicker)findViewById([Link]);
t1.setIs24HourView(true);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String day = "Day = " + [Link]();
String month = "Month = " + ([Link]() +
1);
String year = "Year = " + [Link]();
[Link](getApplicationContext(), day + "\n" + month +
"\n" + year, Toast.LENGTH_LONG).show();
}
});

}
[Link] a program to create a Hello World Activity using all lifecycles method
to display messages using log.d.
Now press the back button on the Emulator and exit the App:

Go to Logcat again and scroll down to bottom. You will see 3 more methods were called:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
public String tag="application is:";
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
[Link](this,"on create",Toast.LENGTH_SHORT).show();
Log.d(tag,"Activity is created");
}
@Override
protected void onStart() {
[Link]();
[Link](this,"on start",Toast.LENGTH_SHORT).show();
Log.d(tag,"Started");
}
@Override
protected void onResume() {
[Link]();
Log.d(tag,"Resumed");
}
@Override
protected void onPause() {
[Link]();
Log.d(tag,"Paused");
}
@Override
protected void onStop() {
[Link]();
Log.d(tag,"Stoped");
}
@Override
protected void onDestroy() {

[Link]();
Log.d(tag,"Destroyed");
}
@Override
protected void onRestart() {
[Link]();
Log.d(tag,"Restarted");
}
}
13. Write a program to create a text field and a button ‘Navigate’. When you enter
[Link] and press navigate button it should open google home page.

Xml:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<AbsoluteLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">

<EditText
android:id="@+id/editTextText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="91dp"
android:layout_y="110dp"
android:ems="10"
android:inputType="text"
android:hint="Enter url" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="152dp"
android:layout_y="201dp"
android:text="Navigate" />
</AbsoluteLayout>
</[Link]>
Java
package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


Button b1;
EditText e1;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

b1 = findViewById([Link]);
e1 = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String url = [Link]().toString();
if (![Link]()) {
// Check if the URL is not empty
if (![Link]("[Link] && ![Link]("[Link] {
// If the URL doesn't start with http:// or [Link] add http:// prefix
url = "[Link] + url;
}
Intent implicitIntent = new Intent(Intent.ACTION_VIEW, [Link](url));
startActivity(implicitIntent);
}
}
});
}
}
12. Develop a program to implement new activity using explicit intent and implicit
intent.

Activity_main.Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/explicitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launch Second Activity (Explicit)" />

<Button
android:id="@+id/implicitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/explicitButton"
android:layout_marginTop="16dp"
android:text="Open URL (Implicit)" />

</RelativeLayout>

[Link]:

package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

Button explicitButton = findViewById([Link]);


[Link](new [Link]() {
@Override
public void onClick(View v) {
// Explicit Intent
Intent explicitIntent = new Intent([Link], [Link]);
startActivity(explicitIntent);
}
});

Button implicitButton = findViewById([Link]);


[Link](new [Link]() {
@Override
public void onClick(View v) {
// Implicit Intent
Intent implicitIntent = new Intent(Intent.ACTION_VIEW,
[Link]("[Link]
startActivity(implicitIntent);
}
});
}
}

Manifest:
Add the below tag on the <Application> tag in the manifest file:
<activity android:name=".SecondActivity" />

Create a another xml file with name activity_second.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textSize="24sp"
android:layout_centerInParent="true"/>

</RelativeLayout>

Create a new java file with name [Link]

package [Link];

import [Link];

import [Link];

public class SecondActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
}
}
14. Write a program to create a button “Start Dialer”. When you click on this button it
should open phone dialer.

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/startDialerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Start Dialer" />

</RelativeLayout>

[Link]
package [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


Button b1;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

b1=findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}
});
}
}
15. Write a program to demonstrate any system broadcast message.

[Link]
package [Link];

import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

AirplaneModeChangeReceiver airplaneModeChangeReceiver = new AirplaneModeChangeReceiver();

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}

@Override
protected void onStart() {
[Link]();
IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(airplaneModeChangeReceiver, filter);
}

@Override
protected void onStop() {
[Link]();
unregisterReceiver(airplaneModeChangeReceiver);
}
}

Create a new java file with name [Link]


package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class AirplaneModeChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (isAirplaneModeOn([Link]())) {
[Link](context, "AirPlane mode is on", Toast.LENGTH_SHORT).show();
} else {
[Link](context, "AirPlane mode is off", Toast.LENGTH_SHORT).show();
}
}

private static boolean isAirplaneModeOn(Context context) {


return [Link]([Link](), [Link].AIRPLANE_MODE_ON, 0) != 0;
}
}
16. Develop a program to implement to build camera

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Take a Photo" >
</Button>

<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/button1"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher_background" />

</RelativeLayout>

[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends Activity {


private static final int CAMERA_REQUEST = 1888;
ImageView imageView;
public void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);
setContentView([Link].activity_main);

imageView = (ImageView) [Link]([Link].imageView1);


Button photoButton = (Button) [Link]([Link].button1);

[Link](new [Link]() {

@Override
public void onClick(View v) {
Intent cameraIntent = new Intent([Link].ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {


if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) [Link]().get("data");
[Link](photo);
}
}

}
[Link] a sample application with login module (Check user name and password) on

successful login, Change Text view “Login Successful” and on fail, alert user using toast

“Login Fail”.

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="20dp"
android:hint="Username"
android:inputType="text" />

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/usernameEditText"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:hint="Password"
android:inputType="textPassword" />

<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/passwordEditText"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:text="Login" />

<TextView
android:id="@+id/loginResultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/loginButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text=""
android:textSize="18sp" />

</RelativeLayout>

[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private EditText usernameEditText, passwordEditText;


private Button loginButton;
private TextView loginResultTextView;

// Predefined username and password


private final String USERNAME = "user";
private final String PASSWORD = "password";

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

usernameEditText = findViewById([Link]);
passwordEditText = findViewById([Link]);
loginButton = findViewById([Link]);
loginResultTextView = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
login();
}
});
}

private void login() {


String username = [Link]().toString().trim();
String password = [Link]().toString().trim();

if ([Link](USERNAME) && [Link](PASSWORD)) {


// Login successful
[Link]("Login Successful");
} else {
// Login failed
[Link](this, "Login Fail", Toast.LENGTH_SHORT).show();
}
}
}
[Link] a program to send and receive SMS

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/phoneNumberEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:inputType="phone" />

<EditText
android:id="@+id/messageEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/phoneNumberEditText"
android:hint="Message" />

<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/messageEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Send" />

</RelativeLayout>

[Link]
package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_SEND_SMS = 1;

private EditText phoneNumberEditText;


private EditText messageEditText;
private Button sendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

phoneNumberEditText = findViewById([Link]);
messageEditText = findViewById([Link]);
sendButton = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
sendSMS();
}
});

// Check for SMS permission


if ([Link](this, [Link].SEND_SMS) !=
PackageManager.PERMISSION_GRANTED) {
// Permission not granted, request it
[Link](this, new String[]{[Link].SEND_SMS},
REQUEST_SEND_SMS);
}
}

private void sendSMS() {


String phoneNumber = [Link]().toString();
String message = [Link]().toString();

if ([Link]() || [Link]()) {
[Link](this, "Phone number and message cannot be empty", Toast.LENGTH_SHORT).show();
return;
}

try {
SmsManager smsManager = [Link]();
[Link](phoneNumber, null, message, null, null);
[Link](this, "SMS sent", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
[Link](this, "Failed to send SMS", Toast.LENGTH_SHORT).show();
[Link]();
}
}
}

[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]

<uses-feature
android:name="[Link]"
android:required="false" />

<uses-permission android:name="[Link].SEND_SMS" />


<uses-permission android:name="[Link].RECEIVE_SMS" />

<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/[Link]"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />

<category android:name="[Link]" />


</intent-filter>
</activity>
<receiver android:name=".SmsReceiver"
android:exported="true">>

<intent-filter>
<action android:name="[Link].SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>

Create a new file with name [Link]


package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = [Link]();
if (bundle != null) {
Object[] pdus = (Object[]) [Link]("pdus");
if (pdus != null) {
for (Object pdu : pdus) {
SmsMessage smsMessage = [Link]((byte[]) pdu);
String senderPhoneNumber = [Link]();
String messageBody = [Link]();

// Do something with the received message


[Link](context, "SMS from: " + senderPhoneNumber + "\nMessage: " + messageBody,
Toast.LENGTH_SHORT).show();
}
}
}
}
}
[Link] a program to send email.

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="22dp"
android:layout_marginTop="16dp"
android:ems="10" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="18dp"
android:ems="10" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="28dp"
android:ems="10"
android:inputType="textMultiLine" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="To:" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:text="Subject:" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
android:layout_alignParentLeft="true"
android:text="Message:" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginLeft="76dp"
android:layout_marginTop="20dp"
android:text="Send" />

</RelativeLayout>

[Link]
package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends Activity {


EditText editTextTo,editTextSubject,editTextMessage;
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

editTextTo=(EditText)findViewById([Link].editText1);
editTextSubject=(EditText)findViewById([Link].editText2);
editTextMessage=(EditText)findViewById([Link].editText3);

send=(Button)findViewById([Link].button1);

[Link](new OnClickListener(){

@Override
public void onClick(View arg0) {
String to=[Link]().toString();
String subject=[Link]().toString();
String message=[Link]().toString();

Intent email = new Intent(Intent.ACTION_SEND);


[Link](Intent.EXTRA_EMAIL, new String[]{ to});
[Link](Intent.EXTRA_SUBJECT, subject);
[Link](Intent.EXTRA_TEXT, message);

//need this to prompts email client only


[Link]("message/rfc822");

startActivity([Link](email, "Choose an Email client :"));

});
}

You might also like