Android - ListView with ArrayAdapter and hidden TextViews - java

I have a problem with the ArrayAdapter in the ListView in my Android application. The TextViews that should be displayed have two possibilites, with an info text or without an info text. The problem appears while scrolling. When I scroll down and up again the TextViews that didn't had an info text now have a random info text (from one of the TextViews with info text). The problem appears because of the memory usage of the ListView. Now I am looking for a possibility to solve the problem. The result should show the TextViews (without the info text) after scrolling without the info text. So is there any good solution with using the ArrayAdapter in a ListView or is there any other Android widget to use?
Java Code (Activity class):
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(layout, null);
holder.text = (TextView) view.findViewById(R.id.list_layout_textview);
holder.infoText = (TextView) view.findViewById(R.id.list_layout_infos);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
final OutputTweet outputTweet = tweetObjects.get(position);
if (outputTweet.getText() != null) {
holder.text.setText(outputTweet.getText());
}
if (!outputTweet.getInfoText().equals("")) {
holder.infoText.setVisibility(View.VISIBLE);
holder.infoText.setText(outputTweet.getInfoText());
}
return view;
}
}
Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list_layout"
>
<TextView
android:id="#+id/list_layout_infos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_info_color"
android:layout_marginTop="2dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:textSize="12sp"
android:textStyle="italic"
android:visibility="gone"
/>
<TextView
android:id="#+id/list_layout_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textColor"
android:layout_marginTop="2dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:fontFamily="sans-serif-light"
android:typeface="sans"
android:textSize="15sp"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
Thank you for every solution :)

You need to add an else clause to your last if clause that sets the view back to View.GONE or View.INVISIBLE. The view is being recycled, and since it was being shown before, it will still be visible when it is passed back to you.
In general when binding you need to bind ALL state, including visibility changes. Don't assume any state from the inflation because of the reuse of views.
Try this:
if (outputTweet.getText() != null) {
holder.text.setText(outputTweet.getText());
} else {
holder.text.setText("");
}
if (!outputTweet.getInfoText().equals("")) {
holder.infoText.setVisibility(View.VISIBLE);
holder.infoText.setText(outputTweet.getInfoText());
} else {
holder.infoText.setVisibility(View.GONE);
}

Related

ListView doesn't display on screen

I recently started to use ListView on Android Application and I'm not making it to display at the screen.
I saw many tutorials on how to do a custom Adapter and followed it line by line, but at the end I wasn't able to make the ListView display on my screen, I even let a fixed Text in one TextView to display at the ListView but it still didn't displayed it.
This is the constructor of my entity code:
public Classificacao(int colocacao, int time, int pontos) {
this.colocacao = colocacao;
this.time = time;
this.pontos = pontos;
}
This my Custom Adapter that extends "ArrayAdapter"
private Context context;
private ArrayList<Classificacao> classificacaoList;
ClassificacaoAdapter(Context context, ArrayList<Classificacao> classificacaoList) {
super(context, R.layout.layout_classificacao, classificacaoList);
this.context = context;
this.classificacaoList = classificacaoList;
}
#SuppressLint("SetTextI18n")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.layout_classificacao, parent, false);
}
TextView colocacao = convertView.findViewById(R.id.texto1);
colocacao.setText(Integer.toString(classificacaoList.get(position).getColocacao()));
ImageView imagem = convertView.findViewById(R.id.imagem1);
imagem.setImageResource(classificacaoList.get(position).getImagem());
TextView nome = convertView.findViewById(R.id.texto2);
nome.setText(classificacaoList.get(position).getTime());
TextView pontos = convertView.findViewById(R.id.texto3);
pontos.setText(Integer.toString(classificacaoList.get(position).getPontos()));
return convertView;
}
This the layout "activity_atividade02" that have the ListView, and some others Text views that I won't display here.
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
This is the layout "layout_classificacao" that will be filled by the adapter
<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="wrap_content"
android:paddingTop="20dp">
<TextView
android:id="#+id/texto1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:textColor="#android:color/black"
android:textSize="30sp" />
<ImageView
android:id="#+id/imagem1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="imagem do time"
app:srcCompat="#drawable/flamengo" />
<TextView
android:id="#+id/texto2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:textColor="#color/colorPrimary"
android:textSize="30sp" />
<TextView
android:id="#+id/texto3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:textColor="#android:color/black"
android:textSize="30sp" />
</LinearLayout>
And this is the main class "Atividade02"
that start everything
public class Atividade02 extends AppCompatActivity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atividade02);
listView = findViewById(R.id.listView);
ArrayList<Classificacao> classificacaoArrayList = new ArrayList<>();
classificacaoArrayList.add(new Classificacao(1, 2,46));
classificacaoArrayList.add(new Classificacao(2, 1,42));
classificacaoArrayList.add(new Classificacao(3,0,38));
classificacaoArrayList.add(new Classificacao(4,3,35));
classificacaoArrayList.add(new Classificacao(5,5,33));
classificacaoArrayList.add(new Classificacao(6,4,33));
ArrayAdapter classificacaoAdapter = new ClassificacaoAdapter(this, classificacaoArrayList);
listView.setAdapter(classificacaoAdapter);
}
}
Resuming, the ListView isn't displaying the "ClassificacaoAdapter" and I want it to do it.
Make sure adapter has the method getItemcount() and it returns the classificacaoList.size(). It should not return 0;
Well... it was a pretty obvious problem, in the main layout activity_atividade02 there was a LinearLayout above the ListView that it's layout_height was set to match_parent and it was occupying the entire layout and not letting the ListView being displayed.
I test your code as you write and found an error here and it works normally but I comment imageView line because you don't define it in Classificacao class
an error here
TextView nome = convertView.findViewById(R.id.texto2);
nome.setText(classificacaoList.get(position).getTime());
setText() accept String only ..but you pass integer rather than String you can do that
nome.setText(""+classificacaoList.get(position).getTimee());
or use
Integer.toString( classificacaoList.get(position).getTimee() )
as you use in other lines
please add getCount() method in your ClassificacaoAdapter class, it is an override method. It will return the size of the list,if you will not override this it will return 0 as your list size.
public int getCount() {
return classificacaoList .size();
}

Interacting with one element in Android ListView affects multiple

I have a list view of search items fetched from various web pages like so:
There is a horizontal scroll view on the titles so that users can fully see the title. However, when I scroll on one of the items, another item about 6 down from the item I was interacting with is also scrolled to the right. How can I prevent this from happening?
Update: Here's my CustomAdapter Code:
public class CustomAdapter extends ArrayAdapter<Item> implements View.OnClickListener{
private ArrayList<Item> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView itemTitle;
TextView itemStore;
TextView itemPrice;
ImageView storeLogo;
}
public CustomAdapter(ArrayList<Item> data, Context context) {
super(context, R.layout.row_item, data);
this.dataSet = data;
this.mContext=context;
}
#Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
Item item=(Item)object;
switch (v.getId())
{
case R.id.store:
Snackbar.make(v, "This item is being sold at " + item.getStore() + "." , Snackbar.LENGTH_LONG)
.setAction("No action", null).show();
break;
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Item item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.itemTitle = (TextView) convertView.findViewById(R.id.title);
//viewHolder.itemStore = (TextView) convertView.findViewById(R.id.type);
viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.price);
viewHolder.storeLogo = (ImageView) convertView.findViewById(R.id.store);
result = convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
viewHolder.itemTitle.setText(item.getTitle());
viewHolder.itemPrice.setText("$" + item.getPrice());
viewHolder.storeLogo.setOnClickListener(this);
viewHolder.storeLogo.setTag(position);
// Return the completed view to render on screen
return convertView;
}
}
Here is my fragment_search.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.painlessshopping.mohamed.findit.Search$PlaceholderFragment">
<Button
android:text="Next Page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonUp"
android:onClick="buttonClicked"
android:elevation="0dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="49dp" />
<Button
android:text="Previous Page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonDown"
android:onClick="buttonClicked"
android:elevation="0dp"
android:layout_marginStart="62dp"
android:layout_alignParentBottom="true"
android:layout_toEndOf="#+id/buttonUp" />
<EditText
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:layout_width="250dp"
android:hint="What can I help you find?"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="match_parent"
android:id ="#+id/listView"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_height="375dp" />
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchBtn"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/editText" />
Here is my row_item.xml (For the custom adapter)
<?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="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:maxLines="1"
android:text="Sample Text for Android Horizontal Scroll View Goes Here"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/dgrey"/>
</HorizontalScrollView>
<ImageView
android:id="#+id/store"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:src="#drawable/ic_favorite_border_black_24dp" />
</LinearLayout>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/horizontalScrollView"
android:text="$39.99"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="20sp"
android:textColor="#color/colorAccent"
android:textStyle="bold"
/>
</LinearLayout>
because of performance in constructor we check and see if we are able , we will use the previous layout that has generated, and just try to replace their values, but in your case because you can not change the scrol bar of view so your new item and previous items will have the same scroll bar view. i hope i were clear. so you have to always generate new layout like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Item item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.itemTitle = (TextView) convertView.findViewById(R.id.title);
//viewHolder.itemStore = (TextView) convertView.findViewById(R.id.type);
viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.price);
viewHolder.storeLogo = (ImageView) convertView.findViewById(R.id.store);
result = convertView;
convertView.setTag(viewHolder);
viewHolder.itemTitle.setText(item.getTitle());
viewHolder.itemPrice.setText("$" + item.getPrice());
viewHolder.storeLogo.setOnClickListener(this);
viewHolder.storeLogo.setTag(position);
// Return the completed view to render on screen
return convertView;
}
also maybe there were a better way that i don't know, but this will solve your problem at least.
UPDATE :
for more information about how listview work and performance tip this link seem's good

Android - ImageView with Another Views in Universal Image Loader

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.

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