Customizing spinner does not work - android

I tried to customize spinner as follows where image is a 9 patch image.
<Spinner
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/spinner"
android:textSize="20sp"
android:background="#drawable/image"
/>
The result is this: Spinner screenshot
The text is completely hidden by the image. How to make the text visible?
Edit:
I verified that I can see the text on spinner if I use the image1. But if I use this image2 (created by yours truly) then I cannot see the text. Seems something is wrong with my 9 patch image. But I can't figure out what ?

try this code
layout:-
<Spinner
android:id="#+id/endmonthsp"
android:layout_width="140dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center" />
Activity:-
ArrayList<String> month = new ArrayList<String>(Arrays.asList("Month",
"January", "February", "March", "April", "May", "june", "July",
"August", "September", "October", "November", "December"));
Spinner spinner5 = (Spinner) findViewById(R.id.endmonthsp);
startdatemonthadapter = new DateAdapter(this, R.layout.spinnerlayout,
month);
spinner1.setAdapter(startdatemonthadapter);
startdatemonthadapter
.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
Adapter:-
public class DateAdapter extends ArrayAdapter<String> {
public DateAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
this.context = context;
}
private Context context;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
if (view == null) {
LayoutInflater inflate = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflate.inflate(R.layout.spinnerlayout, null);
}
TextView spinneritemTV = (TextView) view
.findViewById(R.id.spinneritemTV);
spinneritemTV.setText(getItem(position));
return view;
}
}
spinnerlayout:-
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="50dp" >
<TextView
android:id="#+id/spinneritemTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:padding="10dp"
android:text="Testing "
android:textColor="#868A8D"
android:textSize="18sp" />
</RelativeLayout>

You need to set background of spinner as follows:
In your layout xml :
<Spinner
android:id="#+id/spinner1"
style="#style/spinner_style"
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_height="45dp" />
Now in style.xml add a style for spinner as follows:
` <style name="spinner_style">
<item name="android:background">#drawable/spinner_bg</item>
<!--
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
-->
<item name="android:paddingLeft">5</item>
</style>`
Now make a xml named spinner_bg.xml in drawable :
`
<item><layer-list>
<item><shape>
<gradient android:angle="90" android:endColor="#android:color/transparent" android:startColor="#android:color/transparent" android:type="linear" />
<padding android:left="2dp" android:right="2dp" />
</shape></item>
<item>
<bitmap android:gravity="center_vertical|left" android:src="your background drawable for spinner here" />
</item>
</layer-list></item>
`
According to your question, below part of spinner_bg.xml did the trick.
`<item>
<bitmap android:gravity="center_vertical|left" android:src="your background drawable for spinner here" />
</item>`
Thats it.

There is really nothing wrong in setting the spinner background directly as I have done. Of course you cannot have different "states" of the spinner then. The text was not visible because the 9 patch image was corrupted for some unknown reason. I recreated the image and I can see the text on the spinner now. One more thing: the text will not be visible if "content areas" are not defined on the 9 patch.

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 add badge to list

I want to add badge notification to my arraylist.But I couldn't find a tutorial for this.This is my list code:
public void chatList() {
conversationsAdapter=new ArrayAdapter<JsonObject>(this,0) {
#Override
public View getView(int c_position,View c_convertView,ViewGroup c_parent) {
if (c_convertView == null) {
c_convertView=getLayoutInflater().inflate(R.layout.random_bars,null);
}
JsonObject user=getItem(c_position);
String name=user.get("name").getAsString();
String image_url="http://domain.com/profile/thumb/"+user.get("photo").getAsString();
TextView nameView=(TextView)c_convertView.findViewById(R.id.tweet);
nameView.setText(name);
ImageView imageView=(ImageView)c_convertView.findViewById(R.id.image);
Ion.with(imageView)
.placeholder(R.drawable.twitter)
.load(image_url);
return c_convertView;
}
};
ListView conversationsListView = (ListView)findViewById(R.id.conversationList);
conversationsListView.setAdapter(conversationsAdapter);
conversationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
startChat(conversationsAdapter.getItem(position));
}
});
}
My list:
I want to add badge to this list.How can I do this ?
Ps:I found a library https://github.com/jgilfelt/android-viewbadger but I don't want to use it.
Inflate the layout in custom listview which extends base adapter instead of array adapter. Add following layout into the custom list item layout.
drawer_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/list_selector">
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/desc_list_item_icon"
android:src="#drawable/ic_home"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/list_item_title"
android:gravity="center_vertical"
android:paddingRight="40dp"/>
<TextView android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/counter_bg"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:textColor="#color/counter_text_color"/>
</RelativeLayout>
Use below drawable for background textview counter and set notification count value in textview
counter_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid android:color="#color/counter_text_bg" >
</solid>
<!-- If you want to add some padding -->
<padding
android:right="3dp"
android:left="3dp" >
</padding>
<!-- Here is the corner radius -->
<corners android:radius="2dp" >
</corners>
</shape>

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);
}

Change the background image of listview on Focus

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);
}
});

Categories

Resources