0

So, what I am trying to do here is to have a main activity where, if the requirements are met(correct username, password, and male radio button is checked and not female), the VM switches to the "Success" activity. If it does not meet any of those 3 requirements the VM switches to the "Failed" activity when the button is pressed. I have it working correctly except for the Radio Buttons.

I created a RadioGroup on the layout, but I'm not sure how to implement it in the class itself. I assumed you had to find the ID, override the listener etc etc...but it's not working correctly. Any ideas? I took out most of the RadioGroup properties before posting this so it would be less muddled.

Main Activity

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener
{
    Button button;
    EditText login;
    EditText password;
    RadioGroup mRadioGroup;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button1);
        login =(EditText)findViewById(R.id.editText1);
        password =(EditText)findViewById(R.id.editText2);
        button.setOnClickListener(new View.OnClickListener()  
    {
        @Override
        public void onClick(View view)
        {
            String L,P;             

            L = login.getText().toString();
            P = password.getText().toString();


            if(L.equals("name") && P.equals("123456"))
            {
                Intent intent = new Intent();  
                intent.setClass(MainActivity.this,Welcome.class);
                startActivity(intent);
            }
            else
            {
                Intent intent1 = new Intent();
                intent1.setClass(MainActivity.this,Failed.class);
                startActivity(intent1);
            }
      }   
    });




}



  /*  public void onRadioButtonClicked(View view) 
    {
        boolean checked = ((RadioButton) view).isChecked();

        switch(view.getId()) 
        {
            case R.id.radio1:
                if (checked)
                {
                Intent intent4 = new Intent();
                intent4.setClass(MainActivity.this,Welcome.class);
                startActivity(intent4);
                }
                break;
            case R.id.radio0:
                if (checked)
                {
                Intent intent2 = new Intent();
                intent2.setClass(MainActivity.this,Failed.class);
                startActivity(intent2);
                }
                break;
                }
                }
      */



    @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;
    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) 
    {
        // TODO Auto-generated method stub

    }


}

Failed Activity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Failed extends Activity
{
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.failed);
        button = (Button)findViewById(R.id.button1);       
        button.setOnClickListener(new View.OnClickListener()


    {
        @Override
        public void onClick(View view)
        {
            Intent intent3 = new Intent();
            intent3.setClass(Failed.this,MainActivity.class);
            startActivity(intent3);
        }

    });
}
}

Success Activity

import android.app.Activity;
import android.os.Bundle;

public class Welcome extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.success);
    }

}
user1780149
  • 53
  • 1
  • 5

3 Answers3

0

Try this to get the value of the selected RadioButton and use it to check specific conditions in your code :

private RadioGroup radioOptionGrp;
private RadioButton radioOptBtn;

//Get Reference to Radio group which holds the radio buttons
radioOptionGrp = (RadioGroup) findViewById(R.id.radioOpt);

//Get id of selected radioButton
int selectOptId = radioOptionGrp.getCheckedRadioButtonId();

//Get Reference to the Selected RadioButton
radioOptBtn = (RadioButton) findViewById(selectOptId);

//Get value of the selected Radio button
String value = radioOptBtn.getText().toString();    

Hope this helps!! Here is a sample example code you can download from GitHub to see how RadioButtons work https://github.com/asabbarwal/SimpleRadioButton

Abhishek Sabbarwal
  • 3,758
  • 1
  • 26
  • 41
0

My first met of RadioButtons was too not easy. But basically, it's simple to use it.

private RadioGroup mRadioGroup;
private View radioButton;   
int radioButtonID;
int idx;            //index of radio item in the list

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
        mRadioGroup= (RadioGroup) findViewById(R.id.mRadioGroup);           
....
}

Put this part of code to any click event method, idx always will return an index of checked radio button:

radioButtonID = mRadioGroup.getCheckedRadioButtonId();
radioButton = mRadioGroup.findViewById(radioButtonID);
idx = mRadioGroup.indexOfChild(radioButton);

If you want to catch radio button click event, here the good solution How to set On click listener on the Radio Button in android

Community
  • 1
  • 1
Dimon
  • 790
  • 1
  • 10
  • 24
0
at first you can add two radio button in your activity like other form widgets
than try this code.
pay attention that when you click and set a radio button,you must disable other radio buttons.

arg1 means that the button is selected.

   package com.example.azarbaycannetworkcompany;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RadioButton first = (RadioButton) findViewById(R.id.radioButton1);
        final RadioButton seccond = (RadioButton) findViewById(R.id.radioButton2); 
        first.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if(arg1)
                {
                    seccond.setChecked(false);
                    Toast.makeText(getBaseContext(),"1 set shod",Toast.LENGTH_LONG).show();
                }

            }
        });
        seccond.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if(arg1)
                {
                    first.setChecked(false);
                Toast.makeText(getBaseContext(),"2 set shod",Toast.LENGTH_LONG).show();

                }

            }
        });
    }


}