How do I make a TwoLineListItem? - android

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).

Related

Questions related to android:onClick="selfDestruct" xml method

New to android and programming in general. This question might sound silly but I'd appreciate the answer. The question description and reasoning is at the beginning and the question is at the end of it all.
I want to apply a listener to a button in android. The way I understood from android.googlesource.com is that there is two way to do it:
applying an OnClickListener to the button in the activity.java
or
assign a method to my button in the xml layout using this
{#link android.R.attr#onClick android:onClick}
they gave the following xml layout example:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selfDestruct"
android:text="#string/self_destruct" />
plus the the code in activity.java
public void selfDestruct(View view) {
// Kabloey
}
android.googlesource.com
Questions:
According to this: {#link android.R.attr#onClick android:onClick} android.R.attr in the example are the following:
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
?
Does this:
android:text="#string/self_destruct"
android:onClick="selfDestruct"
mean that the button called self_destruct registered as a listener?
If I want to add more than one button listener in xml form how do I write it in the java document?
Thank you in advance
Really appreciate it.
Does this:
android:text="#string/self_destruct" android:onClick="selfDestruct"
mean that the button called self_destruct registered as a listener?
The button isn't "called" anything, it just has the text of the value for #string/self_destruct defined in the strings.xml file.
But, yes, the public void selfDestruct(View view) method is the method that will be called for the listener that is setup by the XML.
If I want to add more than one button listener in xml form how do I write it in the java document?
You can only set one click listener for a View.
Yes, layout_height, layout_width and text are attributes. You
can learn more about android attributes
here.
But keep in mind, that different views can use the same attributes
in different way.
It does not. The android:text attribute in this sample just refer to a string-resource called self_destruct to decide which text should be shown within the button. To distinguish views you can use android:id.
As already mentioned by other people, you are able to specify only one onClickListener using XML.

HeaderListView: How to change background color in RowView

I'm using HeaderListView(https://github.com/applidium/HeaderListView) and
Navigation-drawer-page-sliding-tab-strip(https://github.com/Balaji-K13/Navigation-drawer-page-sliding-tab-strip). In last library, I've PageContentStripFragment class that I implemented onCreateView method content:
HeaderListView list = new HeaderListView(getActivity(),null);
list.setAdapter(new ListViewSectionAdapter());
It's working!! Hence, ListViewSectionAdapter is a subclass of SectionAdapter. In override getRowView method I inflated my layout called "custom_row_list".
So, I want to set custom background color when RowItem selected.
I did to set drawable selector and custom_row_list.xml:
android:background="#drawable/list_selector"
But not working!! What I doing wrong??
My environment is ADT eclipse + android 4.4.2.
Thanks!
as of now you cant inflate xml layout using this library as applidium people clearly mentioned in Future works.
if you want to set a custom color to rowitem. implement it on onRowItemClick method.
getChildAt(position).setBackgroundColor(android.R.color.black);
you can use StickyListHeaders library too.

Android: Own Font in ListView

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

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.

Customizing CheckBoxPreference via android:widgetLayout

I have a Preference that enables a sync adapter, and takes a while to actually do its work when toggled. Consequently, when the user clicks the Preference, I spin off an AsyncTask to do the work. In the meantime, I disable the Preference and replace the check box with an indeterminate ProgressBar. I have all of this working via a hack involving a subclass of CheckBoxPreference that overlays the ProgressBar on top of the CheckBox. Yuck.
The android:widgetLayout attribute seems like it's designed exactly for this. I should be able to use android:widgetLayout to specify a replacement for the default CheckBox. Said replacement would implement Checkable and use a ViewSwitcher to switch appropriately between a CheckBox and a ProgressBar.
The only problem is that CheckBoxPreference, in its onBindView() method, seems to ignore the possibility that android:widgetLayout may be used. It explicitly does this:
View checkboxView = view.findViewById(com.android.internal.R.id.checkbox);
This effectively makes it impossible to swap in a custom Checkable via android:widgetLayout and have it actually work.
Is this an oversight/bug in CheckBoxPreference, or have I misunderstood android:widgetLayout? Is there a cleaner intended way to do what I'm trying to do?
First, I agree with you that Android should refactor the piece of code view.findViewById(com.android.internal.R.id.checkbox); to "calling a protected method" which can be overrided by subclass.
Luckily, we are still able to override work around as follows:
The idea is simple: declare a checkbox which has id is android default id #android:id/checkbox
<CheckBoxPreference
android:key="autostart"
android:widgetLayout="#layout/customlayout" />
and in customlayout.xml:
<SwitchView>
...
<CheckBox>
android:id="#android:id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mycheck"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
</CheckBox>
It's really important to note that focusable attributes of checkbox must be set to false (I guess the default layout of CheckBoxPreference does the same thing) so that list apdater receives event rather than the checkbox itself.
I guess you didn't success in your try just because you didn't set the focusable state.
greensuisse
(https://sites.google.com/site/greensuisse/)
android:widgetLayout is the right part of the preference. In the CheckBoxPreference, the widgetLayout is the checkbox.
If you take a base Preference and put a ViewSwitcher in the widgetLayout, it should work

Categories

Resources