I'm trying to change the row color of my listView in customAdapter. There's an array of integer that include 0 and 1, I'm trying to read from the array and change the color of rows like this:
0 = white
1 = yellow
but it shows a yellow color in all rows.
This is my CustomAdapter:
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<RowItem> {
ArrayList<Integer> test;
Context context;
public CustomAdapter(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;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
SqliteHelper checkFav = new SqliteHelper(context);
checkFav.open();
test = checkFav.getFavForList();
checkFav.close();
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_layout, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.pTxt);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
int color0 = Color.YELLOW;
int colorDefault = Color.WHITE;
switch (test.get(position)){
case 0:
convertView.setBackgroundColor(colorDefault);
break;
case 1:
convertView.setBackgroundColor(color0);
}
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
And this is my row_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/active_icon" />
<TextView
android:id="#+id/pTxt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20sp"
android:gravity="right"/>
</LinearLayout>
Thanks in advance.
getView() is called for each row in a list so you should not loop over all items in test but only handle the one denoted by position and you should do that after (not in) the if (convertView == null) block.
Edit:
getView() should do the following things:
Check if convertView isn't null (if it is, create a new View as you did)
Check if convertView is of the right type (this only matters if more than one kind of View is used in the list which is not the case here). If the type is wrong, create a new View.
Now we have a valid View for the list row.
Retrieve the data which affects how to display this row. In your case this would be the result of test.get(position). The position is the number of the requested row (starting with 0 at the top of the ListView).
Adjust the View according to your data (you did this in the for-loop but you should do it only once for the requested entry at position).
Return the View
In more complex situations you may have to do the third step before the second but not here.
Fast solution (not very nice code, but works):
#Override
public int getItem(int position){
return test.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int color0 = ....
int color1 = ....
int colorDefault = ...
switch (test.get(position)) {
case 0:
convretview.setBackgroundColor(color0);
break;
case 1:
convretview.setBackgroundColor(color1);
break;
default:
convretview.setBackgroundColor(colorDefault);
}
...
}
Related
I want to create a ListView in a Fragment for chats just like WhatsApp but the adapter is showing null. Below is my code :
ChatsFragment.java
package com.houssup.userchat;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
public class ChatsFragment extends Fragment {
ListView list;
CustomChatAdapter adapter;
public ChatsFragment chatsFragment = null;
public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.customrow_chat, container, false);
ListView lv= ( ListView )view.findViewById( R.id.list ); // List defined in XML ( See Below )
/**************** Create Custom Adapter *********/
setListData();
adapter=new CustomChatAdapter( getContext(), CustomListViewValuesArr);
lv.setAdapter(adapter);
return view;
}
public void setListData()
{
for (int i = 0; i < 11; i++) {
final ListModel sched = new ListModel();
/******* Firstly take data in model object ******/
sched.setName("Swapnil"+i);
sched.setDesignation("Interior Designer"+i);
/******** Take Model Object in ArrayList **********/
CustomListViewValuesArr.add( sched );
}
}
}
CustomChatAdapter.java
package com.houssup.userchat;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
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.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by hp on 01-06-2016.
*/
public class CustomChatAdapter extends BaseAdapter{
private Context context;
private ArrayList data;
private static LayoutInflater inflater=null;
ListModel tempValues=null;
int i=0;
/************* CustomChatAdapter Constructor *****************/
public CustomChatAdapter(Context context, ArrayList d) {
/********** Take passed values **********/
this.context=context;
data=d;
/*********** Layout inflator to call external xml layout () ***********/
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder{
public TextView name;
public TextView designation;
public CircleImageView image;
}
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.customrow_chat, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.name = (TextView) vi.findViewById(R.id.name);
holder.designation=(TextView)vi.findViewById(R.id.designation);
holder.image= (CircleImageView) vi.findViewById(R.id.image);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.name.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = ( ListModel ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.name.setText( tempValues.getName() );
holder.designation.setText( tempValues.getDesignation() );
holder.image.setImageResource(R.drawable.circuladp);
/******** Set Item Click Listner for LayoutInflater for each row *******/
/* vi.setOnClickListener(new OnItemClickListener( position ));*/
}
return vi;
}
/* #Override
public void onClick(View v) {
Log.v("CustomChatAdapter", "=====Row button clicked=====");
}*/
/********* Called when Item click in ListView ************/
/* private class OnItemClickListener implements View.OnClickListener {
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
*//*#Override
public void onClick(View arg0) {
CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
*//**//**** Call onItemClick Method inside CustomListViewAndroidExample Class ( See Below )****//**//*
sct.onItemClick(mPosition);
}*/
}
fragment_chats.xml
<FrameLayout 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"
tools:context="com.houssup.userchat.ChatsFragment">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</FrameLayout>
customrow_chat.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="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/circuladp"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_below="#id/title"
android:text="Yashvant Rai Bacchhan"
android:layout_marginLeft="50dp"
android:textSize="18dp"
android:textColor="#000000"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/designation"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="6dp"
android:layout_marginLeft="55dp"
android:textSize="14dp"
android:layout_height="wrap_content"
android:text="Designer"/>
</LinearLayout>
</LinearLayout>
This fragment is in a TabActivity.I don't know why it is showing null. Any help will be appreciated !!
The code shows that you inflating the fragment view for the layout 'R.layout.customrow_chat' but the layout name is 'fragment_chats.xml'. Typo?
See you code. you convertView is always null.
change it:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
convertView = inflater.inflate(R.layout.customrow_chat, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.designation=(TextView)convertView.findViewById(R.id.designation);
holder.image= (CircleImageView) convertView.findViewById(R.id.image);
/************ Set holder with LayoutInflater ************/
convertView.setTag( holder );
}
else
holder=(ViewHolder)convertView.getTag();
if(data.size()<=0)
{
holder.name.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = ( ListModel ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.name.setText( tempValues.getName() );
holder.designation.setText( tempValues.getDesignation() );
holder.image.setImageResource(R.drawable.circuladp);
/******** Set Item Click Listner for LayoutInflater for each row *******/
/* vi.setOnClickListener(new OnItemClickListener( position ));*/
}
return convertView;
}
Hi I'm trying to show text, image alternatively in list view. To load the content into my list view i had three scenarios.
1. Some times i'm getting only the text from json. So, in this case i have to hide the ImageView
2. Some cases i'm getting only the image url from json. So, in this case i have to hide the TextView
3. In other case i'm getting both image url and text from json, so, in this case i don't have to hide anything
By following these scenarios i'm loading all the content correctly at first time but when i'm scrolling top to bottom and bottom to up. All the image views are filled with images (But there is no image url for that particular position)
My adapter code is
package com.app.listviewitemid.adapter;
import java.util.ArrayList;
import com.app.listviewitemid.R;
import com.app.listviewitemid.model.Items;
import com.bumptech.glide.Glide;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class ItemsAdapter extends ArrayAdapter<Items>{
ArrayList<Items> itemsArrayList;
int Resource;
LayoutInflater inflater;
Context context;
ViewHolder holder;
Button btnCheck;
Items globPosition;
public ItemsAdapter(Context context, int resource, ArrayList<Items> objects){
super(context, resource, objects);
this.context = context;
this.Resource = resource;
this.itemsArrayList = objects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
v = inflater.inflate(Resource, null);
holder = new ViewHolder();
holder.tvPostID = (TextView) v.findViewById(R.id.tv_post_id);
holder.tvPostText = (TextView) v.findViewById(R.id.tv_post_text);
holder.ivPostImage = (ImageView) v.findViewById(R.id.iv_post_img);
btnCheck = (Button) v.findViewById(R.id.btn_check);
v.setTag(holder);
}else{
holder = (ViewHolder) v.getTag();
}
globPosition = itemsArrayList.get(position);
holder.tvPostID.setText(itemsArrayList.get(position).getPostID());
if(globPosition.getPostText().equals(null) || globPosition.getPostText().equals("")){
}else {
holder.tvPostText.setText(globPosition.getPostText());
}
if(globPosition.getPostImage().equals(null) || globPosition.getPostImage().equals("")){
}else {
Glide.with(context).load(globPosition.getPostImage()).placeholder(R.drawable.loading_img).error(R.drawable.bg_480_800).into(holder.ivPostImage);
}
btnCheck.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.e("PostID", globPosition.getPostID());
}
});
return v;
}
static class ViewHolder{
TextView tvPostID;
TextView tvPostText;
ImageView ivPostImage;
}
}
adapter 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" >
<TextView
android:id="#+id/tv_post_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/post_id"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
android:id="#+id/tv_post_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="12sp" />
<ImageView
android:id="#+id/iv_post_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/empty" />
<Button
android:id="#+id/btn_check"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#000000"
android:text="#string/check"
android:textColor="#FFFFFF" />
</LinearLayout>
How to get rid of this?
if(globPosition.getPostText().equals(null) || globPosition.getPostText().equals("")){
holder.tvPostText.setVisibility(View.GONE);
}else {
holder.ivPostImage.setVisibility(View.GONE);
holder.tvPostText.setVisibility(View.VISIBLE);
holder.tvPostText.setText(globPosition.getPostText());
}
if(globPosition.getPostImage().equals(null) || globPosition.getPostImage().equals("")){
holder.ivPostImage.setVisibility(View.GONE);
}else {
holder.ivPostImage.setVisibility(View.GONE);
holder.tvPostText.setVisibility(View.VISIBLE);
Glide.with(context).load(globPosition.getPostImage()).placeholder(R.drawable.loading_img).error(R.drawable.bg_480_800).into(holder.ivPostImage);
}
I got the idea from ankit aggarwal answer (his code is not working properly but he gave me the basic idea). And one more thing, previously i didn't get the correct position, so i changed the adapter code little bit
Code
package com.app.listviewitemid.adapter;
import java.util.List;
import com.app.listviewitemid.R;
import com.app.listviewitemid.model.Items;
import com.bumptech.glide.Glide;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class ItemsAdapter extends BaseAdapter{
List<Items> itemsArrayList;
int Resource;
LayoutInflater inflater;
Context context;
Button btnCheck;
public ItemsAdapter(Context context, List<Items> items) {
this.context = context;
this.itemsArrayList = items;
}
#Override
public int getCount() {
return itemsArrayList.size();
}
#Override
public Object getItem(int location) {
return itemsArrayList.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("InflateParams") #Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.adapter_layout, null);
TextView tvPostID = (TextView) convertView.findViewById(R.id.tv_post_id);
TextView tvPostText = (TextView) convertView.findViewById(R.id.tv_post_text);
ImageView ivPostImage = (ImageView) convertView.findViewById(R.id.iv_post_img);
btnCheck = (Button) convertView.findViewById(R.id.btn_check);
final Items items = itemsArrayList.get(position);
tvPostID.setText(items.getPostID());
if(items.getPostText().equals(null) || items.getPostText().equals("")){
tvPostText.setVisibility(View.GONE);
}else {
//ivPostImage.setVisibility(View.GONE);
if(items.getPostImage().equals(null) || items.getPostImage().equals("")){
ivPostImage.setVisibility(View.GONE);
}else {
ivPostImage.setVisibility(View.VISIBLE);
tvPostText.setVisibility(View.GONE);
Glide.with(context).load(items.getPostImage()).placeholder(R.drawable.loading_img).error(R.drawable.bg_480_800).into(ivPostImage);
}
tvPostText.setVisibility(View.VISIBLE);
tvPostText.setText(items.getPostText());
}
if(items.getPostImage().equals(null) || items.getPostImage().equals("")){
ivPostImage.setVisibility(View.GONE);
}else {
ivPostImage.setVisibility(View.VISIBLE);
//tvPostText.setVisibility(View.GONE);
if(items.getPostText().equals(null) || items.getPostText().equals("")){
tvPostText.setVisibility(View.GONE);
}else {
tvPostText.setVisibility(View.VISIBLE);
tvPostText.setText(items.getPostText());
}
Glide.with(context).load(items.getPostImage()).placeholder(R.drawable.loading_img).error(R.drawable.bg_480_800).into(ivPostImage);
}
btnCheck.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.e("PostID", items.getPostID());
}
});
return convertView;
}
}
Thanks to ankit aggarwal
I have been trying to implement a list view which has five image views in a single row of a list view. I found that it can be done with layout inflater but since I am new to android I could not exactly get how to make the best use of it. I want to get a view of this sort:
L,S,D,A,E are images and it should change accordingly for different users in the list view according to the data provided dynamically. Can anybody please help me with the code snippet for this, or just give me an idea on how to implement it?
Okay so your list view should inflate a layout of this type:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:id="#+id/layoutContainer" >
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/iv1" />
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/iv2" />
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/iv3" />
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/iv4" />
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/iv5" />
</LinearLayout>
Save it to row.xml located in your layout folder.
Next implement this in your activity's onCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomAdapter myAdapter = new CustomAdapter(getApplicationContext());
ListView mainListView = (ListView) findViewById(R.id.lv);
mainListView.setAdapter(myAdapter);
}
Finally, you need to create the CustomAdapter.java class, like this:
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class CustomAdapter extends BaseAdapter {
private Bitmap[][] data;
private int count;
private Context context;
public CustomAdapter(Context context) {
this.context = context;
data = new Bitmap[100][];
count = 0;
}
#Override
public int getCount() {
return count;
}
#Override
public Bitmap[] getItem(int position) {
// TODO Auto-generated method stub
return data[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View adapterView = convertView;
if (adapterView == null) {
adapterView = inflater.inflate(R.layout.row, null);
}
ImageView imageView = (ImageView) adapterView.findViewById(R.id.iv1);
imageView.setImageBitmap(data[position][0]);
//Repeat the last two steps for all five images, changing the last index accordingly
return adapterView;
}
public void addBitmapArray (Bitmap[] newValue) {
data[++count] = newValue;
}
}
In row of the list add a linear layout LinearLayout1 then do something like fallowing in your adapter add dynamically images to the list item..
public View getView(final int position, View convertView, ViewGroup parent)
{
// System.out.println(" inside KeyvalueAdapter..");
ViewHolder holder = null;
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.new_row, null);
holder = new ViewHolder();
holder.tv_title = (TextView) convertView.findViewById(R.id.titleTextView);
ImageView imageView = new ImageView(context);
imageView.setImageResource(resId);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LinearLayout1);
linearLayout.addView(imageView);
convertView.setTag(holder);
}
else holder = (ViewHolder) convertView.getTag();
holder.tv_title.setText(notifList.get(position));
return convertView;
}
If you want to load different image you have to extend your listview adapter:
example if i understand correctly your question.
Check out this thread:
Android custom Row Item for ListView
You have to write your own xml with 5 imageviews
So I have looked through a lot of other answers but have not been able to get my app to work how I want it. I basically want the list view that has the text and check mark to the right, but then an addition button to the left. Right now my list view shows up but the check image is never changed.
Edit: after a helpful comment I discovered that the rows can be selected by using the arrow (up/down) on the emulator, this highlights the row how I want. However, when I click the row it does not become selected like I want it to. Also, if it helps the list view is being used inside a dialog box.
Selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/accept_on" />
<item
android:drawable="#drawable/accept" />
</selector>
Row xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#EEE">
<ImageButton
android:id="#+id/goToMapButton"
android:src="#drawable/go_to_map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left" />
<TextView
android:id="#+id/itemName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1" />
<Button
android:id="#+id/checkButton"
android:background="#drawable/item_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right" />
</LinearLayout>
MapAdapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MapAdapter extends ArrayAdapter<String>{
Context context;
int layoutResourceId;
String data[] = null;
LayoutInflater inflater;
LinearLayout layout;
public MapAdapter(Context context, int layoutResourceId, String[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
inflater = LayoutInflater.from(context);
}
#Override
public String getItem(int position) {
return data[position];
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if(convertView == null)
{
convertView = inflater.inflate(R.layout.map_item_row, null);
layout = (LinearLayout)convertView.findViewById(R.id.layout);
holder.map = (ImageButton)convertView.findViewById(R.id.goToMapButton);
holder.name = (TextView)convertView.findViewById(R.id.itemName);
//holder.check = (Button)convertView.findViewById(R.id.checkButton);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
layout.setBackgroundColor(0x00000004);
holder.name.setText(getItem(position));
return convertView;
}
static class ViewHolder
{
ImageButton map;
TextView name;
Button check;
}
}
Try by setting the selector as background instead of src
Try like this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Planet to display
Planet planet = (Planet) this.getItem(position);
// The child views in each row.
CheckBox checkBox;
TextView textView;
// Create a new row view
if (convertView == null) {
convertView = inflater.inflate(R.layout.simplerow, null);
// Find the child views.
textView = (TextView) convertView.findViewById(R.id.rowTextView);
checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
// Optimization: Tag the row with it's child views, so we don't
// have to
// call findViewById() later when we reuse the row.
convertView.setTag(new PlanetViewHolder(textView, checkBox));
// If CheckBox is toggled, update the planet it is tagged with.
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Planet planet = (Planet) cb.getTag();
planet.setChecked(cb.isChecked());
}
});
}
// Reuse existing row view
else {
// Because we use a ViewHolder, we avoid having to call
// findViewById().
PlanetViewHolder viewHolder = (PlanetViewHolder) convertView
.getTag();
checkBox = viewHolder.getCheckBox();
textView = viewHolder.getTextView();
}
// Tag the CheckBox with the Planet it is displaying, so that we can
// access the planet in onClick() when the CheckBox is toggled.
checkBox.setTag(planet);
// Display planet data
checkBox.setChecked(planet.isChecked());
textView.setText(planet.getName());
return convertView;
}
See this example http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php
On home scree of application, how to display the menu which is similar to android menu but no items needs to be displayed in specific cells. Considering grid of 3 x 3, five items only needs to be displayed at (Row, Col): [0,1], [1,0], [1,1], [1,2], [2,1].
We have tried GridView and set visibility to GONE (convertView.setVisibility(View.GONE);) for items which need not be displayed. Following this, item is not displayed in grid but when user browses through blank item using up and down keys or click directly on blank item, that icon is hihglighted and selected as if it is blank item in grid. We want as it is blank it should not repond to user events neither highlighted not selected.
Code for Grid View:
package org.XXX;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class XXXActivity extends Activity {
GridView MyGrid;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.maingrid);
MyGrid = (GridView)findViewById(R.id.MyGrid);
MyGrid.setAdapter(new ImageAdapter(this));
MyGrid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(arg0.getContext(), position + " selected", Toast.LENGTH_LONG).show();
switch(position) {
case 0:break;
case 1:
//Browse
Intent newIntent = new Intent(XXXActivity.this, YYYListItemIcons.class);
startActivity(newIntent);
break;
case 2:break;
case 3:
//Saved Searches
newIntent = new Intent(XXXActivity.this, ZZZListItemIcons.class);
startActivity(newIntent);
break;
case 4:
//Sign in
break;
case 5:
//Reminders
break;
case 6:break;
case 7:
//Sign up
break;
case 8:break;
}
}
});
//onSearchRequested(); //to open search by default
}
public class ImageAdapter extends BaseAdapter
{
Context MyContext;
public ImageAdapter(Context _MyContext)
{
MyContext = _MyContext;
}
#Override
public int getCount()
{
return 9;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater mInflater = LayoutInflater.from(MyContext);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.grid_item_text);
holder.icon = (ImageView) convertView.findViewById(R.id.grid_item_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(getTextId(position));
holder.icon.setImageBitmap(BitmapFactory.decodeResource(MyContext.getResources(), getIconId(position)));
if(getIconId(position) == R.drawable.nothing) {
convertView.setVisibility(View.GONE);
}
return convertView;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
private int getIconId(int position) {
int iconImages[] = {
R.drawable.nothing,
R.drawable.browse,
R.drawable.nothing,
R.drawable.saved_searches,
R.drawable.sign_in,
R.drawable.reminders,
R.drawable.nothing,
R.drawable.sign_up,
R.drawable.nothing
};
return iconImages[position];
}
private int getTextId(int position) {
int iconNames[] = {
R.string.nothing,
R.string.browse,
R.string.nothing,
R.string.saved_searches,
R.string.sign_in,
R.string.reminders,
R.string.nothing,
R.string.sign_up,
R.string.nothing
};
return iconNames[position];
}
}
static class ViewHolder {
TextView text;
ImageView icon;
}
}
GridLayout:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="20dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
PerItemIconLayout in Grid:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
if(getIconId(position) == R.drawable.nothing) {
convertView.setVisibility(View.GONE);
}
replace the above lines by below and try....
if(getIconId(position) == R.drawable.nothing) {
convertView.setVisibility(View.GONE);
convertView.setClickable(false);
convertView.setEnabled(false);
}
try this code in getview().
Now that you have posted your code, I am not sure if you can technically remove but you can disable the "highlighted" click that you are talking about, this way, when a user clicks on the one of the icons, it will no longer highlight.
This can be done via XML or in your code:
https://stackoverflow.com/questions/2865683/android-disable-highlighting-in-gridview
Code: GridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
XML: android:listSelector="#00000000"
However, this will affect all the icons in your gridview.
Also take a look at this:
https://stackoverflow.com/questions/5514629/how-to-disable-item-click-for-particular-positions-in-grid-view-in-android