0

I already read many posts regarding setting the density based layouts but none is matching the criteria as here.

I want to set the top timeline and bottom timeline as seen in SO post according to differnt DPI values for the same screen size tablet devices in landscape orientation.

See the diagram in the link. The timeline is 1 2 3 4 .. numbers on top of bitmap. They are aligned left in width for tablet 2 due to its higher dpi.

Tablet 1: 160 DPI 7inch tablet.

Tablet 2: 216 DPI 7inch tablet.

The layout files of Tablet 1 and Tablet 2 are placed under layout-large-land. This is causing a shorter timeline for higher density device (Tablet 2).

I adjusted the density for tablet 2 by doing something like:

int screenInches = getScreenDimentions();

if(screenInches < 8)
{
DisplayMetrics dm = new DisplayMetrics();

getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);                   

// Display device dpi value of Y in pixels
int screenDPIy = (int)dm.ydpi;

if(screenDPIy > 180)
{
  //setting the margins for top and bottom timelines pro-grammatically.
}

But this code based idea to set the margins for timelines does not seem appropriate.

What is the standard way of doing that and which folder/layout is considered to be suitable for getting density independent layouts for such scenario?

Is it possible something like layout-tvdpi-land and layout-mdpi-land to set the layouts for the above different density devices?

UPDATE:

My layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/chartmain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

     <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

    <RelativeLayout
            android:id="@+id/relative"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp" >

<android.support.v4.view.ViewPager
                android:id="@+id/HView"
                android:layout_width="560dp"
                android:layout_height="255dp"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="160sp"
                android:layout_marginTop="110sp"
                android:layout_marginBottom="80sp">


            </android.support.v4.view.ViewPager>

<TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="18dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="1"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="40dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="2"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="62dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="3"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="84dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="4"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="109dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="5"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="132dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="6"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/topLtTv7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="155dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="7"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/topLtTv8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="177dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="8"
                android:textColor="#000000" />

             <TextView
                android:id="@+id/topLtTv9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="200dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="9"
                android:textColor="#000000" />

                          <TextView
                android:id="@+id/topLtTv10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="217dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="10"
                android:textColor="#000000" />

                 <TextView
                android:id="@+id/topLtTv11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="242dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="11"
                android:textColor="#000000" />

                 <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="266dp"
                android:layout_marginTop="90dp"
                android:textSize="12sp"
                android:text="Noon"
                android:textColor="#000000" />

                 <TextView
                android:id="@+id/topRtTv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="296dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="1"
                android:textColor="#000000" />

                 <TextView
                android:id="@+id/topRtTv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="315dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="2"
                android:textColor="#000000" />

                  <TextView
                android:id="@+id/topRtTv3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="337dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="3"
                android:textColor="#000000" />

                 <TextView
                android:id="@+id/topRtTv4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="359dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="4"
                android:textColor="#000000" /> 

                  <TextView
                android:id="@+id/topRtTv5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="382dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="5"
                android:textColor="#000000" /> 

                  <TextView
                android:id="@+id/topRtTv6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="404dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="6"
                android:textColor="#000000" />

                    <TextView
                android:id="@+id/topRtTv7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="428dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="7"
                android:textColor="#000000" />

                    <TextView
                android:id="@+id/topRtTv8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="450dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="8"
                android:textColor="#000000" />


            <TextView
                android:id="@+id/topRtTv9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="472dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="9"
                android:textColor="#000000" />


            <TextView
                android:id="@+id/topRtTv10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="490dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="10"
                android:textColor="#000000" />


            <TextView
                android:id="@+id/topRtTv11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="515dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="11"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/totalHoursTop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="550dp"
                android:layout_marginTop="90dp"
                android:textSize="14sp"
                android:text="Total Hours"
                android:textColor="#000000" />

        <RelativeLayout/>

        </LinearLayout>

 </LinearLayout>
Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • you're not supposed to set the density of the screen, use dpi and the OS will do the rest – VinceFR Jul 05 '13 at 08:33
  • Can you elaborate in details about dpi way of setting layouts? – sjain Jul 05 '13 at 08:36
  • http://developer.android.com/training/multiscreen/screendensities.html – VinceFR Jul 05 '13 at 08:38
  • I already looked that. The problem is my device density falls at ~216 DPI which is tvdpi layout. Can I use something like layout-tvdpi-land for that or something similar for this scenario? – sjain Jul 05 '13 at 08:40
  • you don't need a dpi dependent layout file...please add your xml layout file so i could correct it – VinceFR Jul 05 '13 at 08:43
  • gave you the top timeline layout xml. See the update. – sjain Jul 05 '13 at 08:50
  • layout_margins must be in dp, not sp – VinceFR Jul 05 '13 at 08:55
  • ok agreed but that isn't the main concern. The idea is to get density independent layouts for the same screen sized devices. – sjain Jul 05 '13 at 08:57
  • "This is causing a shorter timeline for higher density device (Tablet 2)"What element is your timeline?what do you mean by shorter?width or height? – VinceFR Jul 05 '13 at 09:05
  • See the diagram in the link. The timeline is `1 2 3 4 ..` numbers on top of bitmap. They are aligned left in width for tablet 2 due to its higher dpi. – sjain Jul 05 '13 at 09:06
  • Tablet 1 is showing fine when placed under `layout-large-land`. So I can use `layout-large-land-tvdpi` for tablet 2. Will it auto adjust the width if I do so ? Or I need to do that manually by changing the margins of timelines ? – sjain Jul 05 '13 at 09:09

0 Answers0