Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text size set through variable tag in data binding

I want to change my edit text size through a variable tag in layout tag into data binding but I am not able to set proper size.

I am adding my custom edit text layout here and I am pasting my layout also so please guide me on how I can set dynamic text size when I am using this edit text in the layout.

This is custom edit text layout where I declared textSize variable and type is Float

<layout 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">

    <data>
        <import type="android.text.InputType" />
        <variable name="textSize" type="Float" />
        <variable name="inputType1" type="Integer" />
        <variable name="visibility" type="Boolean" />
        <variable name="textColor" type="Integer" />
        <variable name="holderColor" type="Integer" />
        <variable name="hintColor" type="Integer" />
        <variable name="holderText" type="String" />
        <variable name="hintText" type="String" />
        <variable name="holderVisibility" type="Boolean" />
        <variable name="leftIcon" type="android.graphics.drawable.Drawable" />
        <variable name="editTextBottomLine" type="android.graphics.drawable.Drawable" />
        <variable name="rightIcon" type="android.graphics.drawable.Drawable" />   
        <variable name="fontStyle" type="Integer" />
        <variable name="textField" type="androidx.databinding.ObservableField&lt;String&gt;" />   
        <variable name="isTypePhoneNo" type="Boolean" />
        <variable name="contentMaxLength" type="Integer" />
        <variable name="emojiVisibility" type="Boolean" />
        <variable name="clickListener" type="android.view.View.OnClickListener" />
        <variable name="hasFocus" type="androidx.databinding.ObservableBoolean" />    
        <variable name="enableState" type="Boolean" />
        <variable name="textWatcher" type="android.text.TextWatcher" />    
        <variable name="onFocus" type="android.view.View.OnFocusChangeListener" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/cl_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:id="@+id/edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:drawableStart="@{leftIcon}"
                android:drawableEnd="@{rightIcon}"
                android:drawablePadding="5sp"
                android:fontFamily="@font/inter_regular"
                android:hint="@{hintText}"
                android:inputType="@{inputType1}"
                android:maxLength="@{contentMaxLength?? 256}"
                android:onClick="@{clickListener}"
                android:text="@={textField}"
                android:textColor="@{textColor}"
                android:textColorHint="@{hintColor?? @color/white}"
                android:textCursorDrawable="@drawable/cursor_color"
                android:textSize="@{textSize ?? @dimen/edit_text_size_26dp}"
                app:hasFocus="@{hasFocus}"
                app:isPhoneNoFormat="@{isTypePhoneNo}"
                app:keyListener="@{enableState??false}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:textWatcher="@{textWatcher}"
                app:onFocus="@{onFocus}"
                tools:text="@string/dummy_hint_edittext" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginTop="10dp"
            android:background="@{editTextBottomLine ?? @drawable/dot_line_gray}"
            app:layout_constraintTop_toBottomOf="@+id/cl_edit_text" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:clickable="true"
            android:onClick="@{clickListener}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:visibility="@{enableState?? false}" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

And this is my layout class where I am using this layout and here I am declaring textSize in my this layout but it's not working.

<include
    android:id="@+id/et_userId"
    layout="@layout/edittext_without_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    **app:textSize="@{18}"**
    android:layout_marginTop="@dimen/line_spacing_9dp"
    app:hintColor="@{@color/place_holder_color_dark}"
    app:layout_constraintTop_toBottomOf="@+id/textView2"
    app:hintText="@{@string/dummy_user_id}"
    app:inputType1="@{InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE}"
    app:textColor="@{@color/color_black_welcome}"
    app:textField="@={viewModel.userId}"
    app:editTextBottomLine="@{@drawable/dot_line_gray}"/>

Explain: First I created custom edit text layout with variable tag textSize and after that, I am using this layout in another screen layout and on that screen, I am trying to set edit text size though textSize variable. I tried textSize variable value by changing with Integer also but this is not useful. So if anyone is having a solution for that it will be very good for me.

like image 769
Er. Rakesh Prajapat Avatar asked Nov 22 '19 04:11

Er. Rakesh Prajapat


People also ask

Which method is used to change the text size of the text view?

To use preset sizes to set up the autosizing of TextView programmatically, call the setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit) method. Provide an array of sizes and any TypedValue dimension unit for the size.

How do you use data binding with tag?

Data Binding in <include> Layouts xml layout to enable data binding. The <data> and <variable> tags are used to bind the student object. To pass the user to included content_student_main layout, bind:student=”@{student}” is used. Without this, the user object won't be accessible in content_student_main layout.


2 Answers

I think the best way is to adding a BindingAdapter

First add this part of java code.

Atention This method must be static

@BindingAdapter("android:textSize")
public static void bindTextSize(TextView textView, int size) {
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}

Now you can add text size for text view and edit text using int,Example:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@{12}" />
like image 143
mahdi shahbazi Avatar answered Oct 04 '22 06:10

mahdi shahbazi


See the data-binding expressions for includes ...this would be something alike:

<include
    layout="@layout/edittext_without_textview"
    bind:textSize="@{textSize}"
    ... />

Binding all of the values individually might be way more effort than just binding to some view-configuration class, which has these values; else there is little advance in using data-binding.

like image 23
Martin Zeitler Avatar answered Oct 04 '22 08:10

Martin Zeitler