Android Listview & CustomAdapter getView Issues - android

Currently working to develop a custom adapater, to display data on a list row as follows:
|TextView| |TextView| |TextView| |TextView|
Currently I am using this tutorial for reference.
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
However, as mentioned, I encounter a problem with the customeradapter, specifically when creating an inflator and in also identifying the ID for Textview in the custom row xml.
Here are the two lines that throw up errors:
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
&
holder.food = (TextView)row.findViewById(R.id.foodName);
Errors given are: LayoutInflater cannot be resolved to a type & R cannot be resolved to a variable.
Many thanks for any suggestions.
Imports
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.view.LayoutInflater;
My full implementation for the adapter:
public class CustomBaseAdapter extends ArrayAdapter<ReportSummary>{
private Context context;
private int layoutResourceId;
private List<ReportSummary> report = new ArrayList<ReportSummary>();
public CustomBaseAdapter(Context context, int layoutResourceId, List<ReportSummary> report) {
super(context, layoutResourceId, report);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.report = report;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.food = (TextView)row.findViewById(R.id.foodName);
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
holder.food.setText(report.get(0).getFoodName());
holder.status.setText(report.get(0).getStatus());
//holder.foodRDA.setText(report.get(0).getStatus());
holder.userLvl.setText(report.get(0).getLevel());
return row;
}
class ViewHolder {
TextView food;
TextView status;
TextView userLvl;
TextView foodRDA;
TextView difference;
}
}
XML layouts
activity_diet_analysis
<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=".DietAnalysisActivity" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
row_diet_analysis
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/foodName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="TTTT"
/>
<TextView android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/foodName"
android:text="TJJDFDFDF"
/>
<TextView android:id="#+id/foodRDA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/status"
android:text="TTTT"
/>
<TextView android:id="#+id/userLvl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/foodRDA"
android:text="TTTT"
/>
<TextView android:id="#+id/difference"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/userLvl"
android:text="TTTT"
/>
</RelativeLayout>

Based on the errors you mentioned, you probably need to import LayoutInflator:
import android.view.LayoutInflater;
And import the generated R file for your app:
import com.example.app.R
Be sure you import the correct R file too, not android.R.
Also, if you're using an IDE such as Eclipse or Android Studio (which you probably should be), it will do this for you automatically.
Note that the R file will not be generated for your project if you have any errors in your resources files.

Related

Android Casting Custom View

This is my view's XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="horizontal">
<EditText
android:id="#+id/bsv_edit_beaconname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="Beacon Name" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/bsv_check_fix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/cinit_table_isfixpoint" />
<CheckBox
android:id="#+id/bsv_check_draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/cinit_table_drawbeacon" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/bsv_btn_setpos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="set_position_click"
android:text="#string/cinit_set_position" />
<TextView
android:id="#+id/bsv_text_pos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Position ?" />
</LinearLayout>
With the corresponding java class being the following
package talogs.beacontalogs;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class BeaconSetupView extends LinearLayout {
private static final String TAG = "BeaconSetupView";
private EditText edit_beaconName;
private CheckBox check_isFixpoint;
private CheckBox check_draw;
private Button btn_setpos;
private TextView text_pos;
public BeaconSetupView(Context context) {
this(context, null);
}
public BeaconSetupView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public BeaconSetupView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public void init(Context context) {
// inflate(getContext(), R.layout.beacon_setup_view, this);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate( R.layout.beacon_setup_view, this );
edit_beaconName = (EditText) findViewById(R.id.bsv_edit_beaconname);
check_isFixpoint = (CheckBox) findViewById(R.id.bsv_check_fix);
check_draw = (CheckBox) findViewById(R.id.bsv_check_draw);
btn_setpos = (Button) findViewById(R.id.bsv_btn_setpos);
text_pos = (TextView) findViewById(R.id.bsv_text_pos);
}
public void setPosition(Point point) {
Log.v(TAG, "setting position to " + point.toString());
text_pos.setText(String.format("(%d, %d)", point.x, point.y));
}
}
My custom view BeaconSetupView is created programmatically using this code when a button in an activity is pressed.
public void btn_addbeacon_Click(View view) {
BeaconSetupView beacon = new BeaconSetupView(this);
beaconList.addView(beacon, beaconList.getChildCount() - 1);
}
The button that's part of the custom view calls this method:
public void set_position_click(View view) {
ViewParent parent1 = view.getParent();
ViewParent parent2 = parent1.getParent();
BeaconSetupView bsv = (BeaconSetupView) view.getParent().getParent();
Log.v(LOG_TAG, "set_position called, point is currently " + String.valueOf(lastClicked));
if (lastClicked != null) {
bsv.setPosition(lastClicked);
}
}
The problem is that the button click cannot be implemented outside of the root activity (because it has data that I need), but I also need to get its corresponding BeaconSetupView. Calling view.getParent().getParent() and casting it results in a ClassCastException. So I tried changing the root element of the XML layout to BeaconSetupView, but that causes the app to crash when the view is first added to the activity.
I either need a way to cast my custom class properly and still be able to add it programmatically to my activity, or I need to be able to access the activity's instance from another class that doesn't have a reference to it. What are my options?

Android: Dynamically creating a large number of imageviews in a listView custom framelayout

I threw myself in a task that might be a little too difficult for me.
I'm designing an medical application that displays images of a patient's scan and superimposes several contours that delineate organs.
The patient's scan is a series of PNG images (the background of slices listview rows).
The contours are vectors and a same scan slice may contain several dozens of contours.
To do so, Scan slices are loaded in a listview with custom adapter. Contours are added according to a previously done selection of organs. Contours are imageViews with transparent background added over the scan in a same framelayout. Therefore, imageviews of contours are added dynamically in the layout.
The problem is that I might be asking too much from listview. In fact, I'm getting stuck with the getView method of the adapter. Contours appear, but refresh and iteration through slices seems to be at random.
Could someone throw me a hint about this situation? I'm not sure the dynamic allocation of imageviews is that clever now... What could I do?
Any help would be welcome !
Adapter:
package bhouse.radiovolumes;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by kranck on 8/3/2017.
*/
public class ScannerListAdapter extends ArrayAdapter<SliceItem> {
private Context context;
private LayoutInflater inflater;
private ArrayList<SliceItem> slices;
public ScannerListAdapter(Context context, ArrayList<SliceItem> slices) {
super(context, R.layout.list_view_scan, slices);
this.context = context;
this.slices = slices;
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_view_scan, parent, false);
convertView.setMinimumHeight(parent.getMeasuredHeight());
holder = new ViewHolder();
holder.scanView = (ImageView) convertView.findViewById(R.id.view_scan);
holder.frameLayout = (FrameLayout) convertView.findViewById(R.id.zoomLayout);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
SliceItem item = getItem(position);
int resIdScan = this.context.getResources().getIdentifier(item.getStorageLocation(), "drawable", context.getPackageName());
Picasso
.with(context)
.load(resIdScan)
.error(R.drawable.borabora)
.into(holder.scanView);
for (int i = 0; i < item.getVectorStorageLocation().size(); i++) {
ImageView imageView = new ImageView(context);
String resourceName = "cylindre__2___" + String.valueOf(position);
int resId = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
imageView.setImageResource(resId);
holder.frameLayout.addView(imageView);
}
return convertView;
}
static class ViewHolder {
FrameLayout frameLayout;
ImageView scanView;
}
}
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<bhouse.radiovolumes.ZoomView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" >
<FrameLayout
android:id="#+id/zoomLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/view_scan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
>
</ImageView>
</FrameLayout>
</bhouse.radiovolumes.ZoomView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<bhouse.radiovolumes.ZoomView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" >
<FrameLayout
android:id="#+id/zoomLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/view_scan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
>
</ImageView>
</FrameLayout>
</bhouse.radiovolumes.ZoomView>
</LinearLayout>
I think you make bad usage of getView method
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.list_view_scan, parent, false);
holder = new ViewHolder();
holder.scanView = (ImageView) convertView.findViewById(R.id.view_scan);
holder.frameLayout = (FrameLayout) convertView.findViewById(R.id.zoomLayout);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
// Then do operation here on your view holder
// ...
return convertView;
}
And If you start this project now, think about using RecyclerView instead of ListView.
Now as it is ok with that, you need to clear your Framelayout before adding your ImageViews by calling removeAllViews() method, if not old views remains in it.

Getting error in implementing list view with images in Fragment

I am beginner in android.I am trying to make listview with image in fragment.But its not getting done.
Here my code is
ListoneFragment.java:(here name1 and name2 are string array names in my strings.xml file)
package fragments.h.safmical;
import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import safmical.h.R;
/**
* Created by hadvani on 4/14/2017.
*/
public class ListOneFragment extends Fragment {
String[] titles;
String[] shortforms;
int[] images={R.drawable.hcll,R.drawable.hno,R.drawable.hcll,R.drawable.hno,R.drawable.hcll,R.drawable.hno};
ListView listView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_listone, container, false);
Resources res =getResources();
titles=res.getStringArray(R.array.name1);
shortforms=res.getStringArray(R.array.name2);
listView = (ListView) rootview.findViewById(R.id.list1);
MyAdapter adapter=new MyAdapter(this,titles,images,shortforms);
listView.setAdapter(adapter);
return rootview;
}
}
class MyAdapter extends ArrayAdapter<String>{
Context context;
int[] images;
String[] titleArray;
String[] shortformArray;
MyAdapter(Context c,String[] titles,int imgs[],String[] shortform){
super(c,R.layout.fragment_listpattern,R.id.textView1,titles);
this.context=c;
this.images=imgs;
this.titleArray=titles;
this.shortformArray=shortform;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.fragment_listpattern,parent,false);
ImageView myImage=(ImageView)row.findViewById(R.id.imageView2);
TextView myTitle=(TextView)row.findViewById(R.id.textView1);
TextView myShortform=(TextView) row.findViewById(R.id.textView2);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
myShortform.setText(shortformArray[position]);
return row;
}
}
here my xml file
fragment_listone.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list1">
</ListView>
</FrameLayout>
fragment_listpattern.xml:(for pattern of single row of list view)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="72dp">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher_round" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView2"
android:layout_toRightOf="#+id/imageView2"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_toEndOf="#+id/imageView2"
android:layout_toRightOf="#+id/imageView2"
android:text="TextView" />
</RelativeLayout>
</FrameLayout>
When i am running it.It shows error:
Error:(37, 40) error: incompatible types: ListOneFragment cannot be converted to Context
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
And it shows suggestion in ListoneFragment.java in line
MyAdapter adapter=new MyAdapter(this,titles,images,shortforms); in parameters
that
change 1st parameter of method 'MyAdapter' from Context to 'ListoneFragment'.
And When i do that it makes errors in MyAdapter class's constructer.
What should i do,is there any correction or is there other way to implement this.Please help?
use activity context to initialize the adapter class:
in your ListOneFragment use:
MyAdapter adapter=new MyAdapter(getActivity(), titles, images,shortforms);

Using a layout xml as a template of repeated content in Android

I have a template layout which represents each of my rows in a ListView in my app.
Here is the photorow.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bodylay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/leftbigsquare"
android:layout_width="0dp"
android:layout_height="140dp"
android:layout_weight="1"
android:background="#1000b0"
android:src="#drawable/test1"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#d0b0b0"
android:textSize="15sp" >
<ImageView
android:id="#+id/righttupperleft"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#b170b0"
android:src="#drawable/test1" />
<ImageView
android:id="#+id/rightupperright"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#b110b0"
android:src="#drawable/test1" />
</LinearLayout>
<ImageView
android:id="#+id/righthorizontal"
android:layout_width="match_parent"
android:layout_height="70dp"
android:src="#drawable/test1" />
</LinearLayout>
It is repeated row by row. What I want to do is to change "ImageView" child, when I load the image from my server not redesign the row layout each time. Each row will show different "leftbigsquare", "rightupperleft", "rightupperright", "righthorizontal" (look at ImageViews ids) photos.
I researched and saw some examples, but they did that by hardcoding the layout such as :
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
I want to use my photorow.xml as template and just change its attributes such as source in each row.
Is it possible to use my photorow.xml as the template of a row ?
EDIT
My custom adapter :
package com.example.test2;
import java.util.List;
import com.squareup.picasso.Picasso;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class PhotoAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<PhotoRow> rowList;
Activity context = null;
public PhotoAdapter(Activity activity, List<PhotoRow> rows) {
mInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowList = rows;
context = activity;
}
#Override
public int getCount() {
return rowList.size();
}
#Override
public Object getItem(int position) {
return rowList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
rowView = mInflater.inflate(R.layout.photorow, null);
//Here I get the ImageView of my photorow xml
ImageView leftBigSquare = (ImageView) rowView
.findViewById(R.id.leftbigsquare);
// Here I get the proper URL from rowList by using Picasso framework
Picasso.with(context).load(rowList.get(position).getUrls().get(0))
.resize(50, 50).centerCrop().into(leftBigSquare);
//So how can I put the leftBigSquare ImageView to my photorow template with its proper attributes
return rowView;
}
}
Yes, of course, you can use same rowview XML and still have different data in every row.
You must use a custom adapter and then inflate your row s from that adapter and based on several conditions change your data.
Paste your custom adapter code and I will show you how to do it.

How the display bug of TextView drawableStart can be explained?

Update: Simpler code created and presented here, full layout XML definitions.
When trying to replace the row layout with the icon on the left by the new drawableStart attribute of the TextView element in XML layout, I can observe the following wrongly displayed last item (captured on Nexus 7):
The related code is -- MainActivity:
package cz.skil.android.tut.testtextviewdrawablestart;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_list);
ListView listView = (ListView) findViewById(R.id.mylist);
String[] values = new String[] {"item 1", "item 2", "item 3"};
MyAdapter adapter = new MyAdapter(this, values);
listView.setAdapter(adapter);
}
}
MyAdapter
package cz.skil.android.tut.testtextviewdrawablestart;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MyAdapter(Context context, String[] values) {
super(context, R.layout.row, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.mylabel);
textView.setText(values[position]);
return rowView;
}
}
The main_list.xml layout:
<?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="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <!-- Update: here is the bug -->
</ListView>
</LinearLayout>
and finally the row.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mylabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="4dp"
android:drawablePadding="8dp"
android:drawableStart="#drawable/reminder"
android:lines="1"
android:textSize="24sp" >
</TextView>
I guess the reason is that the height of the area for displaying the items is calculated from dimensions declared for the text; however, the icon possibly forces a bigger height for the list item.
Can you confirm the behaviour? If yes, why it behaves so? I am new to Android. It is likely I am doing it wrongly.
(The question is loosely related to this one: Android layout: on TextView and android:drawableStart -- setting size of the icon?).
It's always wrong to set android:layout_height="wrap_content" for a ListView. It works as expected with android:layout_height="fill_parent". More detailed explanation could be found here The World of ListView
Though I don't know if this is intended behaviour or bug, this happens when you use drawableStart. Change it to drawableLeft and it will draw the layout properly.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mylabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="4dp"
android:drawablePadding="8dp"
android:drawableLeft="#drawable/reminder"
android:lines="1"
android:textSize="24sp" >
</TextView>

Categories

Resources