my ListView I would like to be about a height of 50dp, but the problem is no matter what value I set the xml layout file too, it automatically makes it much bigger.
Here is my listview layout:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainListView"
android:layout_below="#+id/"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp" />
My rowview is
<?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="?android:attr/listPreferredItemHeight">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:textColor="#000000"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false"
android:layout_alignParentLeft="false"
android:layout_toRightOf="#+id/"
android:textColor="#000000"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/usernameTextView"
android:layout_below="#+id/"
android:textColor="#666666"
android:layout_toRightOf="#+id/" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
</RelativeLayout>
And my LocalQueueAdapter
package .skyrealm.com;
import android.content.Context;
import android.graphics.Color;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by RockyFish on 1/24/16.
*/
public class LocalQueueAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
ArrayList<ArrayList<String>> localQueueInfo;
public LocalQueueAdapter(Context context, ArrayList<ArrayList<String>> localQueueInfo)
{
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.localQueueInfo = localQueueInfo;
}
#Override
public int getCount() {
return localQueueInfo.size();
}
#Override
public Object getItem(int position) {
return localQueueInfo.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView songNameTextView, artistNameTextView, usernameTextView, numberEditText;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = layoutInflater.inflate(R.layout.local_queue_list_item, null);
Holder holder = new Holder();
holder.songNameTextView = (TextView) rowView.findViewById(R.id.songNameTextView);
holder.artistNameTextView = (TextView) rowView.findViewById(R.id.artistNameTextView);
holder.usernameTextView = (TextView) rowView.findViewById(R.id.usernameTextView);
holder.numberEditText = (TextView) rowView.findViewById(R.id.numberTextView);
holder.songNameTextView.setText(" " + localQueueInfo.get(position).get(1));
holder.artistNameTextView.setText(" - " + localQueueInfo.get(position).get(2));
holder.usernameTextView.setText(localQueueInfo.get(position).get(4));
holder.numberEditText.setText(String.valueOf(position + 1) + ".");
if(position == 0)
{
rowView.setBackgroundColor(Color.parseColor("#45B8AC"));
} else if(position == 1)
{
rowView.setBackgroundColor(Color.parseColor("#74E2D5"));
}
return rowView;
}
}
Thank you!
The problem is the inflater. I'm not really sure why. The code below should work for you. Also, you didn't understand how to use ViewHolder (Here is an example and the explanation: https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
View convertView = LayoutInflater.from(context).inflate(R.layout.local_queue_list_item, parent, false);
Holder holder = new Holder();
holder.songNameTextView = (TextView) rowView.findViewById(R.id.songNameTextView);
holder.artistNameTextView = (TextView) rowView.findViewById(R.id.artistNameTextView);
holder.usernameTextView = (TextView) rowView.findViewById(R.id.usernameTextView);
holder.numberEditText = (TextView) rowView.findViewById(R.id.numberTextView);
convertView.setTag(holder);
}
Holder holder = convertView.getTag();
....
return convertView;
}
Related
I have a text below a listview and I need that if listview contains more items, to push the text down more. Because many items in my listview, causes the view to look bad. Look at the example to understand
Picture
My code is
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="MaƱana"
android:id="#+id/manianaTitle" />
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/listaManiana"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tarde"
android:id="#+id/tardeTitle" />
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/listaTarde"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Noche"
android:id="#+id/nocheTitle" />
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/listaNoche"
android:layout_gravity="center_horizontal" />
</LinearLayout>
`
It is better to use a ListView with section. In that case you can categorize each section in your ListView instead of using many ListViews and TextViews.
I found this example from this website and I did some modification in the code
Create your List Item View
<?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">
<ImageView
android:id="#+id/photo"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="32dp"
android:src="#drawable/female"
android:contentDescription="#string/app_name"/>
<TextView
android:id="#+id/photo_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/photo"
android:layout_alignTop="#+id/photo"/>
</RelativeLayout>
Create a layout for the header section
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:gravity="center_vertical"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF000000" />
</LinearLayout>
Create an adapter for your List View
import java.util.ArrayList;
import java.util.TreeSet;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
class CustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<EntityObject> mData = new ArrayList<String>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final EntityObject item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final String item) {
mData.add(item);
sectionHeader.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.snippet_item1, null);
holder.textView = (TextView) convertView.findViewById(R.id.photo_details);
holder.photo = (ImageView)convertView.findViewById(R.id.photo)
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.snippet_item2, null);
holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position).getText());
holder.setImageResource(mData.get(position).getImage());
return convertView;
}
public static class ViewHolder {
public TextView textView;
public ImageView photo;
}
}
Yes i know this has been asked many times. I tried every solution provided but ain't working for me. So i am again asking the same question again.
This is my layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="#drawable/view_selector"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:padding="3dp">
<TextView android:id="#+id/calcName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:layout_weight="0.9"
android:gravity="start"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textIsSelectable="false"
android:textColor="#FF000000" />
<ImageView android:id="#+id/nextImg"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="end|right"
android:layout_weight="0.1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:contentDescription="#string/strImg"
android:src="#drawable/next" />
</LinearLayout>
As you can see i have made every view loose focus even the root view does it.
Then here is my adapter class
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.everythingrf.android.rfcalculators.R;
import java.util.ArrayList;
import java.util.List;
public class CommonListAdapter extends ArrayAdapter<String> {
private Context mContext;
private ArrayList<String> mDataObjects;
private int layout;
public CommonListAdapter(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
mContext = context;
layout = resource;
mDataObjects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layout, parent, false);
holder.calcName = (TextView) convertView.findViewById(R.id.calcName);
holder.nextImg = (ImageView) convertView.findViewById(R.id.nextImg);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.calcName.setFocusable(false);
holder.nextImg.setFocusable(false);
holder.calcName.setText(mDataObjects.get(position));
return convertView;
}
public static class ViewHolder{
TextView calcName;
ImageView nextImg;
}
}
Even tried with the adapter
Here is my main class which calls onItemClickListener
myCalcList.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
Log.e("Main","item click");
String selectedCalc = commonList.get(pos);
openSelectedCalc(selectedCalc);
}
Thanks in advance :)
Please just remove android:clickable="true" from row layout,this will work.
As each row is consuming the event which prevent the listview to execute the onItemClick.
If you are extending yout Main from ListActivity in your onCreate method you have to put something like
getListView().setOnItemClickListener(this);
Look I have a example of a custom ListView in my GitHub https://github.com/alvaroroyo/ListViewCustom/tree/master/app/src/main/java/com/example/aroyo/listaliada
I am new to android and facing one very comman problem of listview i googled alot but did'nt found anything usefull for my case.Hope you people show me the right direction.Whenever I am changing my image from "plus" to "tick" on click my last item image also got automatically changed on scroll.
My list_comapare_product 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:background="#color/white_color"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#color/white_color"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/img_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/crane" />
<com.mss.skyjack.custom.views.SkyjackCustomTextview
android:id="#+id/tv_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="test"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/img_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/plus" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#color/black_color" />
</LinearLayout>
Here is my Adapter :-
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ComapreProductSelectionAdapter extends BaseAdapter {
Activity activity;
List<SelectorTest> listSelector;
private LayoutInflater mLayoutInflater;
public ComapreProductSelectionAdapter(Activity activity,
List<SelectorTest> listSelector) {
this.activity = activity;
this.listSelector = listSelector;
mLayoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listSelector.size();
}
#Override
public SelectorTest getItem(int position) {
return listSelector.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final int i = position;
final SelectorTest listItem = listSelector.get(position);
if (convertView == null) {
convertView = mLayoutInflater.inflate(
R.layout.list_comapare_product, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvProductName = (TextView) convertView
.findViewById(R.id.tv_product_name);
viewHolder.imgProduct = (ImageView) convertView
.findViewById(R.id.img_product);
viewHolder.imgadd = (ImageView) convertView
.findViewById(R.id.img_plus);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imgadd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (viewHolder.imgadd.getDrawable().getConstantState() == activity
.getResources().getDrawable(R.drawable.plus)
.getConstantState()) {
viewHolder.imgadd.setImageResource(R.drawable.tick);
} else {
viewHolder.imgadd.setImageResource(R.drawable.plus);
}
}
});
return convertView;
}
static class ViewHolder {
TextView tvProductName;
ImageView imgProduct, imgadd;
View viewRightLine;
}
}
Thanks.
You need to keep track of which items have been "ticked". I copied your adapter and made necessary changes. You can just copy-paste the code and it should work.
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ComapreProductSelectionAdapter extends BaseAdapter {
Activity activity;
List<SelectorTest> listSelector;
private LayoutInflater mLayoutInflater;
private ArrayList<Integer> tickedItems = new ArrayList<Integer>();
public ComapreProductSelectionAdapter(Activity activity,
List<SelectorTest> listSelector) {
this.activity = activity;
this.listSelector = listSelector;
mLayoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listSelector.size();
}
#Override
public SelectorTest getItem(int position) {
return listSelector.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final int i = position;
final SelectorTest listItem = listSelector.get(position);
if (convertView == null) {
convertView = mLayoutInflater.inflate(
R.layout.list_comapare_product, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvProductName = (TextView) convertView
.findViewById(R.id.tv_product_name);
viewHolder.imgProduct = (ImageView) convertView
.findViewById(R.id.img_product);
viewHolder.imgadd = (ImageView) convertView
.findViewById(R.id.img_plus);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imgadd.setTag(position);
viewHolder.imgadd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(tickedItems.contains((Integer)v.getTag()) {
//Already ticked, set to plus
viewHolder.imgadd.setImageDrawable(activity.getResources().getDrawable(R.drawable.plus));
tickedItems.remove((Integer)v.getTag());
} else {
tickedItems.add((Integer)v.getTag());
viewHolder.imgadd.setImageDrawable(activity.getResources().getDrawable(R.drawable.tick));
}
}
});
if(tickedItems.contains(position))
viewHolder.imgadd.setImageDrawable(activity.getResources().getDrawable(R.drawable.tick));
else
viewHolder.imgadd.setImageDrawable(activity.getResources().getDrawable(R.drawable.plus));
return convertView;
}
static class ViewHolder {
TextView tvProductName;
ImageView imgProduct, imgadd;
View viewRightLine;
}
}
As an extra, don't use ImageView.setImageResource() as it is inefficient.
Quote from Android docs about ImageView.setImageReource()
This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.
I am trying to develop a calendar application which displays all the months in a year in a single activity .I am having troubles with it . This is the view that i want to make . I am so lost :/
ListViewAdapter:
package com.example.calendar15;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class CalendarListAdapter extends BaseAdapter {
private Context mContext;
String[] months;
public String[] numbers;
// Constructor
public CalendarListAdapter(Context c,String[] months) {
mContext = c;
this.months=months;
}
public int getCount() {
return months.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid, parent, false);
}
// Lookup view for data population
TextView tvTitle = (TextView) convertView.findViewById(R.id.date);
Log.d("months[position]",""+months[position]);
tvTitle.setText(months[position]);
/**/
String[] numbers = { "1", "2", "3", "4", "5", "6", "7","8","9","10","11","12"};
/*TextView tvDate1 = (TextView) convertView.findViewById(R.id.text1);
Log.d("numbers[position]",""+numbers[position]);
tvDate1.setText(numbers[position]);*/
GridView gridview = (GridView)convertView.findViewById(R.id.gridview);
CalendarGridAdapter calendarGridAdapter = new CalendarGridAdapter(mContext, numbers);
gridview.setAdapter(calendarGridAdapter);
return convertView;
}
}
GridViewAdapter:
package com.example.calendar15;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CalendarGridAdapter extends BaseAdapter {
Context mContext;
String[] number;
CalendarGridAdapter(Context context, String[] numbers) {
this.mContext = context;
this.number = numbers;
Log.d("number.length",""+number.length);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return number.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return number[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.calendar_item, parent, false);
}
// Lookup view for data population
TextView tvTitle = (TextView) convertView.findViewById(R.id.date);
Log.d("number[position]",""+number[position]);
tvTitle.setText(number[position]);
return convertView;
}
}
Grid.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#727272"
android:textSize="16sp"
android:gravity="center"
android:text="date"
android:textStyle="normal" />
<GridView
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:listSelector="#android:color/transparent"
android:numColumns="7"
android:stretchMode="columnWidth" />
</GridLayout>
calendar_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Datelayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:gravity="center"
android:orientation="vertical"
android:padding="2dip" >
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="date"
android:textColor="#727272"
android:textSize="16sp"
android:textStyle="normal" >
</TextView>
</LinearLayout>
Solution
I changed the height of Gridview from wrap_content and set it about 200dp - the height of elements to be inflated multiplied by total no of rows. (you could set it dynamically or in the xml as per your requirements ). I hope this helps others as well
My activity extends the ListActivvity and I added the onListItemClick () inside it but listitem click is not getting called.
When I inflate my view and returning that view inside the getView () method listitem click is not getting called but when I did not inflate view and directly call the super method it works fine.
CustomArrayAdapter.java
package com.mobileinsight.presenter.form;
import android.content.Context;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.mobileinsight.R;
import com.mobileinsight.common.SimpleMap;
import com.mobileinsight.common.Util;
public class CustomArrayAdapter extends ArrayAdapter<SimpleMap> {
private Product[] objects;
private Context _this;
public CustomArrayAdapter(Context context, int resource, Product[] objects) {
super(context, resource, objects);
this.objects = objects;
this._this = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) _this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_row, parent, false);
} else {
rowView = convertView;
}
TextView textView = (TextView) rowView.findViewById(R.id.text1);
TextView detailview = (TextView) rowView.findViewById(R.id.text2);
textView.setText("Text 1"));
// textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
detailview.setText(objects[position].getDetails());
return rowView; // not working
// return super.getview(); // Working
}
}
list_row.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="horizontal" >
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical|left"
android:layout_margin="6dip"
android:layout_weight="2"
android:ellipsize="end"
android:gravity="center_vertical|left"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical|right"
android:layout_margin="6dip"
android:layout_weight="0"
android:ellipsize="none"
android:gravity="center_vertical|right"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Try this code, it may help..
public class CustomArrayAdapter extends ArrayAdapter<SimpleMap>
{
LayoutInflater inflater;
public CustomArrayAdapter(Context context)
{
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (convertView == null) {
rowView = inflater.inflate(R.layout.list_row, parent, false);
} else {
rowView = convertView;
}
TextView textView = (TextView) rowView.findViewById(R.id.text1);
TextView detailview = (TextView) rowView.findViewById(R.id.text2);
return rowView;
}
}