1

I am trying to add margins for each textbox added to a linear layout, which is inside a scrollview. However big margins i set, it doesn't reflect on the UI. Why? I have referred quite a few things on this stack overflow itself.

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ScrollView mainScrollView;
    LinearLayout scrollViewContainer;
    ArrayList<String> stringArr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainScrollView = (ScrollView) findViewById(R.id.mainScrollView);
        scrollViewContainer = (LinearLayout)findViewById(R.id.scrollViewContainer);

        stringArr = new ArrayList<String>();
        String str1 = "In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. " +
                "The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation).";
        String str2 = "Initializes a newly created String object so that it represents the same sequence of characters as the argument; " +
                "in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed," +
                " use of this constructor is unnecessary since Strings are immutable.";

        for(int ii = 0; ii < 10; ii++){
            if(ii%2 ==  0) {
                stringArr.add(str1);
            }else{
                stringArr.add(str2);
            }
        }
        Resources r = getResources();
        float pxLeftMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
        float pxTopMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
        float pxRightMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics());
        float pxBottomMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());

        LinearLayoutCompat.LayoutParams textViewLayoutParams =  new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        for(int ii = 0; ii < stringArr.size(); ii++){
            TextView textView = new TextView(this);
            textView.setTextSize(12f);
            textView.setText(stringArr.get(ii));
            textView.setLayoutParams(textViewLayoutParams);
            textViewLayoutParams.setMargins(10,30,10,30);
            textView.requestLayout();
            scrollViewContainer.addView(textView);
        }

    }
}
Hemant Bavle
  • 3,297
  • 1
  • 24
  • 30

4 Answers4

3

Your problem is in using

LinearLayoutCompat.LayoutParams textViewLayoutParams =  new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

Instead use:

LinearLayout.LayoutParams textViewLayoutParams =  new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
2
textViewLayoutParams.setMargins(10,30,10,30);
textView.setLayoutParams(textViewLayoutParams);

this could be the problem ... you were editing textViewLayoutParams AFTER you were setting it to a textView.setLayoutParams(textViewLayoutParams) ...

Azade
  • 359
  • 2
  • 15
Saim Mahmood
  • 426
  • 2
  • 12
  • 2
    OP was calling `requestLayout()` after manipulating the layout parameters. Your suggestion would make the code more efficient but it is functionally equivalent. – Eugen Pechanec Mar 29 '17 at 20:35
0

You are using LinearLayout so try to use LinearLayout.LayoutParams

John
  • 1,304
  • 1
  • 9
  • 17
-3
`<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:background="#fff"
 tools:context="com.example.sheheryar.quicktransport.Signin">


<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="false">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"            >

        <ImageView
            android:layout_width="250dp"
            android:layout_height="155dp"
            android:id="@+id/imageView2"
            android:background="@drawable/logo1"
            android:layout_marginTop="10dp"
            android:layout_gravity="center_horizontal" />

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40px">
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:textSize="15dp"
                android:inputType="textEmailAddress"
                android:ems="10"
                android:id="@+id/editTextemail"
                android:hint="Enter email"/>
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp">
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:textSize="15dp"
                android:inputType="textPassword"
                android:ems="10"
                android:id="@+id/editTextpassword"
                android:hint="Password"/>
        </android.support.design.widget.TextInputLayout>

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Remember Password"
            android:textColor="#6b6b6b"
            android:id="@+id/cbRemberPassword"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:checked="false" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Sign in"
            android:id="@+id/buttonsigin"
            android:textColor="#ffffff"
            android:onClick="userlogin"
            android:background="@drawable/button_styles1"
            android:layout_marginTop="20dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OR"
            android:id="@+id/textView1"
            android:textColor="#f99906"
            android:textStyle="bold"
            android:textSize="18dp"
            android:layout_marginTop="10dp"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Create New Account"
            android:id="@+id/textView"
            android:textColor="#6b6b6b"
            android:textSize="18dp"
            android:layout_marginTop="5dp"
            android:layout_gravity="center_horizontal" />


    </LinearLayout>
  </ScrollView>

</RelativeLayout>`

Check in XML file. Under resources > layouts. Set the margins. Maybe this can help.