In Android I have a widget with a button:
<Button
android:id="#+id/btnPlayPause"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/ic_button_play"/>
I want to apply a very basic animation to it when the user presses it.
Simple animation means: scale a bit down, and come back to original size.
Is this somehow achievable?
but.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
but.animate().scaleY(2f).setDuration(200).withEndAction(new Runnable() {
#Override
public void run() {
but.animate().scaleY(1f);
}
});
}
});
Related
I'm wrinting an application for andorid and I have difficulties with change buttons background. I have a simple button and when the users click on the button change the background. I'd like to make the button to turn back into the original form when the user click on it again.
I have no idea how to do that, if anyone has one please response!
use the android.R.drawable.btn_default in order to change the button to default color
#Howlett Logan : You can try this way,
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextButton"
/>
Then
public boolean flag=true; // Global
Button buttonTest;
#Override
protected void onCreate(Bundle savedInstanceState)
{
buttonTest=(Button)findViewById (R.id.button);
buttonTest.setBackgroundResource(R.drawable.your_drawble);
buttonTest.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (flag)
{
buttonTest.setBackgroundResource(R.drawable.your_image_1);
}else
{
buttonTest.setBackgroundResource(R.drawable.your_image_2);
}
flag=!flag;
}
});
}
I've searched the forums, but not have found any specific or understandable answers for my problem.
I'd like to change my Imagebutton image to a picture, selected from the gallery. Prefferrably the image should stay changed after closing the application.
My XML for the button is here:
<ImageButton
android:id="#+id/eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:adjustViewBounds="true"
android:background="#drawable/eat"
android:clickable="true"
android:longClickable="true"
android:scaleType="fitCenter" />
The java code for playing the sound is here with the OnClick method.
ImageButton eat = (ImageButton) findViewById (R.id.eat);
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
}
});
I would like to add the OnLongClick method here too, (since to OnClick is allready taken and the image replacing should be a little different), but havent found the right way. Can You please guide me a little bit?
You need to return true from image's onLongClickListener.
Like this:
eat.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//do something
return true;
}
});
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
}
});
This will not cause the image's onClickListener to be called as it means that the action has already been handled in longClickListener.
I am working on a project related with Maps. The problem stands that the MapControls are positioned in right bottom. That position get covered with something else. So i want to remove the controls from there and place them somewhere else. I don't want to use custom positioning. Thanks in advance!
Create your own ZoomControls
<ZoomControls android:id="#+id/zoomcontrols"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
and you can set onclicklistener to that.
zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapController.zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapController.zoomOut();
}
});
I am trying to do a simple thing in my app.
There are three button that take the user to three different screen. Once the user clicks the button and comes back to the activity I want the focus to stay on the button that the user had left before.
I tried saved instance state but its not working.
I've done the same in one of my apps.
btn1,btn2,btn3 are 3 buttons in main activity. On click of them, I'm navigating to deifferent activities.
btnenable(the red one) and btndisable(the grey one) are two images used for displaying highlighted button.
This is the snapshot:
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setBackgroundResource(R.drawable.btnenable);
btn2.setBackgroundResource(R.drawable.btndisable);
btn3.setBackgroundResource(R.drawable.btndisable);
}
});
btn2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
btn2.setBackgroundResource(R.drawable.btnenable);
btn3.setBackgroundResource(R.drawable.btndisable);
btn1.setBackgroundResource(R.drawable.btndisable);
}
});
btn3.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
btn3.setBackgroundResource(R.drawable.btnenable);
btn2.setBackgroundResource(R.drawable.btndisable);
btn1.setBackgroundResource(R.drawable.btndisable);
}
});
Hope it hepls !!!Feel free to ask your query !!
I have a view in the UI that is technically an extended ImageView inside a RelativeLayout that I'd rather not change, if possible. I want to show a Drawable from resources (a small PNG with a gradient or a frame) at specific coordinates as a visual feedback when user is tapping the screen (there even is a onShowPress callback). What is the simplest way to do this? I don't want to modify the layout.xml from which UI is inflated, and I'm not too enthusiastic about overriding onDraw to do extra job there.
This is one easy way to do it using view animation class:
final ImageView aboutButton = (ImageView) findViewById(R.id.AboutButton);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation myAnim2 = AnimationUtils.loadAnimation(LaunchActivity.this, R.anim.buttons);
aboutButton.startAnimation(myAnim2);
myAnim2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
//do on click after animation ends
Intent intent = new Intent(LaunchActivity.this, MyDialog.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
//change the backround of the button etc on click etc...
}
});
}});