Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

How to set both gravity and layout gravity of a LinearLayout programatically


Previously, I have explained the difference between 'gravity' and `layout_gravity' for views within a layout.

Setting the gravity of a LinearLayout itself changes the location of the views within it. Setting the layout_gravity of a LinearLayout changes how the LinearLayout is arranged within its parent layout.

This image shows a LinearLayout (brown) within a FrameLayout (white). The LinearLayout's gravity is set to center_horizontal and its layout_gravity is set to right|bottom.

enter image description here

Here is the xml:

android:layout_height="match_parent" >

    android:orientation="vertical" >

        android:text="TextView 1" />

        android:text="TextView 2" />


Changing things programmatically

The following code shows how to change both the gravity and the layout_gravity of the LinearLayout.

public class LinearLayoutGravity
extends Activity {

    public void onCreate(Bundle
savedInstanceState) {

        // Change the gravity (not layout_gravity)
of the LinearLayout
        LinearLayout ll = (LinearLayout)

        // Change the layout_gravity (not gravity)
of the LinearLayout      
        FrameLayout.LayoutParams params = new
FrameLayout.LayoutParams(400, 400);
        params.gravity = Gravity.TOP;
        params.gravity = Gravity.RIGHT;

And here is the result:

enter image description here

Categories : Android

Related to : How to set both gravity and layout gravity of a LinearLayout programatically
How to make a LayerImageDrawable with different types of gravity
You could make the overlayed image (the one for the bottom right corner) a nine-patch image where the image is located in the bottom right corner of the nine patch. That would require making the leftmost two pixels and the topmost two pixels the scalable part. That way the drawable will stretch to match on the left and top, leaving your image in the bottom right corner. if you need more flexibi

Categories : Android
How do you pass the gravity attribute via xml for a custom view?
by writing: <attr format="integer" name="android:gravity"/> you are intruducing the new attribute android:gravity with integer format, of course android:gravity is already defined in the system, so you need to change it into: <attr name="android:gravity"/>

Categories : Android
How can we hide include layout programatically in Android?
We need to see your actual implementation of hiding that View you mentioned. But, straight from reading of your question, I presume that you've might do it the wrong way. To hide or make a view invisible, use this: yourView.setVisibility(View.INVISIBLE); Bear in mind that this does not remove the view compeletly; it would still remain in your layout and you could get a reference to it or even

Categories : Android
Google Glass Immersion Custom Layout without CardBuilder.Layout.EMBED_INSIDE
You don't need to use the CardBuilder if you prefer to use your own custom layout: make sure to follow our design guidelines to make sure you meet our specs. To use your custom layout, simply inflate a View as you would normally and use in your application. For an Activity, you could something like: @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentVie

Categories : Android
LinearLayout.LayoutParams before KitKat
LinearLayout.LayoutParams has been around since API level 1. You should be able to use it in lower versions without issue as long as your not using any other API's that are not available in previous versions.

Categories : Android
Recently Add
I'm trying to create an onListItemClick even for a customized adapter. The array of items is found in res/values folder
No navigation/status bar touch listener exists, why?
z-index in Relative layout
java.lang.SecurityException: Permission Denial: getIntentSender() when using UiAutomation in a test
mvvmcross videoview URL binding
Android Emulator Unfortunately dev tools has stopped
Square Dagger error: class is exclusively members injected / cannot be scoped required
Installing a widget from an APP
How to stop onDraw() methods causing jerky scrolling of a ListView
How to find my Google Play Services Android BASE64_PUBLIC_KEY
Set style programatically does not work
How to get contacts from phonebook and sim but skype, facebook etc. in android
Where to put JUnit test in Android project
My Android Studio project keeps running my old project/application
how to avoid OUtOfMemory problems with create Bitmap
What tools use and how do continuous integration and test in Android with eclipse?
Converting drawable resource to Bitmap Android
Fragment Transaction show() does not show fragment on second time
Espresso - How to match ActionBar visibility?
Viewport setting ignored in Phonegap Android app, but only on certain phones?
how to open an activity with url scheme android
Android AsyncTask and Thread different behavior
How can one ellicit a Toast from a Service to a Fragment
GCMIntentService onRegistered not called
LinearLayout cropping but not expanding in Scrollview
Get a file from the application directory
How to set layout params using view's on touch listener?
How to disable Google Fit and revoke permissions from the app itself
Android Wear - receiving message from Wear without WearableListenerService?
How can I start new activity from another package?
© Copyright 2017 Publishing Limited. All rights reserved.