user971741
user971741

Reputation:

Unable to click ImageButton

I have a following layout in main_view.xml which contains a ImageButton in it, button when i click/touch the button the ButtonListener function never gets called.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayoutFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent" >

<fragment
    android:id="@+id/mapArea"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</fragment>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/locate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:background="#80000000"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/findingLocation"
        android:textColor="#FFFFFF"
        android:textSize="12sp" />

    <ImageButton
        android:id="@+id/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="@dimen/rightLeftMargin"
        android:layout_marginLeft="@dimen/rightLeftMargin"
        android:background="@null"
        android:clickable="true"
        android:contentDescription="@string/button1text"
        android:src="@drawable/ic_feed_active" />

    <ImageButton
        android:id="@+id/Button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="@dimen/rightLeftMargin"
        android:background="@null"
        android:contentDescription="@string/button2"
        android:src="@drawable/ic_button2" />

    <ImageButton
        android:id="@+id/Button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="@dimen/rightLeftMargin"
        android:layout_marginRight="@dimen/rightLeftMargin"
        android:background="@null"
        android:contentDescription="@string/button3View"
        android:src="@drawable/ic_settings_active" />
</RelativeLayout>

And following code in MainActivity.Java public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    User usrObj = new User();
    if (usrObj.authenticateUser())
        setContentView(R.layout.main_view);
    else
        setContentView(R.layout.login);
}

public void ButtonListener() {

    ImageButton oggleButton = (ImageButton) findViewById(R.id.Button1);

    ToggleButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Toast.makeText(MainActivity.this, "ImageButton is clicked!",
                    Toast.LENGTH_SHORT).show();

        }

    });

}
}

please help.

Upvotes: 0

Views: 84

Answers (2)

AAnkit
AAnkit

Reputation: 27549

apart from above suggestions from ρяσѕρєя K, here is one more thing you have to take care is.

instead of this below code.

oggleButton.setOnClickListener(new OnClickListener() {

use this one

oggleButton.setOnClickListener(new View.OnClickListener() { // use View.oncli......

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

you forget to call ButtonListener method when usrObj.authenticateUser() is true. do it as:

if (usrObj.authenticateUser())
   {
        setContentView(R.layout.main_view);
        ButtonListener ();  << call here
   }
    else
        setContentView(R.layout.login);

and also attach setOnClickListener to oggleButton instead of ToggleButton

Upvotes: 1

Related Questions