I am trying to inflate this list_item.xml, but apparently eclipse can't find list_item.xml, when I try to inflate R.layout.list_item and I also get an error when I try to find R.id.counter (the third element in the xml file).
I am not sure if there is a problem in the XML, but I really don't think so. Since I am pretty new to Android, I was guessing there could be a problem with the name of the file? list_item or an error after the first TextView, which I really can't see.
<?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="48dp"
android:background="#drawable/list_selector">
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/desc_list_item_icon"
android:src="#drawable/ic_home"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:textColor="#color/list_item_title"
android:gravity="center_vertical"
android:paddingRight="40dp"/>
<TextView android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/counter_bg"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:textColor="#color/counter_text_color"/>
</RelativeLayout>
Edit:
The error I am getting is the following:
"list_item cannot be resolved or is not a field"
This is how I am inflating the file and trying to get R.id.counter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
TextView txtCount = (TextView) convertView.findViewById(R.id.counter);
This is how it should be,
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_item,null);
TextView tvCounter = (TextView) view.findViewById(R.id.counter);
Related
I've been having some troubles with excess whitespace at the bottom of ListView items. Example with white/negative space marked with red arrows at http://i.stack.imgur.com/wpm7a.png
Here's my layout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.thereversecookbook.ResultsList" >
<ListView
android:id="#+id/results_list_lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
And my list item XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- cooking time -->
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:clickable="false"
android:longClickable="false"
android:paddingBottom="50dp"
android:textColor="#CC0033"
android:textSize="16sp" />
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerVertical="true"
android:clickable="false"
android:longClickable="false"
android:maxHeight="50dp"
android:maxWidth="50dp"
android:src="#drawable/ic_clock" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:layout_centerVertical="true"
android:clickable="false"
android:longClickable="false"
android:textColor="#3399FF"
android:textSize="14sp" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:isIndicator="true"
android:longClickable="false"
android:scaleX="0.5"
android:scaleY="0.5"/>
</RelativeLayout>
And CustomListAdapter:
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
public CustomListViewAdapter(Context context, int resourceId, List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtCookingTime;
RatingBar ratingBar;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtCookingTime = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
holder.ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar1);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtCookingTime.setText(rowItem.getCookingTime());
holder.txtTitle.setText(rowItem.getTitle());
//holder.imageView.setImageResource(rowItem.getImageId());
holder.ratingBar.setRating(rowItem.getRating());
holder.ratingBar.setStepSize((float)0.5);
return convertView;
}
}
Anyone have any ideas what could be causing this? Many thanks!
convertView = mInflater.inflate(R.layout.list_item, null);
Bzzzt. Don't use that inflate() method. You want to use the three-parameter version, passing in your convertView ("the inflated widgets will eventually be a child of this...") and false ("...but don't make them be children of it right away"):
convertView = mInflater.inflate(R.layout.list_item, parent, false);
That's particularly important in your case, as RelativeLayout gets weird if it's the root container of your layout file and you don't use this version of the inflate() method.
You can read more about this in Dave Smith's "Layout Inflation as Intended" blog post.
I am working on app with ListView with buttons on list item.
It's working fine but I can't find way to make buttons on items clickable.
I want to make this Silver arrows clickable. How can I do that?
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_alignParentRight="true"
android:onClick="btnclick"
android:layout_centerVertical="true"/>
How can I make this to be different, like ItemClickListener with position?
Here is my Adapter
class DAdapter extends ArrayAdapter<String>{
Context context;
int [] images;
String[] titleArray;
DAdapter(Context c, String [] titles, int imgs[])
{
super(c,R.layout.single_row, R.id.title,titles);
this.context=c;
this.images=imgs;
this.titleArray=titles;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
if (row==null) { LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.single_row, parent,false);
}
ImageView myImage=(ImageView) row.findViewById(R.id.list_image);
TextView myTitle=(TextView) row.findViewById(R.id.title);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
return row;
I'm not sure if I should add whole code but if its needed please tell me.
single_row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="#drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text=""
android:textColor="#040404"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="bold"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_alignParentRight="true"
android:onClick="btnclick"
android:layout_centerVertical="true"/>
</RelativeLayout>
First add id to your ImageView like..
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_alignParentRight="true"
android:id="#+id/arrowImage"
android:onClick="btnclick"
android:layout_centerVertical="true"/>
then get the ImageView and write listener ..change your getView like..
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_row, parent, false);
}
ImageView myImage = (ImageView) row.findViewById(R.id.list_image);
TextView myTitle = (TextView) row.findViewById(R.id.title);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
ImageView arrowImage = (ImageView) row.findViewById(R.id.arrowImage);
arrowImage.setTag(position);
arrowImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int clickedposition=(Integer) v.getTag();
//Write your Activity launch code here..
}
});
return row;
}
You should set a general onclick handler for the image view. Then while doing the job in the onclick listener just check its parent to know the parent of this button. And if you want some custom style for this arrow to be shown, you can use a button instead of imageButton and set its background to an xml file to make it dynamic.
Is this enough?
I'm working on an e-commerce application, i need to display a list of products -a product = one ImageView and some textViews- from the database, but when i extract the data from the database, everting works fine except the imageView, it shows the same image that is in the source Layout.
this is the getView() Method in my adapter.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null)
{
holder=new ViewHolder();
convertView = inflater.inflate(R.layout.lesproduits, null);
holder.nomduProduit = (TextView)convertView.findViewById(R.id.nomProduit);
holder.prixDuProduit = (TextView)convertView.findViewById(R.id.prixProduit);
holder.imageDuProduit = (ImageView)convertView.findViewById(R.id.imageProduit);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Bitmap bitmapImage = BitmapFactory.decodeFile(path+File.separator+lesProduits.get(position).getImage());
Drawable drawableImage = new BitmapDrawable(bitmapImage);
System.out.println(path+File.separator+lesProduits.get(position).getImage());
holder.imageDuProduit.setBackgroundDrawable(drawableImage);
holder.nomduProduit.setText(lesProduits.get(position).getNomDuProduit());
holder.prixDuProduit.setText(lesProduits.get(position).getPrixDuProduit());
return convertView;
}
and below is the source Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageProduit"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/nomProduit"
android:layout_marginTop="5dp"
android:layout_width="170dp"
android:layout_height="30dp"
android:layout_toRightOf="#+id/imageProduit"
android:text="Smart phone"
android:textSize="25sp" />
<TextView
android:id="#+id/prixProduit"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/nomProduit"
android:layout_below="#+id/nomProduit"
android:layout_marginTop="5dp"
android:text="Large Text"
android:textSize="15sp" />
</RelativeLayout>
You are changing the background, not the foreground.
ImageView has both foregrond and background images.
instead of this
holder.imageDuProduit.setBackgroundDrawable(drawableImage);
use this
holder.imageDuProduit.setImageDrawable(drawableImage);
In my application i have list which i inflate it with array adapter. every thing is OK and i have my result in the list.
I like to change background color of list (even rows red for example and odd rows green).
This is xml of my rows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:id="#+id/rllist"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
>
<TextView
android:id="#+id/outstanding_contractdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="#+id/outstanding_contractno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/outstanding_contractdate"
android:paddingLeft="20dip"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="#+id/outstanding_contractamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="15sp" />
</RelativeLayout>
</LinearLayout>
I have written following codes:
static class ViewHolder {
RelativeLayout rlList;
TextView tvContDate;
TextView tvContNo;
TextView tvContAmount;
}
and in Array Adapter I have this one:
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
View row = convertView;
if(row == null){
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.list_outstanding, parent, false);
//convertView = mInflater.inflate(R.layout.list_outstanding, parent, false);
holder = new ViewHolder();
holder.rlList = (RelativeLayout) convertView.findViewById(R.id.rllist);
holder.tvContDate = (TextView) row.findViewById(R.id.outstanding_contractdate);
holder.tvContNo = (TextView) row.findViewById(R.id.outstanding_contractno);
holder.tvContAmount = (TextView) row.findViewById(R.id.outstanding_contractamount);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.tvContDate.setText(llData.get(position).split(",")[0].trim());
holder.tvContNo.setText(llData.get(position).split(",")[1].trim());
holder.tvContAmount.setText(llData.get(position).split(",")[2].trim());
if(position%2 != 0)
holder.rlList.setBackgroundColor(0x00FF00);
else
holder.rlList.setBackgroundColor(0XFF0000);
return(row);
}
after running application when i reach to this code, an error occurs. according to LogCat it is "Null Pointer Exception" and points to this line in getView():
holder.rlList = (RelativeLayout) convertView.findViewById(R.id.rllist);
it seems everything is OK, but I don't know where my problem is?!!!
===========
Update
I have changed above line to:
holder.rlList = (RelativeLayout) row.findViewById(R.id.rllist);
and now i have my result but background did not applied?!!!
Try using
if(position%2 != 0)
holder.rlList.setBackgroundColor(Color.parseColor("#00ff00"));
else
holder.rlList.setBackgroundColor(Color.parseColor("#ff0000"));
Hi Guys i got the android.view.InflateException: Binary XML file line #1: Error inflating class linearlayout, Caused by: java.lang.ClassNotFoundException: android.view.linearlayout in loader dalvik.system.PathClassLoader[/data/app/com.project.news-2.apk] in the indicate line below.
Have a look and
HomeActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
another method inside of HomeActivity
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dashboard_home ,(ViewGroup) findViewById(R.id.layout_root));
GridView grid = (GridView) layout.findViewById(R.id.gridview);
adapter = new HomeAdapter(this);
adapter.setItems(config); grid.setAdapter(adapter);
HomeAdapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder
if (convertView == null) { // if it's not recycled,
convertView = mInflater.inflate(R.layout.dashboard_home, null); //exception
convertView.setLayoutParams(new GridView.LayoutParams(90, 90));
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.titleIcon);
holder.icon = (ImageView) convertView.findViewById(R.id.imageIcon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout style="#style/TitleBar">
<ImageView style="#style/TitleBarLogo"
android:contentDescription="Text testing" />
<ImageView style="#style/TitleBarSeparator" />
<TextView style="#style/TitleBarText" />
<ImageButton style="#style/TitleBarAction"
android:contentDescription="#string/description_about" android:src="#drawable/info"
android:onClick="onClickAbout" />
</LinearLayout>
<GridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/gridview"
android:numColumns="2"
/>
</LinearLayout>
dashboard_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="10dp">
<ImageView android:id="#+id/imageIcon" android:layout_width="50dip"
android:layout_height="50dip" android:layout_alignParentRight="true"
android:layout_marginRight="3dp" />
<TextView android:id="#+id/titleIcon" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerHorizontal="true"
android:layout_alignParentLeft="true" android:layout_marginLeft="3dp"
android:textColor="#FFF" />
</LinearLayout>
Remove these two lines and run your code in the HomeActivity.
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dashboard_home ,(ViewGroup) findViewById(R.id.layout_root));
the exception happens why because you are inflating the dashboard_home.xml into that itself..