checkbox label to string - java

I have 12 checkboxes and I need to make sure the user selects only 5 of them and then make the 5 choices labels enter into 5 different strings.
How can I do that?
This is my XML file:
<CheckBox
android:id="#+id/chkInterestsMovies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="223dp"
android:layout_y="257dp"
android:text="סרטים" />
<CheckBox
android:id="#+id/chkInterestsAnimals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="222dp"
android:layout_y="306dp"
android:text="חיות" />
<CheckBox
android:id="#+id/chkInterestsShopping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="222dp"
android:layout_y="356dp"
android:text="קניות" />
<CheckBox
android:id="#+id/chkInterestsBooks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="149dp"
android:layout_y="257dp"
android:text="ספרים" />
<CheckBox
android:id="#+id/chkInterestsRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="147dp"
android:layout_y="305dp"
android:text="מסעדות" />
<CheckBox
android:id="#+id/chkInterestsComputers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="153dp"
android:layout_y="356dp"
android:text="מחשבים" />
<CheckBox
android:id="#+id/chkInterestsTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="72dp"
android:layout_y="255dp"
android:text="טלויזיה" />
<CheckBox
android:id="#+id/chkInterestsPubs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="76dp"
android:layout_y="306dp"
android:text="פאבים" />
<CheckBox
android:id="#+id/chkInterestsDancing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="76dp"
android:layout_y="355dp"
android:text="ריקודים" />
<CheckBox
android:id="#+id/chkInterestsMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="7dp"
android:layout_y="257dp"
android:text="מוזיקה" />
<CheckBox
android:id="#+id/chkInterestsCoffe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="8dp"
android:layout_y="304dp"
android:text="בתי קפה" />
<CheckBox
android:id="#+id/chkInterestsOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="7dp"
android:layout_y="350dp"
android:text="אחר" />

You need to consider selecting and unselecting of checkboxes as well. At any given point, if user selected 5 among all, you can go forward with.
Roughly, in your onCreate
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_LAYOUT);
//...
CheckBox checkboxMovies = (CheckBox)findViewById(R.id.chkInterestsMovies);
checkboxMovies.setOnCheckedChangeListener(this);
CheckBox checkboxAnimals = (CheckBox)findViewById(R.id.chkInterestsAnimals);
checkboxAnimals.setOnCheckedChangeListener(this);
CheckBox checkboxShopping = (CheckBox)findViewById(R.id.chkInterestsShopping);
checkboxShopping.setOnCheckedChangeListener(this);
CheckBox checkboxBooks = (CheckBox)findViewById(R.id.chkInterestsBooks);
checkboxBooks.setOnCheckedChangeListener(this);
CheckBox checkboxRestaurants = (CheckBox)findViewById(R.id.chkInterestsRestaurants);
checkboxRestaurants.setOnCheckedChangeListener(this);
CheckBox checkboxComputers = (CheckBox)findViewById(R.id.chkInterestsComputers);
checkboxComputers.setOnCheckedChangeListener(this);
CheckBox checkboxTV = (CheckBox)findViewById(R.id.chkInterestsTV);
checkboxTV.setOnCheckedChangeListener(this);
CheckBox checkboxPubs = (CheckBox)findViewById(R.id.chkInterestsPubs);
checkboxPubs.setOnCheckedChangeListener(this);
CheckBox checkboxDancing = (CheckBox)findViewById(R.id.chkInterestsDancing);
checkboxDancing.setOnCheckedChangeListener(this);
//...
}
& your onCheckedChanged
Map<String, String> states = new HashMap<String, String>();
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
states.put(buttonView.getId(), buttonView..getText().toString());
} else
{
states.remove(buttonView.getId());
}
if(states.size() >= 5)
{
// Your Condition
// selectedStrings will now have all 5 checked CheckBox's Texts
List<String> selectedStrings = new ArrayList<String>(states.values());
}
}
Hope it helps.

Try out this one may be it will help you.
int checkBoxCounter = 0;
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked && checkBoxCounter<6){
// apply check and increment check counter
checkBoxCounter++;
}else{
//todo nothing
}
}

Related

Issue with RadioButton with RadioGroup in Android using a toggle feature/isChecked and cleaning code up

Hi I been trying for a while and going through lots of theories and stack answers. Though I just can't figure out how to take the code below for a simple trivia app, using a RadioButton in a RadioGroup, then add one to score when checked. The issue is if you keep pressing the correct answer the score just increments. I would like to apply some type of toggle if the answer is changed the points adjust accordingly.
I would like to also clean this code up so it's not so repetitive. Using a new class or method. Though I can't figure out how to implement it.
Main
package com.example.samuel.trivaapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//Score keeper.
private int score = 0;
private int correct = 0;
private int wrong = 0;
// determines if the selected was right or wrong, then used to add up the score.
boolean question1 = false;
boolean question2 = false;
boolean question3 = false;
// private boolean question3 = false;
// private boolean question4 = false;
// private boolean question5 = false;
// private boolean question6 = false;
// private boolean question7 = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup rGroup1 = (RadioGroup) findViewById(R.id.first_question_group);
RadioButton checkedRadioButton1 = (RadioButton) rGroup1.findViewById(rGroup1.getCheckedRadioButtonId());
rGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer1 == checkedId) {
question1 = true;
} else {
question1 = false;
}
}
});
RadioGroup rGroup2 = (RadioGroup) findViewById(R.id.second_question_group);
RadioButton checkedRadioButton2
= (RadioButton) rGroup2.findViewById(rGroup2.getCheckedRadioButtonId());
rGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer2 == checkedId) {
question2 = true;
} else {
question2 = false;
}
}
});
RadioGroup rGroup3 = (RadioGroup) findViewById(R.id.third_question_group);
RadioButton checkedRadioButton3
= (RadioButton) rGroup3.findViewById(rGroup3.getCheckedRadioButtonId());
rGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer3 == checkedId) {
question3 = true;
} else {
question3 = false;
}
}
});
}
//used to diplay the players previous score on screen.
public void displayScore() {
TextView scoreView = (TextView) findViewById(R.id.score_keeper);
score = correct + wrong;
scoreView.setText(Integer.toString(correct));
}
//used to submit results with button and tally score.
public void submitResults(View view){
question1();
question2();
question3();
displayScore();
score= 0;
correct = 0;
wrong = 0;
}
//selector for radioGroup1 check if id matches correct answer.
//submit results calls this to find out if the answer was correct for question 1 to use for tally.
public void question1() {
if(question1) {
correct++;
} else {
wrong--;
}
}
//submit results calls this to find out if the answer was correct for question 2 to use for tally.
public void question2() {
if(question2) {
correct++;
} else {
wrong--;
}
}
//submit results calls this to find out if the answer was correct for question 3 to use for tally.
public void question3() {
if(question3) {
correct++;
} else {
wrong--;
}
}
}
XML
<?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="com.example.samuel.trivaapp.MainActivity">
<TextView
android:id="#+id/header_logo"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/player_name"
android:layout_below="#+id/header_logo"
android:inputType="textCapWords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/enter_player_name"
android:layout_margin="8dp"/>
<LinearLayout
android:id="#+id/score_board"
android:layout_below="#+id/player_name"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score:"
android:textStyle="bold"
android:layout_marginLeft="8dp"
/>
<TextView
android:id="#+id/score_keeper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginLeft="8dp"/>
</LinearLayout>
<!--questions scroll view.-->
<ScrollView
android:id="#+id/question_scroll_view"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/score_board">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="#+id/first_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 1? "/>
<RadioButton
android:id="#+id/correct_answer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="#+id/test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/test"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="#+id/second_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 2? "/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:id="#+id/correct_answer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="#+id/test3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/test2"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="#+id/third_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 3? "/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:id="#+id/correct_answer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:onClick="submitResults"
android:layout_below="#id/test3"
android:layout_centerInParent="true"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Okay I believe I figured out what I wanted to do. If anyone else has this issue or a similar problem, of using multiple RadioGroups I hope this helps.
https://github.com/raregamer/TrivaApp1/tree/trivia2/app/src/main/java/com/example/samuel/trivaapp

Disabling RadioGroup to avoid multiple counts of a score

I am trying to disable my RadioGroup so that the user cannot re-select and therefore my score count rises. I have seen an implementation here:
How to disable a RadioGroup until checkbox is checked
Albeit tried to go about it this way
My xml:
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/sm_margin"
android:orientation="horizontal">
<RadioButton
android:id="#+id/pates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/pates"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/chips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/chips"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/frites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/frites"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/crepes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/def_margin"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/crepes"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="#+id/question2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/def_margin"
android:background="#drawable/layout_bg"
android:fontFamily="#font/cambria"
android:padding="#dimen/tv_padding"
android:text="#string/question2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="70dp"
android:adjustViewBounds="true"
android:src="#drawable/jc_fav" />
<RadioGroup
android:id="#+id/rg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/sm_margin"
android:orientation="vertical">
<RadioButton
android:id="#+id/mille_feuille"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/milleFeuille"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/creme_chantilly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cremeChantilly"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/opera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/opera"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/creme_brulee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/def_margin"
android:text="#string/cremeBrulee"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
Java:
int score = 0;
RadioGroup rgQ1;
RadioGroup rgQ2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rgQ1 = findViewById(R.id.rg1);
rgQ2 = findViewById(R.id.rg2);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked? then assign into a boolean named 'checked'
boolean checked = ((RadioButton) view).isChecked();
// Question 1 logic
// Check correct answer is checked and update score
switch (view.getId()) {
case R.id.frites:
if (checked) score += 1;
Log.v("MainActivity", "score" + score);
break;
}
// Disable RadioButtons of rg1
for (int i = 0; i < rgQ1.getChildCount(); i++) {
(rgQ1.getChildAt(i)).setEnabled(false);
}
// Question 2 logic
// Check correct answer is checked and update score
switch (view.getId()) {
case R.id.mille_feuille:
if (checked) score += 1;
Log.v("MainActivity", "score" + score);
break;
}
// Disable RadioButtons of rg2
for (int i = 0; i < rgQ2.getChildCount(); i++) {
(rgQ2.getChildAt(i)).setEnabled(false);
}
}
}
It disables the first radiogroup when an item is selected and also the 2nd (before a user has selected for the 2nd radiogroup.
Any ideas?
You are disabling your second radio group in the same click event. Check which radio button group is selected.
Like this, Similarly check group two and disable radio button group two!
int selectedId = radioGroup.getCheckedRadioButtonId();
if(selectedId != null){
//Then disable radio button group one
}

null pointer exception on radio group

I am trying to make a popup window in an activity. The popup window shows up when we click a button in the activity. The layout for popup window is to show a radio group containing 4 radio buttons, a button and a seek bar and has to return a value to the main activity based on the selected radio button when the button in the pop up window is pressed.
When i run the app and open the pop up window it is giving we this error:
"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference "
The code for my main activity is:
public class CustomMenuActivity extends Activity
{
String ingredientName;
String ingredientQuantity;
private PopupWindow pw;
Button setQuantity;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_menu);
setQuantity = (Button) findViewById(R.id.btnSetQty);
setQuantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
initiatePopupWindow();
}
});
}
private void initiatePopupWindow()
{
try {
//We need to get the instance of the LayoutInflater, use the context of this activity
LayoutInflater inflater = (LayoutInflater) CustomMenuActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.set_quantity_popup,
(ViewGroup) findViewById(R.id.popupElementid));
// create a 600px width and 570px height PopupWindow
pw = new PopupWindow(layout, 600, 570, true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroupid);
final String[] tempQtyVar = new String[1];
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rbqty30id:
tempQtyVar[0] = "30";
break;
case R.id.rbqty60id:
tempQtyVar[0] = "60";
break;
case R.id.rbqty90id:
tempQtyVar[0] = "90";
break;
case R.id.rbqtycutomid:
tempQtyVar[0] = "120";
break;
}
}
});
Button setQtyBtn = (Button) layout.findViewById(R.id.buttonOkSetQtyid);
setQtyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ingredientQuantity = tempQtyVar[0];
Toast.makeText(getApplicationContext(),ingredientQuantity,Toast.LENGTH_LONG).show();
pw.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
The layout.xml for my pop up window is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#fd050505"
android:padding="30dp"
android:id="#+id/popupElementid">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SET QUANTITY"
android:textColor="#84e9e6"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="#+id/popupTitleid"/>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_below="#+id/popupTitleid"
android:orientation="horizontal"
android:background="#drawable/btn_rounded_boarder"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroupid"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" 30ml "
android:textColor="#84e9e6"
android:id="#+id/rbqty30id"
android:checked="true" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 60ml "
android:id="#+id/rbqty60id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 90ml "
android:id="#+id/rbqty90id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" Custom Value "
android:id="#+id/rbqtycutomid"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true">
<SeekBar
android:id="#+id/customqtySBid"
android:layout_height="fill_parent"
android:layout_width="600dp"
android:textColor="#84e9e6"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
android:paddingBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonOkSetQtyid"
android:textColor="#84e9e6"
android:text="OK"
android:background="#drawable/btn_rounded_boarder"
android:layout_marginTop="275dp"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I tried looking for answer online but didn't really find solution specific to my problem. Please suggest any solutions.
Thanks in advance.
You seem to be inflating the wrong layout. Change the correct layout.xml in
setContentView(R.layout.activity_custom_menu);

Exception setBackgroundColor

I have a question for my code, it should change the background color of my Start-Activity. I should choose between blue and red with radio buttons (in RadioGroup). I always get an Error when i click on the RadioButton Red/Blue in the settings menue.
These two lines are wrong?
ChangeToRed(background); background.setBackgroundColor(0x0000FF00);
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference
at com.example.clecks.reaction_game.activity_settings.ChangeToBlue(activity_settings.java:67)
at com.example.clecks.reaction_game.activity_settings$2.onCheckedChanged(activity_settings.java:42)
Here is my java code:
public class activity_settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_settings);
ColorChange();
}
public void ColorChange() {
final RelativeLayout background = (RelativeLayout) findViewById(R.id.start);
final RadioButton ChangeToBlue = (RadioButton) findViewById(R.id.button_blue);
final RadioButton ChangeToRed = (RadioButton) findViewById(R.id.button_red);
final Button button_save = (Button) findViewById(R.id.button_save);
button_save.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
ChangeOption(background);
}
});
ChangeToBlue.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ChangeToBlue(background);
}
});
ChangeToRed.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ChangeToRed(background);
}
});
}
public void ChangeOption(RelativeLayout background) {
if (background.isEnabled()) {
background.setEnabled(false);
} else {
background.setEnabled(true);
}
}
public void ChangeToBlue(RelativeLayout background) {
background.setBackgroundColor(Color.BLUE);
background.invalidate();
}
public void ChangeToRed(RelativeLayout background) {
background.setBackgroundColor(Color.RED);
background.invalidate();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_settings, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
XML Code:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.clecks.reaction_game.activity_settings">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Einstellungen"
android:id="#+id/textView2"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView3"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Singalton"
android:id="#+id/textView4"
android:layout_gravity="center_horizontal" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="#+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="#+id/radioButton2"
android:layout_gravity="center_horizontal" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hintergrundfarbe"
android:id="#+id/textView6"
android:layout_gravity="center_horizontal" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/RadioGroup_Color">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rot"
android:id="#+id/button_red"
android:checked="false"
android:layout_gravity="center_horizontal" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blau"
android:id="#+id/button_blue"
android:checked="false"
android:layout_gravity="center_horizontal" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grün"
android:id="#+id/button_green"
android:checked="false"
android:layout_gravity="center_horizontal" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView7"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Spielmodus"
android:id="#+id/textView8"
android:layout_gravity="center_horizontal" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handy"
android:id="#+id/radioButton6"
android:layout_gravity="center_horizontal"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wii-Mote"
android:id="#+id/radioButton7"
android:layout_gravity="center_horizontal"
android:checked="false" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView9"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView10"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speichern"
android:id="#+id/button_save"
android:layout_gravity="center_horizontal" />
</LinearLayout>
You have no R.id.start in your layout, so findViewById(R.id.start) is returning null.
As your exception says, the method RelativeLayour.setBackgroundColor expects an int
Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference at com.example.clecks.reaction_game.activity_settings.changeToBlue
find here setBackgroundColor method documentation
Instead of hardcoding the color, you can use:
public void changeToBlue(RelativeLayout background) {
background.setBackgroundColor(Color.BLUE);
background.invalidate();
}
public void changeToRed(RelativeLayout background) {
background.setBackgroundColor(Color.RED);
background.invalidate();
}
Check here all Color references.

Checking at least one radio button is selected from each radiogroup in android?

How do I ensure that user is checked at least one radiobutton from each radiogroup. Like I have this radiogroup:
<RadioGroup
android:id="#+id/myradio_group_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="third" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fourt" />
</RadioGroup>
<RadioGroup
android:id="#+id/myradio_group_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="third" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fourt" />
</RadioGroup>
I want to check programmatically if user select at least one radiobutton or not?
Get a reference of the RadioGroup and then call getCheckedRadioButtonId() on the radio group if nothing is selected then the call will return -1.
See public int getCheckedRadioButtonId ()
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);
// Returns an integer which represents the selected radio button's ID
int selected = g.getCheckedRadioButtonId();
// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);
// Now you can get the text or whatever you want from the "selected" radio button
b.getText();
if (radioGroup.getCheckedRadioButtonId() != -1)
{
// hurray at-least on radio button is checked.
}
else
{
// pls select at-least one radio button.. since id is -1 means no button is check
}
This is the code i have used in one of my quiz app.. Have a look at the code if this helps..
private boolean checkAnswer(){
String answer = getSelection();
if(answer==null) {
Log.d("Questions", "No checkbox selected");
return false;
}
else {
// Do your stuff here
return true;
}
}
private String getSelection(){
if (c1.isChecked())
{
return c1.getText().toString();
}
if (c2.isChecked())
{
return c2.getText().toString();
}
if (c3.isChecked())
{
return c3.getText().toString();
}
if (c4.isChecked())
{
return c4.getText().toString();
}
return null;
}
You can use this method
radioGroup.getCheckedRadioButtonId();
It returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.
As mentioned in the documentation https://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId()

Categories

Resources