Md. Arafat Al Mahmud
Md. Arafat Al Mahmud

Reputation: 3214

inflated view does not appear

I have a simple TextView with the following xml markup under the file view_inflated.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:text="Inflated view"
    >
</TextView>

I want to inflate the TextView inside a LinearLayout declared under the file activity_main.xml as follows:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

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

        </ListView>

    </LinearLayout>

Now for inflating the view I tried like this:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        resource=new String[]{"Arafat","Anik","Prottoy","Progga","Prapti",
                "Oishy","Nijha","Adil","Troyee","Kushum","Manik","Shahin","Limon"
                ,"Shumon","Lipi","Lucy","Shaikot"};

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,resource);

        lv=(ListView) findViewById(R.id.listView1);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);

        LayoutInflater inflater=getLayoutInflater();
        View v=inflater.inflate(R.layout.view_inflated, null);
        LinearLayout rl=(LinearLayout) findViewById(R.id.ll1);
        rl.addView(v);
    }

Problem is-the list view appears as expected but the TextView doesn't appear. How to fix this issue ?

Upvotes: 2

Views: 1399

Answers (4)

Jitender Dev
Jitender Dev

Reputation: 6925

Messing around with your problem for a while now and then figured out what you have missed

android:orientation="vertical"

in your activity_main.xml :)

Do it and your problem will get solved.

To set textview at top, Replace your main layout with Relative Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:layout_alignParentBottom="true"
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="400dp"
       android:layout_gravity="bottom" >
    </ListView>

</RelativeLayout>

TypeCast in the code as well and then add view to this Relative Layout prior setting adapter to list.

Like this

LayoutInflater inflater = getLayoutInflater();
    View v = inflater.inflate(R.layout.view_inflated, null);
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.ll1);
    rl.addView(v);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, resource);

    ListView lv = (ListView) findViewById(R.id.listView1);
    lv.setAdapter(adapter);

Upvotes: 1

Amit Gupta
Amit Gupta

Reputation: 8939

You can use Section ListView

enter image description here

You can refer to this below link

http://amitandroid.blogspot.in/2013/05/android-section-listview.html

Hope this help.

Upvotes: 0

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try this
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.view_inflated,resource);

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Add your inflated view as a footer to listview.

Before setAdapter

 lv=(ListView) findViewById(R.id.listView1);
 LayoutInflater inflater=getLayoutInflater();
 View v=inflater.inflate(R.layout.view_inflated, null);
 lv.addFooterView(v);
 lv.setAdapter(adapter);
 lv.setOnItemClickListener(this);

Upvotes: 0

Related Questions