Android: Own Font in ListView - android

I'm trying to have my own font in a listview using Java in Android OS 2.3.x
After reading the following links, i'm stuck:
http://developer.android.com/guide/topics/ui/themes.html
http://stackoverflow.com/questions/4576441/custom-font-in-android-listview
http://androidforums.com/android-lounge/143874-custom-typface-listview-row-layout.html
i can't post something usefull here.
My main problems are:
Why can't i change the Font for a listview, using setTypeface?
Why can't i define a Font for ALL texts in my application without putting it in every activity again?
Is there a documentation, which handles this problems? The Android SDK documentation is lacking a huge amount of details, like at which version otf Fonts are working.
I know that i have to learn many things and i'm ready as much books about this topics as i can. But after two days guessing and trying around, i have to ask for help.
Propably someone can push me in the right direction ;)
To give you a small idea, i tried to clean up some code:
File: res/layout/scores.xml (snippet from it)
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:cacheColorHint="#00000000"
android:transcriptMode="disabled" >
</ListView>
File: src/notneededasinfo/ScoresActivity.java (snippet from it)
Resources myResources = getResources();
Typeface tf = Typeface.createFromAsset(myResources.getAssets(),"fonts/searstower.ttf");
TextView tv = (TextView) findViewById(android.R.id.list);
tv.setTypeface(tf);
Thx for help!

Well... first of all android:textStyle and setTypeface(Typeface) are not a property and function from ListView, it is from TextView.
Try to use this tutorial: http://developer.android.com/resources/tutorials/views/hello-listview.html
Anyway, you have to define a .xml file that defines the style for the itens in the ListView. At the link I posted before you can see on topic 4 the handler onCreate the following line:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
So, you have to define the layout for the ListView item on the file list_item.xml used above as R.layout.list_item. On this file, list_item.xml, you define new TextViews or EditTexts or whatever and changes the fonts individualy for each View.
Hope it helps you...

TextView tv = (TextView) findViewById(android.R.id.list);
You need to have a text view to set the font. You are missing the list row view.

I have a answer for one of your questions
Why can't i define a Font for ALL texts in my application without
putting it in every activity again?
This is a known problem but easy to solve. Just implement a custom activity class which extends Activity and overwrite the setContentView() method. In this Method you can get all your TextViews for your current view and set the Typface
See here: Add custom font for complete android application

Related

How to change Android spinner text color?

I know this question has been asked a lot (see: How to customize a Spinner in Android,
How to change layout of spinner in Android, How to change spinner text size and text color?, How to change spinner text color)
All the answers suggest to create a custom_spinner.xml file to accomplish this task. This file must be something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textColor="#color/#FFFFFF" />
However, no one says where to locate this file. In this answer https://stackoverflow.com/a/37859442/5616178 says this file must be in Drawable folder, but when I do that, an "Element TextView must be declared" error is raised by Android Studio. When I locate it in the layout folder, the R.layout won't resolve it either.
Thank you for your answers.
EDIT
I was able to solve the problem. As many of you said, the file must be located inside the res/layout since it's a layout resource. At first, my code looked like this:
citiesSpinner.setAdapter(
new ArrayAdapter<String>(SignUp.this,
android.R.layout.custom_spinner,
cityNames)
);
When I declared the adapter outside the constructor, i.e.:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
SignUp.this,
R.layout.custom_spinner,
cityNames);
citiesSpinner.setAdapter(adapter);
The R class was able to resolve the layout. I don't know why that happens, it would be useful if someone can explain it. Thank you again for your answers!
However, no one says where to locate this file.
That is a layout resource. It would go in res/layout/ by default.
When I locate it in the layout folder, the R.layout won't resolve it either.
Then you have some other problem. For example, perhaps there is a bug in your layout resource that is preventing the R class from being regenerated.
Your resource file must be in the layout folder
Your android:id="#android:id/text1" need to be android:id="#+id/text1"
Just declare your array like the links above recommended :
ArrayAdapter<String> adapter = new ArrayAdapter<String>this,R.layout.custom_spinner,yourArray);

How to change the color of list item using List Activity file

I have seen some links which am not able to trace it out.I am trying to change the color of the list item using java code in my activity class. Iam not using any Code for list view in my XML file. So, please Can any one tell me how to change the color of the list item from default white to black and here is my sample code .
ListView listView;
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "India", "Malaysia" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
TextView tv = new TextView(getApplicationContext());
tv.setText("Select Country");
listView = getListView();
listView.addHeaderView(tv);
listView.setCacheColorHint(Color.rgb(36, 33, 32));
listView.setBackgroundColor(Color.rgb(225, 243, 253));
this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names));
}
Thanks in advance
I don't think that changing the code in core Android components is a good idea. If you want to try something along the lines suggested by krishna5688, a better idea would probably be to better to create your own simple_expandable_list_item_1.xml (I would suggest giving it a more relevant name).
The SDK version of the layout file can be found in <your SDK folder>\platforms\android-<whichever version you're using>\data\res\simple_expandable_list_item_1.xml.
Put the contents into an xml file in your own res/layout folder, and make the changes there.
Then change the line of code
this.setListAdapter(new ArrayAdapter<String>(this,R.layout.<whatever you named your list item>,names));
Note that it's no longer android.R.layout...
If this is a change that you want to apply to the whole application (i.e. you generally want white backgrounds and black text for your UI widgets), then you're probably best changing the theme of the whole application in your manifest file to a 'Light' theme. Try adding this attribute to the application node in your manifest file:
android:theme="#android:style/Theme.Light"
Hope that helps!
You are using android.R.layout.simple_expandable_list_item_1.xml file as a ListView item. in xml code of android.R.layout.simple_expandable_list_item_1.xml you can set background tag in main Layout with some color as shown bellow.
android:background="#00FF44"
You can use any hexadecimal color code there.
I hope it will help you.

Android custom Preference title size comes out incorrect on some devices. What is the right place to pick it up?

This problem turned out to be much more tricky than it first appeared as it wasn't a problem on all but a few devices.
When creating a custom Preference by making it a subclass of Preference and also use a custom layout defined in an XML file in res/layout and use that layout in onCreateView() in the custom Preference it seems you completely loose the layout information the Preference-s normally have. Nothing strange with that, but if you still want a title (together with the new stuff), how do you specify the size of that title if you want it to be the same size as for other preferences? I was sure I had found the correct answer, setting textAppearance to textAppearanceLarge (either in the layout XML-file which seems like the better way or in the Java code in the Preference class) and this works fine on most devices.
This is from the custom Preference layout file:
<TextView
android:id="#+android:id/title"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
>
</TextView>
And this is from CustomPreference.onCreateView():
LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout)mInflater.inflate(R.layout.custompreference, parent, false);
// Setting up new stuff
return layout;
This works and I can change the size of the title to Small or Medium and I need no Java code for the title in the custom Preference class. textAppearanceLarge is 22dp which seems to be the size of the Preference titles on most devices.
But on some, for instance the HTC Sensation an Desire S the Preference title is smaller. The title of the custom Preference comes out too large on these devices. So, the layouts for non custom Preferences clearly don't use textAppearanceLarge but something else.
My question now is: What do they use? Where can I get it for use in my custom Preferece? I have searched the Internet like crazy but I simply fail to find the answer. All examples I can find use textAppearanceLarge (or simply 22dp), probably because nobody has ever noticed that it doesn't always work correctly.
Use android:textAppearance="?android:attr/textAppearanceListItem"

Create "Sub-Views" in Android (Newbie-Question)

sorry for the poor choice of words, I dont know exactly how the Android terminology is, therefore google wasn't much help either.
What I need to do seems simple: I have a table as part of my "main view" which is created when the activity launches. No problem here, I can get the table in code and e.g. add rows.
What I would like to do is "prepare" the row in a different XML file, basically creating the layout using XML and then creating an instance of that TableRow in code, adding it to my main table.
I know the alternative approach (to create the whole row in code), but this would be more cumbersome then using XML for most of the attributes, and only fill the ones I need later by code (label and image).
[UPDATE]
Ok, I managed to solve half of my problem using the ListActivity. There is one little problem remaining, I don't know how to tell Android to use more then one string.
The code looks like this:
public class ListScreenAlle extends ListActivity {
String[] listItems = {"exploring", "android", "list", "activities"};
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// We'll define a custom screen layout here (the one shown above), but
// typically, you could just use the standard ListActivity layout.
setContentView(R.layout.list_screen_layout_alle);
//setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems));
setListAdapter(new ArrayAdapter(this, R.layout.list_row_single, listItems));
}
}
And the XML used for the row looks like this:
<?xml version="1.0" encoding="utf-8"?><TextView android:id="#+id/text1" xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:textColor="#color/text_color_list_label"/>
How do I change this code to support a more complex object than a single string. Eg a person with a thumbnail and firstname, lastname.
The ArrayAdapter in its simple form only allows a single textfield, how would I explain to it that my row has 3 fields, an image, two strings, and that it should "BIND" the image to property Thumbnail, and the textfields to firstname and lastname?
Have a look at ListActivity and/or ListView.
edit regarding your update: use SimpleAdapter instead of ArrayAdapter
Example similar to what I understand you want to do:
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Another example that outputs two strings per row:
WANTED: TableLayout-Like ListView
EDIT: Ah, I see you found a solution.

Scrollable TextView in Android Widget

I would like to create an android widget with a scrollable textview.
The solutions given to this question
Making TextView scrollable on Android
cannot be applied because it is a widget:
1.
This
findViewById(R.id.textview).setMovementMethod(new MovementMethod());
does not work, since findViewById is not available in AppWidgetProvider but only in Activity.
2.Putting a ScrollView around the TextView also does not work, because I get an
InflateException:
android.view.InflateException: Binary XML file line #23: Error inflating class ScrollView
Can anybody give me a hint, how to make a TextView in a Widget scrollable?
I solved this problem by putting the text view inside ListView with single node, which is supported in App widget.
http://developer.android.com/guide/topics/appwidgets/index.html#collections
It looks like this is not possible.
More on this can be found here: http://code.google.com/p/android/issues/detail?id=9580
and here:
How to make a scrollable app widget?
So, probably it is possible to do make appwidgets scrollable in the HTC sense environment but not for normal android environments.
A way around this is to add buttons that go up and down in a list.
I have been working on this with two buttons which increment and decrement through an array. Just having trouble accessing the global variables. Did you make any headway on this?
Another solution is:
Add to textview any web link - for example: www.google.com.
Setting text value with HtmlCompat.fromHtml method:
textView.setText(HtmlCompat.fromHtml("some text" + "\nwww.google.com", HtmlCompat.FROM_HTML_MODE_COMPACT));
After that vertical scrollbar is appeared.
But it's not elegant and full solution. It's temporary workaround maybe...
The complete full solutiion is bat-el-g 's answer - with adding ListView.
Current marked solution (which just tell: "it's not possible") - is wrong.
In mainactivity.java:
after code text view:
TextView txt = (TextView) findViewById(R.id.textView id);
Enter this code:
txt.setMovementMethod(new ScrollingMovementMethod());
and you will be ok.

Categories

Resources