I have ReltiveLayout with and ImageView and a TextView inside.
So I have two event listeners:
onClick
onTouch
I am using 9patch
for the image. When I use the onTouch event onClick is not firing at all and also onTouch is not working very good. If I disable onTouch and leave onClick it fires and changes the Activity. I want to make onTouch and onClick work together. I want:
- When the RelativeLayout is clicked it shows the selected button ImageView
- Otherwise display the normal button ImageView
- onClickListener changing the activity.
Heres my code:
StartBtn = (RelativeLayout) findViewById(R.id.StartBtn);
StartBtnImage = (ImageView) findViewById(R.id.StartBtnImage);
StartBtnText = (TextView) findViewById(R.id.StartBtnText);
StartBtn.setOnTouchListener(new TouchButton(StartBtnImage)); //Commenting this line makes the onClickListener work
StartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StartBtnImage.setImageResource(R.drawable.btn_selected);
Log.d("ButtonClick", "Done");
Intent myIntent = new Intent(MainMenu.this, Game.class);
startActivity(myIntent);
}
});
And my TouchButton class:
public class TouchButton implements View.OnTouchListener {
ImageView IV;
public TouchButton(ImageView Image) {
IV = Image;
}
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
IV.setImageResource(R.drawable.btn_selected);
return true;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_OUTSIDE:
default:
IV.setImageResource(R.drawable.btn);
return false;
}
}
}
Here is my layout
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainMenu">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MainMenuBg"
android:background="@drawable/main_menu_bg" />
<RelativeLayout
android:layout_width="250dp"
android:layout_height="75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:id="@+id/StartBtn"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/StartBtnImage"
android:src="@drawable/btn" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Start"
android:id="@+id/StartBtnText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:singleLine="false"
android:gravity="center|center_vertical|center_horizontal"
android:textSize="50dp" />
</RelativeLayout>
I know I'm bad at explaining, so here is a video: https://www.youtube.com/watch?v=PtdUsW-Dnqk