Question

I currently have a program that displays a listview when the user clicks a button. This view takes up the whole screen. How can I (instead of having to press a button to get to the listview) divide the main screen into 2 parts, one being a large listview and two being a few buttons?

This is how I was using my listView adapter: public void showPopUp(){ ListArrayAdapter adapter = new ListArrayAdapter(this, R.layout.list_view_row_item, foodList);

ListView listViewItems = new ListView(this);
listViewItems.setAdapter(adapter);
listViewItems.setOnItemClickListener(new OnListItemClick());

alertDialogStores = new AlertDialog.Builder(MainScreen.this)
    .setView(listViewItems)
    .setTitle("Food List")
    .show();
}

and this is the adapter:

public class ListArrayAdapter extends ArrayAdapter<ListItem> {

Context mContext;
int layoutResourceId;
ListItem data[] = null;
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
ArrayList<ListItem> foodList = new ArrayList<>();

//public ArrayAdapterItem(Context mContext, int layoutResourceId, ListItem[] data) {
public ListArrayAdapter(Context mContext, int layoutResourceId, ArrayList<ListItem> foodList) {

    super(mContext, layoutResourceId, foodList);

    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    this.foodList = foodList;
    //this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    /*
     * The convertView argument is essentially a "ScrapView" as described is Lucas post 
     * http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
     * It will have a non-null value when ListView is asking you recycle the row layout. 
     * So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
     */
    if(convertView==null){
        // inflate the layout
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        convertView = inflater.inflate(layoutResourceId, parent, false);
    }

    // object item based on the position
    //ListItem ListItem = data[position];

    // get the TextView and then set the text (item name) and tag (item ID) values
    TextView textViewItem = (TextView) convertView.findViewById(R.id.itemName);
    textViewItem.setText(foodList.get(position).getItemName());
    textViewItem.setTag(foodList.get(position).getItemIdInt());

    progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar1);



    return convertView;

}

}
I also would like to know if it's possible to have a message appear when the user presses and holds an item (and have it linger a little after they let go).

Was it helpful?

Solution

You can use android:layout_weight attribute.

Accept the answer if it helped you. :)

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />
    </LinearLayout>

</LinearLayout>

** To get the message on click of item in ListView **

    listViewItems.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

                String str = adapterView.getItemAtPosition(arg2).toString();
Toast.makeText(getApplicationContext(), "You clicked "+ str, 100).show();


 }
    }

Instead of adapterview, you can use

String str = foodList.get(arg2).getItemName().toString() + " " +foodList.get(arg2).getItemIdInt();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top