Roshni Kyada
Roshni Kyada

Reputation: 725

Android : onClickListener for ImageButton

I have one imageButton. When I clicked on ImageButton. It makes VISIBLE another layout(childLayout) at top of the Parent Layout android:id="@+id/mainLayout".Now ChildLayout have three buttons (Back, Call, Web). I implemented OnClickListeners for Back, Call & Web buttons.But when i clicked on "back" button it not fired event. But when i double click on buttons OnClickListener worked. Why OnClickListener not worked at single click.?

         @Override
                public void onSingleTapConfirmed() {
                    childLayout = (LinearLayout) findViewById(R.id.childView);
                    childLayout.setVisibility(View.VISIBLE);
                    backButton = (Button)findViewById(R.id.back);
                    web = (ImageButton) findViewById(R.id.web);
                    call = (ImageButton) findViewById(R.id.call);
                    backButton.setOnClickListener(backButtonListener);
                    call.setOnClickListener(callListener);
                    web.setOnClickListener(webListener);
                }

            OnClickListener callListener = new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Intent callIntent = new Intent(Intent.ACTION_CALL);
                            callIntent.setData(Uri.parse("tel:415-817-9955,’10"));
                            startActivity(callIntent);                  
                        }
                    };

            OnClickListener webListener = new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Intent web = new Intent(Intent.ACTION_VIEW);
                            Uri u = Uri.parse("http://google.com");
                            web.setData(u);
                            startActivity(web);
                        }
                    };  
             OnClickListener backButtonListener = new OnClickListener() {

                @Override
                public void onClick(View v) {
                    finish();
                }
            };

and my xml file :

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


    <LinearLayout
        android:id="@+id/childView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/nav_bar"
        android:padding="5dp"
        android:visibility="gone" >

        <Button
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/title_btn"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="Business"
            android:textColor="@color/white" >
        </Button>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Image"
            android:textStyle="bold" />

        <ImageButton
            android:id="@+id/call"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/phone" />

        <ImageButton
            android:id="@+id/web"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/web" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</LinearLayout>

or i should have to use other method to show child layout on Single Tap event of imageButton. Thanks in advance.

Upvotes: 0

Views: 2158

Answers (1)

AAnkit
AAnkit

Reputation: 27549

Have a boolean, Make it true on first click, and if user click again check if the boolean is true, then do what you want to do in double click.

You can define a time suppose 30 ms if user clicks again in you do what you want to do in double click else you can perform operation for single click.

But in Android we do single click and Long click/press, Long press can replace double click thing. And even long press is recommended instead of double click.

Here is same answer asked before, see the link >> here

Upvotes: 1

Related Questions