Radio Buttons Tutorial - Android Studio Tutorials

    Create New Project

    • Application Name : Radio Group Example
    • Company Name : tutorialscafe.com (this will make the package name : com.tutorialscafe.radiogroupexample)
    • Project Location : C:\Users\USER\AndroidStudioProjects\RadioGroupExample
    • Select the Minimum SDK for you app:
      • Check the Phone and Tablet option if you are going to create app for mobile and Tab
    • Add an activity to Mobile : slect an empty activity template
    • Name your activity and layout file
    • Click on finish Button (it will take some time to finish the Gradle Build)

    Create XML

  1. Click on the Project Tab to open the project files
  2. Navigate to app > res > layout > activity_main.xml in project column
  3. place that code in there
  4. 
    
    
        
        
    
            
    
            
            
    
            
    
    
        
    
        

    Create JAVA

  5. Click on the Project Tab to open the project files
  6. Navigate to app > java > Package Name > MainActivity.java in project column
  7. place that code in there
  8. package jovial.etutorialscafe.radiobutton;
    
    import android.support.annotation.IdRes;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        RadioGroup rate;
        RadioButton exce,good,avg,poor;
        String ratecomment = "";
        Button buttonSumbit;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            rate = (RadioGroup)findViewById(R.id.radioGroup);
    
            exce = (RadioButton)findViewById(R.id.excelentID);
            good = (RadioButton)findViewById(R.id.goodID);
            avg = (RadioButton)findViewById(R.id.avdID);
            poor = (RadioButton)findViewById(R.id.poorID);
    
            buttonSumbit = (Button)findViewById(R.id.buttonID);
    
            rate.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
    
                    switch (rate.indexOfChild(findViewById(group.getCheckedRadioButtonId()))){
                        case 0:
                            ratecomment = "Excellent";
                            break;
                        case 1:
                            ratecomment = "Good";
                            break;
                        case 2:
                            ratecomment = "Average";
                            break;
                        case 3:
                            ratecomment = "Poor";
                            break;
                    }
                }
            });
    
            buttonSumbit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, ratecomment + " : Ratting Submited", Toast.LENGTH_SHORT).show();
                }
            });
    
        }
    }
    
    

    Run App

  9. Start Emulator by clicking on the AVD Manager
  10. Start Emulator
  11. Click on the RUN icon or press SHIFT + F10
  12. Comments

    Popular posts from this blog

    Android Call Blocker - Android Studio Tutorials

    Toggle Buttons Tutorial - Android Studio Tutorials