I wanted to make a plain button in the menu act as a on/off button like a toggle button. But I'm not sure how I can make a single button act like a switch?
switch(item.getItemId()){
case R.id.switcher:
View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.parseColor("#80FFFFFF"));
//I want to change the color of the background by clicking once
//and set the background color back to normal. How will I achieve this ?
return true;
Try this:
#Override
public void onClick(View v) {
if ((v.getId() == R.id.my_button){
buttonOnClick(v);
}
}
private void buttonOnClick(View v) {
switch (v.getId()) {
case R.id.my_button: {
if (v.isSelected()) {
// is selected, deselect!
v.setSelected(false);
//do your staff here
} else {
// is not selected, select!
v.setSelected(true);
//do your staff here
}
break;
}
default:
break;
}
Related
As I mentioned in the title ,I want to add text to a Textview without replacing the previous text .
In my application I have a TextView and 7 buttons .On every button click I set the text of button to the TextView.
If the button is clicked on first time ,Setting the text to TextView ,and if the same button is clicked 2nd time I am removing that button's text from TextView.
Here What I want to do is for 7 buttons I want to set positions(uniqueness for sun to sat) in TextView and when the respective button is clicked that text is set to the TextView and if the button is clicked 2nd time that specific position of the text should remove .
Here text shouldn't replace the previous text that is important to have and if some button's are selected and again that are deselected means TextView should show the default text as "Never"
I tried to get source from SO but I can't find a clear solution for this .
If anyone helps me to come out from this ,that's much helpful for me .
coding
public class CreateAlarm extends Activity implements View.OnClickListener {
private Button mbtn_Sun, mbtn_Mon, mbtn_Tue, mbtn_Wed, mbtn_Thu, mbtn_Fri, mbtn_Sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_alarm);
mRepeat = (TextView) findViewById(R.id.mRepeat);
mbtn_Sun = (Button) findViewById(R.id.mbtn_Sun);
mbtn_Mon = (Button) findViewById(R.id.mbtn_Mon);
mbtn_Tue = (Button) findViewById(R.id.mbtn_Tue);
mbtn_Wed = (Button) findViewById(R.id.mbtn_Wed);
mbtn_Thu = (Button) findViewById(R.id.mbtn_Thu);
mbtn_Fri = (Button) findViewById(R.id.mbtn_Fri);
mbtn_Sat = (Button) findViewById(R.id.mbtn_Sat);
mbtn_Sun.setOnClickListener((View.OnClickListener) this);
mbtn_Mon.setOnClickListener((View.OnClickListener) this);
mbtn_Tue.setOnClickListener((View.OnClickListener) this);
mbtn_Wed.setOnClickListener((View.OnClickListener) this);
mbtn_Thu.setOnClickListener((View.OnClickListener) this);
mbtn_Fri.setOnClickListener((View.OnClickListener) this);
mbtn_Sat.setOnClickListener((View.OnClickListener) this);
int hours = mTimePicker.getCurrentHour();
mCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mRepeat.setText("");
} else
mRepeat.setText("Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mRepeat.setText("");
} else
mRepeat.setText("Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mRepeat.setText("");
} else
mRepeat.setText("Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mRepeat.setText("");
} else
mRepeat.setText("Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mRepeat.setText("");
} else
mRepeat.setText("Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mRepeat.setText("");
} else
mRepeat.setText("Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mRepeat.setText("");
} else
mRepeat.setText("Sat");
break;
default:
mRepeat.setText("Never");
}
}
}
Image :
By default the TextView text is "Never".
You can define a TreeMap as:
private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
as a field of your class and add/remove the days to/from the TreeMap when the corresponding button is clicked. So the implementation of onClick method will be:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mAlarmDays.remove(0);
} else
mAlarmDays.put(0, "Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mAlarmDays.remove(1);
} else
mAlarmDays.put(1, "Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mAlarmDays.remove(2);
} else
mAlarmDays.put(2, "Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mAlarmDays.remove(3);
} else
mAlarmDays.put(3, "Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mAlarmDays.remove(4);
} else
mAlarmDays.put(4, "Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mAlarmDays.remove(5);
} else
mAlarmDays.put(5, "Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mAlarmDays.remove(6);
} else
mAlarmDays.put(6, "Sat");
break;
}
StringBuilder repeatDays = new StringBuilder();
if (mAlarmDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (String day:mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
mRepeat.setText(repeatDays.toString());
}
You should set each button id first,add this to your xml for each specific button : android:id="sun" and ...
My suggestion is: use a single TextView can make your logic quite complex
Use a horizontal LinearLayout instead, you will have 7 TextView inside with predefine text and position. Just simply show/hide them according to which button is clicked and you don't have to deal with any complex string analize.
I am creating this activity that I request the user to find the correct answer using check boxes. My layout has multiple checkboxes but one of them is correct one. The main idea is to give a user 3 tries to guess the correct answer( the one checkbox). I manage to implement a code that when the correct checkbox is clicked to do something, but I can't get the code to work when user is clicking on any other checkbox.
My code is below (currently implemented the correct checkbox, but code for any other checkbox does not work)
UPDATE
CheckBox centerBack, checkBox1;
int counts = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tennis_facts);
CheckBox centerBack = (CheckBox) findViewById(R.id.centerBack);
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
centerBack.setOnClickListener(this);
checkBox1.setOnClickListener((this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.centerBack:
Toast.makeText(getApplicationContext(), "correct", Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox1:
Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_SHORT).show();
break;
}
}
You mean these code below do not work, right ?
else{
counts ++;
DisplayToast("Incorrect Answer" + counts);
}
because you set listener for only this checkbox by code :
checkBox.setOnClickListener(new View.OnClickListener());
Others checkbox not set setOnClickListener yet. it a reason why when others checkbox checked, Android did not get this event
Should do:
checkBox.setOnClickListener(this);
checkBox2.setOnClickListener(this);
checkBox3.setOnClickListener(this);
checkBox4.setOnClickListener(this);
And You also should implement OnClickListener interface and override onClick method like this below :
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.checkBox:
//your code:
break;
case R.id.checkBox2:
//your code:
break;
case R.id.checkBox3:
//your code:
break;
case R.id.checkBox4:
//your code:
break;
}
}
Try to imlement View.OnClickListener interface in your Activity. then add to every chechkBox:
checkBox.setOnClickListener(this);
Inside the onClick:
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.checkBox1:
//your code:
break;
case R.id.checkBox2:
//your code:
break;
//so on...
}
}
Hi guys really needing a bit of help on this one. Im wanting to use 1x Button that has 2x Functions. I believe the answer if some kind of IF Statement but unsure how to carry it out. Im wanting the user to press a button that will change images on the screen. Then when it is pressed again it is reverted back to its original images.
Currently doing it with two buttons (switch) and the other is (switchback) as seen below
// Button changes the letter images to Small Letters from Capital Letters
btnswitchback = (Button) findViewById(R.id.btnswitchback);
btnswitchback.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When user clicks button it Changed each letter to small case
buttona.setBackgroundResource(R.drawable.a);
}
});
// Button changes the letter images to Small Letters from Capital Letters
btnswitch = (Button) findViewById(R.id.btnswitch);
btnswitch.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When user clicks button it Changed each letter to small case
buttona.setBackgroundResource(R.drawable.aa);
}
});
Any idea ?
Thanks in advance
You can determine which button has been clicked on like this if you don't want repeated code:
Button btnSwitchBack = (Button) findViewById(R.id.btnswitchback);
Button btnSwitch = (Button) findViewById(R.id.btnswitch);
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnswitchback:
// switch back button clicked
// do stuff here
break;
case R.id.btnswitch:
// switch button clicked
// do other stuff here
break;
}
}
};
btnSwitch.setOnClickListener(listener);
btnSwitchBack.setOnClickListener(listener);
This is the simple way you can do this using same button. Set a flag and switch it accordingly.
Int click = 0;
btnswitch = (Button) findViewById(R.id.btnswitch);
btnswitch.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When user clicks button it Changed each letter to small case
if(click == 0){
click = 1;
buttona.setBackgroundResource(R.drawable.a);
}
if(click == 1){
click = 0;
buttona.setBackgroundResource(R.drawable.aa);
}
}
});
Otherwise you can use ToggleButtons in android.
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});
I'm developing an Android app and I have a question:
Is there a way to pause a method until the user presses a view? I am considering doing something like this:
Make a boolean for my class called "wait".
When someone clicks on the view, it will change the boolean to true.
Finally, in the main method, do something like:
while (!wait)
{
//do nothing
}
Is there any better way to do this?
I suppose you are doing it like this...
boolean wait = false;
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
doSomething();
break;
case R.id.button2:
wait = true;
break;
}
}
public void doSomething() {
//
// Do Task 1
//
while(!wait) {}
//
// Do Task 2
//
}
Instead, you can perform it like this...
boolean viewClicked = false;
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
doSomething();
break;
case R.id.button2:
viewClicked = true;
doSomething();
break;
}
}
public void doSomething() {
if(!viewClicked)
{
//
// Do Task 1
//
}
else
{
//
// Do Task 2
//
}
}
I hope this helps. :)
So right now I'm tidying up some code, and I have a lot of else/ifs for buttons and was wondering what is a good way to do it and make it neater?
So I have like 12 buttons, and each button plays a sound and changes colour when clicked. I have a method for this but I was wondering is there a good way to just detect the button instead of if/else?
public void onClick(View v) {
int id = v.getId();
changeToWhite();
if (id == R.id.a_button) {
currentButton(a, 81);
} else if (id == R.id.aSharp_button) {
currentButton(aSharp, 82);
} else if (id == R.id.b_button) {
currentButton(b, 83);
} else if (id == R.id.c_button) {
currentButton(c, 72);
}
Etc...
So is there a better way of having this? I know having a lot of else/ifs is bad so I wanted to try improve it.
Thanks!!
You can use "switch-case" instead.
>
public void onClick(View v) {
switch(v.getId())
{
case R.id.a_button:
changeToWhite();
break;
case R.id.aSharp_button:
currentButton(aSharp,82);
break;
.....
default:
break;
}
}
How about using a case statement instead?
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.a_button:
currentButton(a, 81);
break;
case R.id.aSharp_button:
currentButton(aSharp, 82);
break;
/*
and the rest of the cases here.
*/
}
}
I assume you're using XML and setting the onClick property.
An easier/tidier way is to use anonymous inner classes.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foo);
findViewById(R.id.view_buttonone).setOnClickListener(new OnClickListener(){
public void onClick(View view){
// button one clicked
}
});
findViewById(R.id.view_buttontwo).setOnClickListener(new OnClickListener(){
public void onClick(View view){
// button two clicked
}
});
}
First of all there is virtually no penalty for using if/else nesting. There's no need to try this level of micromanaging your app. You will gain no benefits from it. Try to think more of optimizing this point in terms of readability.
Now, to answer your question, you could use a switch/case construct instead.
public void onClick(View v) {
switch (item.getItemId()) {
case R.id.aBar_item1:
//Item onClick logic
return true;
case R.id.aBar_item2:
//Item onClick logic
return true;
case R.id.aBar_item3:
//Item onClick logic
return true;
...
}
}