user4058730
user4058730

Reputation:

ListActivity Full Screen Method

I have created a list of 7 days and want the list to take the full screen. But the list is having a gap in the bottom. I want to remove the gap.

enter image description here

My code is as follows :

package com.example.collegehack;

import android.app.ListActivity;

import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Days extends ListActivity {

    String days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
            "Saturday", "Sunday" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Days.this,
                android.R.layout.simple_list_item_1,days));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String temporary = days[position];
        try {
            Class ourClass = Class.forName("com.example.collegehack"
                    + temporary);

            Intent ourIntent = new Intent(Days.this, ourClass);
            startActivity(ourIntent);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Please help me with this problem. Thanks in advance.

Upvotes: 0

Views: 184

Answers (4)

saman
saman

Reputation: 448

you can make font of your list bigger or use custom listview or if your activity do not need expand or scroll use linearlayout and some buttons in that

Upvotes: 1

nbaroz
nbaroz

Reputation: 1795

To do so you need to inflate your own view.

Follow these steps:

1. Create my_layout.xml contained textView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/textView1"
    android:gravity="center_vertical"
    android:layout_centerVertical="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</RelativeLayout>

2. In your adapter, you need to override the getView() method and set the rows height like this:

setListAdapter(new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_list_item_1,days) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.my_layout, parent, false);
                convertView.getLayoutParams().height = parent.getHeight() / getCount();
                convertView.requestLayout();
                TextView tv = (TextView) convertView.findViewById(R.id.textView1);
                tv.setText(getItem(position));
            }
            return convertView;
        }
    });

3. Disable the list scrolling because the dividers height doesn't count in this workaround and your listView will scroll by a millimeter:

getListView().setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                return true; // Indicates that this has been handled by you
                                // and will not be forwarded further.
            }
            return false;
        }
    });

That's it! Have fun :)

Upvotes: 0

Mohamed Ghareeb
Mohamed Ghareeb

Reputation: 19

you can use the custom list view with custom list item like the below link then modify height of the single item.

http://www.codelearn.org/android-tutorial/android-listview

Upvotes: 0

ekchang
ekchang

Reputation: 939

If your list does not need to expand or scroll down and will always have a finite number of rows (ie. days of the week), consider not using a ListView at all. Use a LinearLayout with seven TextViews, and set each of their android:layout_weight=1 and android:layout_height="0dp" LinearLayout will evenly distribute the white space among the TextViews.

Then you can set setOnClickListener for each of the TextViews to perform the action you're looking for.

Upvotes: 2

Related Questions