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
Related
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;
}
}
});
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;
}
}
I'm trying to create an android application with multiple toggle button.
Each ToggleButton has two state. either ON or OFF and initially all the button are set to default, being OFF.
I have to listen to the User Action and act accordingly. How do I do this with Multiple Toggle Button.
Toggle button is arranged inform of a Matrix and I want read the state to a matrix, once user clicks the Simple Button.
Please be advised that I'm a beginner to Mobile App Development.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.toggleOne:
// In this section u can pass intent , toast[message] on button click.
break;
case R.id.toggleTwo:
// do your code
break;
case R.id.toggleThird:
// do your code
break;
default:
break;
}
}
you have to implement onClickListener interface to override onClick method
Im coding a little Android-App at the moment and discovered a strange issue regarding multiple Buttons. I have an Activity with 4 Buttons on it. When I press multiple Buttons at once, both actions are executed. So I can press all 4 Buttons and all 4 following Activities are started.
This is my onButtonClick-Method
public void onButtonClick(View view) {
Intent intent = new Intent(this, RunActivityConfirm.class);
switch(view.getId()) {
case R.id.btnRunAcceleration:
intent.putExtra("DisciplineName", "Acceleration");
startActivity(intent);
break;
case R.id.btnRunSkidPad:
intent.putExtra("DisciplineName", "Skid Pad");
startActivity(intent);
break;
case R.id.btnRunAutocross:
intent.putExtra("DisciplineName", "Autocross");
startActivity(intent);
break;
case R.id.btnRunEndurance:
intent.putExtra("DisciplineName", "Endurance");
startActivity(intent);
break;
}
}
First I thought the problem ist that I always call startActivity() in every single "case" but even if I try the following all 4 Activities get started at the same time
public void onButtonClick(View view){
Intent intent = new Intent(this, RunActivityConfirm.class);
switch(view.getId()) {
case R.id.btnRunAcceleration:
intent.putExtra("DisciplineName", "Acceleration");
break;
case R.id.btnRunSkidPad:
intent.putExtra("DisciplineName", "Skid Pad");
break;
case R.id.btnRunAutocross:
intent.putExtra("DisciplineName", "Autocross");
break;
case R.id.btnRunEndurance:
intent.putExtra("DisciplineName", "Endurance");
break;
}
startActivity(intent);
}
This happens all over my App. No matter which Button I press every associated Action is executed and every associated Activity is started.
Is there anything I can do about it?
When I press multiple Buttons at once, both actions are executed.
This is expected behavior. Your code starts an activity when a button is pressed. Multiple buttons are pressed, so multiple activities are started.
If you want to change this so that only one button can be pressed, then you need to add code to do so. You could disable the other buttons in onButtonClick (and re-enable them when appropriate).
As this is the same Activity add
android:finishOnTaskLaunch="true"
in your manifest under your activity tag.It will prevent to make the multiple instances of same activity.
I'm trying to make an application that the user can slide or drag the finger across a set of buttons to perform a click. I've tried onTouch with MotionEvent.ACTION_MOVE but I can't seem to get it to work. The buttons just don't press at all. Here's the code :
public boolean onTouch(View v, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_MOVE) {
switch (v.getId()) {
case R.id.key1:
key1.performClick();
break;
case R.id.key2:
key2.performClick();
break;
case R.id.key3:
key3.performClick();
break;
case R.id.key4:
key4.performClick();
break;
case R.id.key5:
key5.performClick();
break;
case R.id.key6:
key6.performClick();
break;
case R.id.key7:
key7.performClick();
break;
case R.id.key8:
key8.performClick();
break;
}
}
return true;
}
I think you are better off capturing the key events on all those buttons and then performing some sort of sum on each one. For example
Key1 = 1
key2 = 1+2
key3 = 1+2+3
...
When you reach keyx, you can check if the sum matches your expectation. If it does, the user probably swiped his / her finger across the buttons. Use some sort of timing by which keyx must be pressed and the numbers must add up to N. That will avoid the user pressing button3 and then coming back to button1 etc and still registering a click.
If you want to register a click on each button, set their onclick listeners and ensure that the buttons themselves are clickable (they should already be).