Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created May 25, 2015 23:55
Show Gist options
  • Select an option

  • Save udacityandroid/462d130752ef49bbab92 to your computer and use it in GitHub Desktop.

Select an option

Save udacityandroid/462d130752ef49bbab92 to your computer and use it in GitHub Desktop.
Android Development for Beginners : Court Counter - The Stages
<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:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="Team A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="+3 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="+2 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Free throw" />
</LinearLayout>
@Wandersonjack

Copy link
Copy Markdown

I have a issue using just gravity, according to the code above, I've used this attribute android:layout_gravity="center_horizontal".

@rupendrapratapsingh

Copy link
Copy Markdown

don't write "center_horizontal" just write "center"

@MaijaMaija

Copy link
Copy Markdown

@Wandersonjack, are your TextViews' layout widths the same as in this code? I reckon that's what's making the difference.

@kostromina

Copy link
Copy Markdown

android:gravity="center_horizontal" is working for me

@rohangho

Copy link
Copy Markdown

center_horizontal is not working for me

@pranavsuresh7

Copy link
Copy Markdown

Gravity does'nt works much with ConstraintLayout .

@predskolska

Copy link
Copy Markdown

@pranavsuresh7, change it to LinearLayout (delete and set new one)
NOTE to all! layout_gravity and gravity are two different things.

@lixrg

lixrg commented Nov 14, 2017

Copy link
Copy Markdown

because I just had troubles myself figuring this out I thought I would share my solution to the gravity problem:
using LinearLayout and android:gravity="center" works perfectly fine for me :)

@asafstr2

Copy link
Copy Markdown

layout_gravity and gravity are two different things. agree with @perdskolska
just to clarify since i usually write in Hebrew its a right to left language so my phone is configure to present thing right to left as default
so i have been using gravity to fix it from day 1
so the differences are layout_gravity -working on the children while gravity is working only on the parent
hopefully that helps

@thrwatzidan

Copy link
Copy Markdown

this code looks good to any idea (that's my build)

<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="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.android.courtcounter.MainActivity">

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

    <TextView
        android:id="@+id/Hello"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="4dp"
        android:text=" Team A"

        />

    <TextView
        android:id="@+id/score"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Hello"
        android:gravity="center"
        android:padding="4dp"
        android:text="3" />

    <Button
        android:id="@+id/button_points3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/score"
        android:layout_margin="8dp"
        android:gravity="center"
        android:text="+3Pointes"

        />

    <Button
        android:id="@+id/button_points2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/button_points3"
        android:layout_margin="8dp"
        android:gravity="center"
        android:text="+2 Pointes" />

    <Button
        android:id="@+id/button_points1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/button_points2"
        android:layout_margin="8dp"
        android:gravity="center"
        android:text="+1 Pointes"

        />
</RelativeLayout>

</android.support.constraint.ConstraintLayout>

@lann22

lann22 commented Jan 6, 2018

Copy link
Copy Markdown

the code is easy

@mhmddnour

Copy link
Copy Markdown

its simple as i wrote

@Nogaeman

Nogaeman commented Feb 4, 2018

Copy link
Copy Markdown

untitled

@tahermahmoud

Copy link
Copy Markdown

phone
`

@abdahma01

Copy link
Copy Markdown

untitled

@mxacan

mxacan commented Feb 22, 2018

Copy link
Copy Markdown

untitled

@elghazi82

Copy link
Copy Markdown

court conter

@jimmy8dmt

Copy link
Copy Markdown

image
2 text views and 3 buttons. What am I missing? I can't continue.

@elskeg

elskeg commented Mar 24, 2018

Copy link
Copy Markdown

put a zero in the image view box

@khldonemad

khldonemad commented May 29, 2018

Copy link
Copy Markdown

must put 0 in the image view to continue

@magdyvictor

magdyvictor commented Jun 1, 2018

Copy link
Copy Markdown

can we do like this

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:gravity="center_horizontal"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="4dp"
    android:text="Team A" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="4dp"
    android:text="0" />


<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="+3 Point" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="+2 Point" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="Free throw" />

@ayatbahaa96

Copy link
Copy Markdown

image

@MajidKaabi

Copy link
Copy Markdown

screenshot_2018-06-14-20-31-24

@jamaloumous

Copy link
Copy Markdown

image

@RanaOsama25

Copy link
Copy Markdown

thank you .. i did it

@abdullah-shamali

abdullah-shamali commented Sep 8, 2018

Copy link
Copy Markdown

anybody please tell me whats wrong with this line of code?

capture

@LaszloLajosT

Copy link
Copy Markdown

Hi Ladies and Gentlemen!

I started to learn how to use Github.
And because of that I also tried to save the beginning of the project(Set up)
Here is the link where you can dowload the whole project and open it in Android Studio if you have difficulties to create(Or searching the xml file only):
https://github.com/LaszloLajosT/CourtCounter.git

@MarvUkazu

Copy link
Copy Markdown

layout_width for the text views and buttons should be match_parent
then give the text views a gravity (not layout_gravity) of center_horizontal.

@ravinder5341

Copy link
Copy Markdown

I have a issue using just gravity, according to the code above, I've used this attribute android:layout_gravity="center_horizontal".

you can use this as an alternative

android:textAlignment="center"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment