2

In xml layout, gravity of the TextView is setted. In some condition, I want to change the gravity of the TextView in my coding.I have tried to set like this:

title.setGravity(Gravity.START);

but no change.

In xml I have aligned text view gravity to center. I have to change this gravity to START in some condition programmatically:

<TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text=" "
        android:drawablePadding="5dp"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

So how to do that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sandhiya
  • 339
  • 2
  • 14
  • Possible duplicate of [Android set the gravity for a TextView programmatically](https://stackoverflow.com/questions/3775705/android-set-the-gravity-for-a-textview-programmatically) – Neo Nov 06 '19 at 09:46
  • possible duplicate https://stackoverflow.com/questions/3775705/android-set-the-gravity-for-a-textview-programmatically – Asad Jivani Nov 06 '19 at 09:47
  • Your `TextView` has `android:layout_height="wrap_content"` which as you guess will wrap it's content. That's why if you change the gravity you won't notice any difference. You will have to change `layour_height` to `0dp` because you are using `ConstraintLayout` and it will work. – hardartcore Nov 06 '19 at 11:02
  • @hardartcore I have tried adding like this in my code```textView.setHeight(0); but now my text is not even displayed – Sandhiya Nov 06 '19 at 11:14
  • You should add it in xml file & make your `layour_width` to `0dip` too, so it can use only the constraints. Keep in mind that you have to set all constraints, left / right / top / bottom – hardartcore Nov 06 '19 at 12:37

3 Answers3

1
 TextView tview= (TextView) findViewById(R.layout.text_view);
 LayoutParams lp = new LayoutParams();
 lp.gravity = Gravity.CENTER_HORIZONTAL;
 tview.setLayoutParams(lp);
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
1

Finally found a solution for my question. I have tried doing this in my code

ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams();
params.horizontalBias = 0.2f; 
myView.setLayoutParams(params);

It worked for me...

Sandhiya
  • 339
  • 2
  • 14
0

This is perfect for setting the gravity of constraint layout, Here "container" is parent layout and "popup" also constraint layout but its child layout of "container"..

val container = findviewbyid.....
val DialogPopup = findviewbyid...

     val constraintSet = ConstraintSet()
        constraintSet.clone(container)
//whole code is setting your content or layout at bottom left corner

//below code is setting gravity at start.
  constraintSet.connect(DialogPopup .getId(),ConstraintSet.START,
                container.getId(),
                ConstraintSet.START)


// below code is for setting gravity at bottom
 constraintSet.connect(
            DialogPopup.getId(),
            ConstraintSet.BOTTOM,
            container.getId(),
            ConstraintSet.BOTTOM
        )
        constraintSet.applyTo(mBinding.container)