Smarty Gurl
Smarty Gurl

Reputation: 373

how to set onClick method with linearLayout?

I am creating an application and posted a question yesterday how to start activity by click any where on row. Got an idea to do like below but I am getting this error:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})

Here is my code:

LinearLayout menu_photos = (LinearLayout )findViewById(R.id.picture_part);
menu_photos.setOnClickListener(new OnClickListener() {      
    @Override
    public void onClick(View v) {
        Intent picture_intent = new Intent(CurrentActivity.this,PictureActivity.class);
        startActivity(picture_intent );     
    }
});

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:background="@drawable/main_background"
   android:orientation="vertical" >
   <include
        android:id="@id/includeTop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/private_space_title" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<LinearLayout
    android:id="@id/mail_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_list_bg"
    android:orientation="vertical"
    android:paddingBottom="10.0dip"
    android:paddingLeft="20.0dip"
    android:paddingRight="20.0dip"
    android:paddingTop="15.0dip" >
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@id/picture_part"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10.0dip" >

        <ImageView
            android:id="@id/left_icon1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10.0dip"
            android:src="@drawable/icon_picture" />

        <TextView
            android:id="@id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/function_img_management"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@id/right_icon1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/listview_arrow" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/list_view_divider" />

    <LinearLayout
        android:id="@id/video_part"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10.0dip" >

        <ImageView
            android:id="@id/left_icon2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10.0dip"
            android:src="@drawable/icon_video" />

        <TextView
            android:id="@id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/function_video_management"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@id/right_icon2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/listview_arrow" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/list_view_divider" />

    <LinearLayout
        android:id="@id/sms_call_part"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10.0dip" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@id/left_icon2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10.0dip"
                android:src="@drawable/icon_sms_call" />

            <TextView
                android:id="@id/unread_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@id/left_icon2"
                android:layout_marginRight="5.0dip"
                android:layout_marginTop="3.0dip"
                android:background="@drawable/counter"
                android:gravity="center"
                android:textColor="@color/white"
                android:visibility="visible" />
        </RelativeLayout>

        <TextView
            android:id="@id/textView3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/function_sms_management"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@id/right_icon3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/listview_arrow" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/list_view_divider" />

    <LinearLayout
        android:id="@id/private_contact_part"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10.0dip" >

        <ImageView
            android:id="@id/left_icon4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10.0dip"
            android:src="@drawable/icon_contact" />

        <TextView
            android:id="@id/textView4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/function_privacy_management"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@id/right_icon4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/listview_arrow" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/list_view_divider" />

    <LinearLayout
        android:id="@id/break_in_part"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10.0dip" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@id/left_icon5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10.0dip"
                android:src="@drawable/icon_break_in" />

            <TextView
                android:id="@id/login_record_main_unread_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@id/left_icon5"
                android:layout_marginRight="5.0dip"
                android:layout_marginTop="3.0dip"
                android:background="@drawable/counter"
                android:gravity="center"
                android:textColor="@color/white"
                android:visibility="visible" />
        </RelativeLayout>

        <TextView
            android:id="@id/textView5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/function_break_in_management"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@id/right_icon5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/listview_arrow" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/list_view_divider" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="90.0dip"
    android:layout_weight="1.0"
    android:gravity="center" >

    <Button
        android:id="@id/member_up_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20.0dip"
        android:layout_marginRight="20.0dip"
        android:background="@drawable/upgrade_selector"
        android:text="@string/main_upgrade_button"
        android:textSize="18.0sp" />
</LinearLayout>

Upvotes: 36

Views: 95726

Answers (4)

duggu
duggu

Reputation: 38439

below line is ur code:-

menu_photos.setOnClickListener(new OnClickListener() { 

replace this line to below code:-

menu_photos.setOnClickListener(new View.OnClickListener() { 

Upvotes: 54

Hamidreza Sadegh
Hamidreza Sadegh

Reputation: 2196

LinearLayout app_layer = (LinearLayout) findViewById (R.id.main_activity_app_layer);
app_layer.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(this, "hello", Toast.LENGTH_LONG).show();
    }
});

set LinearLayout attribute android:clickable="true"
if you have any button or textview in layout set android:clickable="false" for all of them

Upvotes: 48

Sagar Wankhede
Sagar Wankhede

Reputation: 312

Great catch by @duggu. Apart from the answer, using ImageView and TextView inside a LinearLayout, nesting LinearLayout is a bad idea. Instead use drawableLeft:@src of a TextView and also for a divider you can use a simple View instead of inflating a drawable.

<View
android:background="#aeaeae"
android:layout_width="match_parent"
android:layout_height="0.5dp"/>

Upvotes: 0

crios
crios

Reputation: 609

From your xml layout file i think what you are looking for is a listview, not loads of linearlayouts. Here is nice tutorial to get you started, might be exactly what you need, row with image and text. For the listview just use setOnItemCLickListener().

Back to what you want, I think you need to make the linearlayout focusable/touchable in order to "receive" clicks.

Upvotes: 0

Related Questions