I've been trying to change the Spinner TextView color. I need to do it programmatically. I have my personal Adapter and resource view. (I don't want to change the pop up sipnner text color, only the text that is shown on the users layout when somenthig is selected)
This is my code:
PersonalArrayAdapter spAdapterInfraccion = new PersonalArrayAdapter(getContext(),
R.layout.item_spinner,
infraccionList);
spinner.setAdapter(spAdapter);
item_spinner.xml:
<?xml version="1.0" encoding="utf-8"?>
<co.my.app.utils.CustomTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
This is how I've try it, but returns null.
CustomTextView text = (CustomTextView)view.findViewById(R.id.text1);
text.setTextColor(getContext().getColor( R.color.black ));
You may try this :-
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int index = adapterView.getSelectedItemPosition();
((TextView) spinner.getSelectedView()).setTextColor(getResources().getColor(R.color.Blue));
Hope it may help you. :)
Related
I changed the text color and its not reflecting.. still showing black by default.Please help
I edited the "android.R.layout.simple_list_item_1" xml file. Is that enough or should i change something else to make it work?
android:id="#android:id/text1"
android:textColor="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
You can do that programmatically by overriding the getView of ArrayAdapter like the following.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, yourList) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.WHITE);
return view;
}
};
you have to change second argument of ArrayAdapter with selfmade xml textview file.
step 1.inside res in layout make textview_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:textColor="#00f">
</TextView>
step 2.
ArrayAdapter adapter=new ArrayAdapter<>(getApplicationContext(),R.layout.textview_blue,arraylist_yours);
step 3.
listview.setAdapter(adapter);
sure this code is work for you, you may change color in xml as per your desire.
this answer will helpful correctly as per your requirement.
I want to get the text of a TextView that is in a ListView's item.
See the code and you will find what's my question.
Activity.java:
SimpleAdapter adapter = new SimpleAdapter(
rootView.getContext(),
result,
R.layout.article_list_item,
new String[]{"article_title", "article_PubDate", "article_category", "article_articleNo"},
new int[]{R.id.article_title, R.id.article_PubDate, R.id.article_category, R.id.article_articleNo});
ListView articleItem.setAdapter(adapter);
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//I want get the text of R.id.article_articleNo(see article_list_item.xml)?
//What should I do?
}
});
article_list_item.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="match_parent"
android:orientation="vertical"
android:id="#+id/article">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文章标题"
android:id="#+id/article_title"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30-Apr-2014"
android:id="#+id/article_PubDate"
android:layout_gravity="center_horizontal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="personal_finance"
android:id="#+id/article_category"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9312"
android:id="#+id/article_articleNo"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</LinearLayout>
That's all!!
I want to get the text of R.id.article_article (see article_list_item.xml).
How can I do it?
Does this work for you?
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = result.get(position).get("article_articleNo");
//^^ does not necessarily need to be a string,
// make this whatever data type you are storing in the map
}
});
The idea is that in your SimpleAdapter, you are populating your ListView with a List<Map<String,Object>> (in this case named result).
Therefore, you can get that specific Map<String,Object> from the List by using .get(position), then once again using .get("article_articleNo") to get the Object from the map.
Edit after seeing your comment: Try this:
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.article_articleNo);
String text = tv.getText().toString();
}
});
First of all, forget about the xml layout. It's just a placeholder for data, which is used to define the layout of the data, and has got nothing to do with storing the data.
Now, your problem statement can be re-stated as: how to fetch data from an array (or list or arraylist or whatever the format of your result is) on clicking list item.
Hmm. Sounds simple now.
Refer to #RougeBaneling's answer for technical details.
I have a custom layout for the ListView which is as follows -
<?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"
android:background="#drawable/plain" >
<TextView
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textSize="16sp"
android:textColor="#000000"
android:paddingRight="10dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
When I click the item in a listview, I am getting error -
02-28 17:37:38.240: E/AndroidRuntime(20869): FATAL EXCEPTION: main
02-28 17:37:38.240: E/AndroidRuntime(20869):
java.lang.ClassCastException: android.widget.LinearLayout cannot be
cast to android.widget.TextView
This is because now I am using custom listview layout as above which is wrapped in a linearlayout.
The error line is-
class ListItemClickedonPaired implements OnItemClickListener
{
#SuppressLint("NewApi")
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
final String info = ((TextView) view).getText().toString(); //ERROR HERE
}
}
My question is-
How to get the text of the clicked item (technically a textview) when we have these as a custom items of a listview defined with above xml ?
Try
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
TextView tv = (TextView)view.findViewById(android.R.id.text1);
String info = tv.getText().toString();
}
You inflate a LinearLayout for each row. What you are doing is casting LinearLayout to TextView.
You can also use setTag and getTag
You have used a id from the android framework. So it should be android.R.id.text1 not R.id.text1 coz you have android:id="#android:id/text1". It is a mistake which i have edited.
http://developer.android.com/reference/android/R.id.html
public static final int text1
Added in API level 1
Constant Value: 16908308 (0x01020014
Instead of writing a text view inside a Linear layout make the TextView as the root Tag so that the view argument in the OnItemClick() returns the reference of text view, Now you can directly typecast the view object to the TextView.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textSize="16sp"
android:textColor="#000000"
android:paddingRight="10dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
Try this :
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
switch(view.getId()) {
case R.id.text1:
final String info = ((TextView) view).getText().toString();
break;
}
}
I have CustomListView ,ie contain a background image and TextView,
When an item is selected i need to change the background image and font color, currently i can change the background of selected row of the listview using an xml, but i can't change the text color.
By default my text color is black when am clicking an item in listview i need to change the text color to white.
am using following layout for my customlistview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="29dp"
android:id="#+id/appcategoryLinearLayout"
android:background="#drawable/appcategorybg1"
android:gravity="left|center_vertical"
>
<TextView
android:gravity="left|center_vertical"
android:text="fdsfsdfsdfdsfdsfdsf"
android:paddingLeft="8dp"
android:textSize="8dp"
android:textColor="#color/black"
android:id="#+id/appCategoryNameTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
The only way to write your own ListAdapter and write custom controls which would have the visual properties you define.
I am not sure if this will help you but you can do some thing like this
Use TextView variables as global
TextView t ;
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
if(t != null)
{
//reset the color to black
}
LinearLayout lay = (LinearLayout)v;
t = lay.getChildAt(0);
//now set text to bold
}
};
I'm having an issue with the Droid X phones where users say that the font color turns out to be white in the spinner, making it invisible unless the users highlight the items. No other phones seem to have this problem. I was going to try to force the font to be black to see if that helps. How can I do that?
Here's how I'm currently populating the spinner. It seems like the simple_spinner_item is broken on Droid X's.
String spin_arry[] = new String[str_vec.size()];
str_vec.copyInto(spin_arry);
ArrayAdapter adapter =
new ArrayAdapter(this,android.R.layout.simple_spinner_item, spin_arry);
I'm going to use Spinner project sample from Android SDK for next code examples.
Code:
First, you need to create you custom adapter which will intercept the creation of views in drop down list:
static class CustomArrayAdapter<T> extends ArrayAdapter<T>
{
public CustomArrayAdapter(Context ctx, T [] objects)
{
super(ctx, android.R.layout.simple_spinner_item, objects);
}
//other constructors
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
//we know that simple_spinner_item has android.R.id.text1 TextView:
/* if(isDroidX) {*/
TextView text = (TextView)view.findViewById(android.R.id.text1);
text.setTextColor(Color.RED);//choose your color :)
/*}*/
return view;
}
}
Then you create adapter in your code like this:
String [] spin_arry = getResources().getStringArray(R.array.Planets);
this.mAdapter = new CustomArrayAdapter<CharSequence>(this, spin_arry);
Explanation:
Because CustomArrayAdapter knows that we use android's built-in layout resource, it also knows that text will be placed in TextView with id android.R.id.text1. That's why it can intercept the creation of views in drop down list and change text color to whatever color is needed.
Screenshot:
Simple and Crisp ...
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
write a R.layout.simplespinneritem:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
The ID is android:id="#android:id/text1", set the color of font and background.
ArrayAdapter adapter =
new ArrayAdapter(this,packagename.R.layout.simple_spinner_item, spin_arry);
public class ee extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ww);
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
ArrayList<String> array = new ArrayList<String>();
array.add("item0");
Spinner spinner1;
ArrayAdapter<String> mAdapter;
spinner1= (Spinner) findViewById(R.id.spinner2);
spinner1= new ArrayAdapter<String>(this, R.layout.spinner_item, array);
spinner1.setAdapter(mAdapter);
}
}
and in xml res/layout add new xml file: type layout, spinner
(in spinner_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textColor="#00f0ff" />
To add to sasad's reply, make a copy of that file, which you can find in your Android folder, in your project, change the text color of the TextView in that file, and use that layout while initializing the Adapter instead of android's.
You could try this approach too wherein you add 2 new Layout Resource Files
Custom_spinner_list_item
Custom_spinner_dropdown_item
and use them in the code .
String spin_arry[] = new String[str_vec.size()];
str_vec.copyInto(spin_arry);
ArrayAdapter adapter =
new ArrayAdapter(this,R.layout.custom_simple_spinner_item, spin_arry);
adapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
custom_spinner_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fontFamily="#font/roman"
android:singleLine="true"
android:textAlignment="inherit"
android:textColor="#color/black"
android:textSize="14sp">
</TextView>
custom_spinner_dropdown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:fontFamily="#font/roman"
android:singleLine="true"
android:textAlignment="textStart"
android:textColor="#color/black"
android:textSize="14sp">
</TextView>
Happy Coding !! :)
make your own layout xml file, and give a android:textColor="#000" for black text
Here is more appropriate way guys,
First find the "simple_spinner_item.xml" file in your system,
Follow the below path,
C:\Users[username]\AppData\Local\Android\sdk\platforms[android-23]\data\res\layout
Now copy the content of "simple_spinner_item.xml" file
Second create the custom_spinner.xml file in your project res\layout folder
and paste the copied content in recently created file
Here is the sample:
res\layout\custom_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:textAlignment="inherit"
android:ellipsize="marquee"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:singleLine="true"
android:textColor="#color/dark_gray"
style="?android:attr/spinnerItemStyle"
android:id="#android:id/text1" xmlns:android="http://schemas.android.com/apk/res/android"/>
Here is the set adapter code:
Spinner ddlArea = (Spinner) findViewById(R.id.ddlArea);
ddlArea.setAdapter(new ArrayAdapter<String>(this, R.layout.custom_spinner, areaList));
Where areaList is the List
Thanks,
Ejaz Waquif