0

I programmatically created four Textviews and added it in my Linearlayout.my linearlayout has 216dp width and TextView which I created pro-grammatically has 48 dp width and height. I want to add padding between TextView. I wrote some code but I received this result

as you can see padding is not correct,because in right side is incorrect

this is a my code

 for (int i = 0; i < mDigits; i++) {
        DigitView digitView = new DigitView(getContext());

        digitView.setWidth(valueInPixels);//48dp
        digitView.setHeight(valueInPixels);//48dp
        digitView.setTextColor(Color.WHITE);
        digitView.setTextSize(mDigitTextSize);
        digitView.setGravity(Gravity.CENTER);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            digitView.setElevation(mDigitElevation);
        }

        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if (i > 0) {
            lp.leftMargin = 10;//problem is this line
        }
        childView.addView(digitView, lp);//childview 216dp
    }

how i can to create correct padding between views programmatically? if anyone has solution please help me thanks.

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
donoachua
  • 193
  • 2
  • 16
  • Add margin to your dynamic text fields and remove this part:LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); if (i > 0) { lp.leftMargin = 10;//problem is this line } – Tharindu Welagedara Nov 06 '15 at 10:24
  • @TdSoft i don't know how to do this.can you show me source or update my code? – donoachua Nov 06 '15 at 10:25
  • 1
    Possible duplicate of [Add padding on view programmatically](http://stackoverflow.com/questions/9685658/add-padding-on-view-programmatically) – Nanoc Nov 06 '15 at 10:42
  • @Nanoc thanks.but i know to to add padding but as i said my container has 216 dp width and in my views has 48dp width and simple i want same padding between my views.please see my picture – donoachua Nov 06 '15 at 10:49
  • @donoachua pls check my answer. – Tharindu Welagedara Nov 06 '15 at 10:53

3 Answers3

0

Add padding on view programmatically

Here you have the solution to create a padding in java code.

Mtoypc
  • 464
  • 1
  • 6
  • 24
  • thanks.but i know to to add padding but as i said my container has 216 dp width and in my views has 48dp width and simple i want same padding between my views.please see my picture – donoachua Nov 06 '15 at 10:40
  • Use values/dimen.xml to define parking sizes for different screen sizes – Tharindu Welagedara Nov 06 '15 at 11:12
0

Try this,

public void setPadding (int left, int top, int right, int bottom)

view.setPadding(0,padding,0,0);
Parth Bhayani
  • 1,894
  • 3
  • 17
  • 35
  • thanks.but i know to to add padding but as i said my container has 216 dp width and in my views has 48dp width and simple i want same padding between my views.please see my picture @Parth Bhayani – donoachua Nov 06 '15 at 10:50
  • for that, you need to give the weight for each of your view and add that view to your activity, that works fine. – Parth Bhayani Nov 06 '15 at 11:09
0

First add appropriate android:paddingLeft to your childview I added as below:

<LinearLayout
        android:id="@+id/childView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:background="#000"
        android:orientation="horizontal">

Then edit you code as below

for (int i = 0; i < 4; i++) {
            DigitView digitView = new DigitView(this);

            digitView.setWidth(valueInPixels);//48dp
            digitView.setHeight(valueInPixels);//48dp
            digitView.setTextColor(Color.WHITE);
            digitView.setBackgroundColor(Color.YELLOW);
            digitView.setTextSize(mDigitTextSize);
            digitView.setGravity(Gravity.CENTER);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                digitView.setElevation(mDigitElevation);
            }

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                            lp.rightMargin = 10;//problem is this line

            childView.addView(digitView, lp);//childview 216dp
        }
Tharindu Welagedara
  • 2,660
  • 18
  • 27