I am having a button in my activity and using a TranslateAnimation to change its position.
I am using anim.setFillAfter(true); to keep the resulting translation, the button shifts its position but the touch area remains same.
Now when i touch on the button it doesn’t work rather touching on the previous location of the button calls its listener..
Here is my code:
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Animation anim=new TranslateAnimation(0, 100, 0, 0);
anim.setDuration(500);
anim.setFillAfter(true);
Button mybtn = (Button)findViewById(R.id.btn);
mybtn.startAnimation(anim);
}
public void myFunc(View v){
Toast.makeText(this, "btn clicked", Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
android:onClick="myFunc"/>
</RelativeLayout>
I have re-generated your case and it seems like only 50% of the button shows the selected color. Is that right ? Try this :