Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] layout – Android change gravity programmatically

Posted on November 11, 2022 By

Solution 1 :

I think the easiest way to sort this out is using the 3rd approach but modify it slightly.

LinearLayout.LayoutParams iconLayoutParams = (LinearLayout.LayoutParams) icon.getLayoutParams();

And then do:

iconLayoutParams.gravity = Gravity.LEFT;

Reason LayoutParams itself does not have Gravity is that not all containers have gravity. LinearLayout does, so you need to cast to LinearLayout.LayoutParams since you surely know that your container is LinearLayout. And the method getLayoutParams is in the View class so it must return the parent of all other LayoutParams, which again, does not have to have Gravity.

I hope I explained properly 🙂

Problem :

I have this layout:

<LinearLayout
    android_layout_width="wrap_content"
    android_layout_height="wrap_content">
    <ImageView
        android_id="@+id/icon"
        android_layout_width="50dp"
        android_layout_height="50dp"
        android_gravity="left" />

    <TextView
        android_id="@+id/button_text"
        android_layout_width="wrap_content"
        android_layout_height="wrap_content"
        android_gravity="right"
        android_textSize="40dp" />
</LinearLayout>

Now, I want to modify gravity of the ImageView to left and gravity of the TextView to right. All other values present in xml (width, height, …) must be preserved.

I tried:

1) setGravity method. Unfortunately, ImageView doesn’t have this method. Why?

2)

LinearLayout.LayoutParams iconLayoutParams = new LinearLayout.LayoutParams(icon.getWidth(), icon.getHeight());

iconLayoutParams.gravity = Gravity.LEFT;

This somehow destroys View’s dimensions

3)

android.view.ViewGroup.LayoutParams iconLayoutParams = icon.getLayoutParams();

This doesn’t have gravity property.

(how is that even possible that icon.setLayoutParams() accepts layout parameters where gravity can be set and icon.getLayoutParams return some different kind of layout params without gravity property? That is a mess)

Can you please help me with that?

READ  [FIXED] java - Data is not showing in app after fetching from an api
Powered by Inline Related Posts
Android Tags:android, layout

Post navigation

Previous Post: [FIXED] activity_main.xml for android studio stuck on loading
Next Post: Can a beginner learn Moonlight Sonata 1st Movement?

Related Posts

[FIXED] react native – Expo android app, PushNotifications doesn’t work in standalone apk? Android
[FIXED] Add text to FAB in Android Android
[FIXED] How can we add android nugets in Xamarin.forms and not only in Xamarin.Android Android
[FIXED] android – How to set Custom Theme for layout generated in code? (viewgroup) Android
[FIXED] android – How can I implement dynamic Layered ImageView Programmatically? Android
[FIXED] Getting “Caused by: java.lang.RuntimeException” in my Android app Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme