简体   繁体   中英

Android - How to get hint size of TextInputLayout programmatically when focused?

How to get hint size of Enter Email Id(mentioned in this image) ?

图片

Text size of hint is same as the text to be inputed in TextInputLayout . To get textSize in Java do like below:

XML:

 <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="157dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="158dp">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:id="@+id/TextInputEditText"
            android:layout_height="wrap_content"
            android:hint="hint" />
    </android.support.design.widget.TextInputLayout>

MainActivity.java:

    final TextInputEditText textInputEditText = findViewById(R.id.TextInputEditText);


    textInputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            float size = textInputEditText.getTextSize();
            Log.i("TEXT_SIZE", String.valueOf(size));
        }
    });

If you want to change textSize you can use textInputEditText.setTextSize();

For example: For get hint size you need to get a edittext text size like this:

    EditText mEmailId= findViewById(R.id.et_email);
    mEmailId.getTextSize();

String.xml

<string name="edittext_hint"><font size="15">Hint set here</font></string>

then in your XML just write

<EditText
    android:id="@+id/input_msg"
    android:inputType="text"
    android:hint="@string/edittext_hint" />

OR

Reduce the font size on the EditText - that will reduce the size of the hint as well. ie android:textSize="15dp"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM