I use a XML file to display a button with 2 states (normal, pressed). How can I find out when the button is pressed? Is there an event like onClick --> onPressed or how can I find it out?
Here is the XML file I use:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/buttonbluepressed" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/buttonbluepressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/buttonbluepressed" />
<item android:drawable="#drawable/buttonblue"/>
</selector>
And here is how I use it:
<Button
android:id="#+id/button_choose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/choose_button"
android:onClick="onButtonClicked"
android:text="#string/button_choose" />
you can use setOnTouchListener as:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Button Pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//finger was lifted
}
return false;
}
});
Related
i would like to change the background color in ontouchListener, i can change textColor but the background color remains unchanged: here is my code:
Button usd=findViewById(R.id.button_divide);
Button cdf=findViewById(R.id.button_multiply);
usd.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
SharedPreferences.Editor editor=sharedPref.edit();
editor.putString("currency","USD");
editor.apply();
usd.setBackgroundColor(Color.parseColor("#515de1"));
usd.setTextColor(Color.WHITE);
cdf.setBackgroundColor(Color.WHITE);
cdf.setTextColor(Color.parseColor("#515de1"));
return false;
}
});
I's better to use the selector instead of code
<Button
android:id="#+id/myButton"
android:background="#drawable/selector_my_button"
android:layout_width="100dp"
android:layout_height="36dp"
android:text="My Button" />
and the selector_my_button.xml is like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/white"/> <!-- pressed -->
<item android:state_focused="true" android:drawable="#color/blue"/> <!-- focused -->
<item android:drawable="#color/black"/> <!-- default -->
</selector>
I have a button background that change drawable if the state is pressed or not. I'm able to change text color and background like below but I would like to change text size. How can I do it .XML side?
drawable/button_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/toggle_pressed" android:state_selected="true" />
<item android:drawable="#drawable/toggle_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/toggle_unpressed" />
</selector>
res/color/src_text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_selected="false" android:color="#color/blue2"/>
<item android:state_pressed="true" android:color="#color/color_concrete"/>
<item android:state_pressed="false" android:state_selected="true"
android:color="#color/color_concrete"/>
<item android:color="#color/blue2" />
</selector>
button:
<Button
android:id="#+id/playButton"
android:layout_width="200dp"
android:layout_height="70dp"
android:background="#drawable/button_selector"
android:onClick="#{(v)->mainViewModel.playButtonClick(v)}"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="#string/play"
android:textColor="#color/src_text"
android:textSize="40sp"
android:visibility="#{mainViewModel.observableVisibilityPlayButton ? v.VISIBLE : v.GONE}"/>
You can create subclass of Button and override onTouch method
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// button pressed, change textSize
return true;
case MotionEvent.ACTION_UP:
// button released, revert textSize
return true;
}
return false;
}
Since you're using databinding you can probably create a BindingAdapter like following:
#JvmStatic
#BindingAdapter("textSize")
fun bindTextSize(button: Button, size: Int) {
if(button.isPressed())
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}
Also keep a MutableLiveData field in the viewmodel (size) and change the value according to your need.
<Button
textSize="#{mainViewModel.size}
android:id="#+id/playButton"
android:layout_width="200dp"
android:layout_height="70dp"
android:background="#drawable/button_selector"
android:onClick="#{(v)->mainViewModel.playButtonClick(v)}"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="#string/play"
android:textColor="#color/src_text"
android:textSize="40sp"
android:visibility="#{mainViewModel.observableVisibilityPlayButton ? v.VISIBLE : v.GONE}"/>
What I want is, When the RelativeLayout press state is active, I want the layout alpha to be 50% and when its not I need 100% alpha.
The following code doesn't seem to be working.
if (relativelayout.hasFocus()){
relativelayout.setAlpha(0.5f);
}
I tried setting the background of the relative layout to this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/drawer_list_pressed" />
<item android:state_focused="false"
android:drawable="#drawable/drawer_list_normal" />
<item android:state_selected="true"
android:drawable="#drawable/drawer_list_pressed" />
</selector>
It is working on changing the background but not alpha of the contents.
after some thinking i found a solution
public void pressed(final View view){
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
view.setAlpha(0.5f);
}
else {
view.setAlpha(1.0f);
}
return true;
}
});
}
I have a problem with OnTouchListener. I created a custom button. This button properly works with onClick event. But it doesn't work with onTouch Event
This is my custom button. I want only two options just button and button_pressed.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button2_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button2_pressed"
android:state_focused="true" />
<item android:drawable="#drawable/button2" />
This is my Ontouch button code. When I keep the button pressed, audio is playing and looping. When I release the button audio is stopped. This code correctly works but it doesn't work with my custom button. So when i have my finger on this button, then when I release the button or when I hold the button shortly, in every case this button show me only button2.png (above the custom button code)
pl6.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
n6=MediaPlayer.create(MainActivity.this, R.raw.audio4);
n6.start();
n6.setLooping(true);
return true;
case MotionEvent.ACTION_UP:
n6.stop();
n6.release();
break;
}
return false;
}
});
This is my other button. it is correctly work. This button is a normal button just click and play.
When i pressed the button button2_pressed is appear and when I unpressed button2 is appear.
play2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopPlaying2();
m2=MediaPlayer.create(MainActivity.this, R.raw.sound2);
m2.start();
}
});
I solved my problem again on my own. Lets explain;
Firstly I clonned all button for example btnPlay1 and btnPlay1c
but the buttons xml different to each other. the difference is only id and custom button xml file. I used 2 custom button xml I show below.
First Custom button mybutton1.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button"
android:state_pressed="true" />
<item android:drawable="#drawable/button"
android:state_focused="true" />
<item android:drawable="#drawable/button" />
</selector>
Second custom button mybutton1c.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_pressed"
android:state_focused="true" />
<item android:drawable="#drawable/button_pressed" />
</selector>
And example from my buttons. clone button is invisible. (btnPlay2c)
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="#+id/btnPlay2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/mybutton1" />
<Button
android:id="#+id/btnPlay2c"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/mybutton1c"
android:visibility="gone" />
</FrameLayout>
Lastly, this is my java file. When I pressed the button, button clone is showing and when I unpressed normal button is showing. That's all.
pl10.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
n10=MediaPlayer.create(MainActivity.this, R.raw.audio7);
n10.start();
n10.setLooping(true);
///focus here\\
pl10c.setVisibility(View.VISIBLE);
return true;
case MotionEvent.ACTION_UP:
n10.stop();
n10.release();
//and here\\\
pl10c.setVisibility(View.INVISIBLE);
break;
}
return false;
}
});
This solution might be tiring but its solve the problem.
Originally I had my button connected to a method through the XML like this:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_button_green"
android:onClick="start_process"
android:text="#string/button_send"
android:textSize="50sp" />
Then I decided to add an onTouchListener so that I could handle ACTION_UP events:
private OnTouchListener myListener = new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//start recording
System.out.println("DOWN");
start_process();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("UP");
}
return true;
}
};
The listener works great, but I've noticed that the states for my button have disappeared. The button had different colors for if it had been pressed, but this no longer happens now that the button is interacting through the listener. Here is what is defined in #drawable/custom_button_green:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed" />
<item android:state_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_green" />
<item android:state_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_normal_disable" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_selected" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_green" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_normal_disable_focused" />
<item
android:drawable="#drawable/btn_default_normal_disable" />
Can anyone help me figure out why the button no longer follows these states?
Thanks!
I think the onTouchmethod of your listener should return false, otherwise you just override the "normal" behaviour of updating drawable on touch events.