android


Layout dynamic grid in middle


I have a grid with (currently) four cells wide. Each cell I am specifying to be 20px wide. I want to position this in the middle and at the top of a portrait layout.
My code is below. My problem is that hte grid seems to fill the whole width whereas it want it to just take number of columns * column width only. And for white space around it.
I've put colours on each of the components to try and make it easy to see where each one is. The two views in the first LinearLayout are never shown. Ive tried
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".EntryPointFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="wrap_content"
android:background="#FFFFFF00"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:background="#FF000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="4"
android:columnWidth="30dp"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:gravity="center"/>
<View
android:layout_width="wrap_content"
android:background="#FFFF00FF"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#FF00FFFF"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_weight="1"/>
</LinearLayout>
This image shows the sort of thing I want.
Can anyone help me tweak this xml? The number of columns and rows in the grid will vary so the space around it should dynamically resize.
Thanks
Try:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:gravity="center"
android:background="#ffff00">
<View
android:layout_width="0dp"
android:background="#ff0000"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<GridView
android:id="#+id/grid_view"
android:background="#ffffff"
android:layout_width="80dp"
android:layout_height="80dp"
android:numColumns="4"
android:columnWidth="20dp"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"/>
<View
android:layout_width="0dp"
android:background="#FFFF00FF"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:background="#FF00FFFF"
android:layout_height="0dp"
android:layout_weight="3"/>
</LinearLayout>
You will get something like that:
BTW: you can change the width/height of your GridView as you wish, you can put there a weight too with a similar approach as for the views next to it.

Related Links

AppCache in Corona SDK Android WebView Not Working
how to know the state in which the navigationbar is shown or hide
java.lang.AssertionError: Expected condition to be true webrtc android
alignParentBottom appears Offscreen in Fragment with Scroll View
listView_trailers.addFooterView(footer, null, false); doesn't working while making a listview as footer of another listview
Volley POST REQUEST error 500
What is the full screen image sizes for an Android app? (Android Studio)
how do i use couchbase lite session to authorize nodejs app?
Strange behavior with the back button in Android when trying to get long presses
“Canvas: trying to draw too large bitmap” when Android N Display Size set larger than Small
View create statement executed before table create statements
How to add actions to service?
RTL ActionBar with Min-SDK 11
Android game GPU compatibility issue
Selecting bluetooth PFS protocol
Cannot access external storage after granting permission for first use of app in android

Categories

HOME
ggplot2
character-encoding
google-cloud-functions
yandex
simulink
cq5
emscripten
visual-studio-emulator
retrofit2
ibeacon-android
enthought
line
onedrive-api
magnetic-cards
internationalization
microsoft-dynamics-nav
mathprog
forgot-password
git-extensions
boost-icl
selinux
asciidoctor-pdf
squashfs
postscript
imageresizer
onload
has-and-belongs-to-many
outliers
underscore.js
calculation
corenlp-server
dotspatial
kudu
utorrent
mailkit
linkedhashmap
python-pptx
google-knowledge-graph
z-notation
odroid
google-data-api
jvmti
spring-mvc-test
unordered-map
smoothstate.js
oim
httrack
libgphoto2
openrasta
yii2-user
rsa-archer-grc
donations
usart
pacemaker
flex4.6
windows-azure-queues
http-status-code-401
dbamp
kiosk
sklearn-pandas
user-management
crouton-os
directdraw
vigenere
morton-number
django-urls
batik
node-serialport
android-broadcast
openbabel
procedural-programming
infosphere-spl
big-ip
encryption-asymmetric
spring-remoting
android-framework
websphere-esb
bin
dtmf
hidapi
facebook-timeline
subproject
database-create
entity-attribute-value
user-tracking
catransform3d
google-floodlight
mtu
ajax-polling
request-headers
burndowncharts
django-piston
windows-identity
perl5.12
movieplayer
image-capture

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App