Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Question about size/resolution independent applications?

I apologize in advance for asking this question, I know similar questions have already been asked hundreds of times, but despite the fact that I read the Android Screen Support guide several times, I still don't understand how to create a basic layout that fits several screen without being able to use proportional dimensions.

So basically, if I sum-up what this guide tells us to do:

  • We should create several layout resources for each "size group" and "density group" of devices you want your app to be compatible with.
  • We should use RelativeLayout or FrameLayout instead of AbsoluteLayout
  • We should use dp dimensions instead of px dimensions to get rid of the density difference problem.

Ok. This makes sense.

Now, here are my questions (I apologize in advance for their silliness):

  • Why do I have to create different layout resources for different density groups if I use Density Independent Pixels (dp) dimensions?
  • I suppose that the point of having different sets of resources for different screen sizes is that you might want your application layout to look different on a small and a large device, not to have the exact same layout with different dimensions, correct? So basically it means that if I just want an application that looks exactly the same on all devices (just shrinking/expanding to fit the screen size), I only have to define one set of resources, correct?
  • If I want to create a really simple layout, that just contains two buttons, where each buttons take 50% of the screen's width, how do I do that by just using dp dimensions?

Thank you, and sorry again for going over this topic again...

like image 581
nbarraille Avatar asked Nov 14 '22 23:11

nbarraille


1 Answers

You do not have to create different layouts. I mostly only use one layout for portrait and one for landscape mode leaving everything else to the system.

If you want to get 2 buttons of the same size just use

android:layout_width="fill_parent"
android:layout_weight="1"

for both buttons and put them into a linear layout container.

edit (complete code, will give two buttons side by side):

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:orientation="horizontal">
    <Button 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:text="@string/b1" android:onClick="btn1" />
    <Button 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:text="@string/b2" android:onClick="btn2" />
</LinearLayout>
like image 193
rurouni Avatar answered Dec 15 '22 09:12

rurouni