I search a lot for an answer and didn't find one that suit me.
I found out that when I set my layout_heigh to 500dp -> all the item shown, but when it has normal size (layout_heigh=wrap_content) then only the first item is shown.
this is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add Task"
android:onClick="addTask" />
<ListView android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
</LinearLayout>
xml_item.xml: this is for one item to duplicate in the listView
<?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:descendantFocusability="blocksDescendants">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/itemText"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="15dp"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/doneBtn"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Done" />
</RelativeLayout>
MyListAdapter: a class in the main_activity that extends the ArrayAdapter class so I can put in one row both TextView and a button.
private class MyListAdapter extends ArrayAdapter<String> {
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout= resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainHolder= null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView= inflater.inflate(layout, parent, false);
ViewHolder viewHolder= new ViewHolder();
viewHolder.item= (TextView) convertView.findViewById(R.id.itemText);
viewHolder.btn= (Button) convertView.findViewById(R.id.doneBtn);
viewHolder.btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), itemArray.get(position), Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
}
else {
mainHolder= (ViewHolder) convertView.getTag();
mainHolder.item.setText(getItem(position));
}
return convertView;
}
public class ViewHolder {
TextView item;
Button btn;
}
}
Thanks for all! :)
but when it has normal size (layout_heigh=wrap_content)
It is a very bad idea using a ListView by giving it a height=wrap_content.
This forces ListView to measure a few children out of the adapter at layout time, to know how big it should be.
You can check it your self debugging the getView() method inside the adapter.
In your case, using a LinearLayout just add a top margin (50dp as the Button) to your listView and use layout_heigh=match_parent.
Otherwise use a RelativeLayout.
Change your listview to take up as much room as is can considering the height of any sibling views (in this case your button). Here's how to do that with the android:layout_weight attribute
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add Task"
android:onClick="addTask" />
<ListView android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
Related
I have made a custom ArrayAdapter and tried to add the another TextView "Breed" of the animals, but when i execute the program , its not showing the Breed. Where am i doing wrong ? I am scratching my head since long. please help where is the mistake ?
MainActivity.Java
public class MainActivity extends AppCompatActivity {
ListView simpleList;
ArrayList<Item> animalList=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = (ListView) findViewById(R.id.simpleListView);
animalList.add(new Item("Lion",R.drawable.lion,"Khatarnak"));
animalList.add(new Item("Tiger",R.drawable.tiger,"Fudu"));
animalList.add(new Item("Monkey",R.drawable.monkey,"Lallu"));
animalList.add(new Item("Elephant",R.drawable.elephant,"Jabardast"));
animalList.add(new Item("Dog",R.drawable.dog,"ItemDog"));
animalList.add(new Item("Cat",R.drawable.cat,"MeeMee"));
MyAdapter myAdapter=new MyAdapter(this,R.layout.list_view_items,animalList);
simpleList.setAdapter(myAdapter);
}
}
MyAdapter.Java
public class MyAdapter extends ArrayAdapter<Item> {
ArrayList<Item> animalList = new ArrayList<>();
public MyAdapter(Context context, int textViewResourceId, ArrayList<Item> objects) {
super(context, textViewResourceId, objects);
animalList = objects;
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_items, null);
TextView textView = (TextView) v.findViewById(R.id.textView);
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
TextView breedView = (TextView)v.findViewById(R.id.breed);
textView.setText(animalList.get(position).getAnimalName());
imageView.setImageResource(animalList.get(position).getAnimalImage());
breedView.setText(animalList.get(position).getBreed());
return v;
}
}
Item.Java
public class Item {
String animalName;
int animalImage;
String breedName;
public String getBreed() {
return breedName;
}
public void setBreed(String breed) {
this.breedName = breedName;
}
public Item(String animalName,int animalImage,String breedName)
{
this.animalImage=animalImage;
this.animalName=animalName;
this.breedName = breedName;
}
public String getAnimalName()
{
return animalName;
}
public int getAnimalImage()
{
return animalImage;
}
}
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity">
<ListView
android:id="#+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="2dp"/>
</RelativeLayout>
list_view_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
This is most likely a problem with the linear layout for each item. The orientation is horizontal, which lays each view one after the other horizontally. The problem is that the text view for the animal type has a width of fill_parent leaving no space for the breed view. If you change it to wrap_content it'll probably work for some of the cases, but might not work with everything.
In any case I think this is a problem with the views' positions and sizes. Try and move them around. Perhaps even a simple change would be placing them vertically:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
There's perhaps a more efficient way of doing this, but this is just an example. The inner linear layout places one text view on to of the other.
PS: your getCount method is not really doing anything. You can remove it.
replace your layout with my code your issue will resolve.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
My Problem is --> I put a white line as a view in the Fragment item xml but it doesn't appear when i debug it. How can i get the changes i make in the fragment item xml file. Because i tried some little changes like alpha property change but it didnt change is it because of this is a listview and has itself design? Please help me about that problem.
The xml code and adapter code are below.
<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="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="5dp"
>
<ImageView
android:id="#+id/menulogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/infoicon"
android:layout_gravity="center"
android:paddingTop="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/txtMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Company Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1" />
</LinearLayout>
<View
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="#+id/txtMenu"
android:background="#FFFFFF"
/>
</LinearLayout>
My listview adapter codes right down below.
public class MenuListAdapter extends ArrayAdapter<Menu_Item> {
private final ArrayList<Menu_Item> list;
private final Activity context;
private int[] colors = new int[]{0x34a4a4a4, 0x34fafafa};
public MenuListAdapter(Activity context, ArrayList<Menu_Item> list) {
super(context, R.layout.menu_item, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
public ImageView menulogo;
public TextView txtMenu;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.menu_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.menulogo = (ImageView) view.findViewById(R.id.menulogo);
viewHolder.txtMenu = (TextView) view.findViewById(R.id.txtMenu);
view.setTag(viewHolder);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtMenu.setText(list.get(position).getMenuName());
//holder.menulogo.setImageBitmap(list.get(position).getMenuImage());
String menuName=list.get(position).getMenuName();
if (menuName.equals("Info")){
holder.menulogo.setImageResource(R.drawable.infoicon);
}
if (menuName.equals("Odalar")){
holder.menulogo.setImageResource(R.drawable.accomodation);
}
if (menuName.equals("Galeri")){
holder.menulogo.setImageResource(R.drawable.galeryicon);
}
if (menuName.equals("Aktiviteler")){
holder.menulogo.setImageResource(R.drawable.activitiesicon);
}
if (menuName.equals("Robin's Kids Club")){
holder.menulogo.setImageResource(R.drawable.kidsclub);
}
if (menuName.equals("Restaurants")){
holder.menulogo.setImageResource(R.drawable.restaurantsicon);
}
view.setId(list.get(position).getMenu_id());
return view;
}
#Override
public int getViewTypeCount() {
if (getCount() != 0)
return getCount();
return 1;
}
}
I found an answer to my question and wanted to share it with you.
I only used these 2 properties below of ListView
android:divider="#FFFFFF"
android:dividerHeight="1dip"
Like this...
<ListView
android:id="#+id/menuList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:divider="#FFFFFF"
android:dividerHeight="1dip"
/>
In ListView XML add these two lines
<ListView
android:divider="#android:color/white"
android:dividerHeight="5dip"
.
.
</ListView>
Try to change the View with id line for a Space component. It is an extension of View, and works better for what you want.
Here is a description of the widget:
https://developer.android.com/reference/android/widget/Space.html
As shown in the image, I am able to render the view. But the onChildClick event of Expandable view is not getting triggered.
Also the touch event in the adapter is not responding. My layout details are mentioned below.
The full source code for this can be found in here.
main_activity.xml
<ExpandableListView
android:id="#+id/list_brands"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null"
android:groupIndicator="#null"
android:dividerHeight="5dp" />
item_parent.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="#+id/text_brand"
android:textSize="18dp"
android:text="Text"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image_indicator"
android:src="#drawable/ic_keyboard_arrow_down_black_18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
item_group_child.xml
<android.support.v7.widget.RecyclerViewxmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mobiles"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
item_child.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="#+id/image_mobile"
android:layout_width="70dp"
android:adjustViewBounds="true"
android:layout_height="120dp" />
<TextView
android:id="#+id/text_mobile_model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:id="#+id/text_mobile_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
**My Solution : **
Touch events are directly passed to the UI Elements of the View holder in the Recycler List view. But to give the touch event to the whole layout I have added the transparent button on top of child_item layout and added a click listener. Following is the updated child_item.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/dummy_click"
android:background="#android:color/transparent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:padding="10dp"
android:background="#color/colorGrey"
>
<ImageView
android:id="#+id/img_subcategory"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher"
android:layout_marginTop="10dp"
android:layout_width="180dp"
android:layout_height="90dp"
android:scaleType="fitXY"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sub Category"
android:textColor="#color/colorWhite"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:id="#+id/txt_subcategory"
android:textSize="#dimen/app_text_title"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Theory
ExpandableListView, child click sample:
for click on child, you can handle this way.
getExpandableListView().setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// your code...
}
});
Expandable RecyclerView, useful library and easy item click support:
This blog post by Hugo shows an alternative way without listeners:
http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/
It comes down to adding these lines:
ItemClickSupport.addTo(mRecyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
// do it
}
});
When using this class:
public class ItemClickSupport {...}
And this value in ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="item_click_support" type="id" />
</resources>
Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview?
Since the introduction of ListView, onItemClickListener has been
problematic. The moment you have a click listener for any of the
internal elements the callback would not be triggered, but it wasn't
notified or well documented (if at all) so there was a lot of
confusion and SO questions about it.
OnItemClickListener doesn't work with ListView item containing button
Just add this line into the item views instead of listView itself
android:focusable="false"
Check more detail about this from: Android custom ListView unable to click on items
Practice
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_group_child, parent, false);
childHolder = new ChildHolder();
// Fix listview onclicklistener issue: inner views can not be focusable
childHolder.YOUR_INNER_BUTTON = (YOUR_BUTTON_TYPE) convertView.findViewById(R.id.YOUR_BUTTON_ID);
childHolder.YOUR_INNER_BUTTON.setFocusable(false);
convertView.setTag(childHolder);
}
else {
childHolder = (ChildHolder) convertView.getTag();
}
childHolder.horizontalListView = (RecyclerView) convertView.findViewById(R.id.mobiles);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
childHolder.horizontalListView.setLayoutManager(layoutManager);
MobileAdapter horizontalListAdapter = new MobileAdapter(context, brands.get(groupPosition).mobiles);
childHolder.horizontalListView.setAdapter(horizontalListAdapter);
return convertView;
}
Added two lines to your code based on my legacy code:
// Creates a ViewHolder and store references to the children views (collapsed)
holder = new ViewHolderChildren();
holder.textLine = (TextView) convertView.findViewById(R.id.usersTextLine);
holder.subtextLine = (TextView) convertView.findViewById(R.id.usersSubtextLine);
holder.iconLine = (ImageView) convertView.findViewById(R.id.usersIconLine);
holder.buttonLine = (ImageButton) convertView.findViewById(R.id.usersButtonLine);
holder.infoLine = (TextView) convertView.findViewById(R.id.usersInfoLine);
holder.infoLine.setVisibility(TextView.GONE);
holder.userprofileButton = (ImageButton) convertView.findViewById(R.id.usersUserprofileBtn);
holder.buttonLine.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// if gone, set visible and arrow up image
if (holder.infoLine.getVisibility() == TextView.GONE) {
holder.infoLine.setVisibility(TextView.VISIBLE);
holder.buttonLine.setImageLevel(1);
}
// if visible, set gone and arrow down image
else {
holder.infoLine.setVisibility(TextView.GONE);
holder.buttonLine.setImageLevel(0);
}
}
});
//fixed listview onclicklistener bug: inner views can not be focusable
holder.buttonLine.setFocusable(false);
convertView.setTag(holder);
I'm beginner in android development and i a have question about Listview and android:onclick attribute.
I try to use attribute android:onclick ="myMethod" on each element of my listview instead of define OnItemClickListener/onItemClick (too much verbose for me)
MyMethod is executed but how can i get clicked element (current context) ?
Thanks in advance
As a beginner, setting an OnItemClickListener might seem a bit scary, but it's really not a big deal at all (and a core concept you should be learning anyway!).
So if you have a simple layout with a ListView in it like this...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/myListview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
You can set up an OnItemClickListener with just this code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_listview_layout);
listView = (ListView)findViewById(R.id.myListview);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Every time an item is clicked, you can handle it right here.
}
});
}
So every time an item in your list is clicked, the onItemClick method is fired, and you can figure out which item was clicked by using the int position argument that comes in.
If i am right your question is about implementing on click listener on each listrow item instead of list row.
If that is correct this is how you should implement:
1.In your adapter of getView() method you might be inflating a layout for list row like below:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_row_layout, parent, false);
return convertView;
}
In the above code list_row_layout is the xml file i'm inflating.
Go to that layout and add the following line android:descendantFocusability="blocksDescendants" to the root level of the xml layout like below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="4dp"
android:descendantFocusability="blocksDescendants"<!--This is the line i'm talking about-->
android:background="#drawable/rounded_border_layout"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="40dp"
android:layout_marginRight="10dp"
android:id="#+id/review_main_user_image_layout"
android:layout_marginLeft="10dp"
android:layout_height="54dp">
<ImageView
android:layout_width="40dp"
android:id="#+id/review_main_user_image"
android:background="#drawable/reviews_x_trail_user_image_border"
android:layout_height="40dp"
android:src="#drawable/ic_help_user" />
<TextView
android:typeface="monospace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Me"
android:textSize="10sp"
android:layout_gravity="center"
android:singleLine="true"
android:id="#+id/review_main_user_name"
android:layout_below="#+id/review_main_user_image" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/review_main_user_image_layout"
android:layout_toEndOf="#+id/review_main_user_image_layout">
<TextView
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:textColor="#color/black"
android:layout_height="wrap_content"
android:singleLine="true"
android:id="#+id/review_main_title"
android:text="Good Product For The Price"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:orientation="horizontal">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/review_main_rating"
android:progressDrawable="#drawable/apptheme_ratingbar_small_holo_light"
android:numStars="5"
style="?android:attr/ratingBarStyleSmall" />
<TextView
android:layout_marginLeft="8dp"
android:text="12-Jun-1993"
android:id="#+id/review_main_date"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
That't it now in your adapter getView method you can implement on click listener for each widget you have in that layout.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ReviewsXTrailViewHolder reviewsXTrailViewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_row_review_main, parent, false);
TextView title = (TextView)convertView.findViewById(R.id.review_main_user_image);
//title.setOnclickListener()
return convertView;
}
In that above code i have added on click listener for textview.I hope it will work
Comment below if that doesn't work
I have created an app which uses a custom listview Fragment. But the custom listview is not displaying properly.
below attached is my screen shot.
ANd here is my Xml file of individual row 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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/Jobtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="2dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textStyle="bold" >
</TextView>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Jobtitle"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<TextView
android:id="#+id/publishdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PubDate:-"
android:textColor="#color/textgrey"
/>
<TextView
android:id="#+id/lefttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/publishdate"
android:layout_alignBottom="#+id/publishdate"
android:layout_alignParentRight="true"
android:text="Time Left:-"
android:textColor="#color/textgrey"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1"
android:background="#drawable/dividr" >
</LinearLayout>
and here is the list fragment layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No text" >
</TextView>
And here is my adapter
package my.newapp.freelanceeye;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class joblisted extends ListFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapt my=new adapt(getActivity(), R.layout.listrow,m);
setListAdapter(my);
}
Context ctx;
public class adapt extends ArrayAdapter<String>{
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.listrow,parent,false);
return row;
//return super.getView(position, convertView, parent);
}
public adapt(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
ctx=context;
}
}
String[] m={"as","asa","sd","Ad","sad","asda"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.listfragmnetlayout,container,false);
return v;
}
}
You should set your fragment's layout something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</<LinearLayout>
I think the problem is with height of your Views, thats why try it with match_parent.
Your are building a custom array adapter but your are not setting the parameters in your row fields.
Take a look at this adapter implementation, there are comments explaining every step:
private class CustomArrayAdapter extends ArrayAdapter<RowData>
{
private ArrayList<RowData> list;
//this custom adapter receives an ArrayList of RowData objects.
//RowData is my class that represents the data for a single row and could be anything.
public CustomArrayAdapter(Context context, int textViewResourceId, ArrayList<RowData> rowDataList)
{
//populate the local list with data.
super(context, textViewResourceId, rowDataList);
this.list = new ArrayList<RowData>();
this.list.addAll(rowDataList);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
//creating the ViewHolder we defined earlier.
ViewHolder holder = new ViewHolder();)
//creating LayoutInflator for inflating the row layout.
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflating the row layout we defined earlier.
convertView = inflator.inflate(R.layout.row_item_layout, null);
//setting the views into the ViewHolder.
holder.title = (TextView) convertView.findViewById(R.id.tvItemTitle);
holder.changeRowStatus = (ImageView) convertView.findViewById(R.id.iStatus);
holder.changeRowStatus.setTag(position);
//define an onClickListener for the ImageView.
holder.changeRowStatus.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(activity, "Image from row " + position + " was pressed", Toast.LENGTH_LONG).show();
}
});
holder.checked = (CheckBox) convertView.findViewById(R.id.cbCheckListItem);
holder.checked.setTag(position);
//define an onClickListener for the CheckBox.
holder.checked.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//assign check-box state to the corresponding object in list.
CheckBox checkbox = (CheckBox) v;
rowDataList.get(position).setChecked(checkbox.isChecked());
Toast.makeText(activity, "CheckBox from row " + position + " was checked", Toast.LENGTH_LONG).show();
}
});
//setting data into the the ViewHolder.
holder.title.setText(RowData.getName());
holder.checked.setChecked(RowData.isChecked());
//return the row view.
return convertView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textColor="#339966"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PubDate:-"
android:textColor="#606060"
/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time Left:-"
android:textColor="#606060"
android:paddingLeft="180dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
try this....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
Change layout_height to fill_parent