EditText wont display at the center of the TextInputLayout - android-layout

No matter what I do my EditText appears like this :
My XML :
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout_search"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="top"
android:gravity="top"
android:layout_height="wrap_content">
<EditText
android:background="#null"
android:textColor="#color/black"
android:layout_centerInParent="true"
android:gravity="top"
android:layout_gravity="top"
android:id="#+id/SearchText"
android:textColorLink="#color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/normatext"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
I changed all possible values for gravity and layout_gravity in every possible variation! Any ideas ?
EDIT :
Full Layout :
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="0dp"
android:paddingTop="6dp"
android:paddingBottom="5dp"
android:layout_margin="3dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="45dp">
<Button
android:layout_width="43dp"
android:layout_height="wrap_content"
android:background="#drawable/ham"
android:id="#+id/hambtn"
android:textColorHint="#color/black"
android:layout_weight="0"
/>
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout_search"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="top"
android:gravity="top"
android:layout_height="wrap_content">
<EditText
android:background="#null"
android:textColor="#color/black"
android:layout_centerInParent="true"
android:gravity="top"
android:layout_gravity="top"
android:id="#+id/SearchText"
android:textColorLink="#color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/normatext"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="55dp"
android:layout_weight="0"
android:layout_height="match_parent"
android:background="#drawable/my_button_bg"
android:text="news"
android:id="#+id/newsbtn"
android:layout_marginLeft="8dp"
android:layout_marginRight="6dp"
android:paddingBottom="5dp"
/>
</LinearLayout>

I think the problem is maybe in your topmost LinearLayout view.
Try removing those lines:
android:layout_marginTop="0dp"
android:paddingTop="6dp"
android:paddingBottom="5dp"
android:layout_margin="3dp"
and add those lines instead to make sure there are no padding and margin at all:
android:padding="0dp"
android:layout_margin="0dp"
If it did solve the problem, you can go ahead and read more about padding and margin here.
If not, I would suspect that your TextInputLayout gets confused because of the inner EditText asking it to be both top and center. remove the "center" part (e.g. remove this - android:layout_centerInParent="true" from your EditText) and check if it solves your issue :)

Never had this problem before but i would make sure explicitly that there is no padding in the parent and no margin in the child. i.e.:
<android.support.design.widget.TextInputLayout
...
android:padding="0dp"
>
<EditText
...
android:layout_margin="0dp"
/>
</android.support.design.widget.TextInputLayout>

Can You remove the background tag of EditText and reply me if it works or not ?
Also provide your full layout file for exact solution.

Related

What's the proper way to resize DatePicker and TimePicker on Android without leaving much margins?

This is a part of activity (RelativeLayout):
<TextView
android:layout_marginTop="30dp"
android:layout_below="#id/tvMerchantAddress"
android:id="#+id/tvWhen"
android:textSize="12sp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text = "When you to come?" />
<DatePicker
android:layout_centerHorizontal="true"
android:id="#+id/datePicker"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleX="0.75"
android:scaleY="0.75"
android:layout_marginLeft="20dp"
android:layout_marginTop="-30dp"
android:layout_marginRight="-50dp"
android:layout_marginBottom="-30dp"
android:calendarViewShown="false"
android:layout_below="#id/tvWhen"
android:datePickerMode="spinner"
android:spinnersShown="true" />
<TimePicker
android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/datePicker"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="-30dp"
android:layout_marginRight="-50dp"
android:layout_marginBottom="-30dp"
android:scaleX="0.75"
android:scaleY="0.75"
android:timePickerMode="spinner" />
What I want to have is a DatePicker and a TimePicker on the center of the parent, which sizes are smalles then their default ones.
And this is the result:
Yes, both DatePicker and TimePicker are smaller, but leaves much unused margins. And also overlaps each other. Is there any way to do this?

I want to make a TextView height "match_parent" whose parent height is "wrap_content" in XML (Android Studio)

Inside the layout for my activity i have two TextView inside a RelativeLayout.
The one with id "descriptionfromDB" takes the text from a database while the other one will only show the text Description.
What I want is that the second one matches the height of the parent when the text of the one that gets modified become longer.
I tried to do it with android:layout_height="match_parent" for the first TextView (since parent height grows when the other TextView's text become longer) but with no results.
Here's an example of how it looks at the moment:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/description"
android:layout_width="100dp"
android:layout_height="match_parent"
android:textSize="18dp"
android:text="Description"
android:background="#drawable/text"
android:textColor="#color/blue_fontcolor"
android:padding="1dp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_toRightOf="#+id/description"
android:text=""
android:background="#drawable/text"
android:textColor="#color/blue_fontcolor"
android:padding="3dp"
/>
</RelativeLayout>
You can achieve the behavior you want using horizontal LinearLayout :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/description"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="Description"
android:textSize="18sp" />
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>
Using weight would be a better idea since there are many different screen sizes. Below attributes determine that the first TextView should take 30% of the parent width and second one 70% which is the remaining width.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/description"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="Description"
android:textSize="18sp" />
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:textSize="16sp" />
</LinearLayout>
If you are going to add more views and nest this LinearLayout into another ViewGroup, since your layout will no longer be flat and also horizontal LinearLayout has double layout taxation, I recommend ConstraintLayout. Same behavior using ConstraintLayout :
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Description"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/description"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Android: Fully Justify Two Buttons

I have a linear layout, with layout parameters of fill parent, and wrap content. The orientation is horizontal.
Inside the linear layout I have two buttons. They appear next to each other on the left hand side.
I would like the two buttons to "fully justify". In other words I want each button to take half the width of the linear layout.
Can anybody help?
use weights like:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="Button1"
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:text="Button2"
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
define a weight sum for the parent set each children to half
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:text="Button1"
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:text="Button2"
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

Can't change the width of Spinner inside TableRow in Android

I placed a TextView and a Spinner inside a TableRow but I cannot change the width of the Spinner. When I put it in RelativeLayout everything works fine, I can set custom width, wrap_content or match_parent but when I put it in TableRow no matter what I do it has a constant width. Can anybody tell me how can I make it to match_parent in TableRow?
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/logo" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text"
android:textSize="18sp" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:drawSelectorOnTop="true"
android:entries="#array/items"
android:prompt="#string/prompt"
android:textSize="18sp" />
</TableRow>
</TableLayout>
For the spinner:
android:layout_weight="1"
This helped me.

LinearLayout won't fill parent; instead if fits to the smallest width view in it

I have a view that looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout android:id="#+id/header"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<ImageView android:id="#+id/title_icon"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:src="#drawable/title_icon" />
<LinearLayout android:id="#+id/title_layout"
android:layout_toRightOf="#id/title_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<TextView android:id="#+id/title_text"
android:text="#string/activity_complete"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="15dp"
android:textSize="12dp"
android:textStyle="bold"/>
</LinearLayout>
<TextView android:id="#+id/status_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textSize="8dp" />
</RelativeLayout>
<TextView android:id="#+id/message_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="5dp"
android:paddingLeft="15dp"
android:textSize="10dp"/>
<ImageView android:id="#+id/main_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/grey"
android:padding="5dp"/>
</LinearLayout>
My problem is that the main LinearLayout view doesn't match_parent or fill_parent. It seems to want to only contain the smallest item within it. In my case it will result in a width the same as main_image, cutting off the content of everything else. However, if I set main_image to visibility.gone then it will match the width of message_text.
I've tried removing everything except main_image and the main LinearLayout still only fits the image width.
The other thing is that this layout is inflated into another LinearLayout which is set to fill_parent.
Wrapping the ImageView in another LinearLayout fixed the issue.
I just had this problem -- adding a LinearLayout configured to "match_parent" to another LinearLayout, but the inner layout did not fill the outer one properly.
Initially I was doing this to add the layout:
LinearLayout inner = getLayoutInflater().inflate(R.layout.inner, null);
outer.addView(inner, 0);
After a few minutes of exasperation I tried this instead:
LinearLayout inner = getLayoutInflater().inflate(R.layout.inner, outer);
And inner matched the outer parent width and height.

Resources