Thursday, May 21, 2015

How to change TextView width programmatically in Android

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set TextView Width"
        android:textAllCaps="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textSize="16sp"
        android:text="Lorem ipsum dolor sit amet, consectetur
         adipiscing elit. Proin aliquet magna vel lorem tempus
         pharetra. Praesent ut erat non mi semper porta.
         Pellentesque in dui aliquam, scelerisque diam at,
         dignissim magna. Vestibulum sollicitudin
         at magna ut pellentesque. Mauris aliquet turpis at eros
         iaculis pellentesque. Nulla varius nibh nec elit lacinia,
         ultrices egestas tellus mollis."
        android:padding="32dp"
        android:background="#FFFFF0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button"/>

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java

package com.cfsuman.androidtutorials;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        Button button = findViewById(R.id.button);
        TextView textView = findViewById(R.id.textView);

        // Button click listener
        button.setOnClickListener(view -> {
            // Set TextView width in pixels
            //textView.setWidth(750);

            // Another way to set TextView width
            // Get the TextView current LayoutParams
            ConstraintLayout.LayoutParams lp =
                    (ConstraintLayout.LayoutParams)
                            textView.getLayoutParams();

            // Set width in LayoutParams in pixels
            lp.width = 750;

            // Apply the updated layout parameters to TextView
            textView.setLayoutParams(lp);
        });
    }
}