Android app custom list view tutorial


In this lesson, you'll learn to customize list views with a custom entry layout. Run this app which shows a list of companies. We have added this XML layout file to define the custom entry layout. Add a TextView with android:layout_width="wrap_content", android:layout_height="wrap_content.
Assign a new ID to the new TextView by setting attribute android:id to value @+id/entryTextView1. To use the new entry layout pass (this, R.layout.entry, R.id.entryTextView1, companies) to the ListAdapter constructor. R.layout.entry makes the adapter use the new entry layout and R.id.entryTextView1 identifies the text view inside the layout, which will be populated with the texts from the companies array. Customize the TextView and set android:padding to 10dp, android:textSize to 25sp and android:textStyle to bold. Congratulations! You've implemented a custom list view. Play with the code on your own a bit.

REQUIREMENTS:
Download:
Eclipse

Instruction:
After you download all the files needed in this tutorial, we need to install and set-up a java JDK path to our environment variables, the step below will show you how.

--Go to Java JDK installation directory in "C:\Program Files\Java\jdk1.7.0\bin" Copy this path
--Go to the environment path by Clicking: Window taskbar--->right click Computer--->Click Advance System setting ---> then go to Advanced tab Then select "Environment Variable" then create new Variable name and variable value by clicking the New radio button. Within the "Variable name" input these "JAVA_HOME" then in the "Variable value" point it to your C:\Program Files\Java\jdk1.7.0\bin 
that's it we are on a way of creating our first game and deploy it to a different platform.

After that we will also set-up Android SDK, to its path by extracting first it to the directory you want for example in "drive D:" or "drive C:"  once we did this, we can now copy the root folder of this SDK which is likely to this format "D:\Android SDK" copy it then add it also to the "Environment path" like what you did before in "Java JDK".



COMPLETE CODES

MAIN ACTIVITY CODE



import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        String[] companies = new String[] { "Google", "Apple", "Facebook",
        "Blackberry", "Samsung", "Twitter", "Intel", "HTC", "Asus" };
       
        ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.entry,R.id.entryTextView1,companies);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
listView.setAdapter(adapter);
        
    }
}


MAIN XML CODE



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<ListView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/mainListView1"> 

</LinearLayout>


ENTRY XML CODE



<?xml version="1.0" encoding="utf-8">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView  
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/entryTextView1"

android:padding="10dp"
android:textSize="25sp"
android:textStyle="bold"
/>
</LinearLayout>



Post a Comment

0 Comments