Android Tutorial: Radio Button

android radio button example
In this android tutorial for beginners we are going to use radio button. Below is a simple example which makes use of android radio button. 
We have used an EditText for the user to enter name then two radio buttons to select sex and then a submit button. When the user clicks the submit button the User's name and sex is displayed in the TextView below the submit button.
How to use Android Radio Button
  1. drag and drop a radioGroup container from containers in palette.
  2. inside the radio group drag and drop two radio buttons and name them male and female.
  3. Then place a button below the radioGroup container and name it submit.
Here is the complete XML code for this radio button in android example.

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 

 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 android:layout_width="match_parent" 
 android:layout_height="fill_parent" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 app:layout_behavior="@string/appbar_scrolling_view_behavior" 
 tools:showIn="@layout/activity_main" 
 tools:context="com.example.saleem.myapplication.MainActivity">

    <RadioGroup 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:id="@+id/radioGroup" 
 android:layout_below="@+id/textView2" 
 android:layout_alignParentLeft="true" 
 android:layout_alignParentStart="true" 
 android:layout_marginTop="58dp">

        <RadioButton 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
             android:text="Male" 
 android:id="@+id/radioButton" 
 android:checked="false" />

        <RadioButton 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Female" 
 
 android:id="@+id/radioButton2" 
 android:checked="false" />
    </RadioGroup>

    <Button 
 style="?android:attr/buttonStyleSmall" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Submit" 
 android:id="@+id/button"  
 android:layout_centerVertical="true" 
 android:layout_centerHorizontal="true" 
 android:onClick="onSubmitButtonClick" />

    <EditText 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_alignParentTop="true" 
 android:layout_alignParentLeft="true" 
 android:layout_alignParentStart="true" 
 android:layout_alignRight="@+id/button" 
 android:layout_alignEnd="@+id/button" 
 android:hint="Name" /> 
 <TextView 
         android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Sex"        android:id="@+id/textView2" 
 android:layout_below="@+id/editText" 
 android:layout_alignParentLeft="true" 
 android:layout_alignParentStart="true" 
 android:layout_marginTop="50dp" />

</RelativeLayout>
java code
public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private RadioButton radioButton;
    // EditText for user to enter name    private EditText nameTextField;



    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameTextField = (EditText)findViewById(R.id.editText);

    }
    public void onSubmitButtonClick(View view){
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        int selectedOption = radioGroup.getCheckedRadioButtonId();
        radioButton = (RadioButton)findViewById(selectedOption);
        String name = nameTextField.getText().toString();
        String sex = radioButton.getText().toString();
        Toast.makeText                (MainActivity.this, "name is " +name +"and sex is" +sex,Toast.LENGTH_SHORT)
                .show();

    }
}
we have created a method named onSubmitButtonClick when the method is called it casts radioGroup.
then it creates an int variable named selectedOption to store the id of the checked radio button in it.
Then it casts radioButton and in the findViewById(selectedOption)variable is passed. So the checked
radio button is assigned to the radioButton variable. String name is used to get the text entered in
the editText field and String sex is assigned, the text it gets from the radioButton.
And finally toast is used to display the name and checked sex of the user. 
 
Android Radio Button example
 
 
 

Android Tutorial ImageView

Android ImageView Tutorial
In this android tutorial for beginners we are going to use ImageView to display images in android apps.
Android apps are created for devices with different screen sizes so an image must be in various sizes to fit the device's screen on which it is running. Thanks to android studio you don't have to resize an image for different devices, android studio automatically converts your image to different sizes.

To generate an image for different screen sizes in Android Studio.
  • Right click on res folder
  • Then New and then click Image Asset
  • Enter a name for your image file then choose Image in the Asset Type
  • Now specify the path of your image file. Click OR press SHIFT+ENTER keys to open the select path Dialog box
  • Locate and select your file and then click OK
  • Then click next and then click finish.
To add an image to your Activity in android studio.
  •  Select your content_main.xml file
  • Then from the palette drag and drop the ImageView widget on the screen.
  •  Now double click your ImageView  then click to select src for the image.
  • Now click Mip Map and inside Mip Map you will see the image you created earlier for different screen sizes.
  • Select your image and click OK.
This was a simple tutorial for adding an image to your activity in android studio.

Android Tutorial Intent

android open another activity on button click

In Android a single screen is called activity. For example a chat application can have many activities like login, register etc.
In android to run another activity from an activity Intent is used. Intent performs many tasks but the most common use of Intent is the opening of another activity.

Android Tutorial OnClickListener

android onclicklistener example
So far in this android tutorial we have used the android:onClick attribute in the xml for the actions that should be performed when we click on the button. There is another alternative to the android:onClick attribute which is the OnClickListener. Android onclicklistener is used on Android views  like buttons,textviews,edittexts etc.

Android Tutorial Button

Android button example
Android Button is an android widget that can be pressed to perform some action.
In this android tutorial we are using a simple button example to help you understand how to use android button.

Android Tutorial TextView

Android TextView example
In this tutorial we are going know how to use android TextView with a simple TextView example.
We will create a simple app which helps us understand how to use Android TextView. In our app the user enters data in the EditText field and when the user clicks the button the entered data is displayed in the TextView.
In Android a TextView is used to show text to the user.

Android tutorial EditText

Android EditText example


EditText is a text box where a user can enter text.
In this android tutorial we are going to know how to use edit text through a simple example.
In the following example we are going to create a simple app that displays the Text entered in the EditText as a Toast message.
Don’t worry about Toast and Button you will learn about them in next chapters of this course right now just learn how to use EditText.

Here is a simple Android EditText Example