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();
}
});
}
}