4

What am I trying to do?

Activity starts with a ImageView taking the upper 9/10 of the screen, and a ListView the remaining bottom 1/10: enter image description here

As more items added to the ListView, the ratio changes, ListView getting 1/10 more of the screen for each item, and ImageView shrinks respectively, up to 50:50 ratio (adding more items after that keeps the ratio fixed) : enter image description here

What I already know: Best practice of statically dividing the screen, is using LinearLayout attribute android:weightSum (and View's android:layout_weight). It should be possible to it dynamically as well using AbsListView.LayoutParams, combined with registering a callback somewhere in ArrayAdapter. However, it fills like an abuse of a static feature (weight). Moreover, I'd prefer to do the transition between to ratios continuously (using Property Animation).

Questions:

  1. Should I prefer using getWindowManager() .getDefaultDisplay().getHieght(); once, and calculating ratio dynamically as desired?

  2. As for ratio updating callback, are there any option other than ArrayAdapter.registerDataSetObserver() ?

Rishabh Srivastava
  • 3,683
  • 2
  • 30
  • 58
nobatlinux
  • 347
  • 2
  • 10

2 Answers2

2

If you want to keep ratio always 10% and 90%, use weight 1 and weight 9. Otherwise better to write xml code to check

Ahmad Ebrahimi
  • 267
  • 5
  • 24
  • Well, I don't want to keep ratio, that's point :) what do you mean by "write xml code to check" ? @Ahmad Ebrahimi – nobatlinux Apr 18 '15 at 12:24
1

I do something like what you want by:

1) Set up layout:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/ll_forImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />    
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_forList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </ListView>    
    </LinearLayout>
</LinearLayout>

2) write some Utility methods for layouting.

private int dpToPx(int dp) {
    return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}

private static void setLayoutSize(View view, int width, int height) {
    ViewGroup.LayoutParams params = view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

3) code:

int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp);
int screenHeight = dpToPx(getResources().getConfiguration().screenHeightDp);
int ratio = ....;//
setLayoutSize(ll_forImage, screenWidth , screenHeight/ratio );
setLayoutSize(ll_forList, screenWidth , screenHeight - screenHeight/ratio );
Robust
  • 2,415
  • 2
  • 22
  • 37