Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6175867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:59:54+00:00 2026-05-23T23:59:54+00:00

I have a relative layout with 3 ImageViews. The first one is a square

  • 0

I have a relative layout with 3 ImageViews. The first one is a square image, the second is just used for spacing, and the third one is another square image.

Here is the xml code for that layout:

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radioSV"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/radioLayout"
    android:orientation="vertical"
    android:gravity="center">

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="center">

    <ImageView
        android:id="@+id/sssImageView"
        android:src="@drawable/radio_sss_400_r"
        android:layout_width="10sp"
        android:layout_height="10sp"
        android:layout_gravity="center"
        android:scaleType="centerCrop">
    </ImageView>

    <!-- spacing -->

    <ImageView
        android:id="@+id/spacing1"
        android:background="@android:color/transparent"
        android:layout_width="wrap_content"
        android:layout_height="10dip"
        android:layout_below="@id/sssImageView">
    </ImageView>

    <ImageView
        android:id="@+id/gaImageView"
        android:src="@drawable/radio_ga_400_r"
        android:layout_width="10sp"
        android:layout_height="10sp"
        android:layout_gravity="center"
        android:scaleType="centerCrop"
        android:layout_below="@id/spacing1">
    </ImageView>

    </RelativeLayout>
</LinearLayout>
</ScrollView>

Now, I want my app to be useful for various screen densities, so therefore, in the java file, I ask for the density and use a switch-case statement afterwards. In this statement, the size (width, height) of the 2 ImageViews (sssImageView and gaImageView) have to be changed.

For instance, if the density of the screen is high, I want the width and height of the ImageViews to be 200sp. I’ve just put 10sp in the xml as a ‘standard’ value.

This is the part of the use-case statement for high screen density:

case DisplayMetrics.DENSITY_HIGH:
    layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
    sssImageView.setLayoutParams(layoutParams);
    sssImageView.setMaxHeight(val_high);
    sssImageView.setMaxWidth(val_high);

    gaImageView.setLayoutParams(layoutParams);
    gaImageView.setMaxHeight(val_high);
    gaImageView.setMaxWidth(val_high);
    break;

Also, val_high is 200sp:

int val_high = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 200, this.getResources().getDisplayMetrics());

Now, the part with the ImageView sssImageView works perfectly. It correctly enlarges the image from 10sp to 200sp (don’t worry – the image is not 10sp originally!).
The problem is the fact that the other ImageView, gaImageView, puts itself on top of sssImageView and destroys the layout. If I comment out the 3 lines with gaImageView, it is on its correct place in the layout, but is still small (10sp) of course.

I have also tried the opposite: commenting out the 3 lines with sssImageView and only manipulating gaImageView. Now the top ImageView (sssImageView) is on its correct place and small as expected. However, the bottom ImageView, gaImageView, positions itself on top of sssImageView, and not below as written in the xml layout file.

What is wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:59:55+00:00Added an answer on May 23, 2026 at 11:59 pm

    I managed to solve this problem.

    Here is the code for the DENSITY_HIGH case statement:

    case DisplayMetrics.DENSITY_HIGH:               
        display = getWindowManager().getDefaultDisplay(); 
        width = display.getWidth();
        height = display.getHeight();               
    
        // 0.36 is a scaling factor
        int val_high = (int) (height*0.36);
    
        sssImageView.setId(1);
        gaImageView.setId(2);
        spacing.setId(3);
    
        layoutParams = new RelativeLayout.LayoutParams(val_high, val_high);
        layoutParams2 = new RelativeLayout.LayoutParams(val_high, val_high);
        layoutParams3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
            (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 10, this.getResources().getDisplayMetrics()));
    
        sssImageView.setMaxHeight(val_high);
        sssImageView.setMaxWidth(val_high);
        relativeLayout.updateViewLayout(sssImageView, layoutParams);            
    
        layoutParams3.addRule(RelativeLayout.BELOW, sssImageView.getId());
        relativeLayout.updateViewLayout(spacing, layoutParams3);
    
        layoutParams2.addRule(RelativeLayout.BELOW, spacing.getId());
        gaImageView.setMaxHeight(val_high);
        gaImageView.setMaxWidth(val_high);
        relativeLayout.updateViewLayout(gaImageView, layoutParams2);                
        break;
    

    So generally, it looks like I had to “re-create” the xml file programmatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relative layout and in that I have 4 imageViews stacked one
I have a relative layout with several ImageViews (with transparent areas) above each other.
I have a relative layout with 2 image views in them, I try and
I have this layout that works correctly, a relative layout with a text view
I have problems getting some of my views aligned in a relative layout that
I have been having problems creating a relative layout in XML for a list
I have a layout which specifies sizes of widgets in relative dimension, for example:
I have developed Relative layout for 3.7 droids with Relative layout and difficult relations
i am making android UI (using Relative Layout)in which, i need to set one
I have created a custom view by extending Relative Layout and it looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.