indian0 girl
indian0 girl

Reputation: 131

Why my Toast not showing up ,android programming

I wrote code where when clicking specific radio button shows up toast of certain message ,but when i clicked why no toast pops up ? why toast is not showing up? I wrote code where when clicking specific radio button shows up toast of certain message ,but when i clicked why no toast pops up ? why toast is not showing up?

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
int id;
String text = "radio button 1!";
String text2="radioi button 2";
String text3="radio button 3";

int duration = Toast.LENGTH_SHORT;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    radioGroup = (RadioGroup) findViewById(R.id.radio_group);
    id = radioGroup.getCheckedRadioButtonId();
}
public void onRadioButtonClicked(View view) {


    switch(id) {
        case R.id.radioButton:
            Toast toast = Toast.makeText(this, text, duration);
            toast.show();

            break;
        case R.id.radioButton2:
            Toast toast2 = Toast.makeText(this, text2, duration);
            toast2.show();

            break;
        case R.id.radioButton3:
            Toast toast3 = Toast.makeText(this, text3, duration);
            toast3.show();
            default:


    }
}
}

This is the xml layout code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ai_agamcompaq.radio.MainActivity">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radio_group">
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New RadioButton"
    android:id="@+id/radioButton"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="onRadioButtonClicked"
    android:layout_marginTop="115dp" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New RadioButton"
    android:id="@+id/radioButton2"
    android:layout_below="@+id/radioButton"
    android:layout_alignStart="@+id/radioButton"
    android:onClick="onRadioButtonClicked"
    android:layout_marginTop="28dp" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New RadioButton"
    android:id="@+id/radioButton3"
    android:layout_below="@+id/radioButton2"
    android:layout_alignStart="@+id/radioButton2"
    android:onClick="onRadioButtonClicked"
    android:layout_marginTop="52dp" />


</RadioGroup>
</RelativeLayout>

Upvotes: 1

Views: 518

Answers (5)

JonZarate
JonZarate

Reputation: 871

EDIT: Full working code

enter image description here

MainActivity.java:

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    // Declare the messages to show
    private String text1 = "radio button 1";
    private String text2 = "radio button 2";
    private String text3 = "radio button 3";

    private int mToastDuarion = Toast.LENGTH_SHORT;

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

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id. radio_group);
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int id) {

        switch(id) {
            case R.id.radioButton:
                Toast.makeText(this, text1, mToastDuarion).show();
                break;

            case R.id.radioButton2:
                Toast.makeText(this, text2, mToastDuarion).show();
                break;

            case R.id.radioButton3:
                Toast.makeText(this, text3, mToastDuarion).show();
                break;

            default:
                break;
        }
    }
}

layout_main.xml (it's your same XML without onClick defined):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RadioGroup
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:id="@+id/radio_group"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="115dp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton2"
            android:layout_below="@+id/radioButton"
            android:layout_alignStart="@+id/radioButton"
            android:layout_marginTop="28dp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton3"
            android:layout_below="@+id/radioButton2"
            android:layout_alignStart="@+id/radioButton2"
            android:layout_marginTop="52dp" />
        
    </RadioGroup>
</LinearLayout>

Upvotes: 0

samus
samus

Reputation: 6202

As an alternative to JonZarate's answer, you could also try this:

@Override
public void onCheckedChanged(RadioGroup group, int id) {

    string text = ((TextView)group.findViewById(id)).getText();
    Toast toast = Toast.makeText(this, text, duration);
    toast.show();
}

Upvotes: 0

Mohammed Atif
Mohammed Atif

Reputation: 4513

public void onRadioButtonClicked(View view) {

    //please update the ID
    id = view.getId();

    switch(id) {
        case R.id.radioButton:
            Toast toast = Toast.makeText(this, text, duration);
            toast.show();

            break;
        case R.id.radioButton2:
            Toast toast2 = Toast.makeText(this, text2, duration);
            toast2.show();

            break;
        case R.id.radioButton3:
            Toast toast3 = Toast.makeText(this, text3, duration);
            toast3.show();
       default:
    }
}

You were not updating the ID, so switch statement was always going to default.

Upvotes: 0

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37594

Your switch statement is incorrect. You will never see a checked box, because you to have ask for the one who got checked.

public void onRadioButtonClicked(View view) {

switch(view.getId()) {
    case R.id.radioButton:
        Toast toast = Toast.makeText(this, text, duration);
        toast.show();
        break;
    case R.id.radioButton2:
        Toast toast2 = Toast.makeText(this, text2, duration);
        toast2.show();
        break;
    case R.id.radioButton3:
        Toast toast3 = Toast.makeText(this, text3, duration);
        toast3.show();
        default:
  }
}

Upvotes: 2

arshad shaikh
arshad shaikh

Reputation: 673

Write one line code...

Toast.makeText(context, "Your text", Toast.LENGTH_SHORT).show();

Upvotes: 0

Related Questions