Styling the android listview with array list adapter - java

I have written the code for creating a list out of some arrays shown below!
code runs properly and output is as expected!
update for people with same prob: nice tutorial for custom listview
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView1 = (ListView) findViewById(R.id.listView1);
String[] items = { "some", "fancy", "items", "to", "show" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listitem, items);
listView1.setAdapter(adapter);
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
</RelativeLayout>
listview.xml
<?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="wrap_content"
android:textSize="20sp"
android:padding="22dp"
android:gravity="center_horizontal"
/>
what I want to accomplish?
change color and font of the text in each list item to a different one..and do some task on tapping on them...
also is it possible to get another listview inside the same listview
eg. if I click on a list item it again shows me a list (kind of a sub list) with different list items on that same activity(or screen).and some action could be done on tapping the sub list items.
Detailed answers are appreciated as I am new to android development..
Thanks!

1.change color and font of the text in each list item to a different one..and do some task on
tapping on them...
Make a Custom Adapter, override getVIew() of that adapter and make
changes on color and text in it.
override onItemClick() for your ListView. to accomplish click event
for list item.
Now
2. also is it possible to get another listview inside the same listview eg. if I click on a
list item it again shows me a list (kind of a sub list) with different list items on that
same activity(or screen).and some action could be done on tapping the sub list items.
3. what are my other list styling options..
Its a Expandable ListView Just look at link for more
information.
And Tutorial

You'll want to look into a custom ArrayAdapter as seen here http://www.vogella.com/articles/AndroidListView/article.html
That will take care of the first and third questions. As for the second one that's not possible with the default implementation of ListView but there are libraries that enable you to create drop down listItems.

For 1st question, you can use paulthom12345' answer.
2nd question: You have to use exapandableListView
for more details please see: Android ExpandableListView - Looking for a tutorial
3rd question is not constructive and very vague. Please edit the question and explain in greater detail.

Use Customized List View for it.
It is not exactly possible to have a list inside a list. Instead of it, use expandable list
Have a look at Android Listview with different layout for each row

Related

Improving performance of instantiating and inflating views used with a ViewFlipper

I am using a ViewFlipper to display all the measurements the app has made (1 measurement = 1 page element). The problem is, the performance of initializing/reloading of the content of the ViewFlipper gets very bad if there are more then 50 elements. i.e., the app gets unresponsive for about 15 seconds after it has launched. I did some logging and found out, that more than 70% of the loading time is spent on View.inflate(ctx, R.layout.view_measurement, null);
From my understanding, inflate(...) does parse the layout xml file to a View so it can be used in Java. Since the layout xml file is everytime the same, it looks to me like there is the same heavy job being done for each element over and over again. I tried many things to change this and let this happen only one time, unfortunately without success. I also tried to use and tags in the xml also but always got errors.
I know that there are better ways rather than the ViewFlipper to do this job, but as I am in a hurry everything else works fine, I would like to keep this and find a fast solution.
MainActivity.java
...
ViewFlipper viewFlipper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewFlipper = (ViewFlipper)super.findViewById(R.id.measurementsViewFlipper);
int measurementsAmount = 100; //example
for(i=0; i<measurementsAmount; i++){
View measurementView = View.inflate(ctx, R.layout.view_measurement, null);
... fill the view ...
viewFlipper.add(measurementView);
}
...
}
view_measurement.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
...
</LinearLayout>
</RelativeLayout>
The only way to reduce the xml inflation time is to reduce the number of views used in the xml layout. For example, just by looking at he layout you posted, you could(and should) remove the root RelativeLayout if it's just wrapping the LinearLayout. Also, using ConstraintLayout might allow you further remove ViewGroups and reduce the nesting level and the view count.
But this will still not be ok because the real problem is you are inflating a lot of views upfront. In your example if the page layout contains just 5 views you'll create 500 views in total(100 x 5). That's a lot and this is why you need to have a view recycle mechanism so you don't need to create all views upfront, just a few for the current pages.
If you want ViewFlipper's behavior, the Android SDK has a component called AdapterViewFlipper which you should use instead. It just needs an additional adapter class, to which you'll pass the measurement data and in which you'll inflate the measurement layout.

AndroidStudio : ListView and TabActivity [duplicate]

I am wondering what's the difference between #+id/android:list and #+id/list. I know the last one which is a regular id assignment but the first looks different. What makes it special?
Where I saw it:
I was studying on ListView, ListAdapter and things like that and the author define the ListView in layout xml file as below :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/main_no_items"/>
</LinearLayout>
and also let me mention #+id/android:empty id as well.
And he also extends ListActivity class.
Here is the source of the article.
And also what's in my mind as questions are :
Should we extend ListActivity? Maybe I want an Activity which also contains other Views.
We use #+id/android:list just because we extend ListActivity or we can use the same convention if we extend Activity?
Thanks.
Resource IDs in Android are specific to a package (which is good, or else you'd have lots of conflicts if your app is dealing with several packages at the same time).
#+id/list will create a resource ID in your app (=your package) with the name "list" and give it a unique ID. In code, that would be R.id.list.
#android:id/list will use the ID "list" from the package android (which, in code, would be android.R.id.list.
EDIT: Need to add the corrections David Hedlund pointed out: The proper reference would be #android:id/list. Also, + indicates you're defining a new ID - you obviously don't need that when you're referencing something that was defined in the Android API.
I think the example code you posted has a typo, so it should be #android:id/list (without the +). From the ListActivity javadoc:
your own view MUST contain a ListView object with the id "#android:id/list"
#android:id/list is specific to ListActivity, so you do not need it if you are adding a ListView into any other kind of Activity. You should extend ListActivity if you want the user to do more than view the list. For example, you can override ListActivity.onListItemClick to respond to clicks on an item in the list.
Similarly, #id/android:empty (again, without the +), is a special case for ListActivity. This allows you to specify an alternative view that should be displayed when your list is empty. That View will not be displayed when the list is populated.
in android,
In XML: #[package:]layout/filename
like
android:id="#+id/android:list"
This is the standard way to refer to a list view when refering to listFragment or listActivity
so filename is android:list is a reference to ListView.
navigate to res/values/ids.xml
you will find <item type="id" name="list" />
ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

listview onclick through xml

This is more of a theoretical question, I have searched in many places, but unable to find the answer:
If i add an android:onClick tag to my ListView inside the layout file, what is the method signature I should use for my java Activity in order to correctly have it been called?
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/models"
android:onClick="getSpecs">
</ListView>
This is the Java method signature, but it will be fired everytime you click anywhere in the ListView. It is not an ItemClickListener, as I assume that was your intention for that XML.
public void getSpecs(View v) {
}
Note that the android:onClick XML attribute is the same as View#setOnClickListener, meaning it sets the generalized view's click listener. For buttons and such this is fine, but ListViews and other complex, nested views, there are other interfaces for catching the appropriate clicks.

Android: Populating a listview with array items

I'm new to Android and I think I'm trying to do something really basic: I have a 5 strings in my Array (say 'One', 'Two', ...). I want to add these 5 strings to my list view in my listactivity.
My List:
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
My List Row:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/homeItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Basically, I want to bind the Array items to the TextView homeItemName. I might add other items in my row later, so I can't just bind the listview to the entries.
Thanks!
For code, take a quick look at this step-by-step tutorial
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
It shows a basic implementation of an ArrayAdapter:
R.layout.list_item : is the xml layout (list_item.xml) that will be used for every ROW of your
listview.
COUNTRIES is the array of Strings.
You can use an ArrayAdapter to bind your data. Since you want to be able to add extra data items to the view, you to give the adapter an ArrayList (since an array is of a fixed size). Items should be added via the ArrayAdapter and your ArrayList will be updated automatically. I have an example at http://www.box.net/shared/yduel9txya

Android ExpandableListActivity issue

I want to customize my ExpandableList. My issue is that I need a button and expandable list on single activity. Can I achieve that? I have seen all the examples but all extends ExpandableListActivity not the Activity in which I can put all the widgets in one activity.
Any help would be appreciated.
According to the documentation this task should not be too hard.
the first thing that you will have to do is create a new xml file to hold your custom layout. The file should be saved in your res/layout folder and be named something like "my_custom_expandable_list_view_layout.xml", it should look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<Button android:id="#id/my_button_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Me"/>
</LinearLayout>
The import part of that layout file is that you include an "ExpandableListView" and give it the id of "android"list".
The next thing that you will need to do is let your activity know that you are using a custom layout by calling setContentView() in your activities onCreate(). The call should look something like this
setContentView(R.layout.my_custom_expandable_list_view_layout);
At this point you should be able to run you program and see a large button at the bottom of the screen. In order to do something with this button you will need to access it via a call to findViewById() in your Activity like this
Button myButton = (Button)findViewById(R.id.my_button_id);
Once you have that myButton object you can add a click listener or whatever else you would like to do. You can pretty much add anything else you want by just adding new things to the layout file.

Categories