Tuesday, 26 November 2019

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.



from Text size set through variable tag in data binding

No comments:

Post a Comment