Set font for ListView, Android - java

I spent all day searching for an answer, found a lot, but all of them are not what I need.
So, I have a pretty simple menu with ListView and a file for items with TextView. I want to set Segoe-Print font for items in my menu, I know, that I need adapter, but I can't do it. Help!!!
menu.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/menu_top"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/menu_top">
</LinearLayout>
<ListView
android:id="#+id/ListView_Menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/fon_android"
android:divider="#drawable/menu_line"
android:textStyle="bold" >
</ListView>
</LinearLayout>
menu_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_prop"
android:layout_width="fill_parent"
android:textSize="19px"
android:text="test string"
android:layout_gravity="left"
android:layout_height="wrap_content"
android:shadowRadius="5"
android:textColor="#color/menu_color"
android:shadowDy="3"
android:shadowDx="3" />
A part of Main_Menu.java:
public class Main_menu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
String[] items = {
getResources().getString(R.string.system),
getResources().getString(R.string.convert),
getResources().getString(R.string.forks),
getResources().getString(R.string.margin)};
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.menu_item, items);
menuList.setAdapter(adapt);
}
CustomAdapter.java:
public class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
}
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.menu_prop);
Typeface font = Typeface.createFromAsset(getAssets(), "segoe_print.ttf");
textView.setTypeface(font);
return textView;
}
private AssetManager getAssets() {
// TODO Auto-generated method stub
return null;
}
}
The font doesn't change...

view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
.getContext().getAssets(), "fonts/DroidSerif.ttf"));

here is code for custom adapter:
private class CostumiseAdapter extends ArrayAdapter<item>{
public CostumiseAdapter(ArrayList<item> items) {
super(getActivity(), 0, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.scroll_fragment, null);
}
Item item = getItem(position);
TextView text = (TextView) convertView.findViewById(R.id.text_view);
text.setText(item.mString);
RadioButton b1 = (RadioButton) convertView.findViewById(R.id.radio_b1);
b1.setChecked(item.mB1);
RadioButton b2 = (RadioButton) convertView.findViewById(R.id.radio_b2);
b2.setChecked(item.mB2);
CheckBox cB = (CheckBox) convertView.findViewById(R.id.check_box);
cB.setChecked(item.mB2);
return convertView;
}
}
I've take it from http://androide-examples.blogspot.com/2013/11/android-scroll-list.html see for the full example.
In this example are some semantik mistakes. So you have to fix some items to Items (capitalise)...

You need to create a custom adapter for setting custom font. Here is the minimal code :
public class CustomAdapter extends ArrayAdapter<String> {
//Contructor here
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.myTextView);
// Here you set the typeface as you do for the TextView
textView.setTypeFace(....);
}
}
That should provide you the custom font for your Listview.

I found a desition!
Well, first of all I create another class for Adapter:
public class Typefaces {
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String name) {
synchronized (cache) {
if (!cache.containsKey(name)) {
String path = "fonts/"+name;
try {
Typeface t = Typeface.createFromAsset(c.getAssets(), path);
cache.put(name, t);
} catch (Exception e) {
e.printStackTrace();
}
}
return cache.get(name);
}
}
}
Then in the main Adapter I added two strings:
Typeface tf = Typefaces.get(getContext(), "tahoma.ttf");
holder.txtTitle.setTypeface(tf);
That's all! It's working! )) Thank you all for help and answers.

try in xml:
android:fontFamily="somefont.ttf"
From android 4.1+ the following font are available: http://robotofont.com/
android:fontFamily="sans-serif"
android:fontFamily="sans-serif-thin"
android:fontFamily="sans-serif-light"
android:fontFamily="sans-serif-condensed"
you can also set font programaticly:
TextView t = (TextView) findViewById(R.id.name);
Typeface font = Typeface.createFromAsset(getAssets(),"font/somefont.ttf");
t.setTypeface(font);
put the font file in a folder...

Related

Android: Set different layouts for Spinner title and dropdown

When my spinner positioned at the actionbar is clicked, it's supposed to present a dropdown with an array of items. Every viewholder for an item has a name and a photo.
However, I want the spinner's title to be another layout (text-only).
In short: How can I set a layout for the title spinner that differs from the dropdown layout?
Main.java
SpinnerAdapter spinnerAdapter = new SpinnerAdapter(this, dummyLists);
mMyListsSpinner.setAdapter(spinnerAdapter);
SpinnerAdapter.java
private class SpinnerAdapter extends BaseAdapter {
private String[] mListNames;
SpinnerAdapter(Context context, String[] listNames) {
this.mListNames = listNames;
}
#Override
public int getCount() {
if(this.mListNames == null) {
return 0;
}
return this.mListNames.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Context context = parent.getContext();
int spinnerItemViewLayout = R.layout.layout_spinner_dropdown_item;
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(spinnerItemViewLayout, parent, false);
TextView nameTextView = (TextView) view.findViewById(R.id.tv_spinner_list_name);
nameTextView.setText(this.mListNames[position]);
return view;
}
}
layout_spinner_dropdown_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearlayout_spinner_dropdown_item"
android:layout_width="222dp"
android:layout_height="#dimen/toolbar_actionbar_height"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_spinner_list_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="LIST"
android:textSize="#dimen/text_size_subheading"
android:textColor="#color/color_green_dark"/>
<ImageView
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="#color/color_green_dark"/>
</RelativeLayout>
This may be helpful: Android Spinner with different layouts for “drop down state” and “closed state”?
Basic points:
Rewrite getView() and getDropDownView() in your Adapter class [SpinnerAdapter]
In getView(), you should inflate normal layout for title bar, in your case it's a TextView only. In getDropDownView(), you should inflate your layout_spinner_dropdown_item.xml.

How can i create a custom ListView with my xml layout file?

Java code
list view layout #1
How can i insert change the graphics of my ListView adding this layout file to my list view? What should i write in my java code for making each row of my ListView just like my layout file. Thank you all guys !!
First of all you need to create the xml for the custom row you want(custom_row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="8dp"
android:id="#+id/text" /> </LinearLayout>
Then you need to create your custom adapter:
public class CustomAdapter extends BaseAdapter {
Context context;
List<String> textArray;
LayoutInflater inflater;
public RutinaAdapter(Context context, List<String> textarray) {
this.context = context;
inflater = LayoutInflater.from(context);
this.textArray = textarray;
}
#Override
public int getCount() {
return textArray.size();
}
#Override
public Object getItem(int position) {
return textArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup vg;
if (convertView != null) {
vg = (ViewGroup) convertView;
} else {
vg = (ViewGroup) inflater.inflate(R.layout.custom_row, null);
}
String text = textArray.get(position);
final TextView text = ((TextView) vg.findViewById(R.id.text));
return vg;
} }
Then you need to add the adapter to the ListView:
list = (ListView) view.findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(getContext(),textArray);
list.setAdapter(adapter1);
Add info to you textArray, and notify the adapter data changed, and thats it.

ListView inside a ListItem displays only one entry

Inside my original Activity, I initialize an Adapter to populate a ListView and that goes smoothly. ArrayList populated with all the data gets passed as an argument to the Adapter and everything goes well.
Now inside that Adapter, or that ListView that gets populated, there's another ListView and another Adapter for it.
Again, I follow the same procedure and populate the Adapter with an ArrayList, but it seems as though only the first entry gets displayed in the nested ListView. I assume this is is because the parent ListView gets populated and the data for the child ListView only populates for one parents entry.
Anyway, here's the adapter initialization and the Adapter class:
PlayerSeasonsStatsAdapter pssAdapter = new PlayerSeasonsStatsAdapter(getContext(), alPlayerSeasonsStats);
vh.lvPlayerSeasonsStats.setAdapter(pssAdapter);
alPlayerSeasonsStats is an ArrayList containing data to populate the child ListView.
Here's the child Adapter:
public class PlayerSeasonsStatsAdapter extends ArrayAdapter{
private ArrayList<PlayerStatsTeamModelSeasons> playerSeasons = new ArrayList<PlayerStatsTeamModelSeasons>();
private LayoutInflater mInflater;
public PlayerSeasonsStatsAdapter(Context context, ArrayList<PlayerStatsTeamModelSeasons> playerSeasons) {
super(context, 0, playerSeasons);
this.playerSeasons = playerSeasons;
mInflater = LayoutInflater.from(context);
}
#Override
public PlayerStatsTeamModelSeasons getItem(int position) {
return playerSeasons.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.player_seasons_stats_row, parent, false);
vh = new ViewHolder();
vh.tvPSSRCompetition = (TextView) convertView.findViewById(R.id.tvPSSRCompetition);
vh.tvPSSRMatchesPlayed = (TextView) convertView.findViewById(R.id.tvPSSRMatchesPlayed);
vh.tvPSSRGoalsScored = (TextView) convertView.findViewById(R.id.tvPSSRGoalsScored);
vh.tvPSSRAssists = (TextView) convertView.findViewById(R.id.tvPSSRAssists);
vh.tvPSSRYellowCards = (TextView) convertView.findViewById(R.id.tvPSSRYellowCards);
vh.tvPSSRRedCards = (TextView) convertView.findViewById(R.id.tvPSSRRedCards);
vh.ivPSSRCompetitionLogo = (ImageView) convertView.findViewById(R.id.ivPSSRCompetitionLogo);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
PlayerStatsTeamModelSeasons pstmsModel = getItem(position);
vh.tvPSSRCompetition.setText(pstmsModel.pstmSeasonModel.name);
vh.tvPSSRMatchesPlayed.setText(pstmsModel.pstmsModel.matchesPlayed);
vh.tvPSSRGoalsScored.setText(pstmsModel.pstmsModel.goalsScored);
vh.tvPSSRAssists.setText(pstmsModel.pstmsModel.assists);
vh.tvPSSRYellowCards.setText(pstmsModel.pstmsModel.yellowCards);
int totalRedCards = Integer.parseInt(pstmsModel.pstmsModel.yellowRedCards) + Integer.parseInt(pstmsModel.pstmsModel.redCards);
vh.tvPSSRRedCards.setText(totalRedCards + "");
//loadLogoToView(vh.ivPSSRCompetitionLogo, pstmsModel.pstmSeasonModel.id, true);
return convertView;
}
public static void loadLogoToView(ImageView iv, String teamId, boolean transform) {
String image = Constant.IMAGES_ROOT +
Constant.LOGO_PATH +
teamId +
Constant.LOGO_EXTENSION;
RequestCreator img = Picasso.with(iv.getContext())
.load(image);
if (transform) img.transform(new RoundedCornersTransform(2));
img.into(iv);
}
static class ViewHolder {
ImageView ivPSSRCompetitionLogo;
TextView tvPSSRCompetition;
TextView tvPSSRMatchesPlayed;
TextView tvPSSRGoalsScored;
TextView tvPSSRAssists;
TextView tvPSSRYellowCards;
TextView tvPSSRRedCards;
}
What are my options here?
Your Adapter should be look like
public class PlayerSeasonsStatsAdapter extends ArrayAdapter<PlayerStatsTeamModelSeasons> {
private class ViewHolder {
TextView tvPSSRCompetition;
}
public PlayerSeasonsStatsAdapter(Context context, List<PlayerStatsTeamModelSeasons> balanceModels) {
super(context, R.layout.row_account_balance, balanceModels);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
PlayerStatsTeamModelSeasons model = getItem(position);
PlayerSeasonsStatsAdapter.ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new PlayerSeasonsStatsAdapter.ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_account_balance, parent, false);
viewHolder.tvPSSRCompetition = (TextView) convertView.findViewById(R.id.tvPSSRCompetition);
convertView.setTag(viewHolder);
} else {
viewHolder = (PlayerSeasonsStatsAdapter.ViewHolder) convertView.getTag();
}
viewHolder.tvPSSRCompetition.setText(model.getPSSRCompetition());
if (position%2== 0) {
convertView.setBackgroundColor(Color.parseColor("#F5EEE5"));
}else{
convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
return convertView;
}
}
Just Call below like
PlayerSeasonsStatsAdapter adapter = new PlayerSeasonsStatsAdapter(MainActivity.this, balanceList);
listView.setAdapter(adapter);
Listview Look like bellow
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
here is row_account_balance layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvPSSRCompetition"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|center_vertical|center_horizontal"
android:text="#string/agent_statement_sl"
android:textStyle="bold"
android:paddingLeft="2dp"
android:textSize="16sp"
android:paddingBottom="5dp"
android:paddingTop="5dp"/>
</LinearLayout>

Why is the ListView empty?

I am trying to make a listView that contains only images without texts but it gives a white screen and I am not getting any error or anything.
Here is my codes:
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="3">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2">
<ListView
android:id="#+id/setOneListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</RelativeLayout>
.........
</LinearLayout>
</LinearLayout>
Activity:
public class Set1_Activity extends Activity {
ListView list;
int[] images = {
R.drawable.iv1, R.drawable.iv2, R.drawable.iv3, R.drawable.iv4,
R.drawable.iv5, R.drawable.iv6, R.drawable.iv11, R.drawable.iv12,
R.drawable.iv13, R.drawable.iv14, R.drawable.iv15, R.drawable.iv16,
R.drawable.iv111, R.drawable.iv112, R.drawable.iv113, R.drawable.iv114,
R.drawable.iv115, R.drawable.iv116, R.drawable.iv7, R.drawable.iv8,
R.drawable.iv9, R.drawable.iv19, R.drawable.iv49, R.drawable.iv50,
R.drawable.iv52, R.drawable.iv54
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
list = (ListView) findViewById(R.id.setOneListView);
HeartlessAdapter adapter = new HeartlessAdapter(this, images);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
switch(position){
case 0:
display.setImageResource(R.drawable.iv1);
tophone = R.drawable.iv1;
break;
.........
class HeartlessAdapter extends ArrayAdapter<String>
{
Context context;
int[] images;
HeartlessAdapter(Context c, int imgs[])
{
super(c, R.layout.imageview);
this.context=c;
this.images=imgs;
}
class MyViewHolder
{
ImageView myImage;
MyViewHolder(View v)
{
myImage = (ImageView) v.findViewById(R.id.imageView);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
MyViewHolder holder = null;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.imageview, parent, false);
holder = new MyViewHolder(row);
row.setTag(holder);
}
else
{
holder = (MyViewHolder) row.getTag();
}
holder.myImage.setImageResource(images[position]);
return row;
}
}
All works fine, no error and the emulator just show empty white screen nothing inside. What could be missing?
Thanks!
Try moving the android:layout_weight="2" up a level to the horizontal LinearLayout.
1) Let HeartlessAdapter extend ArrayAdapter<Integer> instead of ArrayAdapter<String>.
2) on your constructor, call super(c, R.layout.imageview, imgs); It should work like that.
3) If you wanna optimize it a lot, remove the references to Context and the array of images from the adapter. You can get any item you need calling getItem(position) and the Context by calling getContext().
EDIT:
In order for it to work, call this:
super(c, R.layout.imageview, HeartlessAdapter.toIntegerArray(imgs));
and add this method to HeartlessAdapter:
private static Integer[] toIntegerArray(int[] array){
Integer[] finalArray = new Integer[array.length];
for (int i=0; i<array.length; i++){
finalArray[i] = array[i];
}
return finalArray;
}
try this...
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged()
}
});

add unique images to spinner rows in android

I have managed to code to have images in each row of spinner but the images are all the same.I want to change each image. how do you do that. here is my xml and the code
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/calendar"/>
<TextView
android:id="#+id/weekofday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
here is java
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.row, R.id.weekofday, list);
dataAdapter.setDropDownViewResource(R.layout.row);
spinner.setAdapter(dataAdapter);
it displays the same image because it is set in the row.xml file. how do I make it dynamic?
Why don't you create Custom adapter and write the code accordingly. As far as I understand that , its showing same image because you have set it in row.xml. But how would adapter will know that the image should be changed on each row.
In Custom Adapter you can set the image as per the position (i.e. index) of Spinner Item
public class SavedSearchAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<SavedSearch> mData;
private boolean isEditMode = false;
public SavedSearchAdapter(Context context , ArrayList<SavedSearch> data){
mContext = context;
mData = data;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Object getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
TextView tvTitle;
ImageView ivDelete;
if(convertView == null){
view = View.inflate(mContext, R.layout.screen_saved_search_item_row, null);
}
else{
view = convertView;
}
tvTitle = (TextView) view.findViewById(R.id.screen_saved_search_item_row_tv);
tvTitle.setText(mData.get(position).name);
ivDelete = (ImageView) view.findViewById(R.id.screen_saved_search_item_row_iv_delete);
view.setTag(mData.get(position));
return view;
}
}

Categories