Home > Enterprise >  How do I add 'android:layout_marginStart="10pt"' programatically in my textview
How do I add 'android:layout_marginStart="10pt"' programatically in my textview

Time:07-26

I want to add the style android:layout_marginStart="10pt" into my dynamically created textview:

    textswitch.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            text = new TextView(record_viewer.this);
            text.setTextColor(Color.WHITE);
            text.setGravity(Gravity.START); 

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-2, -2); //throws java.lang.ClassCastException
            params.setMargins(10, 0, 0, 0);
            text.setLayoutParams(params);

            return text;
        }
    });

How do I proceed?

EDIT: My layout hierarchy is as follwoing:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"

    <LinearLayout>

        <LinearLayout>

            <TableLayout>

                <TableRow>

                    <TextSwitcher

                        android:id="@ id/text_switch"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                    </TextSwitcher>

                <TableRow>
            
            <TableLayout>

         </LinearLayout>

    </LinearLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

I tried using LinearLayout.LayoutParams but I keep getting the error:

android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

I don't get it. Where is FrameLayout in my xml?

CodePudding user response:

If you using LinearLayout on your textview as a parent view give params like below

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(10,10,10,10); text.setLayoutParams(params);

If you using RelativeLayout on your textview as a parent view give params like below

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.setMargins(10,10,10,10); text.setLayoutParams(params);

If you using ConstraintLayout on your textview as a parent view give params like below

ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT); params.setMargins(10, 10, 10, 10); text.setLayoutParams(params);

Change LayoutParams as per your requirement.

CodePudding user response:

First you need to declare your start margin size as a dimension resource:

<dimen name="switch_margin_start">10pt</dimen>

You must retrieve it as pixel dimension in order to apply it programmatically:

context.getResources().getDimensionPixelSize(R.dimen.switch_margin_start);

Then all you need to do is apply it on your switch's layout, which given it's hosted inside a FrameLayout can look something like this:

applyStartMargin(context, R.dimen.switch_margin_start);
private void applyStartMargin(final Context context, @DimenRes final int dimenResource) {
    final int startMargin = context.getResources().getDimensionPixelSize(dimenResource);
    final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
    params.setMarginStart(startMargin);
}
  • Related