settype of arrayadapter android - android

Here is my code how to set my spinner and I want to set a custom font on its items My class is extends Activity and my partial code is like this
Spinner supportSpinner = (Spinner) findViewById(R.id.supportSpinner);
ArrayAdapter supportAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item,supports);
supportSpinner.setAdapter(supportAdapter);
btw supports is a string arraylist of my items for my spinner
I want to do such a thing but it doesnt allow
Typeface type = Typeface.createFromAsset(getAssets(), "comic.ttf");
supportSpinner.setTypeface(type);
how can I change my spinners textviews typeface?

Instead of using
android.R.layout.simple_spinner_item
Create your own layout, you then will use your current code
Typeface type = Typeface.createFromAsset(getAssets(), "comic.ttf");
supportSpinner.setTypeface(type);
but set it on the textview in your custom layout for each spinner item

instead of using android.R.layout.simple_spinner_item create your own layout, and inside XML layout editor there's the option for the TypeFace you want. No need to do any code.

for array adapters::
Typeface typeNormal = Typeface.createFromAsset(getAssets(), "roboto_lite.ttf");
timearray = new ArrayAdapter<String>(DetailsActivity.this,R.layout.floorrow,R.id.txt, flor){
public View getView(int pos, View convertView, android.view.ViewGroup parent) {
View v = convertView;
if(v== null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.floorrow, null);
}
TextView tv = (TextView)v.findViewById(R.id.txt);
tv.setText(flor.get(pos));
tv.setTypeface(typeNormal);
return v;
};
};
lv_building.setAdapter(timearray);

Related

Losing Reference to Assigned View

So I am extending the ListPreference in order to achieve the effect I need.
I would like to have a titleView, a messageView, and then the listView in my dialog.
I override the onCreateDialogView() method in order to do this and am able to display the messageView successfully by creating a textview and adding it to the view with this code:
#Override
protected View onCreateDialogView() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView lv = (ListView) inflater.inflate(R.layout.dialog_layout, null);
_messageView= (TextView) inflater.inflate(R.layout.dialog_header, null);
_messageView.setText("Hello World");
lv.addHeaderView(_messageView);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), R.layout.dialog_list_choice, getEntries());
lv.setAdapter(adapter);
return lv;
}
Now when I assign the created TextView the variable _messageView and set the text it works fine.
However, when I click the preference, I want the dialog's messageView text to change.
I do so with the following code:
public void showWithCode(String code){
if (_messageView != null) {
_messageView.setText(code);
}
showDialog(null);
}
The Dialog displays, however _messageView is always null so I cannot have my message reflect my actions!
So my question is, how is it that I assigned it earlier yet it is null when I need it?

Inflate two layouts in getView() to implement ListView Section header

I'm trying to create section headers for my ListView in a way that some list rows have headers and some do not. So what i have is three xml layout files. fragment_listview.xml contains the <ListView>
row_listview.xml contains the content for ListView rows
header_listview.xml is for the section headers.
Feeds.java is my model class.
So in my adapter i have something like this:
public class FeedsArrayAdapter extends ArrayAdapter<Feeds> {
private Context context;
private List<Feeds> feeds;
private LayoutInflater vi;
FeedsArrayAdapter(Context c, List<Feed> ff){ //initializes all the class member fields }
public void getView(int position, View convertView, ViewGroup parent){
Feeds f = feeds.get(position);
if(f.headerNeeded()){
convertView = vi.inflate(R.layout.header_listview, null);
TextView textViewHeader = (TextView) convertView.findViewById(R.id.feed_header);
textViewHeader.setText(new SimpleDateFormat("dd/MM/yyyy").format(feed.date));
}
else {
convertView = vi.inflate(R.layout.row_listview, null);
TextView textViewfeedHeading = (TextView) convertView.findViewById(R.id.feed_heading);
TextView textViewfeedSubheading = (TextView) convertView.findViewById(R.id.feed_subheading);
TextView textViewfeedDate = (TextView) convertView.findViewById(R.id.textViewDate);
ImageView imageViewfeedIcon = (ImageView) convertView.findViewById(R.id.feed_icon);
ImageView imageViewfeedBanner = (ImageView) convertView.findViewById(R.id.feed_banner);
textViewfeedHeading.setText(feed.title);
textViewfeedSubheading.setText(feed.subtitle);
textViewfeedDate.setText(new SimpleDateFormat("dd/MM/yyyy").format(feed.date));
}
return convertView;
}
So this gives us something like this:
---header1----
---header2----
___Row 3_____
___Row 4_____
What i want is:
-----header 1------
_____Row 1_______
-----header 2------
_____Row 2_______
_____Row 3_______
_____Row 4_______
If i try to inflate row.listview in if(true) I get an exception.
Any approach how to resolve this.
you can use Sticky Header Library here
yourListView.addHeaderView(yourHeaderView);
See http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29
use expandable listview with BaseExpandableAdapter

Android: Use custom font with Custom Adapter using SQLite

How can I input a text with a custom font, put it to SQLite and then display it in a textview?
I can set font for the EditText and TextView, but I want to get the Typeface from the EditText and put it into the TextView (in my case a text in a ListView)
You can assume my app as a chat application with custom font.
Thanks
OK first, the font doesn't have anything to do with the database. So I'm gonna help you here by pointing you in the right direction:
Here's a tutorial on how to save to a database: http://www.vogella.com/tutorials/AndroidSQLite/article.html (Read it, it's a good one).
And besides, once you want to display your data into a TextView THEN you should set a font to said TextView: Android - Using Custom Font
From what you're describing I'm imagining a ListView which represents a chat thread/log. Each item in the ListView (in this case a TextView) represents a single message. Each message in the chat thread can have a custom font. You want to persist the message's font type to the database. Essentially, you want to change a TextView's font using a custom adapter.
To do this, I would create a Message object. This Message object would have fields (i.e. variables) on it like MessageContent, MessageFont etc. You could then persist this object to your database. Upon retrieval from the database, you could then use a custom adapter to assign the font to your TextView.
public class MessageCursorAdapter extends CursorAdapter {
private Cursor messageCursor;
private Context context;
private final LayoutInflater inflater;
public MessageCursorAdapter(Context context, Cursor cursor) {
super(context, cursor);
this.inflater = LayoutInflater.from(context);
this.context = context;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView messageTextView = (TextView) view.findViewById(R.id.message_item_text);
String messageFont = cursor.getString(cursor.getColumnIndex("name_of_database_column"));
if (messageFont.equals("Epimodem")) {
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/epimodem.ttf");
messageTextView.setTypeface(face);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = this.inflater.inflate(R.layout.message_item, parent, false);
return view;
}
}
In any Adapter Class that you Extend to populate the data you get a Function named getView (int position, View convertView, ViewGroup parent). What you want can be done here.
What you need to do is to create a Class that extends an adapter and then inflate the view that you are going to return and want to be displayed and then on the TextView set the TypeFaces.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Set the TypeFace Here
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
textView.setTypeface(font);
return rowView;
}

Android Custom Spinner not Populating

Having trouble getting my spinner to populate. Well, I was getting it to populate just fine with a string array from strings.xml but now I want to add a custom font to the array. I'm using a Custom ArrayAdapter for the first time and it is overwriting my "android:entries=" and "android:prompt=" inside its layout xml.
So, I need to populate this spinner with an array from my strings.xml
(Inside onCreate)
spinner1 = (Spinner) findViewById(R.id.distancespinner);
MyArrayAdapter ma = new MyArrayAdapter(this, R.layout.my_spinner_style);
spinner1.setAdapter(ma);
Then:
private class MyArrayAdapter extends ArrayAdapter {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.TTF");
public MyArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public TextView getView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(font);
return v;
}
public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(font);
return v;
}
}
I tried adding the entries and prompt again inside the my_spinner_style.xml...
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/location_arrays"
android:prompt="#string/location_prompt"
android:singleLine="True" />
But, that is not working either.
Maybe this is a little late. After I implement your code and some additional test, I think I finally get the answer. The KEY problem the spinner not populating is:
You set the entries of the spinner within the xml file but replace it programmatically with an EMPTY adapter.
I have tried the following ways to populate the spinner with different results:
Set the entries in xml without any adapter.
<Spinner
android:id="#+id/simple_spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/simpleStringArray"
android:prompt="#string/simplePrompt"/>
It populates the spinner with right content without any additional style.
Set the entries in xml with another adapter set to the spinner (as you did in the question)
in .xml
<Spinner
android:id="#+id/simple_spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/simpleStringArray"
android:prompt="#string/simplePrompt"/>
in .java:
Spinner mySpinner2 = (Spinner) findViewById(R.id.simple_spinner2);
MySpinnerAdapter2 spinnerAdapter2 = new MySpinnerAdapter2(this, R.layout.simple_list_item);
It displays an empty spinner.
set the data programmatically to adapter
in .xml
in .java
setContentView(R.layout.activity_spinner);
Spinner mySpinner2 = (Spinner) findViewById(R.id.simple_spinner2);
MySpinnerAdapter2 spinnerAdapter2 = new MySpinnerAdapter2(this, R.layout.simple_list_item);
spinnerAdapter2.addAll(items2);
private class MySpinnerAdapter2 extends ArrayAdapter {
public MySpinnerAdapter2(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTextColor(Color.BLUE);
return v;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/Igualb.ttf");
TextView v = (TextView) super.getDropDownView(position, convertView, parent);
v.setTextColor(Color.RED);
v.setTypeface(myFont);
return v;
}
It displays the content correctly with cumtom-set font.
You can check for the complete project in this github repository if you like to.
Wish this helps.
That's the code I rewrote and it's working fine:
Spinner spinner = (Spinner) findViewById(R.id.distancespinner);
spinner.setPromptId(R.string.location_prompt);
MyArrayAdapter ma = new MyArrayAdapter(this, R.layout.my_spinner_style);
ma.addAll(Arrays.asList(getResources().getStringArray(
R.array.location_arrays)));
spinner.setAdapter(ma);
And the XML
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="True" />
Adapter is the same.
May be the below line is incorrect
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.TTF");
m01.TTF is wrong change to m01.ttf
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.ttf");

Android Listview font style

I am new in android. In android listview i want to change the font in my own style. Please reply. thanks in advance step by step how to change the font in list view.
in xml....
Typeface typeBold = Typeface.createFromAsset(getAssets(),"fonts/helveticabold.ttf");
Typeface typeNormal = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");
More information try the following links
example1
example2
In android listview i want to change the font in my own style.
By this, I suppose you want to change the font in the child view that is being displayed in the list. For that you need to set the typeface of the TextView inside the getView() as follows
First initialize the font in the constructor of the adapter maybe as follows
private Typeface typeFace;
public MyContructor(Context context)
{
super(context);
mInflater = LayoutInflater.from(mContext);
typeFace=Typeface.createFromAsset(mContext.getAssets(), "Fonts/GrinchedRegular.ttf"));
}
and then in the getView()
#Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.sample, null);
}
myText = (TextView) convertView.findViewById(R.id.my_text);
myText.setTypeface(typeFace);
return convertView;
}
First add your font file in your assets folder and use this code
Typeface arial = Typeface.createFromAsset(getAssets(), "fonts/arial.ttf");
name_txt.setTypeface(arial);
Use Customized List -
To change to a different built-in font, use android:typeface in List Item XML
or setTypeface() in getView of ArrayAdopter.
public class CustomeArrayAdopter extends ArrayAdapter<String> {
int res;
Typeface tf;
public CustomeArrayAdopter(Context ctx, int resource,
List<String> items) {
super(ctx, res,items);
res=resource;
tf=Typeface.createFromAsset(ctx.getAssets(),"font/Arial.ttf");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Apply new TypeFace here
TextView item_text=(TextView)findViewById(R.id.listItemtv);
item_text.setTypeface(tf);
.....
}
_ }

Categories

Resources