donoachua
donoachua

Reputation: 193

Android padding between textviews programmatically

i programmatically created textviews and i added it in my layout,but i have one problem.i can't padding between my textviews.this is a my source

private class DigitView extends TextView {
    public DigitView(Context context) {
        this(context, null);
    }

    public DigitView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DigitView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        // If selected draw the accent
        if (isSelected()) {
            this.setBackgroundResource(R.drawable.background_border);

        }
        else
        {
            this.setBackgroundResource(R.drawable.background_border_unselect);
        }

    }

}

this is a my xml code(i added my textviews in this layout)

 <LinearLayout
        android:id="@+id/my_Container_child"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >
    </LinearLayout>




  for (int i = 0; i < mDigits; i++) {
        digitView= new DigitView(getContext());
        digitView.setWidth(valueInPixels);
        digitView.setHeight(valueInPixels);
        digitView.setTextColor(Color.GREEN);
        digitView.setTextSize(mDigitTextSize);
        digitView.setGravity(Gravity.CENTER);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            digitView.setElevation(mDigitElevation);
        }
        childView.addView(digitView);
    }

as i said i can add 4 textview (mDigits is 4) in my layout but i can't add padding between my textviews how i can solve my problem? if anyone knows solution please help me thanks everyone

Upvotes: 0

Views: 1582

Answers (2)

Karakuri
Karakuri

Reputation: 38585

Whatever is the parent layout of the TextViews, you need to create the appropriate LayoutParams object for each TextView. Also, you should use layoutParams for the width and height instead of setting them directly on the view.

LinearLayout layout = (LinearLayout) findViewById(R.id.my_Container_child);
for (int i = 0; i < mDigits; i++) {
    DigitView digitView = new DigitView(getContext());
    digitView.setTextColor(Color.GREEN);
    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(valueInPixels, valueInPixels);
    if (i > 0) {
        lp.leftMargin = ...; // this adds space between this view and the previous one
    }
    layout.addView(digitView, lp);
}

EDIT

Alternatively, you can do this without the LayoutParams if you use a blank divider drawable on the LinearLayout.

in res/drawable/empty_divider.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <size android:height="0dp" android:width="8dp" />
</shape>

in your layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:showDividers="middle"
    android:divider="@drawable/ll_divider">

    <!-- ... -->

</LinearLayout>

Upvotes: 1

AlbAtNf
AlbAtNf

Reputation: 3909

Did you already try this:

digitView.setPadding(...);

From Docs

Upvotes: 0

Related Questions