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

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.

Related

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>

EditText wont display at the center of the TextInputLayout

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.

Android ListActivity doesn't center the items

I have a ListActivity which displays an image and some text.
The imageView has a fixed size and I want to center the list horizontally.
After endless tries, I set any gravity to center_horinzontal, but nothing works.
The list is shown over the whole screen, as I can see by the scrollbar on the right side. But my frame remains on the left side.
In the adapter class, which entends ArrayAdapter, I even tried to set the gravity during runtime, but when I try to manipulate then basic LinearLayout, I get the error:
"java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams"
My attempt to change gravity in the method getView() which throws the error:
LinearLayout layout = (LinearLayout) rowView.findViewById(R.id.bootsFrameLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER_HORIZONTAL;
layout.setLayoutParams(params);
The list row is designed in a layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bootsFrameLayout"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:background="#drawable/card_blank2"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/bootsName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="TextView"
android:textAppearance="#style/BootNameStyle" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginStart="30dp" >
<ImageView
android:id="#+id/bootImage"
android:layout_width="240dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="#drawable/test_boot" />
<TextView
android:id="#+id/textWatchList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/gray_transparent"
android:gravity="end"
android:lines="2"
android:text="#string/watchlist"
android:textAppearance="#style/BootWatchListStyle" />
<ProgressBar
android:id="#+id/bootProgressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="invisible" />
</FrameLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="25dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginStart="25dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/bootMasse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="70"
android:text="TextView"
android:textAppearance="#style/BootInfoStyle" />
<TextView
android:id="#+id/bootBaujahr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="30"
android:gravity="right"
android:text="TextView"
android:textAppearance="#style/BootInfoStyle" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/bootLand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="70"
android:text="TextView"
android:textAppearance="#style/BootInfoStyle" />
<TextView
android:id="#+id/bootPreis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="30"
android:gravity="right"
android:text="TextView"
android:textAppearance="#style/BootPreisStyle" />
</TableRow>
</TableLayout>
The ListView is defined here:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="12dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginStart="12dp"
android:layout_weight="8" >
</ListView>
The activity occupies the whole screen, as it should do.
But although there is place enough, the frame won't center.
Any ideas?

ViewSwitcher with EditText leaves extra padding

I'm using a ViewSwitcher to allow switching from a TextView to an EditText element. What I've noticed is that the EditText element generates extra padding at the bottom of the view (i.e. if I remove the EditText everything looks as expected, but adding it, even when visibility is set to gone, generates extra space at the bottom). What's causing this and how should it be resolved?
<ViewSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/IngredientsLineSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/IngredientsLineText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingBottom="0dip"
android:paddingLeft="10dip"
android:paddingTop="0dip"
android:text=""
android:textColor="#color/black"
android:textSize="#dimen/recipe_label_font_size" />
<EditText
android:id="#+id/IngredientsLineEdit"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingBottom="0dip"
android:paddingLeft="10dip"
android:paddingTop="0dip"
android:text=""
android:visibility="gone"
android:textColor="#color/black"
android:textSize="#dimen/recipe_label_font_size" />
</ViewSwitcher>
You should put a RelativeLayout around the TextView or EditText to control their positioning.
-----
android:layout_height="match_parent" >
mlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/IngredientsLineSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/IngredientsLineText"
android:layout_height="match_parent"
android:layout_width="match_parent"
----
<EditText
android:id="#+id/IngredientsLineEdit"
android:layout_height="match_parent"
android:layout_width="match_parent"
-----
Change from fill_parent to match_parent in all Views.

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