Setting GridView width wrap_content - android

I am trying to use a gridView for a game with different board sizes. I sets the number of columns programmatically for the GridView, each column got an fixed size button inside it.The GridView width is set to "wrap_content" in the xml. However - The gridView is always spread as "match_parent" to the entire screen. Here hows it look:
This is mine GridView xml (It in a constrain layout). I already tried to add weight or wrap it with linear layout. I added : android:gravity="center" and
android:layout_centerInParent="true"
In addition I tried changing the stretchMode but it just got the columns to be with a huge spacing between each other, or got them to be rectangle when I really want to keep them squre.
<GridView
android:id="#+id/gridView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="24dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="32dp"
android:layout_weight="0.5"
android:columnWidth="64dp"
android:gravity="center"
android:layout_centerInParent="true"
android:horizontalSpacing="2dp"
android:stretchMode="none"
android:verticalSpacing="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"></GridView>
I couldn't find any working solution online,
Hope you can help, Thanks !

Related

Android Studio 3 Vertical Buttons Same Width

How do I make the 3 vertical purchase buttons that have different length titles all the same width. The titles can change.
Currently I have them in a constraintlayout but as you can see the widths are different.
I suspect this is easy as all the questions refer to horizontal buttons.
I would personally solve the problem by adding a Linear Layout with orientation set to "Vertical.
Create a Linear Layout and set constraints.
Set width and height to wrap content (that means the width will be as long as the longest child).
Add 3 Buttons inside the Linear Layout.
Make sure you set the width of each button to "Match Parent". This will set the width of a button to the width of the parent Linear Layout.
Check this example xml code:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0.49$ / Month"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4.99$ / Year"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="14.99$ / Forever"/>
</LinearLayout>
I hope that my answer helped you.

Make GridView fit its container/constraints

I'm making small android memory game.
I've been troubling for a few hours with this issue, regarding GridView inside constraint layout. I'd want it to fit its constraints, without need to scroll.
Is there any way to make the GridView completely fit inside the container, so the horizontal axis is going to be exact? Bottom edge should match #+id/text_time top edge. Internet and Stackoverflow are full of these examples, but for a ImageView.ScaleType.
Below is a short layout code:
<android.support.constraint.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:context="com.example.alen.matchinggameaddiko.activities.GameActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="#dimen/gameLogoSize"
android:layout_height="0px"
android:layout_marginLeft="#dimen/gameLogoMarginLeft"
android:layout_marginTop="#dimen/gameLogoMarginTop"
android:src="#drawable/addiko_logo_white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<GridView
android:id="#+id/grid_game"
android:layout_width="0px"
android:layout_height="0px"
android:layout_marginBottom="10dp"
android:layout_marginTop="#dimen/gameGridMarginTop"
android:gravity="center"
android:horizontalSpacing="#dimen/gameGridHorizontalSpacing"
android:numColumns="4"
android:verticalSpacing="#dimen/gameGridVerticalSpacing"
app:layout_constraintBottom_toTopOf="#id/text_time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<TextView
android:id="#+id/text_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/timerMarginBottom"
android:layout_marginRight="#dimen/timerMarginRight"
android:textColor="#color/colorText"
android:textSize="#dimen/timerTextSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
Here is a photo. I draw a red line, so it's more clear where the GridView should end.
Thanks a lot guys.
I've seen your Layout.xml Try to set your GridView, Match_Parent in Both Width and Height. set constraintEnd_toEndOf and toStartof and toTopof and etc on "parent" For the GridView.
Then set those Textview and ImageView to be Aligned with the GridView.
What you've done is to set ImageView and TextView first then you Aligned GridView with them, What I'm Suggesting is Set your GridView to be Matched with Parent, Then put anything else Aligned with the GridView.
Also You must check if XML is not giving you any Warning for Height of the GridView. You may have to Set GridView Height to WRAP_PARENT. Just Check

How can I horizontally centre a single column GridView with a fixed column width?

I have a GridView which has a single column on smaller devices, and I want to ensure that the column is always centred, whilst maintaining a fixed column width (i.e. not using stretchMode="columnWidth").
Here are the current relavent properties on the GridView:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="230dp"
android:numColumns="1"
android:padding="48dp"
android:stretchMode="spacingWidth"
android:verticalSpacing="24dp"
The grid looks (a bit - excuse my funky representation of the system UI) like this:
Basically, the column appears with a left alignment. Is there any way that I can get the single column to appear in the centre of the screen, ideally using XML?
I have already tried the following, and they have no impact on the appearance:
Wrapping the GridView in a subclass of ViewGroup, such as RelativeLayout or FrameLayout, setting the ViewGroup's height and width to match_parent and the GridView's to wrap_content
Including layout_centerHorizontal="true" and android:gravity="center_horizontal" as properties of the GridView when attempting the above
Setting stretchMode="none"
Setting horizontalSpacing="0dp"
Here are an example. I think it will resolve your problem.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:columnWidth="230dp"
android:numColumns="1"
android:padding="48dp"
android:stretchMode="spacingWidth"
android:verticalSpacing="24dp" >
</GridView>
</RelativeLayout>

Android layout:weight doesn't work on device

I'm getting two differents behavior for a ListActivity row. Eclipse Graphical Layout show the right behavior, but at runtime, on the device, the layout:weight doesn't seem to work properly and the Textview is resized to the minimum width, depending on the text property.
This is how I had setup the layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#color/defaultBg"
android:orientation="horizontal">
<ImageButton
android:id="#+id/myButton"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_weight="2"
android:src="#drawable/ic_launcher"/>
<TextView
android:id="#+id/myText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:background="#2288EE"
android:layout_weight="6" android:text="Hello" android:textColor="#android:color/white"/>
</LinearLayout>
EDIT: I just found that the LinearLayout doesn't fill the listRow width. I'm still trying to find how to solve
Any help will be very appreciated.
I fix the issue myself. The problem wasn't in the table row layout posted above. The problem was in the android:layout_width of the parent ListView which was set to 'wrap_content' instead of 'fill_parent'.
Thanks to everyone who offered his contribute!
In your LinearLayout put android:weightSum="8". It should do the trick, on my device works fine.
Try adding android:scaleType="fitCenter" to your ImageButton. If I'm not mistaken, your source Drawable is too big, therefore making the ImageButton take too much horizontal space.

Two widgets one next to other in linear layout

I'm feeling difficult to implement , two widgets(say spinner) in linear layout one next to the other. I mean layout height of both spinner is wrap content, but width should be first half for the first spinner, second half of the screen to the second spinner. In linear layout they coming one down the other. I tried in Relative Layout, but as i gave width as wrap_content both are coming one next to each other but lot of space is remaining right to second spinner. I have seen in few apps this working out, but im not getting it.
Use layout_weight. This will force the two spinners to take up half of the space each.
<LinearLayout
android:orientation="horizontal"
... >
<Spinner
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
... />
<Spinner
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
... />
</LinearLayout>
I had the EXACT same problem one day. I've also tread a lot of different tricks to get it working. I ended putting both spinners in their own layout. Was weird but it worked.
relative layout is intended for putting views related to each other . it has nothing to do with their sizes.
for linearLayout , set the height/width (depending on orientation of the layout) of both views to 0px and the weight to 1 . this will make each take half the space.
<?xml version="1.0" encoding="utf-8"?>
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/testArray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="#array/testArray" />
<Spinner
android:id="#+id/spinner3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>

Categories

Resources