How to make LinearLayout Clickable

I have a linear expression 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 kind of xml to 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>

      

with 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 by clicking on layout, I want to load this xml file like below ... but I cannot get the backgroud loaded on the click event. Please imagine 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>

      

+3


source to share


6 answers


try this .. hope it works ..

  <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:drawable="#ff0000" />//ur onclick desired color 
    <item  android:state_focused="false" 
        android:drawable="@android:color/transparent" />focused color
   </selector>

      



just create a new xml and add this code to drawable and set ur linear layoyut backgrout as its name like android:background="@drawable/createdxmlname"

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

      

+2


source


First put id LinearLayout from xml and then do onClickListener instead of it and not the 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;
      }
   }
});

      

+3


source


   android:clickable="false"


 <LinearLayout  
      android:id="@+id/parent"  
      android:layout_width="match_parent"  
      android:layout_height="wrap_content"  
      android:orientation="horizontal">  
      <ImageButton  
           android:clickable="false"  
           android:layout_width="40dp"  
           android:layout_height="40dp"  
           android:text="Cinema"  
           android:textSize="13sp"  
           android:layout_marginRight="10dp" />  
      <Button  
           android:clickable="false"  
           android:layout_width="match_parent"  
           android:layout_height="40dp"  
           android:text="Check in"  
           android:textSize="13sp" />  
 </LinearLayout>  

      

0


source


Create compressed_layout.xml in drawable with the following content

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true"
      android:drawable="@drawable/button_pressed" /> <!-- pressed -->
   <item android:state_focused="true"
      android:drawable="@drawable/button_focused" /> <!-- focused -->
   <item android:drawable="@drawable/button_normal" /> <!-- default -->
 </selector>

      

And in your layout, add it as a background resource:

<LinearLayout
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:background="@drawable/pressed_layout"
  android:clickable="true">
</LinearLayout>

      

0


source


layout.setclickble = "true" in the XML file and

layout.setOnLongClickListener (new View.OnLongClickListener () {

        @Override
        public boolean onLongClick(View arg0) {
            // TODO Auto-generated method stub

        }
    });  

      

0


source


We can also use onClick in layout XML and use this method in java file, for example if onClick = "next" then in java file we have to use this method.

public void next()
{
// do something
} 

      

-1


source







All Articles