How To Set Android Gravity Programmatically? [You Must Need To Know]

android gravity

For an Android Programmer, the UI is an important aspect. To make this UI much more interactive, Android developers are willing to implement tons of features, including Gravity. But how to set android gravity Programmatically?

 

In simple terms, if you want to set the gravity, you will need to use android:layout_gravity for the content View box. And to align the content within the View, use android:gravity. These two commands, along with the position values, are enough to help you personalize your UI with the utmost freedom.

 

Using gravity is not that complicated, but you need to know what it actually is. If you are a developer, it might be common for you. But for others, it’s a new concept. So remember to look before you leap and learn before you code.

 

What is Gravity?

 

If you are an Android programmer, the term Gravity might not be that uncommon. But for a non-programmer, they might think that it’s similar to the one that caused an apple to fall on Newton’s head. Though these two things are pretty different, they do have some minor similarities.

 

Similar to Newton’s one, the gravity on android or android gravity is an attribute that sets the gravity for a content. But unlike the actual one, this gravity determines the spatial orientation of content within the UI of the phone.

 

This means that the content will be shown on the UI as per the gravity effect on the device. So if the content is horizontal and the phone is vertical, the content will change its axes as per the effect of gravity on the phone.

 

There are mainly two in-code commands used to define programmable gravity in a code. They are:

 

  • android:gravity
  • android:layout_gravity

 

But before we jump into how to set layout_gravity programmatically in android or even the usual gravity one, we need to know the core difference between them. Both of these are similar as they set the gravity of the content. They also use the same set of values to define them further. But there is one core difference.

 

Layout_gravity or android:layout_gravity is used to define the position of a View within the UI. On the flip side, gravity or android:gravity is used to define the position of the content within the View.

 

As for the values that they both use the same commands. They are:

 

  • left
  • right
  • start
  • end
  • top
  • bottom
  • center
  • fill
  • center_horizontal
  • center_vertical
  • fill_horizontal
  • fill_vertical
  • clip_horizontal
  • clip_vertical

 

If you want to use gravity for the center, you should use its corresponding value. In this case, the value is “center.”

So instead of asking how to set gravity center in android programmatically, swap the value with center, and you are good to go!

 

How To Set Android Gravity?

 

Before we tell you how to set gravity to image view programmatically in android, you need to know that there are two possible ways to do it. The first is by TextView, and the other is by Button.

 

Setting Gravity By Button

 

To learn how to set button gravity programmatically in android, use a code similar to the following:

 

 

 

android gravity

 

 

Setting Gravity By TextView

 

For a TextView output, use the following code as an example. This will surely answer your doubts about how to set gravity of textview programmatically in android.

 

 

 

android gravity

 

 

 

How To Set Layout_gravity

 

Similar to gravity, layout-gravity also has two similar outcomes. You can use them by simply changing the android:gravity command in the previous codes with android:layout_gravity.

If things still seem confusing, here are the examples:

Setting Layout_gravity By Button

 

For a Button view, take the help of these codes:

 

 

 

android gravity

 

 

 

Setting Layout_gravity By TextView

 

If you want to know how to set layout_gravity for textview programmatically in android, see this example code:

 

 

 

android gravity

 

 

Gravity Programmatically

 

Using the setGravity() method, it is possible to set gravity for View programmatically. Using this method allows you to use a ton of integers as parameters among many constants.

 

Here are some of the useable parameters:

  • BOTTOM
  • CENTER
  • END
  • FILL
  • LEFT
  • RIGHT
  • START
  • TOP
  • AXIS_CLIP
  • AXIS_PULL_AFTER
  • AXIS_PULL_BEFORE
  • AXIS_SPECIFIED
  • AXIS_X_SHIFT
  • AXIS_Y_SHIFT
  • CENTER_HORIZONTAL
  • CENTER_VERTICAL
  • DISPLAY_CLIP_HORIZONTAL
  • DISPLAY_CLIP_VERTICAL
  • FILL_HORIZONTAL
  • FILL_VERTICAL
  • HORIZONTAL_GRAVITY_MASK
  • NO_GRAVITY
  • RELATIVE_HORIZONTAL_GRAVITY_MASK
  • RELATIVE_LAYOUT_DIRECTION
  • VERTICAL_GRAVITY_MASK

 

Here is an example code for you to understand how to set textview gravity programmatically in android.

 

 

android gravity

 

 

 

Using Multi Gravity

 

Now that you can use gravity and layout gravity for a single value, you might wonder how to set multiple gravity programmatically in android. Using multiple values is not that complicated. You just need to use the symbol “|” between the gravity values.

 

For example, you might want to use gravity on a button so that the text is bottom center. So for that, you will first have to use the command for the bottom and then the one for center_horizontal. This will ensure the text is horizontally centered.

 

 

android gravity

 

 

Frequently Asked Questions (FAQs)

 

What is the use of Gravity in Android?

 

Using a TextView, Button, or EditText in android code lets one set a text field or View for that content. With the help of gravity, one can customize the position of the content within the View field.

 

For instance, you can set the text to be centralized or even on the left-most side if you wish. You will just need to use the android:gravity command, and you will be able to customize with ease.

 

Is gravity an attribute or a library?

 

Gravity or Layout_gravity is an attribute within Java. It is not a library. With the help of this attribute, one can set the spatial alignment of a text box within the 2D interface.

 

This not only helps in making the UI unique but with a little bit of programming, one can make the text interactive.

 

What options are there to use gravity for?

 

To orient the content box or view box within the interface, one must use android:layout_gravity. And if you want to arrange the content within the View box, you can use android:gravity.

 

While using these commands, you will also have to set the values for the text output. For instance, if you want to set the text to be centralized, use the command android:gravity=”center”

 

Is it possible to use multiple values together?

 

Yes, it is possible to use multiple gravity values together. But to do that, you will have to write the full command for all of them and use “|” between them.

 

For instance, if you want to centralize a text and also keep it to the right, the command will be similar to this: setGravity (gravity.right | gravity.center_vertical).

 

How to incorporate gravity in ImageView for android?

 

Using ImageView and gravity together is not as simple as for text. In this case, you will need to use LayoutParams along with the View that you use.

 

But if you already used RelativeLayout, the command will be slightly different. For this, the command is RelativeLayout.LayoutParams.

 

 

You may also be interested to know:

 

what is configupdater android

how to check screen time on android

what is the best android straight talk phone

how to transfer wifi password from android to android

how to save a web page on android

 

 

Final Words

 

Android users care about the UI more than the functionality of an application. So to an Android developer, it is a must to know how to set Android Gravity Programmatically. If not for this, the UI will be bland unappealing.

 

So if you are a developer and want to promote your app, we suggest you focus on the UI. We know not to judge a book by its cover, but the quote is not so practical for Android developers!

By Imran Chowdhury

Imran is a tech-savvy guy, love to share the hacks of technology related tips and tricks, and a certified learner from Android developer platform He has been in the industry since 2017. With his vast knowledge and hundreds of blog publications, he can solve any tech-related problem flawlessly. The main areas of expertise are android and ios app-related problems, tech-tutorial, and operating systems. He explains rocket-science-type difficult things easily so you can follow instructions and solve problems. He is currently working as the founder of Writing hack and some other blogs. His love for writing and solving others' problems sets him apart from other writers.

Leave a comment

Your email address will not be published. Required fields are marked *

Index