RadioButtonGroup are not showing up

181 views Asked by At

After solving buttons successfully with your help, I'm having a problem now that the RadioButtonGroup aren't showing up.

This is the code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android2="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="10"
        >

        <TextView
            android:id="@+id/secondLine"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/Greetings"
            android:textSize="20sp"
            />

        <Button
            android:id="@+id/Button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="HelloWord"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android2:baselineAligned="_baseline"
        android:orientation="horizontal"
        android:weightSum="10"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3.3"
            android:background="#ff0000"
            android:gravity="center"
            android:text="red"
            android:textColor="#000000"
            />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="3.4"
            android:background="#00ff00"
            android:gravity="center"
            android:text="green"
            android:textColor="#000000"

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="3.3"
            android:background="#0000ff"
            android:gravity="center"
            android:text="blue"
            android:textColor="#000000"

    </LinearLayout>

    <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

    <RadioButton
        android:id="@+id/myRadioButtonRed"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weight="1"
        />

    <RadioButton
        android:id="@+id/myRadioButtonGreen"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weight="1"
        />

    <RadioButton
        android:id="@+id/myRadioButtonBlue"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weight="1"
        />

</LinearLayout>

Note: I'm having an error saying this:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="3.3"
    android:background="#0000ff"
    android:gravity="center"
    android:text="blue"
    android:textColor="#000000"
    />

must be followed by a attribute specification.

I hope I can get help and thanks.

6

There are 6 answers

0
Linga On

It should be something like this

button1.setOnClickListener(new OnClickListener()
{
  public void onClick(View v)
  {
     //stuff
  }
});

button2.setOnClickListener(new OnClickListener()
{
  public void onClick(View v)
  {
     // stuff
  }
});
0
Santhosh On

u can also use this way...

Button Button1=(Button)findViewById(R.id.Button1);
Button Button2=(Button)findViewById(R.id.Button2);
    View.OnClickListener myListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
        if(view.getId()==R.id.Button1){
            Toast.makeText(Nasser.this, "Button1", Toast.LENGTH_LONG).show();
        }
        else if(view.getId()==R.id.Button2){
            Toast.makeText(Nasser.this, "Button2", Toast.LENGTH_LONG).show();
        }

    };
    button1.setOnClickListener(myListener);
    button2.setOnClickListener(myListener);
0
Dixit Patel On

Just Use this way

            @Override
            public void onCreate(Bundle savedInstanceState) 
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                Button Button1=(Button)findViewById(R.id.Button1);
                Button1.setOnClickListener(mClickListener);
            }

            View.OnClickListener mClickListener =new OnClickListener()
            {           
                @Override
                public void onClick(View v) {
                // write your code here

                }
            };
1
laalto On
  1. There's no Button.OnClickListener class. Use View.OnClickListener instead. Replace

    Button.OnClickListener myListener = new Button.OnClickListener()
    

    with

    View.OnClickListener myListener = new View.OnClickListener()
    
  2. A class can have only one method with the same signature. Remove the other onClick(View) definition.

0
Sanket990 On

It should implement button(Onclicklistener) like this.. Check below code

 Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button1);

    btn.setOnClickListener(listener);

}

View.OnClickListener listener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (v == btn) {
            Toast.makeText(MainActivity.this, "test", Toast.LENGTH_LONG)
                    .show();
        }
    }
};
1
RoyalGriffin On

You are getting that error because your XML is not-well formed. You have left out closing tags for two of your TextViews, your RadioGroup and there is an extra closing tag of a LinearLayout. Also there is an unknown attribute android2:baselineAligned="_baseline" in your linearLayout2. Replace your XML code with the attached code. You shouldn't get the error you're facing. Also, I have fixed the widths of your RadioButtons. They shouldn't be fill_parent when you want all of them to be in the same line. I also know that you want your each RadioButton to take 1/3rd of the screen width. That is left out as an exercise.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android2="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="10" >

        <TextView
            android:id="@+id/secondLine"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="@string/Greetings" />

        <Button
            android:id="@+id/Button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="HelloWord" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal"
        android:weightSum="10">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3.3"
            android:background="#ff0000"
            android:gravity="center"
            android:text="red"
            android:textColor="#000000" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="3.4"
            android:gravity="center"
            android:background="#00ff00"
            android:text="green"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="3.3"
            android:gravity="center"
            android:background="#0000ff"
            android:text="blue"
            android:textColor="#000000"/>

    </LinearLayout>

    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myRadioGroup"
        android:orientation="horizontal">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:weight="1"
        android:id="@+id/myRadioButtonRed" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:weight="1"
        android:id="@+id/myRadioButtonGreen" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:weight="1"
        android:id="@+id/myRadioButtonBlue" />
    </RadioGroup>

</LinearLayout>