• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem in writing custom layout file

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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<ImageView
android:id="@+id/icon"
android:layout_width="22px"
android:paddingLeft="2px"
android:paddingRight="2px"
android:paddingTop="2px"
android:layout_height="wrap_content"
android:src="@drawable/ok"
/>

<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="44sp"
/>
</LinearLayout>
StaticDemo.java

Code:
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class StaticDemo extends ListActivity {
    TextView selection;
    String items[] = {"Kanpur","Lucknow","Agra","Banaras"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setListAdapter(new ArrayAdapter<String>(this,[B]R.layout.row[/B],R.id.label,items));
        selection = (TextView)findViewById(R.id.selection);
    }
    public void onListItemClick(ListView parent,View v,int position,long id){
        selection.setText(items[position]);
    }
}
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
D:%5Coutput.jpg


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

row.xml (efforts by me):

Code:
<?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="wrap_content" >
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>

regards
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones