1. Check out our companion app, Forums for Android! Download from Google Play

Problem in writing custom layout file

Discussion in 'Application Development' started by shobhitsingh, Jun 21, 2010.

  1. shobhitsingh

    shobhitsingh New Member
    Thread Starter
    5

    Jun 21, 2010
    1
    0
    5
    Hi,

    I am new in Android. I am trying an example for which I am trying to write custom layout file row.xml other then default main.xml.

    Below is main.xml which is default generated.
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="horizontal"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="wrap_content"
    6.     >
    7. <ImageView
    8. android:id="@+id/icon"
    9. android:layout_width="22px"
    10. android:paddingLeft="2px"
    11. android:paddingRight="2px"
    12. android:paddingTop="2px"
    13. android:layout_height="wrap_content"
    14. android:src="@drawable/ok"
    15. />
    16.  
    17. <TextView
    18. android:id="@+id/label"
    19. android:layout_width="wrap_content"
    20. android:layout_height="wrap_content"
    21. android:textSize="44sp"
    22. />
    23. </LinearLayout>
    24.  
    StaticDemo.java

    Code (Text):
    1. import android.app.ListActivity;
    2. import android.os.Bundle;
    3. import android.view.View;
    4. import android.widget.ArrayAdapter;
    5. import android.widget.ListView;
    6. import android.widget.TextView;
    7.  
    8. public class StaticDemo extends ListActivity {
    9.     TextView selection;
    10.     String items[] = {"Kanpur","Lucknow","Agra","Banaras"};
    11.     /** Called when the activity is first created. */
    12.     @Override
    13.     public void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.         setContentView(R.layout.main);
    16.         setListAdapter(new ArrayAdapter<String>(this,[B]R.layout.row[/B],R.id.label,items));
    17.         selection = (TextView)findViewById(R.id.selection);
    18.     }
    19.     public void onListItemClick(ListView parent,View v,int position,long id){
    20.         selection.setText(items[position]);
    21.     }
    22. }
    In StaticDemo.java R.layout.row is used, So I have to write row.xml inside res/layout where default main.xml stay.

    output will be a list in which every list item contains image followed by text
    [​IMG]

    Please suggest how to write row.xml sothat this code will give output as above!

    row.xml (efforts by me):

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout
    3. xmlns:android="http://schemas.android.com/apk/res/android"
    4. android:orientation="vertical"
    5. android:layout_width="fill_parent"
    6. android:layout_height="wrap_content" >
    7. <TextView
    8. android:id="@+id/selection"
    9. android:layout_width="fill_parent"
    10. android:layout_height="wrap_content"/>
    11. <ListView
    12. android:id="@android:id/list"
    13. android:layout_width="fill_parent"
    14. android:layout_height="fill_parent"
    15. android:drawSelectorOnTop="false"
    16. />
    17. </LinearLayout>
    regards
     

    Advertisement

  2. Boogs

    Boogs Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    You might want to try out this link to an Android example which shows the basics of using a list view. Once you have that working, adding an icon is fairly simple. Just add an Imageview to the row.xml.

    Good luck!
    Boogs
     

Share This Page

Loading...