Android Radio Buttons - android

I am developing an android application and have run into a slight problem. On certain questions when a user clicks either yes or no followed by the next button they need to go to a different page, however my code seems to be incorrect can someone please help.
Thanks in advance.
below is my code:
public void Next_Btn3(View view){
boolean checked = ((RadioButton)view).isChecked();
switch (view.getId()){
case R.id.Yes_3:
if (checked)
new Intent(this, Questionnaire4.class);
startActivity(detailIntent);
break;
case R.id.No_3:
if (checked)
new Intent(this, Questionnaire6.class);
startActivity(detailIntent);
break;
}
}

In your code, you declare an Intent without actually assigning it. You also forgot to use brackets around your if body. Lastly, you have casted the clicked Button as a RadioButton, which should instead be found using findViewById(R.id.radio_id_here) ; Try this:
public void Next_Btn3(View view){
boolean checked = ((RadioButton)view).isChecked(); //MUST FIND RadioButton through ID, not cast clicked Button as a Radio Button
switch (view.getId()){
case R.id.Yes_3:
if (checked) {
Intent detailIntent = new Intent(this, Questionnaire4.class);
startActivity(detailIntent);
}
break;
case R.id.No_3:
if (checked) {
Intent detailIntent = new Intent(this, Questionnaire6.class);
startActivity(detailIntent);
}
break;
}
}

Related

How to storage previous clicked button?

I'm making a fragment of language game in which will be a matching of the word and the translation. I need to store the previously clicked button and after clicking on another button compare them, and if they match to make them invisible.
But I'm getting an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.Button.getText()' on a null object reference
On the line String text = buf.getText().toString();
All buttons were defined before.
#Override
public void onClick(View v) {
if (button_prev != 9) {
if (v.getId() != button_prev) {
Button buf = (Button) v.findViewById(button_prev);
String text = buf.getText().toString();
if (book_array.indexOf(text) - book_array.indexOf((String) ((Button) v).getText()) != 0) {
// book_array obj contains word and translating
i--;
v.findViewById(v.getId()).setVisibility(View.INVISIBLE);
v.findViewById(button_prev).setVisibility(View.INVISIBLE);
}
}
}
switch (v.getId()) {
// Storage the previously clicked the button in button_prev
case R.id.button1:
button_prev = Button_1.getId();
// button_prev = R.id.button1;
break;
case R.id.button2:
button_prev = Button_2.getId();
break;
case R.id.button3:
button_prev = Button_3.getId();
break;
case R.id.button4:
button_prev = Button_4.getId();
break;
case R.id.button5:
button_prev = Button_5.getId();
break;
case R.id.button6:
button_prev = Button_6.getId();
break;
case R.id.button7:
button_prev = Button_7.getId();
break;
case R.id.button8:
button_prev = Button_8.getId();
break;
default:
break;
Maybe someone could fix the code? Is there any correct way to store clicked buttons ?
You can declare a POJO for lastclicked view like following to store lastClicked view.
public class LastViewClicked {
private View view;
public View getView() {
return view;
}
public void setView(View view) {
this.view = view;
}
}
Now to when you click on any button, update the view Object inside LastClickedView using setView Method and when you need to know what was the last clicked button, you create a switch statement with all Ids as cases and if it matches you can do your operation.
// This Function gets the last clicked item which has opened up the Alert Dialog for
// selection and Updates its UI...
private void UpdateLastClickedView(int position, ArrayAdapter arrayAdapter) {
View v = lastViewClicked.getView();
// cast according to your views...
AppCompatTextView appCompatTextView = (AppCompatTextView) v;
// do your operation...
switch(appCompatTextView.getId()){
case R.id.tv1:
// hide or whatever you want to do...
break;
case R.id.tv2:
// hide or whatever you want to do...
break;
}
}
Your null pointer error may be the result of fragment recreation, which results in the value of button_prev invalid. And, the initial value of button_prev may be the reason.
findViewById is a time consuming function. The view binding should be processed once, and once only, in onCreate()
It's better if using MVC, MVP or MVVM. User a model to describe your game logic. Focus on the data change, and the UI should be changed with it.
Declare button_prev as field in your activity or fragment and use getter and setter to access it inside onclick.

What difference does it make while using switch-case vs. if-else for radioGroup in Android?

Right now I use switch-case
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.Eng:
// do operations specific to this selection
setLocale("en_US");
Intent intentEng = new Intent(getActivity(), Some.class);
startActivity(intentEng);
break;
case R.id.French:
// do operations specific to this selection
setLocale("fr");
Intent intent = new Intent(getActivity(), Some.class);
startActivity(intent);
break;
}
}
});
What happens is sometimes I click first radio button and I immediately click second one as well.
Within the case block I pass the intent to a Service class which talks to web services and updates an arrayAdapter for ListView.
Since I quickly click both radio buttons, the UI gets results of both the languages (case blocks).
1) How to avoid this ?
2) Does using switch case in place of if-else matters in these issues?
After receiving onCheckedChanged event disable your radio buttons
It is completely depends on Your UI. you disable the radio buttons after "onCheckedChanged" called also not correct. because after selecting one language user may be want to change the selection, so you should provide confirmation component in the UI.
What is the difference here if we use the switch case or if-else for your scenario.
I may be misunderstanding what you are doing but assuming you are launching and showing another activity on top of the current one you can call finish() after starting the new activity.
Otherwise you can declare a boolean field shouldHandleCheckChanged or something to keep track of whether to process any of the cases inonCheckChanged(). You could set the value to false right after starting the new activity and then set it to true once you receive whatever update you are expecting.
You can do something like this, in your activity class make a boolean field:
Private isLoadingNewActivity = false;
In your onResume make it false:
isLoadingNewActivity = false;
And in your listener check it:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
If(isLoadingNewActivity)
return;
switch(checkedId){
case R.id.Eng:
// do operations specific to this selection
setLocale("en_US");
Intent intentEng = new Intent(getActivity(), Some.class);
startActivity(intentEng);
isLoadingNewActivity = true;
break;
case R.id.French:
// do operations specific to this selection
setLocale("fr");
Intent intent = new Intent(getActivity(), Some.class);
startActivity(intent);
isLoadingNewActivity = true;
break;
}
}
});

different URL opening the same

So guys, in my app, i build a menu, with an expandablelistview, and one of the groups have 4 childs, one that open a facebook page, another a website page, a youtube page and a google+ page. But No matter where i click all of them opens the google+ page, and i dont see why. Here's the code:
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
int pos = childPosition;
//clique em contatos
if(groupPosition == 5){
switch(pos) {
//Clique em emails e contatos
case 0:
Intent i = new Intent(MainActivity.this, Contatos.class);
MainActivity.this.startActivity(i);
}
}
//clique em hiperligacoes
if(groupPosition == 3){
switch(pos) {
//click no facebook
case 0:
Intent browserFace = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/moises.transporte.passageiros?fref=ts"));
startActivity(browserFace);
//click no site
case 1:
Intent browserSite = new Intent(Intent.ACTION_VIEW, Uri.parse("http://moises-transportes.pt/"));
startActivity(browserSite);
//click no youtube
case 2:
Intent browserYoutube = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/channel/UCXeHbISNnc0eLCPnTeolxLg"));
startActivity(browserYoutube);
//click no google+
case 3:
Intent browserGoogle = new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/111005531753993560637/about"));
startActivity(browserGoogle);
}
}
Can you guys help me finding why is this happening ?
break.
Use break after every case statement. Orelse all the cases after the correct one is executed one after the other.
if(groupPosition == 3){
switch(pos) {
//click no facebook
case 0:
Intent browserFace = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/moises.transporte.passageiros?fref=ts"));
startActivity(browserFace);
break;
//click no site
case 1:
Intent browserSite = new Intent(Intent.ACTION_VIEW, Uri.parse("http://moises-transportes.pt/"));
startActivity(browserSite);
break;
//click no youtube
case 2:
Intent browserYoutube = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/channel/UCXeHbISNnc0eLCPnTeolxLg"));
startActivity(browserYoutube);
//click no google+
break;
case 3:
Intent browserGoogle = new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/111005531753993560637/about"));
startActivity(browserGoogle);
break;
}
}
You forgot to break your cases in switch. Write break; before you write the next case for each. Also, add a default case.

switch case using variables, repetitive code

I have 50 Buttons. I use the switch statement as shown below. Each case has the same On-Click code. But I make many changes to the code for each Button, and it's really tedious work and very repetitive to change the code the same way 50 times. My code currently looks like this:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case button1:
if(button_list.get(1).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(1).getText().toString());
is_clicked= (String)button_list.get(1).getTag();
if ("0".equals(is_clicked)){
button_list.get(1).setBackgroundResource(R.drawable.button_pressed);
button_list.get(1).setTag("1");
}else{
button_list.get(1).setBackgroundResource(R.drawable.button_normal);
button_list.get(1).setTag("0");
}
break;
case button2:
if(button_list.get(2).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(2).getText().toString());
is_clicked= (String)button_list.get(2).getTag();
if ("0".equals(is_clicked)){
button_list.get(2).setBackgroundResource(R.drawable.button_pressed);
button_list.get(2).setTag("1");
}else{
button_list.get(2).setBackgroundResource(R.drawable.button_normal);
button_list.get(2).setTag("0");
}
break;
case button3:
if(button_list.get(3).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(3).getText().toString());
is_clicked= (String)button_list.get(3).getTag();
if ("0".equals(is_clicked)){
button_list.get(3).setBackgroundResource(R.drawable.button_pressed);
button_list.get(3).setTag("1");
}else{
button_list.get(3).setBackgroundResource(R.drawable.button_normal);
button_list.get(3).setTag("0");
}
break;
case button4:
if(button_list.get(4).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(4).getText().toString());
is_clicked= (String)button_list.get(4).getTag();
if ("0".equals(is_clicked)){
button_list.get(4).setBackgroundResource(R.drawable.button_pressed);
button_list.get(4).setTag("1");
}else{
button_list.get(4).setBackgroundResource(R.drawable.button_normal);
button_list.get(4).setTag("0");
}
break;
case button5:
if(button_list.get(5).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(5).getText().toString());
is_clicked= (String)button_list.get(5).getTag();
if ("0".equals(is_clicked)){
button_list.get(5).setBackgroundResource(R.drawable.button_pressed);
button_list.get(5).setTag("1");
}else{
button_list.get(5).setBackgroundResource(R.drawable.button_normal);
button_list.get(5).setTag("0");
}
break;
case button6:
if(button_list.get(6).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(6).getText().toString());
is_clicked= (String)button_list.get(6).getTag();
if ("0".equals(is_clicked)){
button_list.get(6).setBackgroundResource(R.drawable.button_pressed);
button_list.get(6).setTag("1");
}else{
button_list.get(6).setBackgroundResource(R.drawable.button_normal);
button_list.get(6).setTag("0");
}
break;
case button7:
if(button_list.get(7).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(7).getText().toString());
is_clicked= (String)button_list.get(7).getTag();
if ("0".equals(is_clicked)){
button_list.get(7).setBackgroundResource(R.drawable.button_pressed);
button_list.get(7).setTag("1");
}else{
button_list.get(7).setBackgroundResource(R.drawable.button_normal);
button_list.get(7).setTag("0");
}
break;
case button8:
if(button_list.get(8).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(8).getText().toString());
is_clicked= (String)button_list.get(8).getTag();
if ("0".equals(is_clicked)){
button_list.get(8).setBackgroundResource(R.drawable.button_pressed);
button_list.get(8).setTag("1");
}else{
button_list.get(8).setBackgroundResource(R.drawable.button_normal);
button_list.get(8).setTag("0");
}
break;
case button9:
if(button_list.get(9).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(9).getText().toString());
is_clicked= (String)button_list.get(9).getTag();
if ("0".equals(is_clicked)){
button_list.get(9).setBackgroundResource(R.drawable.button_pressed);
button_list.get(9).setTag("1");
}else{
button_list.get(9).setBackgroundResource(R.drawable.button_normal);
button_list.get(9).setTag("0");
}
break;
case button10:
if(button_list.get(10).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(10).getText().toString());
is_clicked= (String)button_list.get(10).getTag();
if ("0".equals(is_clicked)){
button_list.get(10).setBackgroundResource(R.drawable.button_pressed);
button_list.get(10).setTag("1");
}else{
button_list.get(10).setBackgroundResource(R.drawable.button_normal);
button_list.get(10).setTag("0");
}
break;
Is there a way I can use a general variable to access each case so if I make a change I won't need to make it 50 times for each button? I thought about doing something like this:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case button[i]
if(button_list.get(i).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(i).getText().toString());
is_clicked= (String)button_list.get(i).getTag();
if ("0".equals(is_clicked)){
button_list.get(i).setBackgroundResource(R.drawable.button_pressed);
button_list.get(i).setTag("1");
}else{
button_list.get(i).setBackgroundResource(R.drawable.button_normal);
button_list.get(i).setTag("0");
}
break;
Would this work? Or is there something else I need to do so I don't need to make 50 changes to my code every time I decide I want to change the OnClick method?
I'm going to assume that ButtonOnClick is the click handler for the buttons. I'm going to further assume that for any i, the contents of button_list.get(i) is the button. If that's right, then the argument v is the same Button object stored in the list. You can then reduce your entire handler to:
public void ButtonOnClick(View v) {
Button btn = (Button) v;
if (btn.getTag().equals("0")) {
enter_txt.setText(enter_txt.getText()+btn.getText().toString());
}
is_clicked= (String) btn.getTag();
if ("0".equals(is_clicked)){
btn.setBackgroundResource(R.drawable.button_pressed);
btn.setTag("1");
}else{
btn.setBackgroundResource(R.drawable.button_normal);
btn.setTag("0");
}
}
As an aside, you might consider using Boolean objects instead of String objects as the button tags.
First you should never copy paste code, try to extract your code in another method.
If you are using a arraylist where you store all your buttons, you can probably use the ArrayList.indexOf(Object o)
So just replace your ButtonOnClick method with this one, you can handle other buttons behavior easily too in the future :
public void ButtonOnClick(View v) {
if(button_list.contains(v))
{
setButtonBackground(button_list.indexOf(v));
}
}
private setButtonBackground(int buttonNumber)
{
Button myButton = (Button) button_list.get(buttonNumber);
if(myButton.getTag().equals("0"))
{
enter_txt.setText(enter_txt.getText()+myButton.getText().toString());
}
is_clicked = (String) myButton.getTag();
if ("0".equals(is_clicked))
{
myButton.setBackgroundResource(R.drawable.button_pressed);
myButton.setTag("1");
}
else
{
myButton.setBackgroundResource(R.drawable.button_normal);
myButton.setTag("0");
}
}
When you are developping instead of copy and paste code, just use the shortcut extract to a method so you will get something like this and will save yourself time.
[...]
case button1:
setButtonBackground(1);
break;
case button2:
setButtonBackground(2);
break;
[...]
Let me know if it works for you

Many switch cases but 1 activity

First of all there are few buttons.
I am set a switch case for all buttons in onClick method.
I want to do different task for all buttons like e.g
For add button I want to display questions of Addition , same for subtraction etc.
But I want to start same activity on the click of the buttons, I mean I have a counter class which should start before the actual questions starts getting displayed.
So on the click on any of the buttons I want to start that counter activity an after that display the questions.
#Override
public void onClick(View v) {
Intent Counter = new Intent(this, TYS_Counter.class);
startActivity(Counter);
switch (v.getId()) {
case R.id.BAddition:
break;
case R.id.BSubtraction:
break;
case R.id.BMultiplication:
break;
case R.id.BDivision:
break;
case R.id.BAll:
break;
}
}
I have done this , is this correct or not, I dont think so, so please guide me.
Edit:
I want when the user clicks on any button the the timer of 3 2 1 starts and when it finishes then the activity with questions starts.
Thanks
From what I think you are saying, you want to display the question after the timer expires. You have several options. One is that you can use
Intent Counter = new Intent(this, TYS_Counter.class);
startActivityForResult(Counter); // notice the change here
Then when your timer Activity finishes, it can use setResult() and return a value to onActivityResult() you can then start the Activity with the questions you want depending on what is sent back from the timer Activity.
If you want to start a new Activity when the counter expires then you could change your code like this
#Override
public void onClick(View v) {
Intent Counter = new Intent(this, TYS_Counter.class);
switch (v.getId()) {
case R.id.BAddition:
counter.putExtra("key", "addition");
break;
case R.id.BSubtraction:
counter.putExtra("key", "subtraction");
break;
case R.id.BMultiplication:
counter.putExtra("key", "multiplication");
break;
case R.id.BDivision:
counter.putExtra("key", "division");
break;
case R.id.BAll:
counter.putExtra("key", "whateverThisIs");
break;
}
startActivity(Counter);
}
Then when your counter Activity finishes you can use the extra sent to tell it which Activity to start

Categories

Resources