I have a android spinner. I want to change the size of the spinner text (selected text from the dropdown) run time. I have a button on whose onCLick event I want to do the size change of few things. All other texts are changing by setTextSize() but there are issues with spinner. I have taken the reference of the textview used in getView() of my custom spinner adapter. I apply setTextSize() on it. The first Time it changes the size of he spinner but by again pressing the button ever thing (other textViews on the screen) change their size but not the spinner. Please help..
Regards,
Krishna
try this code
Spinner sp_state = new Spinner(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
sp_state.setLayoutParams(lp);
ArrayAdapter<String> state_Adapter= new ArrayAdapter<String>(getParent(),android.R.layout.simple_spinner_item,ARRLIST_STATE){
public View getView(int position, View convertView,ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(12);
((TextView) v).setTextColor(Color.WHITE);
return v;
}
};
state_Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_state.setAdapter(driver_state_Adapter);
If you are using customize Adapter.Then you have to use one Base Adapter class like in List View.The xml file you inflate, can be used as you want.You can increase size there
Edited
Refer to this link for more details
Related
I have set a text view to a array adapter in order to show the selected item of the spinner. This is the code for the array adapter.
ArrayAdapter<CharSequence> adapterDitrict = ArrayAdapter.createFromResource(this, R.array.district_array,
R.layout.spinner_item);
adapterDitrict.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDitrict.setAdapter(adapterDitrict);`
`
Here R.layout.spinner_item is the text box used to display the selected item of the spinner. Note that this text view is not in the layout of the activity. Now I want to apply a external font to this text box. But I can't get a reference to this text view using findviewbyid since it is not in the activities layout. I am using setTypeface to set the external font. So how to show the selected item of the spinner is the text view using a external font. Please help.
You can achieve this by 2 ways, first one is either take custom adapter and manage your view, and second one override your getView method for this current adapater only.
For First option of custom adapter folow below link
http://androidexample.com/Custom_Spinner_With_Image_And_Text_-_Android_Example/index.php?view=article_discription&aid=84&aaid=107
For second option check my answer below
ArrayAdapter<CharSequence> adapterDitrict = ArrayAdapter
.createFromResource(this, R.array.district_array,
R.layout.spinner_item)
{
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = super.getView(position, convertView, parent);
((TextView) v).setTypeFace(Your Tyope Face);
return v;
}
};
Check both answer and use any one as per your requirement.
I am trying to figure out how I can change the text color of android.R.layout.simple_list_item_multiple_choice
I know you can create your own listview with checkboxes but I really do not want to have to do that as the android.R.layout.simple_list_item_multiple_choice works perfectly, I would just like to know how to change the Text color.
I have come accross this question here but I do not understand how I can use the most voted up answer.
How can I override android.R.layout.simple_list_item_multiple_choice textview so I can change the color of it. Thank you.
You can change color easily.
You just have to override the getView() method.
And inflate the android.R.layout.simple_list_item_multiple_choice in particular view. now you can access the whole template.
So that you can get the TextView used in this template by finding using id android.R.id.text1. after that you can change the behavior of textview.
ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, your_data_container) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(InvitationFlowActivity.this).inflate(android.R.layout.simple_list_item_multiple_choice, null);
TextView tv = (TextView) v.findViewById(android.R.id.text1);
tv.setTextColor("Your_color");
}
};
And if you are using custome adapter then you can also override the same getView() method and you can customize it as per your requirement.
Just make custom layout - simple_list_item_multiple_choice.xml like this -
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF0000"
/>
and use it in place of android.R.layout.simple_list_item_multiple_choice.xml
There are two ways to do this.
1. Override the getView method
2. Use custom layout instead of standard layout.
No. 2 is better as you have more control on the same and you can edit at your ease.
Alt+Click will clone the required layout to your res/layout folder.
Make changes to the new layout and use it in the adapter.
ArrayAdapter aa = new ArrayAdapter(getApplicationContext(),R.layout.text_change,listItems);
listView.setAdapter(aa);
Here listItems is the String array of list.
text_change.xml is the updated layout xml
listView is your listview in the main layout.
In order to dynamically update color of content use the following code
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0)
{
//Intent intent = new Intent(CoffeeShop.this,DrinkCategoryActivity.class);
//startActivity(intent);
TextView txtV = (TextView)view;
txtV.setTextColor(Color.BLUE);
}
else
{
TextView txtV = (TextView)view;
txtV.setTextColor(Color.YELLOW);
Toast toast = Toast.makeText(getApplicationContext(),(CharSequence)"These Methods are yet to be made public", Toast.LENGTH_SHORT);
toast.show();
}
}
};
ListView listView = (ListView)findViewById(R.id.list_opt);
listView.setOnItemClickListener(itemClickListener);
Adding onclicklistner for the view will allow updating the color of the text upon clicking. You can add respective logic as to which color should the text be displayed.
I need to implement function of changing the application theme up on button click.
I have already changed successfully style of buttons, edittexts and textviews. But faced the problem of changing style for Spinner.
The only successful change is change of background color:
spinnerRoutes.setBackgroundResource(R.drawable.dark_theme_spinner_background);
BUT only background of title row changed. I need also to change textColor of rows and items, and background of spinner rows.
I have already tried alot of similar stackoverflow solutions but none of them worked.
Is it even possible to change style of spinner programatically?
If u want to give customize the drop down menu of the spinner u need to override the getView function in adapter class. And also if u want to control the text that appears on the Spinnner u need to this spinner.onItemSelectedListener() and modify the text view in the call back method
sample code for customizing spinner drop down menu:
#Override
public View getDropDownView (int position, View convertView, ViewGroup parent) {
/**The Adapter's view to be supplied for the spinner when constructing a spinner. */
View view = super.getView(position, convertView, parent);
TextView listView = (TextView) view.findViewById(android.R.id.text1);
listView .setBackgroundColor(Color.parseColor(fontBack_BgnColor));
listView .setHeight((int) heightPixels);
listView .setGravity(Gravity.CENTER);
listView .setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
listView .setTextColor(Color.parseColor(fontColor));
listView .setText(super.getItem(position));
return view;
}
Customizing spinner text(text that appears on top of the spinner):
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, 25);
((TextView) view).setTextColor(Color.parseColor(spinnerTextColor));
}
The other guys are right when it comes to fully customizing the items in the spinner.
But if the only thing you need to change is the style of the text in the items (meaning you still want a text view but with say different font, color, gravity) then you only need to pass the adapter a custom item layout:
This is how your adapter should look like:
spinner.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.custom_item, data)
where custom_item is an xml layout file that you create as follow:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center"
android:textColor="#android:color/red"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
Just make sure that the text view is the one and only view in that xml, and that its id is as specified above, so the default adapter can recognize it.
Hope that helped.
I want to show the custom font In my Application. I want to show the custom font inside the spinner. For that I have used the custom adapter for creating the spinner.
I am getting the output that I want when spinner opens And I am getting the custom fonts. But when spinner close that time word is not readable font is not setting.
Image1: Inside the spinner the word is not readable.![enter image description here][1]
Image2: when spinner open the word is readable:
![enter image description here][2]
I have used the
**Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/kalpurush.ttf");**
**textViewlistview.setTypeface(tf);**
But how should I set the font inside the spinner. when settext inside the spinner.
[1]: http://i.stack.imgur.com/i02XA.png
[2]: http://i.stack.imgur.com/Vkydk.png
Below is the adapter that I am using the spinner
private class CustomAdapterSpinner extends ArrayAdapter
{
public CustomAdapterSpinner() {
//super(context, android.R.layout.simple_spinner_item, objects);
super(CustomizeFontsAndroid.this, R.layout.simple_spinner1,R.id.textviewSpinner,strarraySpinner);
// TODO Auto-generated constructor stub
tf1=Typeface.createFromAsset(CustomizeFontsAndroid.this.getAssets(), "fonts/kalpurush.ttf");
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textviewSpinner = (TextView)view.findViewById(R.id.textviewSpinner);
textviewSpinner.setTypeface(tf1);
textviewSpinner.setCompoundDrawables(null, null, null, null);
textviewSpinner.setTextSize(16);
textviewSpinner.setText(strarraySpinner[position].toString()+"\n");
//textviewSpinner.setText(R.string.bangla);
return view;
}
}//end of the Spinner Adapter
You need to be using an adapter to create the views for the spinner. Even if the view the adapter creates is just a TextView. Doing that will let you call setTypeface() on the TextView.
I read your comment, but you need to do this in the getView() method of your Adapter. Make sure your Adapter implements SpinnerAdapter. SpinnerAdapter has two methods that return views, getView() and getDropDownView().
getView() returns the view that is used for display purposes, i.e. when the popup for the spinner is not displayed (the one that is not using the right font for you).
getDropDownView() returns the view that is used in the list of items for the spinner.
Check out the reference for SpinnerAdapter here.
I have a ListView powered by a custom adapter that dynamically changes the background color of each row based on its contents. The getView looks like this:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
Power power = powers.get(position);
if(row == null)
{
row = getLayoutInflater().inflate(android.R.layout.simple_list_item_2, parent, false);
}
TextView text1 = (TextView)row.findViewById(android.R.id.text1);
TextView text2 = (TextView)row.findViewById(android.R.id.text2);
text1.setText(power.name);
text2.setText(power.getAttackLine());
row.setBackgroundColor(0xFF0000FF);
return row;
}
All that works peachy, except that whenever I select an item from the list, the standard red/orange highlight only shows up behind the row, only visible for a few pixels on each side of it. Is there a way to get the selection to show up on top of the background?
Try android:drawSelectorOnTop="true" in the <ListView> element in your layout.
I think the problem is that you're using simple_list_item_2, which has some padding/margin built into it. I recommend making your own version of simple_list_item_2, wherein you have both the height and the width set to fill_parent. You can see the latest simple_list_item_2 here.