activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal"> <RadioGroup android:id="@+id/rg1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:paddingTop="20dp" android:gravity="center_horizontal" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Frag 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Frag 2" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Frag 3" /> <RadioButton android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Frag 4" /> <RadioButton android:id="@+id/radioButton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Frag 5" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image1" /> </LinearLayout> </LinearLayout>
MainActivity.java
package in.kantapp.example4; import android.app.Activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends AppCompatActivity implements OnCheckedChangeListener { RadioGroup group1, group2; Button gen; ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); group1 = (RadioGroup) findViewById(R.id.rg1); group1.setOnCheckedChangeListener((OnCheckedChangeListener) this); img = (ImageView) findViewById(R.id.imageView1); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch (checkedId) { case R.id.radioButton1: img.setImageResource(R.drawable.image1); break; case R.id.radioButton2: img.setImageResource(R.drawable.image2); break; case R.id.radioButton3: img.setImageResource(R.drawable.image3); break; case R.id.radioButton4: img.setImageResource(R.drawable.image4); break; case R.id.radioButton5: img.setImageResource(R.drawable.image5); break; default: break; } } }
Download Image:
No comments:
Post a Comment