How to set the font inside the spinner in android - 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.

Related

Setting external font for selected item of the spinner 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.

android subscript with a custom adapter and listview

I was wondering if it is possible to add subscript values to listview using a custom adapter. My adapter is expecting two List<String> and I'm using this method to populate my ListView. I looked around and a lot of people are recommending the
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));
...but, I do not want the subscript applied to any additional list item that I add. I would only like it added to specific items/instance.
Fields.add("My Sample\nMy Sample\nMy Sample");
Values.add("Value\nValue\nValue");
For example I'd like to be able to pick and choose when it is applied:
Is this possible?
Rows in list view are associated with data in the adapter. To control the content of each row like applying subscript, you need to add necessary information in the data. So you cannot do it if your adapter uses solely List<String>. But if you change it to say a list of Item where Item is
class Item {
String title;
String subtitle;
}
and use List<Item> instead in the adapter. You can apply a subscript given subtitle is null or not in the adapter's getView
class MyAdapter extends ArrayAdapter<Item>{
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// assuming proper view holder pattern is applied here
Item = getItem(position);
if (item.subtitle != null){
//apply subscript state
}else{
//un-apply subscript state
}
return view;
}
}

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 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

How can I create icons for menu items in Android's ListView?

I am using a ListView to display the main screen of my application.
The main screen is essentially a menu to get into the different sections of application. Currently, I have the ListView whose contents are added programmatically in the onCreate method.
Here is the code snippet that does this:
String[] mainItems = {
"Inbox", "Projects", "Contexts", "Next Actions"
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, mainItems));
registerForContextMenu(getListView());
}
So the menu is essentially just a bunch of nodes with the text contained in the mainItems array. I know that I can create an XML layout (i.e. R.layout.mainMenu_item) that has an ImageView and TextView in it, but I am unsure how to set the ImageView's icon. I have seen that there is a setImageResouce(int resId) method, but the way to use this when generating with an ArrayAdapter is eluding me. Is there a better way to do this?
What I typically do for a ListView is to implement my own Adapter by extending the handy BaseAdapter class. One of the abstract methods you'll implement will be getView() as the previous poster mentioned. From there you can inflate a layout containing an ImageView, get a reference to it using findViewById, and set the image to whatever drawable you've added into your resources.
public View getView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.menu_row, null);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
icon.setImageResource(..your drawable's id...);
return view;
}
From the google docs for ArrayAdapter.
To use something other than TextViews
for the array display, for instance,
ImageViews, or to have some of data
besides toString() results fill the
views, override getView(int, View,
ViewGroup) to return the type of view
you want.

Categories

Resources