EditText gravity doesn't work with wrap_content

android:layout_gravity not working
android:gravity
textview android
android:gravity="bottom"
android:layout_gravity="start"
linearlayout gravity
android:textalignment
android:gravity="center_vertical"

Following code doesn't match gravity of EditText component on a vertical axis in its box.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="Test" />
</RelativeLayout>

Is there any workaround to have fallowing properties and have text centered?

EDIT:

This might not be seen at first but i have in both center_vertical with wrap_content on height but in first example text is definetly centered in its box, and in second its very off.

Edit2:

    <carbon.widget.LinearLayout
        android:id="@+id/linearLayout_login_footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="@dimen/general_footer_P"
        android:background="@android:color/transparent"
        android:orientation="vertical">

        <carbon.widget.TextView
            android:id="@+id/textView_login_footer_info1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:carbon_fontFamily="sans-serif-light"
            android:gravity="center_horizontal|center_vertical"
            android:text="@string/activity_footer_bottom_info1"
            android:textColor="@color/text_edit"
            android:textSize="@dimen/general_footer_size_P" />

        <carbon.widget.LinearLayout
            android:id="@+id/linearLayout_login_footer_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal|center_vertical"
            android:orientation="horizontal">

            <carbon.widget.TextView
                android:id="@+id/textView_login_footer_info2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:carbon_fontFamily="sans-serif-light"
                android:text="@string/activity_footer_bottom_info2"
                android:textColor="@color/text_edit"
                android:textSize="@dimen/general_footer_size_P" />

            <carbon.widget.TextView
                android:id="@+id/textView_login_footer_info3_link"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="2dp"
                android:autoLink="all"
                app:carbon_fontFamily="sans-serif-light"
                android:text="@string/activity_footer_bottom_info3"
                android:textColor="@color/link"
                android:textSize="@dimen/general_footer_size_P" />

        </carbon.widget.LinearLayout>
    </carbon.widget.LinearLayout>

    <carbon.widget.LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/linearLayout_login_footer"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="@integer/login_layout_weight_sum_P">

        <carbon.widget.LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="@integer/login_layout_left_margin_weight_P" />

        <carbon.widget.LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="@integer/login_layout_weight_P"
            android:orientation="vertical">

            <carbon.widget.Button
                android:id="@+id/button_login_facebook"
                android:layout_width="match_parent"
                android:layout_height="@dimen/login_button_height_P"
                android:layout_marginBottom="@dimen/login_layout_button_bottom_margin_P"
                android:background="@color/facebook_main"
                android:drawableLeft="@drawable/icons_fb_icon_on"
                android:drawableStart="@drawable/icons_fb_icon_on"
                app:carbon_fontFamily="sans-serif-light"
                android:text="@string/activity_login_facebook_login"
                app:carbon_textAllCaps="false"
                android:textColor="@color/text_white"
                android:textSize="@dimen/login_button_text_size_P" />


            <carbon.widget.Button
                android:id="@+id/button_login_twitter"
                android:layout_width="match_parent"
                android:layout_height="@dimen/login_button_height_P"
                android:layout_marginBottom="@dimen/login_layout_button_bottom_margin_P"
                android:background="@color/twitter_main"
                android:drawableLeft="@drawable/icons_twitter_icon_on"
                android:drawableStart="@drawable/icons_twitter_icon_on"
                app:carbon_fontFamily="sans-serif-light"
                android:text="@string/activity_login_twitter_login"
                app:carbon_textAllCaps="false"
                android:textColor="@color/text_white"
                android:textSize="@dimen/login_button_text_size_P" />

            <carbon.widget.EditText
                android:id="@+id/editText_login_email"
                android:layout_width="match_parent"
                android:layout_height="@dimen/login_button_height_P"
                android:layout_marginBottom="@dimen/login_layout_button_bottom_margin_P"
                android:background="@color/button_white"
                android:cursorVisible="true"
                android:ellipsize="end"
                app:carbon_fontFamily="sans-serif-light"
                android:gravity="center"
                android:hint="@string/activity_login_email_hint"
                android:inputType="textEmailAddress"
                android:textColor="@color/text_edit"
                android:textColorHint="@color/text_footer"
                android:textCursorDrawable="@null"
                android:textSize="@dimen/login_button_text_size_P"
                android:singleLine="true" />

            <carbon.widget.EditText
                android:id="@+id/editText_login_password"
                android:layout_width="match_parent"
                android:layout_height="@dimen/login_button_height_P"
                android:layout_marginBottom="@dimen/login_layout_button_bottom_margin_P"
                android:background="@color/button_white"
                android:cursorVisible="true"
                android:ellipsize="end"
                app:carbon_fontFamily="sans-serif-light"
                android:gravity="center"
                android:hint="@string/activity_login_password_hint"
                android:inputType="textPassword"
                android:textColor="@color/text_edit"
                android:textColorHint="@color/text_footer"
                android:textCursorDrawable="@null"
                android:textSize="@dimen/login_button_text_size_P"
                android:singleLine="true"/>

            <carbon.widget.Button
                android:id="@+id/button_login_login"
                android:layout_width="match_parent"
                android:layout_height="@dimen/login_button_height_P"
                android:layout_marginBottom="@dimen/login_layout_button_bottom_margin_P"
                android:background="@drawable/shadow_border_blue_panel"
                app:carbon_fontFamily="sans-serif"
                android:text="@string/activity_login_LOGIN"
                android:textColor="@color/text_white"
                android:textSize="@dimen/login_button_text_size_P" />

            <carbon.widget.TextView
                android:id="@+id/textView_login_forgot_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/login_layout_forgot_password_margin_top_P"
                app:carbon_fontFamily="sans-serif-light"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/activity_login_forgot_password"
                android:textColor="@color/text_edit"
                android:textSize="@dimen/login_layout_forgot_password_text_size_P" />
        </carbon.widget.LinearLayout>

        <carbon.widget.LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="@integer/login_layout_right_margin_weight_P" />
    </carbon.widget.LinearLayout>


</carbon.widget.RelativeLayout>

Full code didnt fit :/ This is not carbon related issue as i get sure by its author

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="Test" />
</RelativeLayout>

Understanding the difference between gravity and layout_gravity , layout_gravity − Layout gravity same as gravity but it going to put view based on parent layout corners as shown below. <EditText android:id� The EditText in your layout has its height set to wrap_content, so gravity set to center_vertical has no effect. You're right, not all Carbon's widgets are working correctly with the preview. For example ImageView cannot render SVG using src parameter, because it's not possible to load drawable resources other than xml and bitmaps in Android

use android:layout_centerVertical="true" instead in relativeLayouts

What is difference between gravity and layout_gravity on Android?, com.google.android.material:material:1.2.0-alpha05. If it helps, setting the TextInputLayout's attribute hintEnabled=false does put the EditText to� Questions: I have an EditText in my application. I want to align the text in it to the right instead of the default left. I tried adding android:layout_gravity="right" but this doesn’t seem to work. any other suggestions please?

first of all you should use only android:gravity and layout_gravity for Views inside LinearLayout and not RelativeLayouts

you are probably mixing the definitions of android:gravity and android:layout_gravity...in your case you should add the tag android:layout_gravity:center_vertical and remove android:gravity from Edittext...

setting android:layout_gravity tag in LinearLayout you simple tell the framework to set the gravity of the View in its parent(in your case the view is Editext and it is going to get your desired gravity in its parent which is the LinearLayout)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res   /android"
 android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical">

   <EditText
     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Test" />
</LinearLayout>

on the other hand android:gravity set the gravity of the content of the View where is on...so setting android:gravity inside Editext just change the gravity of the text of the Editext..

TextView, android - EditText vertical alignment android:gravity Specifies how to align the text by the view's x- and/or y-axis when the text is smaller than� They appear side by side at the bottom of the screen. But when i type multiple lines into the first EditText(on the left), the second one(at the right) also keeps moving upwards even though it is empty. I have specified android:layout_gravity="bottom" and android:gravity="bottom" for the second EditText. But it has no effect.

If you got here, you are probably looking for this:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    tools:background="@color/quiz_background_tan_color">

    <TextView
        android:id="@+id/your_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:gravity="start|center_vertical"
        />

</android.support.constraint.ConstraintLayout>

[TextInputLayout] gravity "top" is not working for hint � Issue #933 , If you are using a custom EditText as TextInputLayout's child and your text field theme overlays (themes all text fields but does not affect other components):. Android: Edittext with gravity center not working on device (7) In Android I'm using a single line edittext with gravity set to center. In the Emulator this works great both the hint and a blinking cursor shows up.

Try This:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:text="Test" />
</RelativeLayout>

EditText vertical alignment, setMargins(t, 0, t, 0); layoutParams.gravity = Gravity.CENTER_HORIZONTAL; editText.setLayoutParams(layoutParams); int padding = (int) (15 * dpi); editText. EditText is a fixed item on the screen. we want scroll the layout contains the EditText is enough. that is the best design too. you have put android:ellipsize="end" instead of android:scrollHorizontally.

Text fields - Material Design, android:gravity, Used to specify how to align the text like left, right, center, top, etc. android:hint, Used to display the hint text when text is empty. android:text� However, Theme.AppCompat doesn’t implement every material item used in Google’s official applications. One of the features that is not present in the AppCompat theme is the possibility to position a floating label on top of an EditText widget. You can see what I mean in the below example.

android.widget.EditText.setLayoutParams java code examples , gravity: The gravity attribute is an optional attribute which is used to control the alignment of the text like left, right, center, top, bottom, center_vertical,� editText.setFocusableInTouchMode(true) The EditText will be able to get the focus when the user touch it. When the main layout (activity, dialog, etc.) becomes visible the EditText doesn't automatically get the focus even though it is the first view in the layout.

Android EditText in Kotlin, Overview. Every Android device comes with a collection of standard fonts: Droid Sans, Droid Sans Mono and Droid Serif. They were designed to be optimal for mobile displays, so these are the three fonts you will be working with most of the time and they can be styled using a handful of XML attributes.

Comments
  • what you want exactly? where to align your text?
  • Will edit with screenshoot since i just saw my question is unclear
  • @TomaszJuszczak screenshot will be better
  • Gravity set the position of an INNER element. So, as you set the wrap_content height, the height of the EditText will match your text height and you will see no difference. In a LinearLayout's child you can use layout_gravity to set the position of it inside its parent, but with relative layout you have to play with the center_vertical/center_horizontal/alignParent attributes
  • @TomaszJuszczak what this means--> "have text centered" in your question
  • RelativeLayout height is match parent how the layout will move to center...did you test it
  • This is not anwser to this question since full gravity i want should look like this android:gravity="left|center_vertical" and yet center doesn't change anything on a vertical axis. Only move it on center_horizontal
  • @TomaszJuszczak I think you want your text to be center..check now i update it