Reputation: 323
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>
Upvotes: 7
Views: 25705
Reputation: 37
We can also use onClick in XML of layout,and use that method in java file,like if onClick="next",then in java file we have to use the method .
public void next()
{
// do something
}
Upvotes: -1
Reputation: 19
layout.setclickble="true" in XML file and
layout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: -1
Reputation: 2057
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 new xml and add this code in 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">
Upvotes: 5
Reputation: 2482
Create in drawable an pressed_layout.xml 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 background resource:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/pressed_layout"
android:clickable="true">
</LinearLayout>
Upvotes: 0
Reputation: 406
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;
}
}
});
Upvotes: 6
Reputation: 379
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>
Upvotes: 1