William Kinaan
William Kinaan

Reputation: 28829

android listview fixed height independent of screen size

I want to have a listview for English alphabets and I want this listview to keep fixed and show all the time, I mean I don't want to allow the user to scroll in it, because he will see it all. what should I do to make it shown always and fit all the screen size for all the mobile devices? I tried like this:

ListView

<ListView
            android:divider="#000000"
            android:id="@+id/lv_profile_listview_with_search_alphabets"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="0.1"
             >
        </ListView>

List Item

<TextView
        android:id="@+id/tv_list_item_alphabet_oneAlphabet"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/alphabet_selector"
        android:gravity="center_horizontal"
        android:textColor="#040404"
        android:textSize="10dip"
        android:typeface="sans" 
        />

But On my device I still have blank area at the button of the screen.

one of the solution I guess, that I set the height of each list item programatically on my adapter , something like

li.setHeight(ScreenHeight/26);

but I don't know how to get the ScreenHeight nor than this is a good way.

any help please

Upvotes: 0

Views: 8530

Answers (2)

Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17115

Setting item height is a really bad idea. If you want to have the height relative to screen size you should use vertical LinearLayout, set item height to zero and add weight on it. To disable scrolling try adding android:clickable="false" attribute.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1" >

    <ListView
        android:divider="#000000"
        android:id="@+id/lv_profile_listview_with_search_alphabets"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"
        android:clickable="false"/>

</LinearLayout>

If the weight sum of vertical layout is 1 and your weight 0.3 it means it takes a little less then 1/3 of your screen height. You can set either relative width or height in LinearLayout by weights and orientation. For example, to make ListView's width 0.5 width of screen and set height to match_parent it would be like this. Note that LinearLayout is now horizontal and width if item is 0dp

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1" >

    <ListView
        android:divider="#000000"
        android:id="@+id/lv_profile_listview_with_search_alphabets"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:clickable="false"/>

</LinearLayout>

It's rather a bad practice to set the item height based on screen size in code. You can make custom item layout and set the layout_height of your item from dimen and add different dimens for different resolution

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

http://developer.android.com/guide/topics/resources/providing-resources.html

These articles might help you

http://developer.android.com/guide/practices/screens_support.html

http://developer.android.com/training/multiscreen/index.html

Upvotes: 2

lokoko
lokoko

Reputation: 5803

To get the height of the screen do something like this :

  listView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    public void onGlobalLayout() {
      item.setMinimumHeight(listView.getHeight() / 26);
    }
  });

For not allowing scrolling try :

listView.setScrollContainer(false)

Upvotes: 1

Related Questions