Content in Listview is invisible - java

I am new to Android and I am currently developing an app that contains a listview with simple text. When the user clicks the text in the listview a sharing intent is triggered. I have used a custom adapter to set a different typeface to the text in my listview. The problem is when I run the app the listview is being seen but the content is invisible. But When I tap on the listview the sharing intent is triggered as it should be. I don't know what is causing it. I searched in Stack Overflow and google and tried some steps but it didn't work out for me. Please help me out. Thanks in advance.
Code:
Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.proda.technologies.app.testapp"
android:background="#drawable/inspirationalbg">
<ImageButton
android:layout_width="130dp"
android:layout_height="40dp"
android:id="#+id/back_Button"
android:background="#drawable/backbutton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/list"
android:layout_below="#+id/back_Button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
list_item.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">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:id="#+id/instView"/>
</LinearLayout>
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context,R.layout.list_item,values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rview = inflater.inflate(R.layout.list_item,null);
TextView txt = (TextView)rview.findViewById(R.id.instView);
AssetManager mgr = context.getAssets();
Typeface tf = Typeface.createFromAsset(mgr,"fonts/RalewayRegular.ttf");
txt.setTypeface(tf);
return rview;
}
}
Main.java
String[] phone = {"Nexus","Htc","Samsung","Oppo","Apple"};
Listview inslist;
inslist = (ListView)findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(Main.this,phone);
inslist.setAdapter(adapter);
I am not getting any errors. I can see the listview with the lines seperating each content. But the actual content(text) is invisible. Please help me

Your ListViewItem contain a TextView but its value was not init.
Try to set value for textView in xml file or in java code.
Try one of below ways:
1. By XML file: list_item.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">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:id="#+id/instView"
android:text="This is text sample"/>
</LinearLayout>
2. by Java code: CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context,R.layout.list_item,values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rview = inflater.inflate(R.layout.list_item,null);
TextView txt = (TextView)rview.findViewById(R.id.instView);
txt.setText(values[position]);
AssetManager mgr = context.getAssets();
Typeface tf = Typeface.createFromAsset(mgr,"fonts/RalewayRegular.ttf");
txt.setTypeface(tf);
return rview;
}
}

I didn't see anywhere that you added text inside TextView.
Can you put the data to show in TextView view inside getView() method like :
txt.setText(values.get(position));

Related

Responsive UI GridView layout

I have surveyed in other sites and got different responses about how I can solve my problem. In fact the problem is that I am trying to make a responsive gridView layout to display 25 textviews with numbers. I had hard time in constructing it the way I want but here is what I got:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
android:layout_gravity="center"
android:gravity="center"
tools:context="com.example.hristodraganov.bingo.MainActivity"
tools:showIn="#layout/activity_main">
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
</RelativeLayout>
<GridView
android:paddingTop="8dp"
android:id="#+id/gridview"
android:background="#000"
android:layout_width="match_parent"
android:layout_weight="6"
android:layout_height="0dp"
android:gravity="center"
android:columnWidth="70dp"
android:numColumns="5"
android:verticalSpacing="10dp"
android:horizontalSpacing="1dp"
android:stretchMode="spacingWidth"
/>
This is the layout that is used on the MainActivity. I inflate it with textview item with this code:
<?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="#d3d3d3"
android:gravity="center">
<com.example.****.****.SquareView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cell"
android:textColor="#D0583B"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp">
</com.example.****.****.SquareView>
There is the extension of the BaseAdapter class that is used to inflate the grid with the textviews:
public class GridAdapter extends BaseAdapter {
private final Context mContext;
private String[] numbers;
public GridAdapter(Context context, String[] numbers) {
this.mContext = context;
this.numbers = numbers;
}
#Override
public int getCount() {
return numbers.length;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(mContext);
gridView = inflater.inflate(R.layout.textview_layout, null);
TextView textView = (TextView) gridView.findViewById(R.id.cell);
textView.setText(numbers[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
}
Also, I am forcing the textView to be squared in this SquareView class:
public class SquareView extends android.support.v7.widget.AppCompatTextView {
public SquareView(Context context) {
super(context);
}
public SquareView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}}
The layout is doing fine by now, here is an image of the size I used to construct it Nexus 5X. It looks good but when I change to 4" -Nexus S the whole last row is somewhere below the screen. Also this happens to 5"-Nexus 5 where the last row is slightly visible. Above 5.2" to 6.0" the layout fits perfectly. So my question is what should I do in this scenario to make the layout fit for small-sized screens without making a duplicate layout for them. (Note: I was told that the BaseAdapter implementation would fix the responsiveness of the layout). Any ideas what could I do? (Sorry for the long texts.)
Because there are a lot of different Android devices out there your best choice would be to encapsulate your layout in a ScrollView.
Another option is to get rid of the weights and let the top RelativeLayout grow/shrink after the GridView has taken all the space it needs. This still might not work on smaller screens or in landscape.
```
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</RelativeLayout>
<GridView
android:paddingTop="8dp"
android:id="#+id/gridview"
android:background="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:columnWidth="70dp"
android:numColumns="5"
android:verticalSpacing="10dp"
android:horizontalSpacing="1dp"
android:stretchMode="spacingWidth"/>
```

Android Custom ListView Adapter doesn't populate

Long time stack user, first time poster. I have followed several Custom ListView Adapter tutorials and have not been able to get this one to work (I have successfully created these adapters before). I have spent way too many hours trying to get this to work!!! Can someone please have a look and hopefully find the silly mistake that I made?
This is in MainActivity.java onCreate() to call the list
//Declaration for the list
private ArrayList<Consumption> meals = new ArrayList<Consumption>();
private RAdapter rAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meal);
//This is the first of two listviews on the activity
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
//This is the listview I am having problems with
ListView listView2 = (ListView) findViewById(R.id.resultList);
rAdapter = new RAdapter(this,meals);
listView2.setAdapter(rAdapter);
}
I have triple checked that meals has data, it does.
This is the RAdapter.java (The Custom Adapter) code:
public class RAdapter extends ArrayAdapter<Consumption>{
static class holder{
TextView rName;
EditText quan;
ImageButton delete;
}
private ArrayList<Consumption> meals;
private final Context context;
public RAdapter(Context context) {
super(context, R.layout.result_row);
this.context = context;
}
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row);
this.context = context;
this.meals = meals;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
holder h = null;
if (v == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
v = inflater.inflate(R.layout.result_row, parent, false);
h = new holder();
h.rName = (TextView) v.findViewById(R.id.resultName);
h.quan = (EditText) v.findViewById(R.id.resultGrams);
h.delete = (ImageButton) v.findViewById(R.id.resultDelete);
v.setTag(h);
}else{
h = (holder) v.getTag();
}
Consumption i = meals.get(position);
h.rName.setText(i.getShortName());
h.quan.setText(i.getQuantity());
return v;
}
}
Here is result_row.xml (The specific row to populate):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/resultDelete"
android:layout_marginLeft="10dp"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#android:drawable/ic_notification_clear_all"
android:contentDescription="#string/meal_remove" />
<TextView
android:id="#+id/resultName"
android:layout_marginLeft="10dp"
android:layout_width="150dp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/resultDelete" />
<EditText
android:id="#+id/resultQuantity"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="50dp"
android:layout_height="40sp"
android:inputType="numberDecimal"
android:layout_toRightOf="#+id/resultName"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/resultGrams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/resultQuantity"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="#string/grams" />
</RelativeLayout>
Here is activity_meal.xml (which has the listview "resultList" in it):
<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:background="#drawable/semislantedbacktransparent"
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=".MealActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/translateButton"
android:layout_width="100dp"
android:layout_height="65dp"
android:background="#drawable/addtorecipe"
android:contentDescription="#string/dummy_button"
android:text="#string/button_add_recipe" />
</LinearLayout>
<SearchView
android:id="#+id/searchView1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/search"
android:focusable="true"
android:iconifiedByDefault="false"
android:onClick="enterTomInput"
android:queryHint="Search for food items"
android:showAsAction="always"
android:textColor="#000000" >
</SearchView>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignLeft="#+id/searchView1"
android:layout_below="#+id/searchView1"
android:cacheColorHint="#color/black_overlay" >
</ListView>
<ListView
android:id="#+id/resultList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_below="#+id/listView1" />
<ImageButton
android:id="#+id/completeButton"
android:layout_width="100dp"
android:layout_height="65dp"
android:layout_alignRight="#+id/resultList"
android:layout_alignTop="#+id/linearLayout1"
android:background="#drawable/completemeal"
android:contentDescription="#string/dummy_button" />
</RelativeLayout>
I can post a logcat, but there is no error message to report. I have tried putting System prints everywhere, but cannot figure the problem out.
Any help would be much appreciated!!
Just change following code and fill your arraylist with data.
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row);
this.context = context;
this.meals = meals;
}
to
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row,meals);
this.context = context;
this.meals = meals;
}
in your adapter class.
change your adapter like this
public class RAdapter extends ArrayAdapter<Consumption> {
private Activity activity;
private ArrayList<Consumption> meals;
private LayoutInflater inflater = null;
public RAdapter (Activity act, int resource, ArrayList<Consumption> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.meals= arrayList;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.result_row, parent, false);
}
//remaining code
return view;
}
}
and call this adapter
rAdapter = new RAdapter(MainActivity.this,R.layout.result_row,meals);
listView2.setAdapter(rAdapter);

Make Listview to Occupy Whole Screen

I have listview that i want to fill the whole screen,There are four items in listview. It leaves empty space below after four items are filled.You can see in the screenshot. It leaves the blank space. I want whole screen to be covered.
I would like to have like this:
Here is the source Code
MainActivity.java.
public class MainActivity extends Activity {
ListView resultPane;
List<Taskinfo> list;
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultPane = (ListView) findViewById(R.id.mylist);
list = new ArrayList<Taskinfo>();
Resources res = getResources(); // resource handle
Drawable drawable = res.getDrawable(R.drawable.browse_home);
list.add(new Taskinfo("Browse", drawable));
drawable = res.getDrawable(R.drawable.jewelry);
list.add(new Taskinfo("Whats New", drawable));
drawable = res.getDrawable(R.drawable.show);
list.add(new Taskinfo("Upcoming Show", drawable));
drawable = res.getDrawable(R.drawable.contact);
list.add(new Taskinfo("Contact Us", drawable));
adapter = new CustomAdapter(this, list);
resultPane.setAdapter(adapter);
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<Taskinfo> list;
public CustomAdapter(Context context, List<Taskinfo> list) {
this.context = context;
this.list = list;
}
public View getView(int index, View view, final ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.single_list_item, parent, false);
}
Taskinfo t = list.get(index);
RelativeLayout l = (RelativeLayout) view
.findViewById(R.id.testrelative);
l.setBackgroundDrawable(t.getImage());
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(t.getName());
return view;
}
}
single_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/testrelative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/browse_home"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
</LinearLayout>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:text=""
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45sp"
android:id="#+id/llayout"
android:background="#drawable/navbar"
android:padding="3sp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Golden Stone"
android:textColor="#color/white"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_below="#id/llayout"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
Using ListView in such case is strange and unnecessary. Think about your constraints (like: what if items overflow?) and just use a proper layout manager. Like a vertical LinearLayout with the last item having a non-zero layout weight.
ListViews make sense only if you need an abstraction that generates list items on-the-fly.
You would want to set a background on the ListView. Currently your view should be going to the bottom, but the background of the ListView is transparent, so setting it white should achieve what you're asking.
<ListView ...
android:background="#android:color/white"
... />
if there are so few items, you can use the LinearLayout instead, and give weights for each of its items.
However, do note that android supports many devices and screens, so you might want to have a limitation of how small each row would be.
anyway, in case you wish to make each row the fitting height, you can check its size and then divide by the number of items.
in order to get the size of the listView , you can use this small snippet i've made .

Button disappears at runtime, visible in Eclipse design view

My app is for reading news from one site. The news parse from RSS feed and display as list of elements that include title and date. The main layout is ListView, and the layout post_entry for messages (news) looks like this:
<?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:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="#+id/post_title">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:paddingLeft="5dp"
android:paddingBottom="5dp"
android:id="#+id/post_pubDate">
</TextView>
</LinearLayout>
One TextView for the title, and another is for date.
Adapter for this view looks like this:
public class PostAdapter extends ArrayAdapter<PostItem> {
public ArrayList<PostItem> messages;
public LayoutInflater inflater;
public PostAdapter(Activity context, int resource,
ArrayList<PostItem> objects) {
super(context, resource, objects);
messages = objects;
inflater = LayoutInflater.from(context);
}
static class ViewHolder {
public TextView titleView;
public TextView pubDateView;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.post_entry, null, true);
holder = new ViewHolder();
holder.titleView = (TextView) convertView
.findViewById(R.id.post_title);
holder.pubDateView = (TextView) convertView
.findViewById(R.id.post_pubDate);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleView.setText(messages.get(position).title);
holder.pubDateView.setText(messages.get(position).date);
return convertView;
}
}
I want to add the button for refresh in the main activity.
After in the main activity looks like that:
<?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:orientation="vertical"
>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.8" />
<Button
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="Refresh"
android:onClick="Update"/>
</LinearLayout>
In graphical mode in Eclipse I see the list of items and the button below it.
Everything seems ok, but when I run my app there is no button on the screen. I see just list of the news.
Do you know why can this be? And how to add the button below the list?

Android listview array adapter selected

i'm trying to add a contextual action mode to a listview, but i'm having some problems with the selection, if i make aList1.setSelection(position) it doesn't select anything, and if i make List1.setItemChecked(position, true) it works but it only changes the font color a little and i want it to change the background or something more notable, is there any way to detect the selection and manually and change the background, or i'm missing something?
the list:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:drawSelectorOnTop="false">
</ListView>
</RelativeLayout>
the adapter:
public class ServicesRowAdapter extends ArrayAdapter<String[]> {
private final Activity context;
private final ArrayList<String[]> names;
static class ViewHolder {
public TextView Id;
public TextView Date;
public RelativeLayout statusbar,bglayout;
}
public ServicesRowAdapter(Activity context, ArrayList<String[]> names) {
super(context, R.layout.servicesrowlayout, names);
this.context = context;
this.names = names;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.servicesrowlayout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Id = (TextView) rowView.findViewById(R.id.idlabel);
viewHolder.Date = (TextView) rowView.findViewById(R.id.datelabel);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.Date.setText(names.get(position)[2]);
holder.Id.setText(names.get(position)[1]);
return rowView;
}
}
with the use of a layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/idlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="right"
android:text="#+id/idlabel"
android:textSize="20dp"
android:width="70dp" >
</TextView>
<TextView
android:id="#+id/datelabel"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/datelabel"
android:textSize="20dp"
android:layout_marginLeft="90dp" >
</TextView>
</RelativeLayout
This is becasue in touch mode there is no focused or selection. You're on the right track using the checked state. You just need to use a state drawable with different states.
Steps:
1) In your row_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/item_selector"
>
2) Create a new xml file (match the name to the one you used in your row layout) in your drawable folder like so (note that I have a color.xml file set up so I can use #color/color, if you don't you can simply put a hex color code in there):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="true"
android:drawable="#color/blue" />
</selector>
That's it. It's not reaql intuitive, but "activated" basically means "checked" when you have enabled the checked state.
With that setup (and enabling the list rows to be checkable) you will now have the background changed from whatever it is normally to blue when in the checked state. Add code to set that state in onTouch or onClick and you'll be all set. The background will stay that way until you touch another item.

Categories

Resources