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();
}
Related
I am using a swipe button from com.ebanx:swipe-button library in my application and I wish to change the state of the swipe button to enable (based on the information recieved via another Bluetooth device) when I open the button's activity. ie: Without any user input I have to change swipe button's state to enable !
You can use toggleState()
SwipeButton mSwipeButton; = findViewById(R.id.my_swipe_button);
mSwipeButton.toggleState();
if you use an older version where toggleState is not available, use collapseButton(); or expandButton(); to collapse or expand the swipe button
There are two issues with the library you're using, first is coding bug, second is wrong documentation, but that's not the case.
to make the button active:
SwipeButton swipe_btn = findViewById(R.id.swipe_btn);
swipe_btn.setEnabled(true);
now by default, the button state is closed and you can change that in the xml file i.e the layout where you created the button, you will see something like below:
<com.ebanx.swipebtn.SwipeButton
app:initial_state="disable" //change to enable will make button open by default
app:has_activate_state="true"
/>
Finally to monitor the state of the button, you will have to listen to the state changes like below:
swipe_btn.setOnStateChangeListener(new OnStateChangeListener() {
#Override
public void onStateChange(boolean active) {
Toast.makeText(MainActivity.this, "IS "+active, Toast.LENGTH_LONG).show();
}
});
if active, the button is open, else it's close.
Note: When I say open, I mean the button is toggledOn, and when I say close, it means the other way round(toggleOff).
The bug here is that when you use swipe_btn.toggleState(); The button will be deactivated, meaning it will not even respond to click event which is not right, so the way around is to use the onStateChangeListener as I use it above so that when the button is open you can do something and when it's close you can still do anything.
Note: library version: 'com.ebanx:swipe-button:0.8.3'
I have an application, where you can click on the background, and it's changes from the drawables to another background, but I only want to make it clickable when the user click on a button that i call, "I want to click it".
So, how to write a code like:
If user click on button1 2 times, make layout clickable
else
not make layout clickable
So, I want to store somehow the click, and force my app to remember to it, and I also want to count the clicks.
What chapter of Android are helping me understanding this? Thanks for help, and sorry for the noobish question:)
You can have counter that increments on click and then just disable component when counter reach value that you want
int count = 0;
Button button = (Button) findViewById(R.id.i_want_to_click_it);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (++count == 2) {
// make stuff clickable here on 2nd click
}
// if you also want to make things unclickable if there
// are more than 2 clicks, add the else{} condition
else {
// make stuff unclickable here
}
}
});
You can detect when user clicks a button by registering a onClickListener on it. Inside that callback you can count how many times it has been clicked and store that information inside a variable. If your app can change from portrait to landscape mode, dont forget to store the variable in onSavedInstanceState and then retrieve it in onCreate, because changing layout mode will destroy the activity and rebuild it which will reset your variable. I highlighted keywords to search for.
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.
I develop a application
and in which i have a Menu option which i invoke from onCreateOptionMenu()
But this is called only when any user press the menu button
so now i want that my application start and first Activity is Welcome.java
then in onCreate(Bundle b)
can i write sone line from which the menu is invoked automatically without pressing Menu button
i used openOptionMenu() but it not works.
2) can i create a Button and simulate it as Menu button and then write button.performClick() so it act as a Menu Button and menu option will visible
So give me some suggestion on this
Thanks
You can request the menu be opened with an Activity method
openOptionsMenu();
If you want to show a menu immediately, you'll have to wait for the window focus to change, rather than using onResume:
#Override
public void onWindowFocusChanged(boolean hasFocusFlag) {
super.onWindowFocusChanged(hasFocusFlag);
if (hasFocusFlag) {
openOptionsMenu();
}
}
See openOptionsMenu()
Hi I will like to provide answer for your question
(" can i create a Button and simulate it as Menu button and then write button.performClick() so it act as a Menu Button and menu option will visible")
Answer:
Step 1-Create a button/ image button in your layout
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/a"
android:onClick="expand"
android:src="#drawable/button" />
Here I have set onClick method as "expand"
Step 2-Now in your MainActivity.java class define your "expand" method which will be call once user click on your button
public void expand(View v)
{
ImageButton imgButton=(ImageButton)findViewById(R.id.imageButton1) ;
imgButton.setVisibility(View.GONE);
openOptionsMenu();
}
In this code I have set visibility as "gone", as I want the button to disappear once menu is shown
Step 3-*(In case you are setting visibility for the button)* You can also write code to set visibility as "visible" once menu is closed using below method
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
ImageButton imgButton=(ImageButton)findViewById(R.id.imageButton1) ;
imgButton.setVisibility(View.VISIBLE);
}
Hope this will help you.....
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().