I have created a sliding menu.
In that i put image-view, on clicking image-view it shows me content,all works fine.
Now what i want to do is,
on selecting image-view, which is active i want to change its image and remaining things will still be unchanged... (i have created for that adapter )
private void onMenuItemClick(AdapterView<?> parent, View view,
int position, long id) {
//bla bla bla
//some other code
if (selectedItem.compareTo("ABC") == 0) {
View subview = view.findViewById(R.id.list_image);
((ImageView) subview).setImageResource(R.drawable.img1);
fragment = new abcactivity();
} else if (selectedItem.compareTo("XYZ") == 0) {
View subview = view.findViewById(R.id.list_image);
((ImageView) subview).setImageResource(R.drawable.img2);
fragment = new xyzactivity();
} else if (selectedItem.compareTo("Another") == 0) {
View subview = view.findViewById(R.id.list_image);
((ImageView) subview).setImageResource(R.drawable.img3);
fragment = new anotheractivity();
}
here i put images in
images = new ArrayList<Integer>();
images.add(R.drawable.img1);
images.add(R.drawable.img2);
images.add(R.drawable.img3);
I want change only activated image and that's also i have done but it should change to previous if i am selecting second position image.!
I think what you need is a selector file. You need add a image_change.xml in your /res/drawable folder.
img_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/image_white" android:state_activated="false" />
<item android:drawable="#drawable/image_black" android:state_pressed="true" />
<item android:drawable="#drawable/image_black" android:state_activated="true" />
</selector>
And in your ImageView, you must set the drawable file:
<ImageView
...
android:background="#drawable/img_selector"
... />
Or, in your code:
images.setBackground(R.drawable.img_selector);
I hope I've helped
Related
Following is my source code,
I also tried isPressed , isClicked , but it still doesn't work.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.my_item_layout, parent, false);
}
MyItem myItem = getItem(position);
TextView direction = (TextView) convertView.findViewById(R.id.direction);
direction.setText(myItem .getDirection());
if(convertView.isSelected()){
convertView.setBackgroundResource(R.drawable.list_select_bar);
setTextColor(convertView, textIDs , R.color.white);
}else{
convertView.setBackgroundResource(R.color.light_white);
setTextColor(convertView, textIDs , R.color.black);
}
return convertView;
}
Actually , if I remove the convertView check block, I just need to register an onItemClickListener onto the listview... but if I do this , seems makes getView method meaningless. I am so struggled with this issue.
getView is called only while rendering the list view. Its is not called when an item is selected or clicked.
Probably what u need is to use convertView.setOnFocusChangeListener
getView() creates (or re-creates) a View that wasn't visible. So a new view like this cannot be selected, pressed or clicked.
I believe you want a Color State Selector.
(From the link above)
XML file saved at res/color/button_text.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
This layout XML will apply the color list to a View:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:textColor="#color/button_text" />
Or you can use a similar method with a ListView State Selector for ListViews, GridViews, etc.
I'm assuming you want the background color to change only when you've got your finger down on the row and change back when you let go? To do this, just add on OnTouchListener to your row item.
convertView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
convertView.setBackgroundResource(R.drawable.list_select_bar);
} else if (event.getAction() == MotionEvent.ACTION_UP
|| event.getAction() == MotionEvent.ACTION_CANCEL) {
convertView.setBackgroundResource(R.color.light_white);
}
return false;
}
);
First , I appreciate that Sam gave me such a useful tip.
Following is my resolve procedure.
Write a selector file and below is its content. then put this list_background.xml into drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_select_bar" android:state_pressed="true"/><!--pressed -->
<item android:drawable="#color/light_white"/><!-- default --></selector>
then in your item layout xml , using list_background like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:background="#drawable/list_background"
...
>
That's it. No click listener or other method invoked in the source code.
I have made a linear layout on which I am adding views, simulating a table because I have read that a listview inside a scrollview is not an option :)
So I have a linear layout with wrap_content inside a scrollview.
The problem is that for the views I am adding I can't trigger any press state to change the graphics as before when it was in the listview it worked great.
I have the following:
if (arrayWorksWith.size()!=0)
{
this.listAdapter = new WorksWithListAdapter(this, R.layout.works_with_cell, R.id.lblItemTitle, arrayWorksWith);
for (int i=0; i<arrayWorksWith.size(); i++)
{
View v = this.listAdapter.getView(i, null, layoutTblWorksWith);
v.setTag(new Integer(1000 + arrayWorksWith.get(i).getId()));
v.setOnClickListener(this);
layoutTblWorksWith.addView(v);
}
}
The problem is that the click event gets fired but I don't have any visual feedback. Inside that view I have an Imageview with the background set to the following drawable:
<?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/r1" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="#drawable/r1" /> <!-- focused -->
<item android:drawable="#drawable/transparent_background" /> <!-- default -->
</selector>
Also I have just tried a touch listener on the view I am adding, but this fails as well :(
v.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch (View v, MotionEvent event)
{
TextView lblCategoryTitle = (TextView)v.findViewById(R.id.lblItemTitle);
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
lblCategoryTitle.setTextColor(R.color.white);
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
lblCategoryTitle.setTextColor(R.color.textLabel_blue);
}
return false;
}
});
So when this code was inside a ListView when I tapped on a cell the image was changing the background giving visual feedback, but when I am creating it now and adding it to a LinearLayout it stopped working.
Any ideas? I have tried playing with focusable, clickable, duplicate parent state, but with no luck.
You have to set the listView on to an OnItemClickListener, not on an OnClickListener.
I have some number of rows in my list view and each row have two different images on list click event. When I click on a list view then I want to change the image of that list view. The image changes on the mouse click. But as soon as the click is released the image disappears. I want it to persist even when the click is released.
Below is the code I have used.
public void onItemClick(AdapterView<?> p, View view, int position,
long id) {
listViewpos = position;
listViewName = hospitalList.get(position).HospName;
selectedHosp = WSParser.hospitalInfo.get(position);
hospitalID = selectedHosp.HospID;
Log.d("onCreate", "hospitalID " + hospitalID);
vwSubParent = (RelativeLayout) view.findViewById(R.id.relativeLayout1);
vwChildLay = (LinearLayout) vwSubParent.findViewById(R.id.linearLayout2);
imgSelected = (ImageView) vwChildLay.findViewById(R.id.imageView1);
imgSelected.setBackgroundResource(R.drawable.listviewselectorimage);
}
The listviewselectorimage.xml is as given below
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use selected mark -->
<item android:drawable="#drawable/selectedmark"
android:state_pressed="true" />
<!-- When not selected, use selectedmark_black-->
<item android:drawable="#drawable/selectedmark_black" />
</selector>
});
hi dear i have 5 rows in my listview each listview have image and text associated with it ,i want when i click on listview at any position it changes its background color and when i click another position in list previosly selected list row comes to its former condition and selected change the color and image ...how to achive it ?thanks
my code is ..but it does not retain previously selected row...
public void onListItemClick(ListView parent, View v, int position,
long id) {
if(position == 0)
{
v.setBackgroundColor(Color.WHITE);
}
if(position == 1)
{
v.setBackgroundColor(Color.WHITE);
}
Toast.makeText(getApplicationContext(), "You have selected "
+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}
You can use the concept of selector. Create a selector xml and use it as the background of the row. it may help you.like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/selector_s" />
<item android:state_pressed="true" android:drawable="#drawable/selector_s" />
<item android:drawable="#drawable/selector_d" />
</selector>
you can use this link
http://android-journey.blogspot.com/2009/12/android-selectors.html
I have a custom listview and I am using a custom listadapter to display that list. In my custom listadapter I am trying to set the colour of each item dynamically depending on a value within the object. However whenever I try to do this the items become faded rather than getting the colour they were set to. I am applying a few styles to the project but when I remove their effect it still doesn't work. This is my code to change the background colour of each item:
private class stationAdapter extends ArrayAdapter<Station>{
private ArrayList<Station> stations;
public stationAdapter(Context context, int textViewResourceId, ArrayList<Station> stations) {
super(context, textViewResourceId, stations);
this.stations = stations;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Station temp = stations.get(position);
if (temp != null) {
TextView stationName = (TextView) v.findViewById(R.id.stationname);
TextView serviced = (TextView) v.findViewById(R.id.inservice);
try{
if(temp.getLine().equals("red")){
v.setBackgroundColor(R.color.red);
}
else{
v.setBackgroundColor(R.color.green);
}
}catch(Exception e){
Log.d(TAG, "Null pointer");
}
if (stationName != null) {
stationName.setText("Station: "+temp.getName()); }
if(serviced != null){
serviced.setText("In Service: "+ temp.getInServive());
}
}
return v;
}
}
If anyone could point out what I am doing wrong I would really appreciate it.
You cant use setBackgroundColor and then reference a resource. if you want to use setBackgroundColor() you need to use the Color class like:
setBackgroundColor(Color.BLACK);
Instead if you want to set a resource (R.drawable, R.color etc...) you need to do it like
v.setBackgroundResource(R.color.black);
EDIT:
The cache color hint is what is needed if the items start becoming gray while scrolling the list. You need to set it to a transparent color if you are adding custom backgrounds to items and lists.
Like Darko mentioned, you're trying to set a color but using a resource ID instead. With that said, using a solid color for a list item background is a big no-no, you definitely want to use a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_pressed="false" android:state_selected="false"
android:state_checked="false" android:state_enabled="true" android:state_checkable="false"
android:drawable="#color/red" />
</selector>
Put that in a list_red_background.xml in your drawables folder and use setBackgroundResource() instead.
Have you switched fading off?
http://developer.android.com/resources/articles/listview-backgrounds.html
Add this to the ListView layout file:
android:cacheColorHint="#00000000"