//Create a New Java Project called RadioButtons . Write a program with three rad
ID: 3804320 • Letter: #
Question
//Create a New Java Project called RadioButtons.
Write a program with three radio buttons, labeled CAT, CHICKEN, and OTHER.
//When the user clicks on the radio button labeled CAT, an image of a CAT should be displayed in the window.
//When the user clicks on the radio button labeled CHICKEN, an image of an chicken should be displayed in the window.
//When the user clicks on the radio button labeled OTHER, an image, of your choosing, should be displayed.
//Please note that no more than one image should be displayed at any given time.
Explanation / Answer
First copy cat image,chicken image and nature image in drawable folder which is in res.
activity_project.xml:
<LinearLayout
android:width=match_parent
android:height=match_parent
orientation:vertical
gravity=center>
<RadioGroup android:width=match_parent
android:height=wrap_content
orientation=horizontal>
<RadioButton
android:width=wrap_content
android:height=wrap_content
android:id=@+id/cat/>
<RadioButton
android:width=wrap_content
android:height=wrap_content
android:id=@+id/chicken/>
<RadioButton
android:width=wrap_content
android:height=wrap_content
android:id=@+id/other/>
</RadioGroup>
<ImageView
android:width=wrap_content
android:height=wrap_content
android:id=@+id/imageView/>
</LinearLayout>
Java:
public class ProjectActivity extends Activity
{
RadioButton cat,chicken,other;
ImageView im;
onCreate()
{
setContentView(R.layout.activity_project);
cat=(RadioButton)findViewById(R.id.cat);
chicken=(RadioButton)findViewById(R.id.chicken);
other=(RadioButton)findViewById(R.id.other);
im=(ImageView)findViewById(R.id.imageView);
cat.setOnClickListener(new View.OnClickListener
{
public void OnClick(iew v)
{
im.setImageResource(R.drawable.cat);
}
});
chicken.setOnClickListener(new View.OnClickListener
{
public void OnClick(iew v)
{
im.setImageResource(R.drawable.chicken);
}
});
other.setOnClickListener(new View.OnClickListener
{
public void OnClick(iew v)
{
im.setImageResource(R.drawable.nature);
}
});
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.