I have 4 buttons and i want when user clicked on the buttons, button keep pressed. I searched and finally i use this code for keep pressed button. but when i select back button, the program back to before activity but for example when i use back and go to introduce button, the program cannot shows introduce button pressed. The program shows last button press..
How can i solved this problem? Please dont say to me, i use onTouch method and onPressed method because this method cluttered my animation...
btnIntroduce.setBackgroundResource(R.drawable.introducepress);
btnContact.setBackgroundResource(R.drawable.contact);
btnMore.setBackgroundResource(R.drawable.more);
btnProduct.setBackgroundResource(R.drawable.product);
Thanks
I guess you have written your code to remain in pressed state in onCreate Method, If that is the case Try your code in onResume Method. That will solve the purpose.
Create a new class in your package e.g.
public class SaveState {
public static boolean checkContact, checkIntroduce, checkMore,
checkProduct;
}
Now In your activity ,replace your below code
checkContact = true/false;
checkIntroduce = true/false;
checkMore = true/false;
checkProduct = true/false;
With
SaveState.checkContact = true/false;
SaveState.checkIntroduce = true/false;
SaveState.checkMore = true/false;
SaveState.checkProduct = true/false;
and run your code.
Related
I need to show ripple on my button simply in the onCreate() of my activity. Of all the code I have scanned, ripple effect is only visible when button is pressed. Please guide me on how to show ripple by default without the button click.
You still have to click button but programmatically. Use yourButton.performClick() in your onCreate method and make sure when you do this do not run code that is handle on click event for your button for that you can use one boolean variable to check whether you are doing it programmatically or real action is perform
yourbutton clicklistener {
if(isprogrammatic){
// dont do anything
isprogrammatic = false
}
else{
// run your code
}
}
OnCreate
onCreate(Bundle..){ // your on create method
//yes it is programmatic
isprogrammatic = true;
yourbutton.performClick();
}
I am writing an very simple application with following scenario:
1) Screen A have 3 button to move on other screen's.
2) Now if I hold one button(say Button 1) and perform rapid click on other button then it launch multiple instance of other screen. Which I think should not be happened. How can prevent this.
3) and it's more weird. After move on other screen if I don't release Button 1 which was on Screen A then it still allow to perform click for rest of two button of screen A even I can see second screen.
Here it's clear launch second screen but still first screen button event working.
Any idea how can avoid such scenario.
How you are going to disable other buttons while having 1 enabled, that's an algorhytmic problem. You can try creating a boolean or control variable in your activity (and then pass the final reference of the activity to wherever you need it), or in a static context. But to answer the title of the question - you can "Cancel Touch Event" either by adding an OnTouchListener, or if you're extending class Button, you can override onTouchEvent(MotionEvent ev) method.
Using OnTouchListener will disable any previously defined touch-event behavior. You can call the actual click event from the inside by calling performClick method from your button.
//in order to use button inside OnTouchEvent, its reference must be final
//if it's not, create a new final reference to your button, like this:
final finalButton = button;
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouchEvent(MotionEvent ev) {
// ... additional code if necessary
if(canBeClicked) {
finalButton.performClick();
return true;
}
else return false;
}
}
Overriding onTouchEvent in a class extending Button should look something like this.
#Override
public boolean onTouchEvent(MotionEvent ev) {
// ... additional code if necessary
//here we don't really need to call performClick(), although API recommends it
//we just send the touch event to the super-class and let it handle the situation.
if(activity.canBeClicked) return super.onTouchEvent(ev);
else return false;
}
One solution that I found is to disable click listener in onPause() and enable it in onResume() . Can we have better support for this?
I have scenario with two screens.
Screen 1 shows data from from API in list format
There is a "+" button in menu bar
Clicking this button takes user to screen 2
User can enter some info on screen 2 and press the "save" button on top of this screen. This does a POST to my API and saves the data.
After saving, I would like to put the user back to screen 1. I've done that with this:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getTitle().toString().equalsIgnoreCase("save")) {
new CreateSomethingTask(this,enteredName.getText().toString(), id).execute();
Intent listscreen = new Intent(getApplicationContext(), ShowListActivity.class);
startActivity(listscreen);
return true;
}
return true;
}
However, the added item is not shown. If I close my app and open it again then the item shows up.
Is there a good way to handle this? I like how the Github Android app handles this when creating a new Gist. But I'm not sure how to implement that.
You should start your screen2 with startActivityForResult(). That way you could send a result back and a code and proceed to refresh your screen1. See example : How to manage `startActivityForResult` on Android?
Below function maybe help you, didn't tried.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
callFunctionToRefreshList();
//or redraw data from api
//setContentView(R.layout.activity_book);
}
Actually in my app i have a button on an listView..now on click of that button i have done some changes..so when i move to previous activity after than this changes should appear on that activity..but in my case the changes occur but not appears after i exit from the activity where my listView Button is present..so how can i do that so that my changes occur immediately after i exit from my first activity..code i have wrritten:
code for ListView Button Onclick:
public boolean stopCycleStage(View v)
{
Button butStop=(Button) findViewById(R.id.butStop);
TextView setStopTxtViewTitle =(TextView)findViewById(R.id.setStopTxtViewTitle);
Date currentDate=new Date();
int iStopStartCount = CycleManager.getSingletonObject().getStopStartCount();
Date dtStopDate = currentDate;
CycleManager.getSingletonObject().setStopStartDate(dtStopDate, iStopStartCount);
Date dtStart = CycleManager.getSingletonObject().getStartDate();
if (dtStopDate.getTime() == dtStart.getTime())
CycleManager.getSingletonObject().removeHistoryDate(dtStart);
butStop.setBackgroundResource(R.drawable.settings_but_disabled);
setStopTxtViewTitle.setTextColor(Color.parseColor("#808080"));
return true;
}
In your first activity, you should refresh the view in the onResume function rather than just in the onStart or onCreate.
Refer to the activity documentation to see the lifecycle of an activity
PS: this is just a guess because you have not given enough code to show how you load the data in your 1st acitvity.
Id use onActivityResult() in the waiting activity, and in there I would redraw the elements that should have changed when you clicked your button in the child activity. In any case, onActivityResult() is the correct way to go unless you are not waiting for any data from child activity, then I'd use onResume().
See http://developer.android.com/reference/android/app/Activity.html#StartingActivities for information on what you should use in your particular situation (you didn't exactly give much information :))
How can I trigger a button click event using code in Android? I want to trigger the button click programmatically when some other event occurs.
Same Problem I am Facing
public void onDateSelectedButtonClick(View v){
/*Something Alarm Management
http://www.java2s.com/Code/Android/Core-Class/Alarmdemo.htm
copied code from this site*/
}
Button code:
<Button
android:onClick="onDateSelectedButtonClick"
android:text="Set notification for this date" />
But I want to call that function OnLoadLayout without OnClickEvent
there is a better way.
View.performClick();
http://developer.android.com/reference/android/view/View.html#performClick()
this should answer all your problems. every View inherits this function, including Button, Spinner, etc.
Just to clarify, View does not have a static performClick() method. You must call performClick() on an instance of View.
For example, you can't just call
View.performClick();
Instead, do something like:
View myView = findViewById(R.id.myview);
myView.performClick();
Just to clarify what moonlightcheese stated:
To trigger a button click event through code in Android
provide the following:
buttonName.performClick();
you can do it this way
private Button btn;
btn = (Button)findViewById(R.id.button2);
btn.performClick();
Just write this simple line of code :-
button.performClick();
where button is the reference variable of Button class and defined as follows:-
private Button buttonToday ;
buttonToday = (Button) findViewById(R.id.buttonToday);
That's it.
Android's callOnClick() (added in API 15) can sometimes be a better choice in my experience than performClick(). If a user has selection sounds enabled, then performClick() could cause the user to hear two continuous selection sounds that are somewhat layered on top of each other which can be jarring. (One selection sound for the user's first button click, and then another for the other button's OnClickListener that you're calling via code.)
Starting with API15, you can use also callOnClick() that directly call attached view OnClickListener. Unlike performClick(), this only calls the listener, and does not do any associated clicking actions like reporting an accessibility event.
If you do not use the sender argument, why not refactor the button handler implementation to separate function, and call it from wherever you want (from the button handler and from the other place).
Anyway, it is a better and cleaner design - a code that needs to be called on button handler AND from some other places deserves to be refactored to own function. Plus it will help you separate UI handling from application logic code. You will also have a nice name to the function, not just onDateSelectedButtonClick().