0

I am making an app where it takes two types of users. "Viewers" and "Contractors". I made two radio buttons for each option. I want to know three things: How to activate a button when a radio button is selected. How to inactivate a button when there is no radio button selected. Lastly, how to make both of the radio buttons send you to a unique activity depending of the option chosen. For example, I pick "Contractor" then press the button to continue, it'll send me to a unique layout that connects to that radio button.

Here is my XML:

<?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:id="@+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_background"
tools:context="com.devteam.abire.abire.b">

<android.support.v7.widget.CardView
    app:cardElevation="15dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="300dp"
    android:layout_height="345dp">

</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    app:cardElevation="20dp"
    android:layout_width="320dp"
    android:layout_height="320dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:background="#141526"
            android:layout_width="match_parent"
            android:layout_height="50dp"/>

        <ImageView
            android:id="@+id/abire_app_icon_v2"
            android:layout_marginTop="21dp"
            android:elevation="45dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/abire_logo_v1"
            android:layout_width="55dp"
            android:layout_height="55dp" />

        <TextView
            android:layout_marginStart="20dp"
            android:id="@+id/register_as_text"
            android:layout_marginTop="10dp"
            android:text="Register As A..."
            android:textColor="#141526"
            android:layout_below="@+id/abire_app_icon_v2"
            android:textSize="28sp"
            android:textAlignment="textStart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_marginStart="20dp"
            android:textSize="22sp"
            android:textColor="#141526"
            android:id="@+id/viewer_radioBtn"
            android:text="Viewer"
            android:layout_marginTop="18dp"
            android:layout_below="@+id/register_as_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_marginStart="20dp"
            android:textSize="22sp"
            android:textColor="#141526"
            android:id="@+id/contractor_radioBtn"
            android:text="Contractor"
            android:layout_marginTop="18dp"
            android:layout_below="@+id/viewer_radioBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button

            android:id="@+id/continueBtn"
            android:textSize="18sp"
            android:text="CONTINUE"
            android:textColor="#fff"
            android:layout_marginTop="25dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/contractor_radioBtn"
            android:background="@drawable/ripple_maroon"
            android:layout_width="250dp"
            android:layout_height="38dp" />

    </RelativeLayout>

</android.support.v7.widget.CardView>


</RelativeLayout>

Here is my Java:

package com.devteam.abire.abire;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class b extends AppCompatActivity {

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

2 Answers2

0

Firstly, it is good to use radiogroup instead of radio button if you have multipe buttons.

For "How to activate a button when a radio button is selected. How to inactivate a button when there is no radio button selected.", create references for both buttons and radio buttons in activity. Then, if first radio button is checked, disable button by :

button.setEnabled(false); 

For "Lastly, how to make both of the radio buttons send you to a unique activity depending of the option chosen.", while using radiogroup use :

public void onCheckedChanged(RadioGroup arg0, int arg1) {
  radioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId(););

if (radioButton.isChecked()) {
  text=radioButton.getText().toString();
  if (text.equals("radiobtn1Option")) {
    //TODO : start new activity
    Intent intent = new Intent(this, YourNextSCreen.class);
            startActivity(intent);

  } if (text.equals("radiobtn2Option")) {
    //TODO
  } else {
    //TODO
  }
}

} });

0

If you want to make only one of the buttons clickable at a time, you should put radiobuttons inside radiogroup

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
        <RadioButton
            android:layout_marginStart="20dp"
            android:textSize="22sp"
            android:textColor="#141526"
            android:id="@+id/viewer_radioBtn"
            android:onCLick="onRadioButtonClicked"
            android:text="Viewer"
            android:layout_marginTop="18dp"
            android:layout_below="@+id/register_as_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_marginStart="20dp"
            android:textSize="22sp"
            android:textColor="#141526"
            android:id="@+id/contractor_radioBtn"
            android:onCLick="onRadioButtonClicked"
            android:text="Contractor"
            android:layout_marginTop="18dp"
            android:layout_below="@+id/viewer_radioBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
</RadioGroup>

<Button
            android:id="@+id/continueBtn"
            android:textSize="18sp"
            android:text="CONTINUE"
            android:textColor="#fff"
            android:layout_marginTop="25dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/contractor_radioBtn"
            android:onCLick="onButtonClicked"
            android:background="@drawable/ripple_maroon"
            android:layout_width="250dp"
            android:layout_height="38dp" />

    package com.devteam.abire.abire;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    public class b extends AppCompatActivity {

    private String mClickedRadioButton;

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

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
            boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.viewer_radioBtn":
                    if (checked)
                        sClickedRadioButton = "viewer";
                    break;
                case R.id.contractor_radioBtn"
                    if (checked)
                        sClickedRadioButton; = "contractor";    
                break;
            }
    }   

    public void onButtonClicked(View v){

        if(sClickedRadioButton == null){
            return;
        }else if(sClickedRadioButton.equals("viewer")){
            //Do something when view is checked
        }else if(sClickedRadioButton.equals("contractor"){
            // Do something when contractor is checked
        }   
}
Abdurakhmon
  • 2,813
  • 5
  • 20
  • 41