Setting external font for selected item of the spinner android - android

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.

Related

Change Spinner style programmatically

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.

Change the color of a specified item in a listview for android

I would like to change the text color of only one item in a listview.
This change will be triggered by the result of a running asynctask.
So far I searched on google and all I found was to overwrite the getView() function of the adapter, but this approach is kind of hard since I would need to keep the id of the rows I want to color in a global variable that will be accessed by getView().
Is there another way to simply set the text color of an item from a listview when an event happens ?
EDIT
I create the listview this way:
myListView = (ListView) findViewById(R.id.listView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow);
listAdapter.add("test");
myListView.setAdapter(listAdapter);
For setting a color for a list item definitely you need to override the getView() method of the Adapter. Here is a small example for updating the color of the list item without using the id of the item.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simplerow) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 == 0) { //Place the condition where you want to change the item color.
view.setBackgroundColor(Color.GRAY);
} else {
//Setting to default color.
view.setBackgroundColor(Color.WHITE);
}
return view;
}
};
In the above example, all the list item at even number positions will be in GREY color and others will be WHITE color. We cannot do this without implementing the getView(). For reference Click Here
You may set custom object vis color in adapter then change color in this adapter and call notifyDataSetChanged()

How to set style in spinner

How to set style on spinner ? I have style with font size and family for custom edit text class and I need same for spinner. I tried to add style tag in spinner xml but it is ignored, I tried in adapter but it doesn't work.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tempList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Typeface externalFont = Typeface.createFromAsset(getAssets(), "fonts/CANDARA.TTF");
((TextView) v).setTypeface(externalFont);
return v;
}
}
Instead of using the simple spinner layout android.R.layout.simple_spinner_item, create your own layout file containing a TextView and style it the way you'd like. Then, pass a reference to that layout when you instantiate the ArrayAdapter.
You are passing the default android layout for the spinner items, instead create your own layout like:
<RelativeLayout
android:id="#+id/rel"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtSpinnerItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
stlye="#style/txtViewStyleHere"/>
</RelativeLayout>
Pass this layout to the adapter, in that case, you will have to create your own adapter class to access the textView.
you want to add customize spinner
for that see these two link it might be help you .
Link 1 normal as you want just changing text and all that,
and for hover effect on your spinner look this link . Link2

How to set the font inside the spinner in android

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.

How to change the text Size of Spinner run time

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

Categories

Resources