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.
Related
I am trying to use this : lib to have my list items be deleted when user swipes them. But I can't seem to get how to convert my existing listView to DynamicListView. I've read the instructions given, it just says to add some code to the xml layout to convert listView into DynamicListView, but where should I put that code? In which xml file? I have no idea. Any help would be appreciated.
Here's what is given in wiki:
To use the DynamicListView, include the following in your xml layout:
<com.nhaarman.listviewanimations.itemmanipulation.DynamicListView
android:id="#+id/dynamiclistview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
It looks like you need to make your current list the
<com.nhaarman.listviewanimations.itemmanipulation.DynamicListView
android:id="#+id/dynamiclistview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and add the functionality as described with
MyAdapter myAdapter = new MyAdapter();
AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(myAdapter);
animationAdapter.setAbsListView(mListView);
mListView.setAdapter(animationAdapter);
creating your own adapter to plug in with your existing list view implementation. So Idk if you have your own adapter setup, you didn't provide any code along with this? So mainly though your list view will have to fall in line with the xml "#+id/dynamiclistview" for it to function. I recommend extending there implementation if you already have an Adapter built up, and just providing methodology for the swipe and delete functions.
EDIT:
Please note this is important <com.nhaarman.listviewanimations.itemmanipulation.DynamicListView
that you have this implemented and the library properly imported. (any questions ask)
This is probably a trivia question, but why are there package/class names in some people's XML layout files?
(please don't downvote this question if it is something trivial, i don't even know what this is called, so i couldn't look it up).
i was looking at a tutorial, and i saw something like this (in "sample.xml"):
<com.tutorials.foo
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- some buttons and views here -->
</com.tutorials.foo>
My questions are:
1) i'm assuming that "foo" is a custom view? say, like you want to extend TextView with your own version of TextView?
2) what is this pattern/technique even called?
3) what would be the advantages of using this method?
Thanks so much in advance!!!
Yes, <com.tutorials.foo .../> is a custom view.
Calling it will be as same as others.ex:
Foo foo=(Foo)findViewById(R.id.foo);
I assume you mean creating layout static(.xml) or dynamically with code. xml layout would be in advantage when you know that you will use this layout in the program and will not change its format. Of course you can add to it or edit it with code later on. It is also in advantage for readabilty.
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
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.
I want to make a setings menu like this:
Is there a definitive guide on how to make TwoLineListItem's anywhere?
You will need to use a custom listrow with a custom adapter.
Here is an example:
http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx
In your case, extend PreferenceActivity and in the onCreate method:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
getPreferences();
}
Where you can inflate your view using a preferences.xml file - add a folder called xml to your res folder and add an xml file with stuff like:
<CheckBoxPreference
android:title="#string/pref_sub_notify"
android:defaultValue="true"
android:summary="#string/pref_sub_notify_summary"
android:key="subNotifyPref" />
<ListPreference
android:title="#string/pref_sub_expiry_warn"
android:summary="#string/pref_sub_expiry_summary"
android:key="subExpiryDelayPref"
android:defaultValue="7"
android:entries="#array/sub_expiry_delay"
android:entryValues="#array/sub_expiry_delay_values" />
In this case, the title is your first line and the summary is the second line.
You need a custom View and a ListActivity. This tutorial may help. If you are working on a Settings Activity, why not extend PreferenceActivity?
There is a pretty good tutorial here on how to create custom list items. The basic idea is that you define the layout of the list item just as you define the layout for an Activity. You then subclass ArrayAdapter (or BaseAdapter), and override the getView(...) method to return your custom list item View.
I had a similar problem, but my desired list's item should have an image followed by two strings, one above the other (just as you want). I found the solution here. It worked easily for me and I think it will work well for you too (you just need to remove the ImageView part appropriately).