postheadericon An Android RadioGroup Example Programmatically






MainActivity.java

package bipin.rupadiya.radiogroupexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
Button b = (Button) findViewById(R.id.btnOK);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id=rg.getCheckedRadioButtonId();
RadioButton selectedRedioButton = (RadioButton) findViewById(id);
Toast.makeText(getApplicationContext(), "You have selected "+selectedRedioButton.getText().toString(), Toast.LENGTH_LONG).show();

}
});


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}



activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="54dp"
        android:text="Select Language"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="59dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="English" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hindi" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gujarati" />
    </RadioGroup>

    <Button
        android:id="@+id/btnOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginTop="41dp"
        android:text="OK" />

</RelativeLayout>



2 comments:

Unknown said...

Sir, i have a problem.

When i add any elements and while i need to make object with R.id."THAT ELEMENT ID". it does not show me that element id. .. .

What to do. .sir. .. ?

Unknown said...

It's fabulous :)

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.