I have a list view with an image view and text view for each list element, as per the screenshot below. The image and text views together make the clickable element so if you tap to the right of any text nothing happens, you have to tap on the text or image. How do I make the link span the width of the whole list element, basically to the right hand edge of the screen?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right"
android:src="#drawable/image5" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right" >
</TextView>
</LinearLayout>
Make your Parent Layout width and height as match_parent/fill_parent
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right"
android:src="#drawable/image5" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right" >
</TextView>
</LinearLayout>
In your listview item layout, set the width of textview to 0dp and add android:layout_weight="1" attribute to textview.
You can make something like this inside your Adapters getView method:
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView==null) { ... }
...
vi.setOnClickListener(new OnListItemClickListener(position));
return vi;
}
private class OnListItemClickListener implements OnClickListener {
private int mPosition;
OnListItemClickListener(int pos) {
this.mPosition = pos;
}
#Override
public void onClick(View arg0) {
//DO WHATEVER YOU WANT WITH YOUR "CLICK". THE POSITION CLICKED IS STORED INSIDE mPosition.
}
}
Related
I need to detect the onclick event on a imageview, and get the position to get the row from a cursor, and I don't know how.
I'm trying to modify https://github.com/Odoo-mobile/framework to and shopping cart app.
The code of the layout view where I need to detect the click event is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:padding="10dp" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="7dip"
android:padding="2dip" >
<odoo.controls.BezelImageView
android:id="#+id/image_small"
android:layout_width="40dp"
android:layout_margin="#dimen/default_8dp"
app:maskDrawable="#drawable/circle_mask"
android:layout_height="40dp" />
</LinearLayout>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/thumbnail"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#color/body_text_1"/>
<LinearLayout
android:id="#+id/cart_plus_minus_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/name"
android:orientation="horizontal" >
<TextView
android:id="#+id/list_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="60dip"
android:layout_weight="0.23"
android:paddingRight="5dip"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#color/android_red" />
<ImageView
android:id="#+id/cart_minus_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_minuscircle" />
<TextView
android:id="#+id/cart_product_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:text="0"
android:textSize="18dip"
android:textStyle="bold" />
<ImageView
android:id="#+id/cart_plus_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_addcircle" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
the images I need to detect the the click are: #+id/cart_plus_img #+id/cart_minus_img
When the row is click the function that capture the event is:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ODataRow row = OCursorUtils.toDatarow((Cursor) mAdapter.getItem(position));
//AddToCart(row);
loadActivity(row);
}
What I need is to call loadActivity(row) when the user click the row but when click #+id/cart_plus_img #+id/cart_minus_img call another function but with the row parameter
Any help is thanksfull
Use the Tag property of the View
ImageView imgMinus = (ImageView)view.findViewById(R.id.cart_minus_img);
ImageView imgPlus = (ImageView)view.findViewById(R.id.cart_plus_img);
imgMinus.setTag(position);
imgPlus.setTag(position);
imgMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
...
}
});
imgPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
...
}
});
I have created a custom ListView with 2 ImageView and a TextView.
In the Adapter, I'm doing setText in TextView, but it is not visible in the app.
Here is my List:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#289FA5"/>
<RelativeLayout
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#289FA5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Popular products"
android:textSize="23sp"
android:padding="7dp"/>
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="120dp">
<LinearLayout
android:id="#+id/pop_pro_men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_marginLeft="7dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#289FA5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Catagories"
android:textSize="23sp"
android:padding="7dp"/>
</RelativeLayout>
<ListView
android:id="#+id/cata_men"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
Here is my Custom List:
<LinearLayout
android:id="#+id/custom_list"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:background="#ffffff">
<ImageView
android:layout_gravity="left"
android:id="#+id/imageView2"
android:layout_height="130dp"
android:layout_marginLeft="7dp"
android:layout_width="120dp"
android:src="#drawable/clothing_men"/>
<TextView
android:id="#+id/cata_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clothing"
android:layout_marginTop="35dp"
android:textStyle="bold"
android:textSize="27sp"
android:padding="10dp"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward_arrow"
android:layout_marginTop="42dp"/>
And Here is the getView method of my Adapter:
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom_subcatagory_list, null,true);
TextView sub_catagory= (TextView) rowView.findViewById(R.id.cata_name);
ImageView pic = (ImageView) rowView.findViewById(R.id.imageView2);
sub_catagory.setText(catagory[position]);
pic.setImageResource(image[position]);
Log.d("Strings========", sub_catagory.getText().toString());
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + catagory[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
Please Help as i am unable to find what is wrong in this.
Remove your layout_weight attribute from your TextView. If you remove sub_catagory.setText(catagory[position]); does "Clothing" appear when you run your app?
I got the answer now. I don't know how my textColor was set to White, because i didn't mentioned it. So, change the color to something else and it worked.And it wasted my whole day.
Sorry to disturb you all.
Hey guys I'm newbie at Android development, and I would like to build a sub item for each listview item.
Just like this:
ListView 1
sub item 1
ListView 1
sub item 1
I though that it would be esier to create a new relative layout for each listview item, so my XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrap_layout"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/first_col"
android:layout_width="fill_parent"
android:layout_height="200dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/bg_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/overlay_bg" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="9dp" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/current_event"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:letterSpacing="1.2"
android:shadowColor="#color/dark_grey"
android:shadowDx="-2"
android:shadowDy="2"
android:shadowRadius="0.01"
android:textColor="#color/white"
android:textSize="22sp" />
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/persons" />
<TextView
android:id="#+id/current_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_alignParentBottom="true"
android:text="TextView"
android:textColor="#color/grey"
android:textSize="13sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/second_col"
android:layout_width="fill_parent"
android:layout_marginTop="200dp"
android:layout_height="200dp" >
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="24dp"
android:text="TextView" />
</RelativeLayout>
</RelativeLayout>
Each item is divided by two columns, the second column should be hidden column, thats why I changed the layout height to 200dp.
And to show the full layout, I'm doing that:
public View getView(final int position, View convertView, ViewGroup parent) {
// here you let SimpleAdapter built the view normally.
final View v = super.getView(position, convertView, parent);
final RelativeLayout lebg = (RelativeLayout) v.findViewById(R.id.wrap_layout);
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
}
});
return v;
}
I just need to change the height from 200 to 400 to show the full Listview item.
The issue:
Is not working that well because when I click at the listview it only changes the height when I scroll...
Is there any better way to do what I am trying?
Thanks.
When you change LayoutParams you have to call requestLayout to see changes:
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
requestLayout();
}
});
I want to align text in Spinner to the left but how can i achieve this require help
here is my code & screen shot
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/roundshape" >
<!-- Lable Area -->
<TableRow
android:id="#+id/tblRwspnLbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="1dip">
<TextView
android:id="#+id/lblCust"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="#string/lblCust"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="left"/>
<TextView
android:id="#+id/lblPros"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="#string/lblPros"
android:textSize="14sp"
android:textStyle="bold" />
</TableRow>
<!-- Spinner Area -->
<TableRow
android:id="#+id/tblRwspn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dip"
android:gravity="center" >
<Spinner
android:id="#+id/spnAECust"
android:layout_width="75dp"
android:layout_height="35dp"
android:fontFamily="verdana,arial,helvetica"
android:hint="#string/SelectCust"
android:textSize="14sp"
android:layout_gravity="left"/>
<Spinner
android:id="#+id/spnAEProspect"
android:layout_width="75dp"
android:layout_height="35dp"
android:fontFamily="verdana,arial,helvetica"
android:hint="#string/SelectProspect"
android:textSize="14sp" />
</TableRow>
<!-- Text Area -->
Here is my Screen the red dot is the space which i want to remove so tht my all labels and spinners will come in same aligned line
Just remove android:padding="1dip" from Spinner TableRow and try i am not sure just try
Edit:
You have to use a custom view here to give alignment to the Spinner values. Create a CustomView like this answer has created and add android:gravity' to the TextView asright`.
And set the CustomView to your adapter using
adapter.setDropDownViewResource(android.R.layout.your_custom_created_view);
Thank you all for giving all possible answers here is solution i get
android:layout_width="75dp"
android:layout_height="35dp"
android:layout_marginLeft="5dp"
android:paddingLeft="5dp"
android:gravity="left"
I think you should use custom ArrayAdapter with TextView and play with textview property as per your requirement.
Example :
MyAdapter.java
public class MyAdapter extends ArrayAdapter {
Context context;
public MyAdapter(Context context, int resource) {
super(context, resource);
this.context=context;
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.mylayout, parent,false);
TextView textView=(TextView)view.findViewById(R.id.text);
return view;
}
}
mylayout.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:layout_width="match-parent"
android:layout_height="wrap_content"
android:text="#string/lblPros"
android:textSize="14sp"
android:gravity="right"
android:textStyle="bold" />
</LinearLayout>
Try this:-
use android:layout_span="1". if required then give left margin for the first row.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/roundshape" >
<!-- Lable Area -->
<TableRow
android:id="#+id/tblRwspnLbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" ">
<TextView
android:id="#+id/lblCust"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lblCust"
android:layout_span="1"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="left"/>
<TextView
android:id="#+id/lblPros"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="1"
android:text="#string/lblPros"
android:textSize="14sp"
android:textStyle="bold" />
</TableRow>
<!-- Spinner Area -->
<TableRow
android:id="#+id/tblRwspn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Spinner
android:id="#+id/spnAECust"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="1"
android:fontFamily="verdana,arial,helvetica"
android:hint="#string/SelectCust"
android:textSize="14sp"
android:layout_gravity="left"/>
<Spinner
android:id="#+id/spnAEProspect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="1"
android:fontFamily="verdana,arial,helvetica"
android:hint="#string/SelectProspect"
android:textSize="14sp" />
</TableRow>
<!-- Text Area -->
This is sample code it may useful
<Spinner
android:id="#+id/spinner1"
style="?android:attr/spinnerStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/dropdown"
android:dropDownVerticalOffset="1dp"
android:focusable="false"
android:spinnerMode="dropdown" />
take one more layout for the text view following is the code for layout
spinner_text_layout.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text_for_spinner"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:drawableRight="#drawable/arrow"
android:gravity="center"
android:textColor="#android:color/white" >
</TextView>
now in the custom adapter these are the two methods
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.spinner_text_layout, null);
}
TextView textView = (TextView) convertView
.findViewById(R.id.text_for_spinner);
textView.setText((String) getItem(position));
notifyDataSetChanged();
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.spinner_text_layout, null);
}
TextView textView = (TextView) convertView
.findViewById(R.id.text_for_spinner);
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
textView.setText((String) getItem(position));
textView.setTextColor(Color.BLACK);
textView.setBackgroundResource(R.drawable.drop_down_selector);
return convertView;
}
I have a ListView that its items have some buttons as child/sub view (buttons like share, reply, like, etc). I want to disable clicks on items but not for childs. For example when clicking on a like button, its color changes and nothing happens to the list view item. (see nine gag app)
I read similar questions saying about overriding areAllItemsEnabled and isEnabled and I did that (returning false for both) but didn't get proper result (childs also are unabled)
Can you please help me out? And sorry for my bad English
in custom adapter:
public CustomCommentAdaptor(Context context){
ctx = context;
listData = new ArrayList();
}
#Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return false;
}
listview in main layout xml file
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:groupIndicator="#null"
android:layout_above="#+id/leave_comment" />
and listview items layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<!-- android:padding="10dip" -->
<RelativeLayout
android:id="#+id/comment_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:minHeight="40dp"
android:padding="5dp">
<ImageView
android:id="#+id/user_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#+drawable/brad" />
<TextView
android:id="#+id/comment_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="10dip"
android:layout_toLeftOf="#+id/user_image"
android:textSize="15sp" />
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="10dip"
android:layout_toLeftOf="#+id/user_image"
android:textSize="12sp" />
<RatingBar
style="#style/myRatingBar"
android:layout_height="12dp"
android:layout_width="wrap_content"
android:numStars="5"
android:rating="5"
android:isIndicator="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:stepSize="1"
android:layout_toLeftOf="#id/comment_title"
/>
</RelativeLayout>
<TextView
android:id="#+id/comment_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/comment_header"
android:textSize="8sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/comment_content"
android:orientation="horizontal"
>
<include
android:id="#+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions" />
<include
android:id="#+id/dislike"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
/>
<include
android:id="#+id/reply"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
/>
<include
android:id="#+id/share"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
/>
</LinearLayout>
</RelativeLayout>
Set a click listener only on your buttons. So, in your adapter:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
View likeView = convertView.findViewById(R.id.like);
likeView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Do something.
}
}
}
Don't set a click listener on your list. So, if you are doing this anywhere, take it out!
listView.setOnItemClickListener(new OnItemClickListener() ...);
Finally, to make the like button change colors when pressed, set a selector as the background of your comment_actions layout.