0% found this document useful (0 votes)
3 views9 pages

Android Practical

The document contains XML layout and Java code for two Android activities. The first activity implements a radio button selection for food choices, while the second activity features checkboxes for fruit selection and a toggle button. Both activities utilize Toast messages to display user selections and interactions.
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)
3 views9 pages

Android Practical

The document contains XML layout and Java code for two Android activities. The first activity implements a radio button selection for food choices, while the second activity features checkboxes for fruit selection and a toggle button. Both activities utilize Toast messages to display user selections and interactions.
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

Radio Button

[Link]

<?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:gravity="center" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Choice to eat"
android:textSize="26sp" android:textColor="@color/black"/>
<RadioGroup
android:id="@+id/rgroup" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">

<RadioButton
android:id="@+id/rbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Pizza"
android:textSize="16sp" android:textColor="@color/black"/>
<RadioButton
android:id="@+id/rbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Berger" android:textSize="16sp"
android:textColor="@color/black"/>
<RadioButton
android:id="@+id/rbutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sandwich" android:textSize="16sp"
android:textColor="@color/black"/>

</RadioGroup>

</LinearLayout>

[Link]

package [Link];

import [Link]; import

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

public class MainActivity extends AppCompatActivity { RadioGroup


radioGroup;
RadioButton radioButton1,radioButton2,radioButton3;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
radioGroup=findViewById([Link]);
radioButton1=findViewById([Link].rbutton1);
radioButton2=findViewById([Link].rbutton2);
radioButton3=findViewById([Link].rbutton3);
[Link](new
[Link]() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){ case
[Link].rbutton1:

[Link](getApplicationContext(),"You Selected
Pizza",Toast.LENGTH_LONG).show();
break;
case [Link].rbutton2:

[Link](getApplicationContext(),"You Selected
Berger",Toast.LENGTH_LONG).show();
break;
case [Link].rbutton3:

[Link](getApplicationContext(),"You Selected
Sandwich",Toast.LENGTH_LONG).show();
break;
}
}
});

}
}

Output:
Checkbox,ImageButton,ToggleButton

[Link]

<?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:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fruits" android:textSize="20sp"
android:textColor="@color/black"/>
<CheckBox
android:id="@+id/check1" android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/purple_200"
android:text="Apple"/>
<CheckBox
android:id="@+id/check2" android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/purple_200"
android:text="Orange"/>
<CheckBox
android:id="@+id/check3" android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/purple_200" android:text="Mango"/>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" android:textColor="@color/white"
android:textSize="20sp"
android:backgroundTint="@color/purple_200"/>

</LinearLayout>

Activity [Link]
package [Link];

import [Link]; import

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

public class MainActivity extends AppCompatActivity { CheckBox


c1,c2,c3;
Button b1;
ToggleButton toggleButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
c1=findViewById([Link].check1); c2=findViewById([Link].check2);
c3=findViewById([Link].check3); b1=findViewById([Link]);
toggleButton=findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
if
([Link]()&&[Link]()&&[Link]()){

[Link](getApplicationContext(),""+[Link]()+","+c
[Link]()+","+[Link](),Toast.LENGTH_LONG).show();
}else if ([Link]()&&[Link]()){

[Link](getApplicationContext(),""+[Link]()+","+c
[Link](),Toast.LENGTH_LONG).show();
}else if ([Link]()&&[Link]()){

[Link](getApplicationContext(),""+[Link]()+","+c
[Link](),Toast.LENGTH_LONG).show();
}else if ([Link]()&&[Link]()){

[Link](getApplicationContext(),""+[Link]()+","+c
[Link](),Toast.LENGTH_LONG).show();
}else if ([Link]()){

[Link](getApplicationContext(),""+[Link](),Toast
.LENGTH_LONG).show();
}else if ([Link]()){

[Link](getApplicationContext(),""+[Link](),Toast
.LENGTH_LONG).show();
}else if ([Link]()){

[Link](getApplicationContext(),""+[Link](),Toast
.LENGTH_LONG).show();
}
}
});

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

[Link](getApplicationContext(),"Toggle Button is
ON",Toast.LENGTH_SHORT).show();
}else {
[Link](getApplicationContext(),"Toggle Button is
OFF",Toast.LENGTH_SHORT).show();

}
});
}
}

OutPut:

You might also like