How to access checkboxes android after we choose radiobutton "yes"? - android

I want, if we choose radiobutton "yes" so we can using checkboxes. if we choose radiobutton "no", checkboxes can't be used. How?
its xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#ffffffff"
android:text="ARE YOU CLASS 2B?" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes" />
<RadioButton
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/jimmy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jimmy" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/diana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Diana" />
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/dina"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dina" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/jack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jack" />
</TableRow>
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TableRow
android:id="#+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</LinearLayout>
Its java:
package diahp.d;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
public class example extends Activity implements OnClickListener {
CheckBox jimmy,diana,dina,jack;
RadioButton yes,no;
TextView status;
StringBuilder showing,showing2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
jimmy = (CheckBox) findViewById(R.id.jimmy);
diana = (CheckBox) findViewById(R.id.diana);
dina = (CheckBox) findViewById(R.id.dina);
jack = (CheckBox) findViewById(R.id.jack);
jimmy.setOnClickListener(this);
diana.setOnClickListener(this);
dina.setOnClickListener(this);
jack.setOnClickListener(this);
yes = (RadioButton) findViewById(R.id.yes);
no = (RadioButton) findViewById(R.id.no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
status = (TextView) findViewById(R.id.status);
}
public void onClick(View v) {
showing = new StringBuilder();
if(yes.isChecked()){(diana,jimmy,dina,jack==true)} //can access the checkboxes
if(no.isChecked()){(diana,jimmy,dina,jack==false)} //can't access the checkboxes
if(diana.isChecked()){
showing.append("Diana");
}
if(jimmy.isChecked()){
showing.append("Jimmy");
}
if(dina.isChecked()){
showing.append("Dina");
}
if(jack.isChecked()){
showing.append("Jack");
}
status.setText("Oh, you are:" +showing);
} }
hmm... how if I choose radio button " yes" can access the checkboxes, and if "no" can't access the checkboxes. .

Try like this.
if(no.isChecked())
{
//diana.setChecked(false);
//jimmy.setChecked(false);
//dina.setChecked(false);
//jack.setChecked(false);
diana.setEnabled(false);
jimmy.setEnabled(false);
dina.setEnabled(false);
jack.setEnabled(false);
}
or else use RadioGroup
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
radioButton = (RadioButton) findViewById(radioGroup
.getCheckedRadioButtonId());
if (radioButton.getText().equals("Yes")) {
//diana.setChecked(true);
//jimmy.setChecked(true);
//dina.setChecked(true);
//jack.setChecked(true);
diana.setEnabled(true);
jimmy.setEnabled(true);
dina.setEnabled(true);
jack.setEnabled(true);
} else {
//diana.setChecked(false);
//jimmy.setChecked(false);
//dina.setChecked(false);
//jack.setChecked(false);
diana.setEnabled(false);
jimmy.setEnabled(false);
dina.setEnabled(false);
jack.setEnabled(false);
}
}
});
I hope this will help you.

disable checkbox on change of radio button.
more info link

You have to use a setOnCheckedChangeListener and Change to RadioGroup http://developer.android.com/reference/android/widget/RadioGroup.html
(radiogroup).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.yes) {
XY
} else if (checkedId == R.id.no) {
XY
} else {
XY
}
}
Hope this helps. Happy coding!

Related

Check is any RadioButton is checked in Android

I would like to check is any RadioButton is checked and which is checked.
I have
package promedica.test_osobowosci;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class Question1 extends AppCompatActivity {
private Button nextButton;
private Button stopButton;
private RadioButton answer1;
private RadioButton answer2;
private RadioButton answer3;
private RadioButton answer4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
setTitle("Pytanie 1 z 10");
answer1 = findViewById(R.id.radioButtonQ1_1);
answer1 = findViewById(R.id.radioButtonQ1_2);
answer1 = findViewById(R.id.radioButtonQ1_3);
answer1 = findViewById(R.id.radioButtonQ1_4);
nextButton = findViewById(R.id.nextTo2Button);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isAnyRadioButtonChecke();
}
});
stopButton = findViewById(R.id.stopButton1);
stopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
});
}
public void openQuestion2Activity(){
Intent intent = new Intent(this, Question2.class);
startActivity(intent);
}
public void openMainActivity(){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void noAnswerCheckedAlert() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Nie zaznaczono odpowiedzi!");
alertDialogBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(Question1.this, "Zaznacz odpowiedź", Toast.LENGTH_LONG).show();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
public void isAnyRadioButtonChecked(){
if(answer1.isChecked() || answer2.isChecked() || answer3.isChecked() || answer4.isChecked())
openQuestion2Activity();
else
noAnswerCheckedAlert();
}
}
and I get error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.RadioButton.isChecked()' on a null object reference
at promedica.test_osobowosci.Question1.isAnyRadioButtonChecked(Question1.java:76)
at promedica.test_osobowosci.Question1$1.onClick(Question1.java:38)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
When I click nextButton with checking, without before clicked any RadioButton.
But I click any RadioButton and next click nextButton, the application show alert all the time. I tried declare RadioButtons in OnClickListener button method but is not solve my problems.
What is the problem?
Edit:
activity_question1.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Question1">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="46dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="false"
android:layout_marginTop="40dp"
android:layout_marginLeft="85dp"
android:text="Question1"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title"
app:layout_constraintEnd_toEndOf="parent"
tools:layout_editor_absoluteY="25dp" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="19dp"
android:layout_marginTop="0dp"
android:scaleType="fitXY"
android:cropToPadding="false"
app:srcCompat="#mipmap/obrot" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="19dp"
android:layout_marginTop="0dp"
android:scaleType="fitXY"
android:cropToPadding="false"
app:srcCompat="#mipmap/obrot" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="159dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="109dp"
android:layout_marginLeft="85dp">
<RadioButton
android:id="#+id/radioButtonQ1_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer1"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer2"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer3"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:inputType="textMultiLine"
android:text="Answer4"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
</RadioGroup>
<Button
android:id="#+id/nextTo2Button"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_marginTop="305dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="161dp"
android:background="#drawable/custom_button"
android:text="Next"
android:textAllCaps="false"
android:textColor="#ffffff" />
<Button
android:id="#+id/stopButton1"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_marginTop="395dp"
android:layout_centerHorizontal="true"
android:background="#drawable/custom_button_blue"
android:text="Stop"
android:textAllCaps="false"
android:textColor="#ffffff" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp"
android:layout_alignParentStart="true"
app:srcCompat="#mipmap/oie_transparent" />
</RelativeLayout>
Instead of checking for an individual radiobutton do the following:
if (radioGroup.getCheckedRadioButtonId() == -1)
{
noAnswerCheckedAlert();
}
else
{
openQuestion2Activity();
}
<RadioGroup
android:id="#+id/radio"
android:layout_width="match_parent"
android:layout_height="159dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="109dp"
android:layout_marginLeft="85dp">
<RadioButton
android:id="#+id/radioButtonQ1_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer1"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer2"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer3"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:inputType="textMultiLine"
android:text="Answer4"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
</RadioGroup>
JAVA CODE:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio);
nextButton = findViewById(R.id.nextTo2Button);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (radioGroup.getCheckedRadioButtonId() == -1) {
// no radio buttons are checked
noAnswerCheckedAlert();
} else {
// one of the radio buttons is checked
openQuestion2Activity();
}
}
});
Use the RadioGroup to check if no RadioButton is checked:
RadioGroup radioGroup;
....
if(radioGroup.getCheckedRadioButtonId() != answer1 || ........)
{
AlertDialog...
}
else
{
openQuestion2Activity();
}
you can use RadioGroup in the xml
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/radiobuttongroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_q1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/option1"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_q2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/options2"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
And Activity file
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radiobuttongroup);
final String[] checkedOption = new String[1];
radioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radio_q1:
checkedOption[0] = 1;
break;
case R.id.radio_q2:
checkedOption[0] = 2;
break;
.
.
.
}
}
});

Toasting with Favourite Buttons Android

I'm making a cooking application and I have created a favourite button that seems to work ok i click on it and it changes colour. I tried to create a toast where when I click on the button a message wil come up to say
added to favourites
and
removed from favourites
the code I have used for the toast doesn't have an errors and the application runs perfectly but the toast does not appear
here is my code that I have used
Toast.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;
public class Favourite_Toast extends Activity {
private CheckBox fav;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe);
addListenerOnChk();
}
public void addListenerOnChk() {
fav = (CheckBox) findViewById(R.id.checkBox);
fav.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(Favourite_Toast.this,
"Added to Favourites", Toast.LENGTH_LONG).show();
}
}
});
}
}
favourite.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/heart_red" />
<item android:state_checked="false"
android:drawable="#drawable/heart_grey" />
</selector>
recipe.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/jbsbackground2"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_detail"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="#drawable/barbecuedporkribs" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_below="#+id/iv_detail"
android:background="#3D3C3A" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#+id/scrollView1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:text="#string/textView2"
android:layout_toLeftOf="#id/favourites"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvName"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:button="#drawable/favourite"
android:checked="false"
android:layout_alignTop="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="46dp"
android:layout_marginEnd="46dp"
android:clickable="true" />
<TextView
android:id="#+id/tvTD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvTD"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
android:layout_below="#+id/checkBox"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/tvIngredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvTD"
android:layout_marginTop="2dp"
android:text="#string/tvIngredients"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic" />
<TextView
android:id="#+id/tvK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvIngredients"
android:layout_marginTop="5dp"
android:text="#string/tvK"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/tvPreparation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvK"
android:layout_marginTop="2dp"
android:text="#string/tvPreparation"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I've used a checkbox for the favourite button could that be the reason why the toast is not working
Also would anyone be able to tell me how to make the code so that once you have clicked on the favourite button it will stay clicked, because as it is it does not stay clicked on
Thanks in advance
I recommend you change your OnClickListener with OnCheckedChangeListener. It will get invoked when the user checks / unchecks the state of the CheckBox. IMHO, it's the better listener for this widget.
Try the code below
if ((fav.isChecked()) {
Toast.makeText(Favourite_Toast.this, "Added to Favourites", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Favourite_Toast.this, "Removed From Favourites", Toast.LENGTH_LONG).show();
}
Use this code in MainActivity... THIS WILL SOLVE YOUR PROBLEM:
public class MainActivity extends Activity {
private CheckBox chk;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chk = (CheckBox) findViewById(R.id.checkBox1);
chk.setOnClickListener(checkboxClickListener);
}
View.OnClickListener checkboxClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
boolean checked = ((CheckBox) view).isChecked();
if (checked) {
Toast.makeText(MainActivity.this,"Added to favourite", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"Removed from favourite", Toast.LENGTH_LONG).show();
}
}
};
}

An alert dialog with an image will appear once LinearLayout is clicked

Symptoms.java
package com.example.des;
import java.util.ArrayList;
import com.example.des.R;
import com.example.des.Symptoms;
import com.example.des.Learn_Dengue;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Symptoms extends Activity implements OnClickListener{
//set variables
CheckBox q1; CheckBox q2; CheckBox q3; CheckBox q4; CheckBox q5; CheckBox q6;
CheckBox q7; CheckBox q8; CheckBox q9 ;CheckBox q10; CheckBox q11; CheckBox q12;
LinearLayout sas1; LinearLayout sas2; LinearLayout sas3; LinearLayout sas4; LinearLayout sas5; LinearLayout sas6;
LinearLayout sas7; LinearLayout sas8; LinearLayout sas9; LinearLayout sas10; LinearLayout sas11; LinearLayout sas12;
Button btndone;
Button btnlearn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
q1 = (CheckBox)findViewById(R.id.q1a); //Question number 1--main symptom
q2 = (CheckBox)findViewById(R.id.q2a);
q3 = (CheckBox)findViewById(R.id.q3a);
q4 = (CheckBox)findViewById(R.id.q4a);
q5 = (CheckBox)findViewById(R.id.q5a);
q6 = (CheckBox)findViewById(R.id.q6a);
q7 = (CheckBox)findViewById(R.id.q7a);
q8 = (CheckBox)findViewById(R.id.q8a);
q9 = (CheckBox)findViewById(R.id.q9a);
q10 = (CheckBox)findViewById(R.id.q10a);
q11 = (CheckBox)findViewById(R.id.q11a);
q12 = (CheckBox)findViewById(R.id.q12a);
sas1 = (LinearLayout)findViewById(R.id.sas1view);
sas2 = (LinearLayout)findViewById(R.id.sas2view);
sas3 = (LinearLayout)findViewById(R.id.sas3view);
sas4 = (LinearLayout)findViewById(R.id.sas4view);
sas5 = (LinearLayout)findViewById(R.id.sas5view);
sas6 = (LinearLayout)findViewById(R.id.sas6view);
sas7 = (LinearLayout)findViewById(R.id.sas7view);
sas8 = (LinearLayout)findViewById(R.id.sas8view);
sas9 = (LinearLayout)findViewById(R.id.sas9view);
sas10 = (LinearLayout)findViewById(R.id.sas10view);
sas11 = (LinearLayout)findViewById(R.id.sas11view);
sas12 = (LinearLayout)findViewById(R.id.sas12view);
btndone = (Button) findViewById(R.id.done);
btndone.setOnClickListener(this);
btnlearn = (Button) findViewById(R.id.learn);
btnlearn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// When button "Result" is pressed
switch (v.getId()) {
case R.id.done:
ArrayList<CheckBox> cbList = new ArrayList<CheckBox>();
// Array CheckBox 2 - 12 inside an Array (sub symptoms)
cbList.add((CheckBox)findViewById(R.id.q2a));
cbList.add((CheckBox)findViewById(R.id.q3a));
cbList.add((CheckBox)findViewById(R.id.q4a));
cbList.add((CheckBox)findViewById(R.id.q5a));
cbList.add((CheckBox)findViewById(R.id.q6a));
cbList.add((CheckBox)findViewById(R.id.q7a));
cbList.add((CheckBox)findViewById(R.id.q8a));
cbList.add((CheckBox)findViewById(R.id.q9a));
cbList.add((CheckBox)findViewById(R.id.q10a));
cbList.add((CheckBox)findViewById(R.id.q11a));
cbList.add((CheckBox)findViewById(R.id.q12a));
// Starts Looping if CheckBox number 1 is selected and the others (result SUSPICIOUS)
if (q1.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count == 2 || count == 1 || count == 0) {
new AlertDialog.Builder(this).setMessage(R.string.suspicious).show();
}
}
// START Looping if CheckBox number 1 is selected but 11 and 12 are not (result MOST LIKELY)
if (q1.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count >= 3 && count < 11) {
new AlertDialog.Builder(this).setMessage(R.string.most_likely).show();
}
}
// START Looping if all CheckBox are selected(result POSITIVE)
if (q1.isChecked() && q2.isChecked() && q3.isChecked() && q4.isChecked() &&
q5.isChecked() && q6.isChecked() && q7.isChecked() && q8.isChecked() &&
q9.isChecked() && q10.isChecked() && q11.isChecked() && q12.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count == 11) {
new AlertDialog.Builder(this).setMessage(R.string.positive).show();
}
}
// Starts Looping if CheckBox number 1 is not selected and the others are checked
if (!q1.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count <= 1 || count > 1) {
//this Toast will show when only 1 or more check box will be checked(excluding check box #1)
/*Toast toast= Toast.makeText(getApplicationContext(), getString(R.string.negative), Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
*/
new AlertDialog.Builder(this).setMessage(R.string.negative).show();
}
}
break;
// When button "Learn More" is pressed
case R.id.learn:
Intent q = new Intent(this, Learn_Dengue.class);
startActivity(q);
break;
}
Here start my question.
sas1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
}
}
questions.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
style="#style/styleName"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/warning"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas1view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="#string/sas1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas2view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q3a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas3view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q4a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas4view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas4"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q5a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas5view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q6a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas6view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas6"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q7a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas7view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas7"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q8a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas8view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas8"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q9a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas9view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas9"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q10a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas10view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas10"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q11a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas11view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas11"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q12a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas12view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas12"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" >
<Button
android:id="#+id/done"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="20dp"
android:background="#drawable/btn_orange"
android:text="#string/done"
android:textColor="#ffffff"
android:textSize="15sp" />
<Button
android:id="#+id/learn"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="#drawable/btn_black"
android:text="#string/learn"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
here is my full code as you requested #mobie.. the Symptoms.java and the question.xml..dont matter the other code..just on the alert dialog that i asked.
Create Layout Like Below for Dialog Box : dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/image"/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"
/>
</RelativeLayout>
Then, Where you want to place the Alert, Do like below
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Your Title");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Text");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();

Android Temperature Vogella tutorial error

I'm using the vogella tutorial, and I'm almost followed the instructions exactly, but it is sort of giving me an error on text and radiobutton saying it cannot be resolved or it is not in field. I'm really new to Android. Would you please help me out with the code.
package convert.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText text;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
}
// This method is called at button click because we assigned the name to the
// "On Click property" of the button
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(String
.valueOf(convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(false);
fahrenheitButton.setChecked(true);
} else {
text.setText(String
.valueOf(convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
break;
}
}
// Converts to celsius
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}
Here is my code to activity_main.xml
<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:background="#color/myColor" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="numberSigned|numberDecimal"
>
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/celsius" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fahrenheit" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calc"
android:onClick="myClickHandler">
</Button>
</RelativeLayout>
The error is on editText1, radio0, radio1 and button1... For all them the error is the same,i.e it cannot be resolved or is not in a field.
Here it is...
I used your xml file and it is resolved perfectly
EditText text;
RadioButton r1,r2;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.editText1);
r1 =(RadioButton)findViewById(R.id.radio0);
r1 =(RadioButton)findViewById(R.id.radio1);
}
and do like this
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// your Switch code here
}
});
}
I checked your code it's perfectly working change the activity_main like this
<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"
>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="numberSigned|numberDecimal"
>
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="celsius" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fahrenheit" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/editText1"
android:onClick="myClickHandler"
android:text="calc" />
</RelativeLayout>

Adding RadioButtons in Android

I want to add radio buttons such that only one of them is selected at a time among 4 buttons and I want to place them as:
RadioButton1 RadioButton2
RadioButton3 RadioButton4
I am trying the following code but 1&2 forms a grp and 3&4 forms a different grp and there are two value selected a time. Can anyone check it and share the correct way to do it?
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/apple" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/rdogrp_main"
android:orientation="vertical"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rdogrp1"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"
android:textColor="#000000"
/>
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="BlueBerry"
android:textColor="#000000"
/>
</RadioGroup>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rdogrp2"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple "
android:textColor="#000000"
/>
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Santra :P"
android:textColor="#000000"
/>
</RadioGroup>
</RadioGroup>
</LinearLayout>
Ok I find a solution for you:
I know it is not the best solution but it works! :)
Just copy the xml file and activity code bellow:
Activity:
package com.example.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RadioButton;
public class MainActivity extends Activity {
private static String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RadioButton radiobutton1 = (RadioButton) findViewById(R.id.RadioButton1);
final RadioButton radiobutton2 = (RadioButton) findViewById(R.id.RadioButton2);
final RadioButton radiobutton3 = (RadioButton) findViewById(R.id.RadioButton3);
final RadioButton radiobutton4 = (RadioButton) findViewById(R.id.RadioButton4);
radiobutton1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton1.setChecked(true);
radiobutton2.setChecked(false);
radiobutton3.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton2.setChecked(true);
radiobutton1.setChecked(false);
radiobutton3.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton3.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton3.setChecked(true);
radiobutton1.setChecked(false);
radiobutton2.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton4.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton4.setChecked(true);
radiobutton1.setChecked(false);
radiobutton2.setChecked(false);
radiobutton3.setChecked(false);
return true;
}
});
}
}
activity_main.xml :
<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"
tools:context=".MainActivity" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FFDDDDDD" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioGroup
android:id="#+id/rdogrp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="BlueBerry"
android:textColor="#000000" />
</RadioGroup>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioGroup
android:id="#+id/rdogrp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple "
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Santra :P"
android:textColor="#000000" />
</RadioGroup>
</TableRow>
</TableLayout>
</RelativeLayout>
For that you need to create only one RadioGroup and add four different RadioButton.
And for adding UI refer this url : Manage Layout Inside a Horizontal Oriented Radiogroup .
I hope this will help you.
Try Like this..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/rdogrp_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="India"
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="74dp"
android:layout_height="wrap_content"
android:layout_weight="0.57"
android:text="Austraila"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Srilanka "
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.48"
android:text="SouthAfrica"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
Let me know if you solve the problem...

Categories

Resources