I'm trying to create a simple ListView. the items shown on the ListView are just strings from an array i have. Only that i want just one of the strings to be shown in bold. At first when i used a long TextView that contained all of the strings with line breaks tags inside a ScrollView, i could simply use
<b> </b>
around the string i wanted to be shown bold on the gui, this way:
TextView tv.setText(Html.fromHtml(str));
But using a big TextView inside a ScrollView caused other problems, and seemed a bad option when you have ListView exactly for this matter. So now I use a ListView, that gets all the strings from an ArrayList and displays them. Only that however I try to turn this around, I can't seem to change the style of one item into Bold. I also tried creating a TextView for each of the strings, and than add the TextViews to the List, but I don't know if ArrayAdapter or SimpleAdapter can do this. SimpleAdapter only seems to be able to get the TextView as a resource ID, which is the same for all items in the list, which prevents me from achieving what i want.
Long story short: after researching this issue here I know there are ways of extending BaseAdapter for a custom Adapter that allows any custom layout for the list, but is there really no other way? Do i really need to implement a BaseAdapter and #override getItem only to make one item bold? This seems odd to me. Does anyone know of a way I can just add TextViews to the ListView? using an ArrayAdapter displays the TextViews toString :/
If you know another simple way to show just one bold item, I'll really appriciate any help. Thanks!
Alright, I have finally decided to solve this by making a custom adapter which extends ArrayAdapter. There are many examples out there, but some of them provide a lot of extras and over complicate this issue in my opinion, and I just wanted to solve this decently. I found this example extremely useful and friendly, in my research here i saw a lot of developers asking similar questions, so I really hope this helps you too.
An adapter that extends ArrayAdapter isn't too complicated, but moreover allows you to have it exactly your way with the layout in a fairly simple way. After the adapter is done, you can just add html tags to your specific strings in your source array, and when you bind them on the Adapter, use Html.fromHtml . This is how I solved it. If anybody knows if there's a way to make bold or colored text in different specific items on a ListView without the need to make this adjustment yourself (extending ArrayAdapter and overriding getItem()) please do share it with us.
I hope this helps others.
Overwrite the getView(...) of your ArrayAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
View returnedView = super.getView(position, convertView, parent);
if (position == specialPosition) {
TextView text = (TextView) returnedView.findViewById(R.id.text);
text.setTypeface(null, Typeface.BOLD);
}
return returnedView;
}
Inside the getView function, make a reference to the TextView like:
text = (TextView) convertView.findViewById(R.id.textBox);
and then:
text.setTypeface(null, Typeface.BOLD);
and you're done.
Related
Shockingly there doesn't seem to be any questions that successfully answer this. I am trying to change the font in the AutoCompleteTextView. How can I accomplish this? I'm assuming this would involve creating a custom class that extends AutoCompleteTextView but how would I do this specifically?
You can implement an Adapter and use the setAdapter(...) method to apply it. Adapters are used to serve up and cache views for efficient scrolling in Android. The AutoCompleteTextView will call the getView(...) method on your Adapter giving it the position that it wants. You create or modify a view with the content at that position (in this case the text) and return it.
Here's a good example article of it:
http://www.codeofaninja.com/2013/12/android-autocompletetextview-custom-arrayadapter-sqlite.html
You might just want to scroll to the adapter part.
You are showing the drop-down list. You control the font yourself when your code creates your rows in your Adapter implementation used for suggestions. Override getView() and adjust the font in your TextViews in the row layout.
I have several ListActivities, in which I need to set up, for each row, cusom typeface I am loading from assets. In TextView this is not a problem, but I am facing problem of accessing my list items. ListActivity seems to not have method *.setTypeface(), so I have come to the dead end. Anyone had faced same problem before?
How are you populating your ListView? Most likely you are using one of the generic ListAdapter implementations which seem to only provide bare-bones functionality and not much else.
My suggestion would be to design your own View to use in your ListView along with your own ArrayAdapter implementation that configures type face etc. from within it's getView(...) method. You'll find quite a few tutorials on the subject if you google "custom android ArrayAdapter".
You do not set the type face on the listview. You need to set the typeface on a textview. So use a custom array adapter for your listview, inflate your own custom view which would contain a textview, and then set the typeface on that text view.
To go along with what Nick said, google "custom android arrayadapter"
Does anyone have a good tutorial for implementing onItemClick() for a ListView populated by a custom ArrayAdapter?
I'm struggling with how Android is recycling views, and how to affect the layout of the selected view (which is a RelativeLayout). The way I've implemented it, I'm just doing a simple background colour change, but everytime I choose a row, when I scroll the ListView there are other off screen items selected too.
Any ideas?
Have a look to this answer. Custom code of ArrayAdapter.getView() (where view recycling is done) might be useful to you, I was trying to achieve something similar and it worked for me at last :)
You can find lots of good examples of the custom Listview with Adapters.Some of the Examples are listed below:
(1)custom-listview-android
(2)android-custom-listview-with-image-and-text
(3)Custom Array Adapter
I want to create list view just like this: http://dl.maximumpc.com/galleries/androidpower/Alarm_full.jpg
I need to create view like on second and fourth screens. As for fourth screen, it seems that each row has it's own layout...
I've searched Internet and even downloaded alarm source code from git repository, but it doesn't contains what I want. Any help would be useful. Thanks!
The second screen is pretty straightforward. It's basically a RelativeLayout with a full-width button at the top and a listview. The listview items will use a custom layout e.g. a RelativeLayout with a Button showing the time, a TextView to show the description, a TextView to show the selected days and a CheckBox to indicate selection. The fourth screen looks like a preference activity which can be built up from an xml file and/or custom preferences.
Yes, as John J Smith said, it's not very complicated. And i'm sure there are many articles on Internet about how to build a custom list view(at least there're many in China).
And here is a general way to do this:
To custom a list view, follow these ways in general:
1. create your own list adapter, usually extends BaseAdapter, write getView method etc;
2. bind your adapter to a list view;
3. write a layout file implements your list view item, and bind data in your adapter.
Especially, if your custom list view item has a button/checkbox/etc, you'll need more work.
My scenario is that I have a activity which shows the cursor stored in SQLite DB. The main layout contains textview at top and a listview. And then I use simplecursoradapter to populate cursor into listadapter and put listadapter into listview. simplecursoradapter use another layout. The problem now is that when I use simplecursoradapter I bring three columns into listview, example: item name, date and price. That is ok if I don't change these values.
Actually I want to add some string to price and form new string such as currency sign. According to my understanding we only can setContentView for one layout not two layouts.
I also tried to populate a new layout and set value but failed
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View textEntryView = inflater.inflate(R.layout.itemlist, null);
TextView price = (TextView) textEntryView.findViewById(R.id.price);
price.setText(currency + c.getString(4).toString());
Even there is no problem in syntax and run apps. But when I run the app and check listview, the price still show price only without adding currency sign. I only can add currency sign under the main layout not the second layout used in simplecursoradapter.
In fact, currency is chosen in user preference, and I use sharedpreference to retrieve its value and add to price value in cursor. It seems that simplecursoradapter is using different layout, so cannot do that.
Does anyone has ideas about this case ?
I would be appreciated if methods and codes are provided for similar approach.
Thanks !!
I'm not sure I follow your question 100%, so this might not be exactly the answer you're looking for.
If you want to have control over how the layout of items look in a list view, you should make your own custom implementation of ArrayAdapter:
A ListAdapter that manages a ListView
backed by an array of arbitrary
objects. (...) 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.
If you just google for custom arrayadapter example, you should find enough examples showing you how to implement this. Two such examples are:
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
http://android-er.blogspot.com/2010/06/custom-arrayadapter-with-with-different.html
Good luck :)
Finally I used bindview to solve my problem. In my question I already mentioned that I use simplecursoradapter to get data from SQLiteDB not array. I know arrayadapter works but it doesn't work for SQLite efficiently. Before I extended baseadapter to design my own to add custom views on textviews. It works but too slow if there are lots of data. So simplecursoradapter is more efficient.
I extended simplecursoradapter and override bindview for my own purpose. It works for what I need.
Thanks for your help and link for arrayadapter