Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make LinearLayout Clickable

I have a linearlayout like this

<LinearLayout
    android:id="@+id/optionlist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/divider"
    android:orientation="vertical" >

</LinearLayout>

I want to add another xml view into this layout

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

    <!--
               <View 
               android:layout_width="match_parent"
               android:layout_height="0.5dip"
               android:layout_marginLeft="10dp"
                 android:layout_marginRight="10dp"
                android:background="#2fd275"
               />
    -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp" >

        <ImageView
            android:id="@+id/option_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/logout" />

        <TextView
            android:id="@+id/option_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#000000"
            android:visibility="gone" />

        <TextView
            android:id="@+id/option_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"
            android:text="Signout"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#616161" />
    </LinearLayout>

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="0.5dip"
        android:background="#d3d3d3" />

</LinearLayout>

via the code

    for (int i = 0; i < opt.length; i++) {

        View view = inflater.inflate(R.layout.option_item, optionlist,
                false);
        view.setBackgroundResource(R.drawable.optionlist);
        view.setTag(opt_image[i]);
        view.setClickable(true);
        view.setFocusable(true);

        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // ADD your action here

                int res = (Integer) v.getTag();

                switch (res) {
                case R.drawable.logout:
                    signout();
                    break;
                }

            }
        });
        final TextView tv = (TextView) view.findViewById(R.id.option_name);
        final ImageView iv = (ImageView) view
                .findViewById(R.id.option_image);
        tv.setText(opt[i]);
        iv.setImageResource(opt_image[i]);
        optionlist.addView(view);
    }

Now on click of the layout i want to load this xml file as below...but i am not able to get the backgroud loaded on the click event. Please give suggestion what am I doing wrong?

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/holo_green_dark" android:state_focused="true"/>
    <item android:drawable="@color/holo_green_dark" android:state_enabled="false" android:state_pressed="true"/>
    <item android:drawable="@color/white"/>

</selector>
like image 923
Jay Dhamsaniya Avatar asked Dec 09 '14 11:12

Jay Dhamsaniya


People also ask

Can you make a layout clickable android studio?

You can make a view clickable, as a button, by adding the android:onClick attribute in the XML layout.

Which has better performance LinearLayout or RelativeLayout?

Relativelayout is more effective than Linearlayout.

What is the difference between RelativeLayout and LinearLayout?

LinearLayout means you can align views one by one (vertically/ horizontally). RelativeLayout means based on relation of views from its parents and other views.


1 Answers

First get id of LinearLayout from xml and then perform onClickListener on it instead of whole View

View view  = inflater.inflate(R.layout.option_item, optionlist, false);
LinearLayout layout = view.findViewByIt(R.id.optionlist);
layout.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
      // ADD your action here
      int res=(Integer) v.getTag();
      switch(res)
      {
         case R.drawable.logout: signout();
                                break;
      }
   }
});
like image 75
Rahul Patidar Avatar answered Sep 19 '22 23:09

Rahul Patidar