<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.abbu.radio_power.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enquire Teacher-in-Charge"
android:id="@+id/textView" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/choices"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true">
<RadioButton
android:text="Computer Science"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:id="@+id/radioButtonCS" />
<RadioButton
android:text="Commerce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/radioButtonCS"
android:layout_toEndOf="@+id/textView"
android:layout_alignParentStart="false"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:id="@+id/radioButtonCO" />
<RadioButton
android:text="Chemistry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/radioButtonEL"
android:layout_alignStart="@+id/radioButtonCO"
android:layout_alignParentStart="false"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:id="@+id/radioButtonCH" />
<RadioButton
android:text="Electronics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButtonEL"
android:layout_alignParentStart="false"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/radioButtonCS"
android:layout_alignStart="@+id/radioButtonCS" />
</RadioGroup>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:id="@+id/imageView"
android:layout_marginTop="41dp"
android:layout_below="@+id/choices"
android:layout_centerHorizontal="true" />
</RelativeLayout>
File Name: MainActivity.java
package com.example.abbu.radio_power;
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TypedArray images;
private ImageView itemImage;
RadioButton rch = null;
RadioButton rco = null;
RadioButton rcs = null;
RadioButton rel = null;
RadioGroup rg = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
images = getResources().obtainTypedArray(R.array.radiobtn_image);
itemImage = (ImageView) findViewById(R.id.imageView);
rch = (RadioButton) findViewById(R.id.radioButtonCH);
rco = (RadioButton) findViewById(R.id.radioButtonCO);
rcs = (RadioButton) findViewById(R.id.radioButtonCS);
rel = (RadioButton) findViewById(R.id.radioButtonEL);
rg = (RadioGroup) findViewById(R.id.choices);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == rch.getId()) {
itemImage.setImageResource(R.drawable.pic2);
Toast.makeText(MainActivity.this, "Dr. Neelu Dhir",
Toast.LENGTH_SHORT).show();
}else if (checkedId == rco.getId()) {
itemImage.setImageResource(R.drawable.pic4);
Toast.makeText(MainActivity.this, "Ms. Sangeeta Relan",
Toast.LENGTH_SHORT).show();
} else if (checkedId == rcs.getId()) {
itemImage.setImageResource(R.drawable.pic1);
Toast.makeText(MainActivity.this, "Dr. Vibha Gaur",
Toast.LENGTH_SHORT).show();
} else {
itemImage.setImageResource(R.drawable.pic3);
Toast.makeText(MainActivity.this, "Dr. Ravneet Kaur",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
File Name: strings.xml
<resources>
<string name="app_name">Radio_Power</string>
<string-array name="radiobtn_image">
<item>@drawable/pic1</item>
<item>@drawable/pic2</item>
<item>@drawable/pic3</item>
<item>@drawable/pic4</item>
</string-array>
</resources>
Output:
|
Radio Button, ImageView and Toast Usage in Android |
|
Radio Button, ImageView and Toast Usage in Android |
No comments:
Post a Comment