Change the background image of listview on Focus - android

My code is :
ListContacts.java
public class ListContacts extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("List Contacts : onCreate : ");
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1", null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
startManagingCursor(cursor);
ImageCursorAdapter .java
public class ImageCursorAdapter extends SimpleCursorAdapter implements SectionIndexer{
public View getView(int pos, View inView, ViewGroup parent) {
View v = inView;
String phoneNumber = null;
String contactID = null;
// Associate the xml file for each row with the view
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.contact_listview, null);
}
this.c.moveToPosition(pos);
/**
* Get the strings with the name and number of the person that the
* current row
*/
//
String lName = this.c.getString(this.c
.getColumnIndex(EMPLOYEE_NAME))
}
}
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent" android:paddingLeft="5dp" android:paddingRight="5dp"
android:layout_height="fill_parent" android:background="#drawable/listview_bac">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="4dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/callimage"
android:layout_width="36dp"
android:layout_height="49dp"
android:clickable="true"
android:background="#android:color/transparent"
android:paddingRight="4dp"
android:src="#drawable/phone">
</ImageView>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<TextView android:id="#+id/contact_name"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<ImageView
android:id="#+id/favimage"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="top|right"
android:background="#android:color/transparent"
android:src="#drawable/favorites" android:visibility="gone">
</ImageView>
<!-- <TextView android:id="#+id/contact_number"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:textAppearance="?android:attr/textAppearanceSmall"
/> -->
</LinearLayout>
</LinearLayout>
</LinearLayout>
I need to change the image, color of textview of the listview on focus

Add an xml file to manage the background of the LinearLayout.
list_bg_selector.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="#drawable/list_selected_bgimage" />
<!-- Inactive tab -->
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="#drawable/list_default_bgimage" />
<!-- Pressed tab -->
<item android:state_pressed="true" android:drawable="#drawable/list_selected_bgimage" />
<!-- Selected tab (using d-pad) -->
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/list_selected_bgimage" />
</selector>
To manage the text color, set the text color to another xml.
text_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#android:color/black"/>
<item android:state_focused="true" android:color="#android:color/black"/>
<item android:state_pressed="true" android:color="#android:color/black"/>
<item android:color="#android:color/white"/>
</selector>

in getView
if(position == focusedPosition) {
textView.setColor(context.getResources().getColor(R.color.color_for_focused_item);
} else {
textView.setColor(context.getResources().getColor(R.color.color_for_normal_item);
}

try this
listViewContactList.setOnFocusChangeListener(new OnFocusChangeListener(){
#Override
public void onFocusChange(View v, boolean hasFocus) {
v.setBackgroundColor(Color.BLACK);
ImageView img=(ImageView)v.findViewById(R.id.callimage);
img.setImageResource(R.drawable.yourimage);
TextView tv=(TextView)v.findViewById(R.id.your_textview_id);
tv.setBackgroundResource(R.drawable.your_back);
}
});

Related

Highlight selected item on a gridview

I am trying to highlight a selected item on a gridview (dinamically populated with an adapter), but it is not working.
I did research and i even tried to copy exactly the selector of other people and even the way that they put it on the gridview but i am not being able to put it working.
It just doesn't do anything. The background of each item is white (like i wanted), but when i press it (it it is on top of a textview or a imageview (part of the gridview item) it doesn't do anything. If i press out of the imageView or textview, it will do what i want.
EDIT : I have listeners for the images and the textviews, so it might be interfering with this selector ? How could i solve this problem?
Here is the code of the activity where i create the gridview :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highway_appselection_activity);
gridView= (GridView) findViewById(R.id.HighwayGridView);
gridView.setSelector(new ColorDrawable(Color.BLACK));
Here is the xml of each item of this gridview : (where i define the background as the selector)
<?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:id="#+id/HighwayGridViewItem"
android:orientation="vertical"
android:background="#drawable/highway_appselection_selector"
android:padding="5dp">
<cm.aptoide.lite.HighwayCustomImageView
android:layout_width="72dp"
android:layout_height="72dp"
android:id="#+id/highwayGridViewItemIcon"
android:background="#FFFFFF"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:padding="5dp"
android:clickable="true"/>
<!-- does this need to be my custom image view anymore? CHeck on that-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/highwayGridViewItemName"
android:textColor="#000000"
android:text="texto de teste"
android:textSize="10sp"
android:focusable="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:layout_weight="2"
android:textStyle="bold"
android:paddingRight="5dp"
android:layout_marginLeft="5dp"
android:clickable="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/info_icon"
android:padding="5dp"
android:clickable="true"
android:id="#+id/highwayGridViewItemInfoButton"/>
And here is my selector :
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#color/green_main_color" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#color/green_main_color" />
<item android:state_enabled="true" android:state_selected="true" android:drawable="#color/green_main_color" />
<item android:drawable="#android:color/white" />
I might be missing something, I am new to Android, sorry if there is any rookie mistake.
Create file selector.xml as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/green_main_color" android:state_pressed="true"/>
<item android:drawable="#color/green_main_color" android:state_selected="true"/>
<item android:drawable="#color/white"/>
</selector>
Put your selector file in drawable folder as drawable/selector.xml and then in your gridView:
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:listSelector="#drawable/list_selector"
android:scrollbars="none" />
try this :
int nPrevSelGridItem = -1;
gridview.setOnItemClickListener(new OnItemClickListener() {
View viewPrev;
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
try {
if (nPrevSelGridItem != -1) {
viewPrev = (View) gridview.getChildAt(nPrevSelGridItem);
viewPrev.setBackgroundColor(Color.WHITE);
}
nPrevSelGridItem = position;
if (nPrevSelGridItem == position) {
//View viewPrev = (View) gridview.getChildAt(nPrevSelGridItem);
view.setBackgroundColor(getResources().getColor(R.color.orange));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});

How to change TextView text color inside a ListView when list item is clicked?

I am trying to change text color of a TextView inside a listview item.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
ViewGroup selectedGroup = (ViewGroup)view;
((TextView)selectedGroup.getChildAt(4)).setTextColor(Color.parseColor("#336699"));
String mID =String.valueOf(((TextView) selectedGroup.getChildAt(4)).getText());
}
});
But nothing happens, I can get text value of the textview, though. What might be wrong in this code?
This is, by the way, my listView layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="4dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#336699"
android:id="#+id/questionTitle"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#444444"
android:id="#+id/questionDate"
android:textSize="12sp"/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="#+id/questionContent" android:value=""/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="#+id/questionSenderContact" android:value=""/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="#+id/messageID" android:value=""/>
</LinearLayout>
EDIT: I was getting wrong textview. Index of it was not 4, but 0. Sorry for a question caused by lack of attention.
try this code:
public View row;
your_list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v,
int position, long id) {
if (row != null) {
row.setBackgroundResource(R.color.orange);
}
row = v;
v.setBackgroundResource(R.color.transparent_green);
)};
Create a new StateListDrawable like you did before but with black as default color and white when pressed.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/black" />
<item android:state_focused="true" android:color="#color/black" />
<item android:state_pressed="true" android:color="#color/black" />
<item android:color="#color/white" />
</selector>
Now for in the TextView change the text color to the new drawable:
android:textColor="#color/list_item_text"
More on StateListDrawables: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
you will have to set textview as tag to the clickable button as:
...
view.setOnClickListener(this);
view.setTag(textView);
...
public void onclick(View v){
...
TextView textView = (TextView)v.getTag();
textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));
...
}

How to Make LinearLayout Clickable

I have a linearlayout like this
<LinearLayout
android:id="#+id/optionlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/divider"
android:orientation="vertical" >
</LinearLayout>
I want to add another xml view into this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!--
<View
android:layout_width="match_parent"
android:layout_height="0.5dip"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#2fd275"
/>
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="#+id/option_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/logout" />
<TextView
android:id="#+id/option_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:visibility="gone" />
<TextView
android:id="#+id/option_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:text="Signout"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#616161" />
</LinearLayout>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="0.5dip"
android:background="#d3d3d3" />
</LinearLayout>
via the code
for (int i = 0; i < opt.length; i++) {
View view = inflater.inflate(R.layout.option_item, optionlist,
false);
view.setBackgroundResource(R.drawable.optionlist);
view.setTag(opt_image[i]);
view.setClickable(true);
view.setFocusable(true);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ADD your action here
int res = (Integer) v.getTag();
switch (res) {
case R.drawable.logout:
signout();
break;
}
}
});
final TextView tv = (TextView) view.findViewById(R.id.option_name);
final ImageView iv = (ImageView) view
.findViewById(R.id.option_image);
tv.setText(opt[i]);
iv.setImageResource(opt_image[i]);
optionlist.addView(view);
}
Now on click of the layout i want to load this xml file as below...but i am not able to get the backgroud loaded on the click event. Please give suggestion what am I doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/holo_green_dark" android:state_focused="true"/>
<item android:drawable="#color/holo_green_dark" android:state_enabled="false" android:state_pressed="true"/>
<item android:drawable="#color/white"/>
</selector>
First get id of LinearLayout from xml and then perform onClickListener on it instead of whole View
View view = inflater.inflate(R.layout.option_item, optionlist, false);
LinearLayout layout = view.findViewByIt(R.id.optionlist);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ADD your action here
int res=(Integer) v.getTag();
switch(res)
{
case R.drawable.logout: signout();
break;
}
}
});
try this.. hope it works..
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#ff0000" />//ur onclick desired color
<item android:state_focused="false"
android:drawable="#android:color/transparent" />focused color
</selector>
just create new xml and add this code in drawable and set ur linear layoyut backgrout as its name like android:background="#drawable/createdxmlname"
<LinearLayout
android:id="#+id/optionlist"
android:layout_below="#id/divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/createdxmlname"
android:clickable="true">
android:clickable="false"
<LinearLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:clickable="false"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="Cinema"
android:textSize="13sp"
android:layout_marginRight="10dp" />
<Button
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Check in"
android:textSize="13sp" />
</LinearLayout>
Create in drawable an pressed_layout.xml with the following content
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
And in your layout add it as background resource:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/pressed_layout"
android:clickable="true">
</LinearLayout>
layout.setclickble="true" in XML file and
layout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
}
});
We can also use onClick in XML of layout,and use that method in java file,like if onClick="next",then in java file we have to use the method .
public void next()
{
// do something
}

Properly update textColor and Drawable in customised Layout in selected/pressed ListView items

I have a customised Layout for a ListView, hosted in a Navigation Drawer. Here's the Layout for the adapter:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="100dp" >
<ImageView
android:id="#+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:duplicateParentState="true"
android:src="#drawable/nav_drawer_icon_states" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/icon"
android:duplicateParentState="true"
android:text="#string/hello_world"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#drawable/nav_drawer_text_states" />
</RelativeLayout>
In my /res/drawable folder I have the nav_drawer_text_states.xml as:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/blue"/>
<item android:state_enabled="true" android:state_focused="true" android:state_window_focused="true" android:color="#color/white"/>
<item android:color="#color/gray"/>
</selector>
and have tested with simple selector as this too:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/blue" />
<item android:state_selected="true" android:color="#color/white" />
<item android:state_checked="true" android:color="#color/white" />
<item android:color="#color/gray" />
</selector>
The pressed and default color works, but the intermediate state, when I have selected an item the ListView:
<ListView
android:id="#+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:paddingTop="?attr/actionBarSize" />
with the code:
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
doesn't change the selected text to white.
Any help is appreciated.
UPDATED
I changed the nav_drawer_text_states.xml to:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/pink"/>
<item android:state_activated="true" android:color="#android:color/white"/>
<item android:color="#color/second_row_playlists"/>
</selector>
and it worked, BUT the state_activated="true" is API11+ and my app is API8+.
Any help is appreciated.
< rant > Unfortunately Android is really bad in pre-Honeycomb versions, and the market don't want to let these versions die. < / rant >
I solved by doing by hand everything in an Adapter. If anyone still cares, here's the solution:
public class NavigationAdapter extends ArrayAdapter<String> {
private int mSelectedPosition;
public NavigationAdapter(Context context, String[] items) {
super(context, R.layout.cell_navigation_drawer, items);
this.mSelectedPosition = 0;
}
public void setItemChecked(int position) {
mSelectedPosition = position;
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
...
}
...
Resources res = getContext().getResources();
if (position == mSelectedPosition) {
textView.setTextColor(res.getColor(R.color.white));
} else {
textView.setTextColor(res.getColor(R.color.gray));
}
return convertView;
}
}
and changed the method when clicked to:
public void setItemChecked(int position, boolean value) {
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
mMyAdapter.setItemChecked(mCurrentSelectedPosition);
}

Android: Changing the size of an image in a ListView item

I have this code for a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Focused-->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/gradient_background"
/>
<!-- Button Focused Pressed-->
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/gradient_background"
/>
<!-- Button Pressed-->
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#android:drawable/ic_search_category_default"
/>
<!-- Button Default Image-->
<item android:drawable="#drawable/gradient_background"/>
</selector>
And in the activity that uses this selector, I have this:
<ListView
android:id="#+id/contactsView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/searchButton"
android:divider="#color/DarkGoldenrod"
android:dividerHeight="0.1dp"
android:listSelector="#drawable/list_selector"
android:drawSelectorOnTop="true" />
Now this is an example of what a listview item looks like when you click it and hold it down...
My Question is: Is there anyway I can make that picture smaller and align it to the right?
Thank you.
Have a look at point 10 of this Tutorial, get to know how to use your own Layout for a ListView Item and you can do any manipulation you want.
I think that Not the good way to do what you want :) ..
you have To use Costume Array Adapter for that ..
public class CustomBookItem extends ArrayAdapter<string>{
ArrayList<string> names;
Context context;
public CustomBookItem(Context context, int textViewResourceId,ArrayList<string> names )
{
super(context, textViewResourceId, names);
// TODO Auto-generated constructor stub
this.names=names;
this.context=context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v= convertView;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=inflater.inflate(R.layout.custome_listitem, parent,false);
ImageView imageView = (ImageView) v.findViewById(R.id.icon);
Textview name=(Textview)v.findViewById(R.id.name);
imageView.setImageDrawable(r.drawbale.icon_search);
return v;
}
R.layout.custome_listitem
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Textview
android:id="#+id/icon"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2" />
<ImageView
android:id="#+id/icon"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />

Categories

Resources