0

Whenever I make a linear layout, the whole layout appears on the design page perfectly, but when I run the app on my phone, this layout appears completely blank.

This is activity_order_list.xml file that doesn't appear:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/txtInput"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="New item"
        />
    <Button
        android:id="@+id/btAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:layout_toRightOf="@+id/txtInput"
        />
</RelativeLayout>
<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</ListView>
</LinearLayout>

This is item_list.xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/txtview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

This is its Java code:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

public class OrderList extends Activity {
ArrayAdapter<String> adapter;
EditText editText;
ArrayList<String> itemList;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_item);
    String[] items={"Apple","Banana","Clementine"};
    itemList=new ArrayList<String>(Arrays.asList(items));

    adapter=new ArrayAdapter<String>         (this,R.layout.list_item,R.id.txtview,itemList);
    ListView mainlist=(ListView)findViewById(R.id.list);

    mainlist.setAdapter(adapter);
    editText=(EditText)findViewById(R.id.txtInput);
    Button btAdd=(Button)findViewById(R.id.btAdd);

    btAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String newItem=editText.getText().toString();
            // add new item to arraylist
            itemList.add(newItem);
            // notify listview of data changed
            adapter.notifyDataSetChanged();
        }

    });



   }
}
Roberts
  • 101
  • 2
  • 9

3 Answers3

0

change fill_parent to match_parent

EKN
  • 1,886
  • 1
  • 16
  • 29
0

Try to change setContentView(R.layout.list_item); to setContentView(R.layout.activity_order_list);

Also you have to set up the layout_weight of ListView if you want to fill all space of the LinearLayout

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>
Roman Black
  • 3,501
  • 1
  • 22
  • 31
0

First of all you have typed item_list.xml as the name of the Layout that you are using in your ListView and in the Activity you have written list_item so first make sure that names are correct. And secondly you are setting the contentview in setContentView as R.layout.list_item which is the layout of ListView which contains only a TextView without any text. So, when your application will run it will not show you the layout of activity_order_list.xml instead it will display the layout of list_item.xml which contains just a TextView without any text. Which will make the screen looks blank. So first change your setContentView(R.layout.list_item); to setContentView(R.layout.activity_order_list); also change the layout_height from match_parent to wrap_content of the item_list.xml and then let us know if that solved your problem...hope it helped you :)