Android ListView fixed height item - android

Got a little problem. I'd like to create an android list view activity with all items in the list having a fixed height.
So, my item layout (thread_item.xml) looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="300dip"
>
<TextView android:id="#+id/thread_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[dummy]"
android:textSize="20dip"
android:textStyle="bold"
/>
<ImageView android:id="#+id/thread_first_image"
android:layout_below="#id/thread_item_title"
android:scaleType="centerInside"
android:maxWidth="100dip"
android:maxHeight="100dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
<TextView
android:id="#+id/thread_item_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/thread_first_image"
android:text="[dummy]"
android:textSize="15dip">
</TextView>
</RelativeLayout>
I set layout_height of the root element to 300dip and expect all items to have the same height, but they don't. When I run the application it looks like the height having a wrap_content value.
In addition the activity itself looks like this:
public class ThreadListActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
Code to get items from the storage.
*/
setListAdapter(new ThreadItemAdapter(this, R.layout.thread_item, itemsArray)));
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
/*Start new Activity. Unrelated stuff.*/
}
});
}
}
And adapter I'm using looks like this:
public class ThreadItemAdapter extends ArrayAdapter<ThreadItem> {
Activity context;
List<ThreadItem> items;
public ThreadItemAdapter(Activity context, int textViewResourceId, List<ThreadItem> items) {
super(context, textViewResourceId, items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inf = this.context.getLayoutInflater();
View result;
if (convertView == null) {
result = inf.inflate(R.layout.thread_item, null);
} else {
result = convertView;
}
TextView tbTitle = (TextView) result.findViewById(R.id.thread_item_title);
TextView tbPreview = (TextView) result.findViewById(R.id.thread_item_preview);
ImageView ivFirstImage = (ImageView) result.findViewById(R.id.thread_first_image);
ThreadItem item = items.get(position);
tbTitle.setText(item.getThreadTitle());
ivFirstImage.setImageBitmap(item.getFirstImage());
SimpleLeadingMarginSpan span = new SimpleLeadingMarginSpan(item.getFirstImage() != null ? 5 : 0, 115); // TODO: const
SpannableString previewText = new SpannableString(item.getThreadPreview());
previewText.setSpan(span, 0, previewText.length(), 0);
tbPreview.setText(previewText);
return result;
}
}
I can't see why all list items still wrap their content and don't stay 300dip in height. They might be both smaller or bigger then 300dip.
I'm using android 2.3.3, testing on HTC Evo 3D device and an emulator (both show same result).
Thanks a lot in advance.
UPD:
Thanks to Miguel and Sam. The solution is to set maxHeight to the textView, that makes my list item grow (that would be +id/thread_item_preview) and setting the RelativeLayout's minHeight to 300dip as well, to prevent it from shrinking.

when inflating for convertView, instead of just
result = inf.inflate(R.layout.thread_item, null);
do
result = inf.inflate(R.layout.thread_item, parent, false);
The method in question is inflater.inflate(int viewId, ViewGroup parent, boolean attachToRoot) -- because you're not honoring the supplied parent (which in this case is the ListView), whatever dimension you supply to the listview item will by default be set to layout_width=fill_parent, layout_height=wrap_content, ignoring the 300dip height you specified in xml. By supplying the parent view and passing false, the inflater will honor the 300dip height, while not attaching it to the root (parent).

What if you change all the child view heights in the row from wrap_content to match_parent?
From comments
Have you tried the minHeight and maxHeight attributes? For example:
android:minHeight="300dp"
You should also watch Android's Romain Guy discuss efficiency in adapters and getView().

Try changing this
android:layout_height="300dip"
to
android:minHeight="300dip"
This worked for me using an ExpandableListView, so I suppose it will work for this case.

You can achieve this by specifying the same dimension for Min and Max. This fixed my problem.
<ImageView
android:id="#+id/appIconImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:maxHeight="50dp"
android:maxWidth="50dp"
android:minHeight="50dp"
android:minWidth="50dp"
android:src="#drawable/ic_launcher" />

Try setting the content TextView's height to 0dp and then setting its layout_alignParentBottom to true.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:minHeight="60dp"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="1"
android:background="#color/main_color">
<ImageView
android:id="#+id/img_left_menu"
android:layout_weight="0.4"
android:focusable="false"
android:maxHeight="50dp"
android:maxWidth="50dp"
android:minHeight="50dp"
android:minWidth="50dp"
android:layout_width="0dp"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tx_left_menu"
android:layout_width="0dp"
android:textSize="18dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:singleLine="true"
android:layout_height="wrap_content"
android:focusable="false"
/>
</LinearLayout>

If I'm not mistaken, the listView automatically modifies the LayoutParams of a custom view to wrap_content, wrap_content. However, the answers above are correct, if you set a minHeight or minWidth it will work brilliantly.

Related

Add multiple GridView in the same scrollable layout/fragment

I know there are several questions for this problem already as I have searched for an hour, but neither of those has solved my problem nor the solution is too old.
I'm working on an event app and it displays the attendance of people going to it. It need to has two GridView: YES and NO, depending on the attendance, and it shows the picture of the guests.
I have already made the "YES" GridView and I tried adding a new TextView and GridView to my layout + getting the layout of it in Java for my "NO" GridView but it only shows the first one.
What am I doing wrong? I'm using Fragments for each of my tabs.
This is what I have v.s. what I want:
This is my fragment's layout:
[fragment_one.xml]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.example.MainActivity.OneFragment">
<TextView android:id="#+id/title_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="Yes"
android:textAllCaps="true"
android:textSize="14sp"
android:fontFamily="sans-serif-regular" />
<GridView android:id="#+id/gridview_yes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title_yes"
android:numColumns="4"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center" />
<TextView android:id="#+id/title_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="No"
android:textAllCaps="true"
android:textSize="14sp"
android:fontFamily="sans-serif-regular" />
<GridView android:id="#+id/gridview_no"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title_no"
android:numColumns="4"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
This is my fragment's Java:
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
GridView gridViewYes = (GridView) view.findViewById(R.id.gridview_yes);
GridView gridViewNo = (GridView) view.findViewById(R.id.gridview_no);
gridViewYes.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity()
gridViewNo.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity()
gridViewYes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "Esta es la imagen " + position + ".", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
GridView is probably not your best option here. That widget is intended to be used when you have a potentially long list of things to display in a scrolling container. TableView is better when you have a manageable set of items that doesn't need its own scrolling behavior.
Also, you probably don't want a layout_height of match_parent for these things. Use wrap_content to only let them be as tall as their content suggests.
If you need the whole thing to scroll because you aren't certain of the entire height, place the LinearLayout in a ScrollView and let that determine if the collection of tables and text should be scrollable if they're too tall for the space allotted.
Your problem is you have set GridView yes height to match_parent.
android:layout_height="match_parent"
You should change as follows:
android:layout_height="wrap_content"

Android - How to make child LinearLayout match parent RelativeLayout's height

I am putting this LinearLayout inside RelativeLayout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66000000"
android:gravity="center">
<ImageView
android:layout_width="30dp"
android:layout_height="23dp"
android:src="#drawable/big_tick" />
</LinearLayout>
What I am trying to do is - to show which item has been chosen. I set LinearLayout's height and width match_parent. It is only matching parent(RelativeLayout)'s width, not height.
This is what I have intended to do:
This is what I am getting:
LinearLayout is not taking whole height of its parent.
There are 2 reasons, why I am using Relative layout as parent:
The LinearLayout should be in upside(should cover) book information with 40% black color
To show this symbol on the top of book
The whole XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<LinearLayout
android:id="#+id/llBook"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="6dp"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="11dp"
android:paddingRight="2dp"
android:paddingTop="8dp">
<ImageView
android:id="#+id/ivCover"
android:layout_width="95dp"
android:layout_height="146dp" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#232425"
android:textSize="12sp" />
<TextView
android:id="#+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#848586"
android:textSize="10sp" />
</LinearLayout>
<ImageView
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignRight="#id/llBook"
android:layout_marginRight="6dp"
android:background="#drawable/shape_round_book_status"
android:padding="8dp"
android:src="#drawable/icon_new" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66000000"
android:gravity="center">
<ImageView
android:layout_width="30dp"
android:layout_height="23dp"
android:src="#drawable/big_tick" />
</LinearLayout>
</RelativeLayout>
I am using this RelativeLayout in BaseAdapter class using which I am filling GridView with items.
public class LibraryAdapter extends BaseAdapter {
Context context;
RealmResults<RBook> rBooks;
LayoutInflater inflater;
public LibraryAdapter(Context context, RealmResults<RBook> rBooks) {
this.context = context;
this.rBooks = rBooks;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return rBooks.size();
}
#Override
public Object getItem(int position) {
return rBooks.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RBook rBook = (RBook) getItem(position);
View view = convertView;
if(view == null) {
// new row is needed to inflate new row
view = inflater.inflate(R.layout.listitem_library_book, parent, false);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(rBook.getCover() , 0, rBook.getCover().length);
ImageView ivCover = (ImageView) view.findViewById(R.id.ivCover);
ivCover.setImageBitmap(bitmap);
TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
tvTitle.setText(rBook.getTitle());
TextView tvAuthor = (TextView) view.findViewById(R.id.tvAuthor);
tvAuthor.setText(rBook.getAuthor());
return view;
}
}
EDIT:
Try1: as cj1098 suggested I have changed child LinearLayout to RelativeLayout. Unfortunately, no effect
Try2: as challenger suggested, I have used this inside my child LinearLayout, but effect was like this:
Try3: as codeMagic suggested, I have changed child LinearLayout's
gravity="center"
to ImageView's
layout_gravity="center"
In result, LinearLayout is not taking whole height.
So my question is how make child LinearLayout match parent RelativeLayout's height?
Try to add this to the LinearLayout:
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
Any way use can use this without even using the LinearLayout (with fixing the size of the drawable "big_tick" ):
<ImageView
android:layout_width="30dp"
android:background="#66000000"
android:layout_height="23dp"
android:src="#drawable/big_tick" />
I think that using this layout by itself is fine. It is the fact that you are putting it into a ListView is causing some sort a havoc.
However, you can force the overlay to align itself bottom and top with the book layout:
android:layout_alignBottom="#id/llBook"
android:layout_alignTop="#id/llBook"
After that maybe you still have a few margins to fix, but the two lines above will make the overlay be as big as the book layout.
it is taking the full height of its parent. Get rid of the padding on your child linear layout. Then if that doesn't work. Switch your linearLayout to a relativeLayout. (It's better anyway) another limitation is the actual images you are using. Do they have differing heights?
Remove the LinearLayout totally, use the imageview with src 'tick' as a direct child of the Relative layout with:
andriod:centerInParent=true
and then add View with the desired background transparency that has
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"

Android - ListView Changes View Attribute

I have a ListView and I wanted to place a small rectangle on the side of each item. To do so I made the following rendered layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp">
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:id="#+id/item_color_image"
android:background="#color/wallet_holo_blue_light"
android:layout_marginRight="3dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_icon_image"
android:src="#drawable/m"
android:layout_toRightOf="#+id/item_color_image"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Business Name"
android:id="#+id/item_title_text"
android:layout_toRightOf="#+id/item_icon_image"
android:layout_marginLeft="3dp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignTop="#+id/item_icon_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Distance"
android:id="#+id/item_distance_text"
android:layout_alignLeft="#+id/item_title_text"
android:layout_alignBottom="#+id/item_icon_image"
android:layout_marginBottom="-10dp" />
</RelativeLayout>
When I inflate this layout in a frame, it works just fine.....BUT when I inflate it inside a ListView I get this:
The rectangle's height is being ignored to match the container's height.
I inflate it using this code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = inflater.inflate(R.layout.business_item_view, parent, false);
}
Business currentBusiness = businesses.get(position);
ImageView icon = (ImageView) itemView.findViewById(R.id.item_icon_image);
icon.setImageBitmap(currentBusiness.getIcon());
TextView name = (TextView) itemView.findViewById(R.id.item_title_text);
name.setText(currentBusiness.getName());
TextView distance = (TextView) itemView.findViewById(R.id.item_distance_text);
distance.setText("2 Km"); // TODO: Calculate distance
return itemView;
}
How can I fix this? Thanks!!
Having your root item for a ListView row be match_parent for height doesn't make sense; each item would fill the ListView. I expect that's automatically being changed to wrap_content somewhere.
And RelativeLayouts don't support children with match_parent for height if the RelativeLayout's height is wrap_content. It works with LinearLayouts, so you should look into switching to that. Source: combining wrap_content on parent and fill_parent on child

android How to stretch rows in the gridview to fill screen?

I have simple GridView (with one column and six rows), that displays ImageView with TextView in the cell (I create adapter). How to stretch rows to fill entire screen height?? Now I have some space below cells...
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout android:layout_height="fill_parent"
android:layout_width="150dp"
android:orientation="vertical"
android:id="#+id/left">
<include layout="#layout/menu_grid"></include>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="#+id/right">
<ImageView android:src="#drawable/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/imageMain"
android:layout_gravity="center">
</ImageView>
</LinearLayout>
menu_grid.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<GridView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/gridView"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="1"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:gravity="center"></GridView>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="fill_horizontal">
<ImageView android:src="#drawable/icon"
android:layout_height="70dp" android:id="#+id/imageIcon"
android:layout_width="70dp" android:layout_gravity="center_horizontal"></ImageView>
<TextView android:id="#+id/textIcon" android:text="TextView"
android:layout_gravity="center" android:layout_height="wrap_content"
android:layout_width="wrap_content"></TextView>
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
//....some code
//....
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item, null);
TextView text = (TextView) view.findViewById(R.id.textIcon);
text.setText(labels[position]);
ImageView image = (ImageView) view.findViewById(R.id.imageIcon);
image.setImageResource(icons[position]);
} else {
view = convertView;
}
return view;
}
}
As of what i understood, the question is- if i have 6 rows of data to be filled in the gridView, how can i make these 6 rows fill the whole screen, instead of showing 6 rows and some empty space under the last row.
To achieve this you must set minimum height of the gridView item layout (in your case its item.xml) to (height of the screen)/6, where 6 is the (no.of rows you have to fill). This minimum height can be set in the adapter you are using.
To find the height of the screen of the device:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
find these values in the Activity and make them public static and use them in the adapter, i had a problem in finding these values in the adapter class
then to set minimum height:
view = inflater.inflate(R.layout.item, null);
view.setMinimumHeight(GridViewActivity.height/6);
I tried this after seeing the post, and it worked perfectly.
If you just want to increase the space between the lines You can use padding it should work.
you can set padding through xml or programmatically based on your requirement.
// this you can set inside your view.
android:paddingLeft="5px"
android:paddingRight="5px"
android:paddingTop="10px"
android:paddingBottom="10px"
// this you can set to your any widgets like TextView/Layouts/Buttons etc..
setPadding(top, left, bottom, right);
Extend a layout say Framelayout then override the onMeasure method.
public class CameraView extends FrameLayout
public TextView titleView;
public CameraView(Context context,AttributeSet attrs) {
super(context, attrs)
titleView = rootView.findViewById(R.id.titleView);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
You can then use this view in your BaseAdapter and set the layout_width and layout_height to match_parent
I hope I am understanding you correctly, if so it is as easy as changing your GridView to fill_parent instead of wrap_content.
Change
<GridView android:layout_height="wrap_content"
to
<GridView android:layout_height="fill_parent"
If this is not the case then it is probably the layout height on the LinearLayout of the item.xml that you want to modify.

ListView with clickable/editable widget

Is it possible to use a OnItemClickListener on a ListView when the Items layout has a clickable/editable widget (RadioButton,EditText, or CheckBox)?
You might want to take a look at this issue. Having a focusable item in a row of a ListView causes the OnItemClickListener NOT to be invoked. However, that does not mean you cannot have focusable/clickable items in a row, there are some workarounds like this one.
Also, you can take a look at the Call Logs screen. It has a ListView with clickable item(the call icon on the right).
See Source code here
Quoting comment #31 in the link mentioned by Samuh (which solved the problem for me):
In fact you can add it to the layout XML (if inflated by one): android:descendantFocusability="blocksDescendants".
Adding here JIC that webpage is down in the future.
If any row item of list contains focusable or clickable view then OnItemClickListener won't work.
row item must be having param like android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
Tried many complex solutions, but this was the simplest one that worked:
Just use android:focusable="false" as in:
<CheckBox
android:id="#+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
Two best solution
Add android:descendantFocusability="beforeDescendants" to listView
in xml OR
Set given two attributes to false
like
android:focusable="false"
android:focusableInTouchMode="false"
Then it will handle the listView row item child(Button,EditText etc) events instead of listView.setOnItemClick .
I fixed my problem different , in my item I have more than one LinearLayout
so if you give id to your linearayout and setOnclickListener in adapter class it will work, only original effect of touching will dissapear.
but this link Making a LinearLayout act like an Button is usefull to make linearlaout act like button on click
item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/txt_item_followers_name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center|start"
android:paddingLeft="15dp"
android:text="Ali"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/txt_item_followers_name"
android:layout_marginLeft="10dp"
android:src="#drawable/puan_icon" />
<TextView
android:id="#+id/txt_item_followers_mark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_toEndOf="#+id/imageView"
android:background="#color/red_400"
android:paddingLeft="10dp"
android:text="25.5"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
android:id="#+id/linear_one"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/txt_item_followers_name"
android:background="#color/red_400"
android:orientation="vertical">
<ImageView
android:id="#+id/btn_item_followers_2b_follow"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_marginLeft="10dp"
android:src="#drawable/follow_buton" />
</LinearLayout>
</RelativeLayout>
inside getView method
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
if (convertView == null)
view = inflater.inflate(R.layout.deneme, null);
final Followers2 myObj = myList.get(position);
LinearLayout linear_one = (LinearLayout) view.findViewById(R.id.linear_one); // HERE WE DOMUNÄ°CATE IT
linear_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(parentActivity, "One Two", Toast.LENGTH_SHORT).show();
}
});
TextView name = (TextView) view.findViewById(R.id.txt_item_followers_name);
TextView mark = (TextView) view.findViewById(R.id.txt_item_followers_mark);
final ImageView btn_follow = (ImageView) view.findViewById(R.id.btn_item_followers_2b_follow);
name.setText(myObj.getName());
mark.setText(myObj.getScore());
/* if (myObj.isFollow() == true) {
btn_follow.setImageResource(R.drawable.following_buton);
} else {
btn_follow.setImageResource(R.drawable.follow_buton);
}
btn_follow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Followers2 myObj = myList.get(position);
if (myObj.isFollow() == true) {
btn_follow.setImageResource(R.drawable.following_buton);
} else {
btn_follow.setImageResource(R.drawable.follow_buton);
}
}
});*/
return view;
}

Categories

Resources