Image button example

MainActivity.java
package com.example.imagebutton_android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button imageButton;
Button imageButton1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton1 = (Button) findViewById(R.id.imageButtonSelector);
imageButton1.setFocusableInTouchMode(true);
addListenerOnButton();
}

public void addListenerOnButton() {

imageButton = (Button) findViewById(R.id.imageButtonSelector);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this,
"Button clicked!",
Toast.LENGTH_SHORT).show();
imageButton.setFocusableInTouchMode(false);
imageButton.setFocusable(false);
}
});

}

}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/imageButtonSelector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_button" />

</LinearLayout>
Previous Post Next Post