I have a ListView which derives from a custom row which I have created. This is all working fine, accept the fact that my EditText refuses to show the number keyboard.
Below is my XML:
<?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_gravity="center_vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/chkPlayer" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/txtName"
android:layout_toRightOf="#+id/chkPlayer"
android:layout_alignBottom="#+id/chkPlayer"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/started"
android:layout_below="#+id/txtName"
android:layout_marginLeft="26dp"
android:id="#+id/chkStarted" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/captain"
android:layout_below="#+id/txtName"
android:layout_toRightOf="#+id/chkStarted"
android:id="#+id/chkCaptain" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gk"
android:layout_below="#+id/txtName"
android:layout_toRightOf="#+id/chkCaptain"
android:id="#+id/chkGK" />
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/txtTime"
android:layout_marginLeft="#dimen/margin10"
android:hint="#string/time"
android:layout_alignBottom="#+id/chkGK"
android:layout_toRightOf="#+id/chkGK" />
</RelativeLayout>
I have tried several additional approaches however simply cannot get it to work, below is my custom Array Adapter:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if(rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row_squad, parent, false);
holder = new Holder();
holder.txtName = (TextView)rowView.findViewById(R.id.txtName);
holder.chkPlayer = (CheckBox)rowView.findViewById(R.id.chkPlayer);
holder.txtTime = (EditText)rowView.findViewById(R.id.txtTime);
rowView.setTag(holder);
}
else {
holder = (Holder) convertView.getTag();
}
holder.txtName.setText(playerNames.get(position));
holder.chkPlayer.setFocusable(false);
holder.chkStarted.setFocusable(false);
holder.chkCaptain.setFocusable(false);
holder.chkGK.setFocusable(false);
//holder.txtTime.setFocusable(true);
//InputMethodManager im = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
//im.showSoftInput(holder.txtTime, InputMethodManager.SHOW_IMPLICIT));
//EditText txtTime = (EditText)rowView.findViewById(R.id.txtTime);
return rowView;
}
Try to use like this
android:inputType="numberDecimal"
Related
Here is my custom adapter code. I get an error that my textView.setText() is null
"Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"
I usually retrieve data from my database, but right now I used hardcoded string just to check if the problem is with the database, but the problem persist with the hardcoded string.
public class CardViewAdapter extends BaseAdapter {
Context context;
List<Note> notes;
TextView cardTitleText;
TextView cardDescriptionText;
TextView cardDateText;
ImageView cardImage;
public CardViewAdapter(Context context, ArrayList<Note> notes) {
this.context = context;
this.notes = notes;
}
#Override
public int getCount() {
return notes.size();
}
#Override
public Object getItem(int position) {
return notes.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.card_view, parent, false);
cardTitleText = (TextView) view.findViewById(R.id.titleTextCardView);
cardDescriptionText = (TextView) view.findViewById(R.id.descriptionTextView);
cardDateText = (TextView) view.findViewById(R.id.dateTextCardView);
cardImage = (ImageView) view.findViewById(R.id.cardImage);
Note note = notes.get(position);
Log.v("NOTE", note.getTitle() + " " + note.getDescription() + " " + note.getDate());
cardTitleText.setText("Title");
cardDescriptionText.setText("Description");
cardDateText.setText("Date");
return view;
}
}
Here is my card_view layout (item layout for my listView):
<RelativeLayout
android:id="#+id/relativeLayoutCardView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/relative_layout"
android:elevation="6dp"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="#+id/titleTextCardView"
android:layout_below="#+id/frameLayoutCardView"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingTop="10dp"
android:text="Example Title"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/descriptionTextCardView"
android:layout_alignParentLeft="true"
android:layout_below="#+id/titleTextCardView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="Example description"
android:textSize="13sp"
android:textStyle="bold" />
<FrameLayout
android:id="#+id/frameLayoutCardView"
android:layout_height="130dp"
android:layout_width="fill_parent">
<ImageView
android:id="#+id/cardImage"
android:layout_height="130dp"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/placeholder" />
<TextView
android:id="#+id/dateTextCardView"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Example date" />
</FrameLayout> </RelativeLayout>
I know that this is probably a stupid and easy to fix problem, but I just keep banging my head because I don't know how to fix it because I can't see how can my TextView be null.
You have a typo in your XML... this line references an element that does not exist in your XML:
cardDescriptionText = (TextView) view.findViewById(R.id.descriptionTextView);
I think it should be:
cardDescriptionText = (TextView) view.findViewById(R.id.descriptionTextCardView);
based on the XML element:
<TextView
android:id="#+id/descriptionTextCardView"
android:layout_alignParentLeft="true"
android:layout_below="#+id/titleTextCardView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="Example description"
android:textSize="13sp"
android:textStyle="bold" />
I am building an app and have some difficulties on setting backround on a array adapter, hope someone can help!
public class ListAdapter extends ArrayAdapter {
// List context
private final Context context;
// List values
private final List<RssItem> items;
public ListAdapter(Context context, List<RssItem> items) {
// Set the layout for each item
super(context, R.layout.item, items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Build each element of the list
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
// Populate each layout element with the data from the items list
TextView itemPos = (TextView) rowView.findViewById(R.id.position_aa);
itemPos.setText(items.get(position).getPosition());
// Populate each layout element with the data from the items list
TextView itemTitle = (TextView) rowView.findViewById(R.id.textViewTitle);
itemTitle.setText(items.get(position).getTitle());
TextView itemPublishDate = (TextView) rowView.findViewById(R.id.textViewPublishDate);
itemPublishDate.setText(items.get(position).getPublishDate());
// Set the icon base on the post's category
ImageView icon = (ImageView) rowView.findViewById(R.id.imageViewCategoryIcon);
icon.setImageResource(CategoryMapper.getIconIdForCategory(items.get(position).getCategory()));
return rowView;
}
}
And this is the item.xml
<?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="fill_parent"
android:id="#+id/bg">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#0FFF3300"
android:id="#+id/item">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageViewCategoryIcon"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="5dp"
android:src="#drawable/ic_launcher" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="1dp">
<TextView
android:id="#+id/textViewTitle"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="[TITLE-GOES-HERE]"
android:textColor="#000"
android:textStyle="bold"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textViewPublishDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#555"
android:text="[PUB-DATE-GOES-HERE]"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/position_aa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#555"
android:text="[AA-GOES-HERE]"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
Here I deploy some rss data into RowViews Via the list adapter class, but how can I set a parent layout so I can give a fixed upon scroll background?
Thanks in advance
P.S I`m stuck for a day at this......
Set background color to your ListItem like
rowView.setBackgroundResource(R.color.yourcolor);
Well, I've got a ListViewAdapter.java, it looks like :
ListViewAdapter.java
public class ListViewItem {
public final Drawable icon; // the drawable for the ListView item ImageView
public final String title; // the text for the ListView item title
public final String precio; // the price for the ListView item
public final String descuento; // the price for the discount for the ListView item
// the text for the ListView item description
public ListViewItem(Drawable icon, String title, String precio, String descuento) {
this.icon = icon;
this.title = title;
this.precio = precio;
this.descuento = descuento;
}
}
At the time that I create the ListView on my Fragment doesn't say nothing wrong... The code is :
// initialize the items list
mItems = new ArrayList<ListViewItem>();
Resources resources = getResources();
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.tomate_oferta), getString(R.string.aim), getString(R.string.aim_precio), getString(R.string.aim_descuento)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.levadura_oferta), getString(R.string.youtube), getString(R.string.youtube_precio), getString(R.string.youtube_descuento)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.sopa_oferta), getString(R.string.bebo), getString(R.string.bebo_precio), getString(R.string.bebo_descuento)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.zumo_oferta), getString(R.string.pew), getString(R.string.pew_precio), getString(R.string.pew_descuento)));
// initialize and set the list adapter
setListAdapter(new ListViewDemoAdapter(getActivity(), mItems));
}
And finally my xml looks like :
ListViewItem.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- the parent view - provides the gray background -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="#color/frame_background"
android:padding="5dp" >
<!-- the innner view - provides the white rectangle -->
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/frame" >
<!-- the icon view -->
<ImageView android:id="#+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:contentDescription="#string/icon_content_description"
android:scaleType="fitXY"
android:layout_alignParentLeft="true" />
<!-- the container view for the title and description -->
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/ivIcon"
android:layout_centerVertical="true" >
<!-- the title view -->
<TextView android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textAppearance="#android:style/TextAppearance.Medium" />
<!-- the description view -->
<TextView android:id="#+id/tvDiscount"
android:layout_below="#id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textSize="12dp"
android:textAppearance="#android:style/TextAppearance.Small" />
<TextView android:id="#+id/tvPrice"
android:layout_below="#id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textAppearance="#android:style/TextAppearance.Small" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
ListViewDemoAdapter.java
public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> {
public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
super(context, R.layout.listview_item, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listview_item, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
viewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvPrice);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListViewItem item = getItem(position);
viewHolder.ivIcon.setImageDrawable(item.icon);
viewHolder.tvTitle.setText(item.title);
viewHolder.tvDiscount.setText(item.descuento);
viewHolder.tvPrice.setText(item.precio);
return convertView;
}
private static class ViewHolder {
ImageView ivIcon;
TextView tvTitle;
TextView tvDiscount;
TextView tvPrice;
}
}
Result:
I don't get what I'm doing wrong... I thing the main problem is on the XML, but I don't see where.
Hope you guys can help me out. Thanks.
Your Problem seems to be in Your layout_below attributes:
Every textView is set layout_below="#id/tvTitle", but with that every other textView is hidden by tvPrice. You have to change this like that:
<!-- the title view -->
<TextView android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textAppearance="#android:style/TextAppearance.Medium" />
<!-- the description view -->
<TextView android:id="#+id/tvDiscount"
android:layout_below="#id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textSize="12dp"
android:textAppearance="#android:style/TextAppearance.Small" />
<TextView android:id="#+id/tvPrice"
android:layout_below="#id/tvDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textAppearance="#android:style/TextAppearance.Small" />
The second Problem:
In Your Adapter, You are referencing two views with the same id_
viewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvPrice);
You have to change it to:
viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount);
I see 2 mistakes:
First is in layout, the tvprice and tvdiscount are overlapping as they are displayed one over the other.
<!-- the description view -->
<TextView android:id="#+id/tvDiscount"
android:layout_below="#id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textSize="12dp"
android:textAppearance="#android:style/TextAppearance.Small" />
<TextView android:id="#+id/tvPrice"
android:layout_below="#id/tvDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textAppearance="#android:style/TextAppearance.Small" />
Second is you are not setting the tv discount value:
// update the item view
ListViewItem item = getItem(position);
viewHolder.ivIcon.setImageDrawable(item.icon);
viewHolder.tvTitle.setText(item.title);
viewHolder.tvPrice.setText(item.precio);
viewHolder.tvDiscount.setText(item.descuento);
Another Problem is viewHolder.tvDiscount is pointing to tvPrice, correct as below:
viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount);
Change your xml to this. Use #+id/tvTitle instead of #id/tvTitle
<!-- the innner view - provides the white rectangle -->
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/frame" >
<!-- the icon view -->
<ImageView android:id="#+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:contentDescription="#string/icon_content_description"
android:scaleType="fitXY"
android:layout_alignParentLeft="true" />
<!-- the container view for the title and description -->
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/ivIcon"
android:layout_centerVertical="true" >
<!-- the title view -->
<TextView android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textAppearance="#android:style/TextAppearance.Medium" />
<!-- the description view -->
<TextView android:id="#+id/tvDiscount"
android:layout_below="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textSize="12dp"
android:textAppearance="#android:style/TextAppearance.Small" />
<TextView android:id="#+id/tvPrice"
android:layout_below="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textAppearance="#android:style/TextAppearance.Small" />
</RelativeLayout>
</RelativeLayout>
holder.item3.setImageResource(custom.getcustomImage());
This is a line of code in my custom ListViewAdapter that holds an imageview. I need to get the images that i put in to shrink down to the size of my ImageView within the ListView.
I want to apply imageview.setImageResource(custom.getcustomImage()); to the item in my adapter but i get an error.
Cannot invoke setScaleType(ImageView.ScaleType) on the primitive type int
My code look like this
holder.item3.setImageResource(custom.getcustomImage().setScaleType(ScaleType.FIT_XY);
How am I suppose to use this FIT_XY, and is there another way I can fit an image the ImageView within my ListView?
My Adapter.
public class CustomAdapter extends ArrayAdapter<Custom> {
private ArrayList<Custom> entries;
private Activity activity;
public CustomAdapter(Activity a, int textViewResourceId,
ArrayList<Custom> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
// TODO Auto-generated constructor stub
}
public class ViewHolder {
public TextView item1;
public TextView item2;
public ImageView item3;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.rowlayout, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.secondLine);
holder.item2 = (TextView) v.findViewById(R.id.firstLine);
holder.item3 = (ImageView) v.findViewById(R.id.icon);
v.setTag(holder);
} else
holder = (ViewHolder) v.getTag();
final Custom custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getcustomBig());
holder.item2.setText(custom.getcustomSmall());
holder.item3.setImageResource(custom.getcustomImage());
holder.item3.setScaleType(ScaleType.FIT_XY);
}
return v;
}
}
}
Line Calling code
a = new Custom("String one","Stringtwo", R.drawable.image);
Row Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#FFFFFF"
android:padding="6dip" >
<!-- android:background="#color/Cream" -->
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/launchicon" />
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Example application"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:text="Description"
android:textColor="#000000"
android:textSize="16sp" />
</RelativeLayout>
I see that item3 is an ImageView. You are trying to apply an image to that ImageView. Here, your getcustomImage() probably returns an imageResId.
If your getcustomImage() method returns an image res id(which is an integer value), use;
holder.item3.setImageResource(custom.getcustomImage());
to apply image to ImageView.
At the end there is no problem to use setScaleType on ImageView
holder.item3.setScaleType(ScaleType.FIT_XY);
Read more about setImageResource and setScaleType
Edit:
Shortly;
Change this;
holder.item3.setImageResource(custom.getcustomImage().setScaleType(ScaleType.FIT_XY);
With this:
holder.item3.setImageResource(custom.getcustomImage());
holder.item3.setScaleType(ScaleType.FIT_XY);
Edit:
With saying wrap_content to your ImageView at your rowlayout.xml you are eliminating scaleType option because your ImageView is going to resize itself to your image's size. You may want to give a static width to get a better visual on your list(all images width will be equal). And you want fitXY but consider using centerinside as an option(to a better view). And try to use scaleOption at your rowlayout so you can see its output at graphical layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#FFFFFF"
android:padding="6dip" >
<!-- android:background="#color/Cream" -->
<ImageView
android:id="#+id/icon"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/launchicon"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/firstLine"
android:layout_width="wrap_content"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Example application"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/firstLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:text="Description"
android:textColor="#000000"
android:textSize="16sp" />
</RelativeLayout>
I been struggling right now to customize the such that I can include another types of views in the gridview(Image). I want to achieve the layout below but in the Universal Image Loader it only displays images. Any experience with this?
Xml:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="120dip"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:scaleType="centerCrop" />
JAva
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
} else {
imageView = (ImageView) convertView;
}
imageLoader.displayImage(imageUrls[position], imageView, options);
return imageView;
}
I tried this code but it's not working. Basically I wanted to have the layout above.
XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_x="201px"
android:layout_y="165px"
android:gravity="center_horizontal"
>
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="120dip"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/icon_text"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textStyle="bold"
android:lines="2">
</TextView>
</LinearLayout>
Java
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.icon_text);
holder.image = (ImageView) view.findViewById(R.id.image);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.text.setText(album[position].getName());
imageLoader.displayImage(albumsPhoto[position], holder.image, options, animateFirstListener);
return view;
}
ViewHolder is a custom class which is not part of Android SDK. It holds other views like TextView, ImageView and so many. This class can be used in ListView where Adapter is used. Read More to know more about Holder.
As you have layout, following can be your ViewHolder class structure.
static class ViewHolder {
public TextView text;
public ImageView image;
}
You can see that, there is TextView and ImageView in ViewHolder. You need to define such many views as your custom_list_item layout has.
Read -> ViewHolder Pattern – Caching View Efficiently
Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_alignRight="#+id/ratingBar1"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/imageView1"
android:layout_below="#+id/imageView1" >
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="text1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="text2" />
</LinearLayout>
</RelativeLayout>
Hi Universal Image Loader is an example for gallery view. In order to achieve the above layout . You need view Holder class and an adapter ,, LIstadapter. You can list you images in list adapter and use ViewHolder for Image and Subtitle.
The MosT important thing will be bitmap crashing if you are loading lot of Images ,, so each tiem you scroll Do delete teh cache mem for Bit maps.