0

I'm fairly new to android and xml layouting. I've been trying to make an app, and already done with the programming bit. However i'm stuck on the finalisation of the layout. Really need a hand. My app should look like this:

Layout

I've divided the screen in 4 Linear Layouts as I have numbered in the picture as seen below:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.FirstVersion.android.cwd.Play"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/material_blue_grey_800">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnGoBack"
        android:text="Go Back"
        android:layout_weight="1"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Profile Information"
        android:background="@color/accent_material_dark"
        android:layout_weight="10" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LDrawerAction"
        android:text="Menu"
        android:clickable="false"
        android:layout_weight="1"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp" />

</LinearLayout>

//endregion

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Section2"
android:orientation="vertical">

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Section3"
android:orientation="horizontal">

    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="5sp"
    android:textColor="@color/colorWhite"
    android:background="#990000"
    android:maxLines = "3"
    android:scrollbars = "vertical"
    android:id="@+id/txtSection3" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="15"
android:orientation="vertical"
android:baselineAligned="false">

    <TableLayout
    android:id="@+id/Section4"
    android:layout_width="match_parent"
    android:layout_weight="15"
    android:layout_height="wrap_content">

    </TableLayout>
</LinearLayout>

</LinearLayout>
</android.support.v4.widget.DrawerLayout>

The problem now is that, the second section is filled programmatically and it has a range of rows from 8 to 14. The same for the columns. Every block is a different button which I add with this code:

Lay= (LinearLayout)findViewById(R.id.Section2);
for (int row = 0; row < numberOfRows; row++)
    {
        LinearLayout Linearrow = new LinearLayout(this);
        Linearrow.setOrientation(LinearLayout.HORIZONTAL);

        for (int column = 0; column < numberOfColumns; column++)
        {
            View a = Chosen.blocks[row][column]; //This is a View the extend Button that has no layout properties defined 

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT,  1.0f);

            a.setLayoutParams(params);

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            lp.weight = 1.0f;
            Linearrow.addView(a, lp);
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        lp.weight = 1.0f;
        Lay.addView(Linearrow, lp);
    }

I have tried even using a TableLayout and assigning a small image as background so it could get resized with the following code:

TL =(TableLayout)fa.findViewById(R.id.Section2);

    for (int i = 0; i < numberOfRows; i++)
    {
        TableRow tableRow = new TableRow(c);

        for(int j = 0; j < numberOfColumns; j++)
        {
            String s = T[(i * 10) + j];
            Block b = new Block(c, s);
            //int w = Math.round(c.getResources().getDisplayMetrics().density * 20);
            //int h = Math.round(c.getResources().getDisplayMetrics().density * 80);
            //b.setLayoutParams(new  TableRow.LayoutParams(w, h, 2.0f));

            b.setLayoutParams(new  TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
            b.setBackgroundResource(R.drawable.g_3);
            tableRow.addView(b);
            tableRow.setLayoutParams(new android.widget.LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5f));

            final Block fb = b;
        }
        TL.addView(tableRow);
    }

When I run the app, I can see that unless I open it on a big screen phone, the content is not visible entirely(as if the height is set fixed manually of the layouts), and since it is not scroll-able, the content is not visible. How can I rearrange or modify the layout so it looks as in the picture above?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • For the columns of the Table layout, you may find the stretch columns attribute useful – OneCricketeer Jul 31 '16 at 17:27
  • Why dont you use `android.support.v7.widget.RecyclerView` for that dynamic content? In other case add a layout that scrolls viz `android.support.v4.widget.NestedScrollView` as parent to `LinearLaout` with height and width as `match_parent`. – JavaGhost Jul 31 '16 at 17:28
  • @cricket_007 I have already set it as a property and doesn't make a difference. – blu3f1rest0rm Jul 31 '16 at 19:55
  • @JavaGhost I do not need the Layout scroll-able. I need it to fit 100% on the screen, regardless the number of rows in the view – blu3f1rest0rm Jul 31 '16 at 19:56
  • I will make a difference if you set it to `*`. Note I said columns, not the rows / overall layout. – OneCricketeer Jul 31 '16 at 20:00
  • @cricket_007 Retried that. Nothing changed. As width, the layout is always inside the limit and resized in automatic for every screen. I think it is because the width is always set as MatchParent. Instead the height is wrap content, and I don't have a constant number of rows – blu3f1rest0rm Jul 31 '16 at 20:42
  • @cricket_007 I dint realize that I had clicked d 2nd comment button. Using stack exchange android app, scrolled and mistakenly clicked 2nd button. Thanks for pointing out. blu3f1rest0rm you will have to use weights to the dynamic views you are adding. Let's say you have 3 rows then each row will have height 0dp & 0.33 weight and parent will have weight sum as 1. Since the height is now fixed, the children views' heights have to adapt according to the ratio. I suggest you use support library's percentage layout for simplifying the computations. – JavaGhost Jul 31 '16 at 22:25
  • @JavaGhost trying to put the height at 0dp and dividing the weight like you suggested, but the VIews with 0dp of height, disappear from the screen. Can you suggest me a library o use, or point me to an example? – blu3f1rest0rm Aug 01 '16 at 20:29

3 Answers3

0

Use

 android:layout_weight

attribute to arrange in equal parts in linear layout. See in the Link

Community
  • 1
  • 1
NIKHIL SONI
  • 84
  • 1
  • 1
  • 1
    I have already set the weight of the elements inside the linear. The problem lies in the linearLayout itself, since it extends of the screen when the elements are added. I have even add the weight sum. – blu3f1rest0rm Jul 31 '16 at 18:23
  • Add weight to linear layouts so that they have fixed heights based on screen size. Then if you add children, they will fit in. – JavaGhost Jul 31 '16 at 20:00
  • @JavaGhost Will it fit the screen when views are dynamically loaded? – OneCricketeer Jul 31 '16 at 20:02
  • Try this, to your 1st vertical linear layout give weight sum as 1. To first child linear layout give height 0dp and weight 0.1, section2 linear layout height 0dp and weight 0.4, section3 layout height 0dp and weight 0.1, and the last layout (parent of section4) height 0dp and weight 0.4. You can see the height adaptations in the preview itself. Adjust the weights values so that it fits the content as you need. – JavaGhost Jul 31 '16 at 20:09
  • I have been playing with the weight parameter as @JavaGhost said. I can't seem to find an equilibrium since in more than one case, the 4 views get under each other because it might get more rows than expected. – blu3f1rest0rm Jul 31 '16 at 21:20
0

Make fixed height of layouts using layout_weight

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:weightSum="12"
        android:orientation="horizontal"
        android:background="@color/material_blue_grey_800">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnGoBack"
            android:text="Go Back"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Profile Information"
            android:background="@color/accent_material_dark"
            android:layout_weight="10" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/LDrawerAction"
            android:text="Menu"
            android:clickable="false"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4"
        android:weightSum="1"
        android:id="@+id/Section2"
        android:orientation="vertical">

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:id="@+id/Section3"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="5sp"
            android:textColor="@android:color/white"
            android:background="#990000"
            android:maxLines = "3"
            android:scrollbars = "vertical"
            android:id="@+id/txtSection3" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4"
        android:weightSum="15"
        android:orientation="vertical"
        android:baselineAligned="false">

        <TableLayout
            android:id="@+id/Section4"
            android:layout_width="match_parent"
            android:layout_weight="15"
            android:layout_height="wrap_content">

        </TableLayout>
    </LinearLayout>

</LinearLayout>
</android.support.v4.widget.DrawerLayout>

And in your java class where you add views dynamically, calculate the weights of children accordingly as given below

Lay= (LinearLayout)findViewById(R.id.Section2);
Lay.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int totalWidth = Lay.getMeasuredWidth();
float rowWeight = 1 / numberOfRows;
int columnWidth = totalWidth / numberOfColumns;
for (int row = 0; row < numberOfRows; row++)
{
    LinearLayout Linearrow = new LinearLayout(this);
    Linearrow.setOrientation(LinearLayout.HORIZONTAL);

    for (int column = 0; column < numberOfColumns; column++)
    {
        View a = Chosen.blocks[row][column]; //This is a View the extend Button that has no layout properties defined
        // height is determined by row's layout weight
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(columnWidth,
                LinearLayout.LayoutParams.MATCH_PARENT,  1.0f);

        a.setLayoutParams(params);
        Linearrow.addView(a);
    }
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    lp.weight = rowWeight;
    Lay.addView(Linearrow, lp);
}

I've not tested these, but just might work with few changes that you can figure out once you run this code.

JavaGhost
  • 406
  • 4
  • 8
  • I tried the solution here and added even the method specified from you in the comments of the question. What I was doing wrong was the height of the sections since using the weight it needed ALL of those to get a height of 0d and not only some. Thank you – blu3f1rest0rm Aug 03 '16 at 19:05
0

It is better to use View inside ScrollView. Use the below code:-

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.FirstVersion.android.cwd.Play"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/material_blue_grey_800">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnGoBack"
            android:text="Go Back"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Profile Information"
            android:background="@color/accent_material_dark"
            android:layout_weight="10" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/LDrawerAction"
            android:text="Menu"
            android:clickable="false"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp" />

    </LinearLayout>

    //endregion
    <ScrollView 
android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Section2"
    android:orientation="vertical">

    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Section3"
    android:orientation="horizontal">

        <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="5sp"
        android:textColor="@color/colorWhite"
        android:background="#990000"
        android:maxLines = "3"
        android:scrollbars = "vertical"
        android:id="@+id/txtSection3" />

    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="15"
    android:orientation="vertical"
    android:baselineAligned="false">

        <TableLayout
        android:id="@+id/Section4"
        android:layout_width="match_parent"
        android:layout_weight="15"
        android:layout_height="wrap_content">

        </TableLayout>
    </LinearLayout>
    </LinearLayout>
</ScrollView>
    </LinearLayout>
    </android.support.v4.widget.DrawerLayout>
Aditi
  • 455
  • 4
  • 13