RelativeLayout set view to the right of a centered view - android

I try to set a view to the right of a first view and bottom align them inside a RelativeLayout.
The following code looks to me like it should work.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#999999"
android:padding="10dp" >
<View
android:id="#+id/v1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:background="#FFFFFF" />
<View
android:id="#+id/v2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBottom="#id/v1"
android:layout_toRightOf="#id/v1"
android:background="#FF0000" />
</RelativeLayout>
But the result is not really what i've expected.
What is the problem here?
Just to be clear what i expect: both cubes should be bottom aligned against each other and the red cube should be to the right of the white one (outside).
Edit:
I have found the problem. it's not this layout, but the parent list, where it is included (it's a propriety HorizontalListView). it seems to resize its child views somehow and that causes the problem.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#999999"
android:padding="10dp" >
<View
android:id="#+id/v1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:background="#FFFFFF" />
<View
android:id="#+id/v2"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FF0000"
android:layout_alignBottom="#+id/v1"
android:layout_alignBaseline="#+id/v1"
android:layout_alignRight="#+id/v1"/>
</RelativeLayout>
Am I right? ;)

It worked for me. My guess is that the sp for whatever you are testing on is scaling down from the default sp=dp. This would make your dp padding push the block over, which it already might since the amount of available space between the grey and white blocks is:
(300-200)/2 - 10 = 40
and your red block is 50.
If you want more consistent results use dp since sp will scale based on preferences and is usually used for text scaling.

Do not use sp as the unit. Try instead to use dp (device pixels). This would take the device into consideration. In your case, the images are not scaled properly and hence this result. You could also set the gravity on the view v2. Something like :
android:gravity="botton|right"
Or you could also try setting the baseline to something like :
android:layout_alignBaseline="#+id/v1"
and set the alignRight to something like :
android:layout_alignRight="#+id/v1"

Related

Image in toolbar: layout:height

In a basic activity, I am trying to put an image attached to the tool bar. In this code in the ImageView section android:layout_height="217dp". I don't want to give it this way, as I recently read that for the algorithm to work for all devices. So my ultimate choice was to use: match_parent or wrap_content. If I use 217dp my app will look different. However if I use match_parent or wrap_content for the image view, I am getting a huge tool bar, which takes almost half of the screen. What should I do?
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:background="#color/colorWagner">
<ImageView
android:layout_width="wrap_content"
android:layout_height="217dp"
app:srcCompat="#drawable/wagner"
android:layout_gravity="left"
android:id="#+id/imageView" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_first"
android:layout_height= "wrap_content" />
This is happening because of the size of the image. You will need to reduce the size of the image via some tools. I have had similar issue in past. Try setting a fixed height for the image before you save the image in android. It should resolve your issue
What if you try using percentage:
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="#drawable/wagner"
android:layout_gravity="left"
android:id="#+id/imageView" />
<Space
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
You can vary the numbers in layout_weight, to adjust the percentage. In this case the percentage will be 50% for each element which are your button, and an empty space.
In that case you can use the following code in your .java file
getSupportActionBar().setLogo(R.drawable.ic_launcher);

ImageView in fixed position

In my RelativeLayout, I have a large background picture of an ocean view (1870px x 756px in drawable-xxhdpi) that I center for all the devices :
<ImageView
android:src="#drawable/bg_image"
android:scaleType = "centerCrop"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageView>
On top of it, I want to put another Image of a ship (500px x 300px also in drawable-xxhdpi), centred horizontally, but should be 230px away from the top of the screen to be on the horizon line.
<ImageView
android:src="#drawable/another_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="230px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
I'm not getting the correct results and android is complaining about the use of px as a unit for the margin top.
Results : Device 1 (adjusted on the horizon)
Smaller Device 2 (not adjusted on the horizon):
Any suggestions?
I think you have to use "dp" unit... You could try using diferent devices (emulators, for example) with diferent sizes to guarantee it.
I hope this will help you
Make sure that the horizon on your background image is centered vertically. Then you can use this simple trick of RelativeLayout that will position your ship image above vertical center of the view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="#drawable/bg_image"
android:scaleType = "centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
<View
android:id="#+id/center"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true" />
<ImageView
android:src="#drawable/another_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/center">
</ImageView>
</RelativeLayout>
each device has it's own screen resolution. I think you're using boat image with one screen resolution support. if you want to solve this problem you need to add boat image with different resolutions. Like HDPI,LDPI,MDPI,xhdpi

How to resize an ImageView within a given Layout in Android?

I have a problem to make a proper layout for a special case. I experimented on that already for a while both in the designer and in code, but I couldn't find a solution, that's why I need your help.
I have to create a layout which should have a structure like pictured in the images below. It is mainly a combination of several linearLayouts. The problem I have is, that the picture can only be added within the code, because this layout is a detail view that displays information about items from a list.
On the top is the layout without an image place holder (no loaded picture - indicated in black), here the width of "linearLayout_BigLeft" is given by the width of the two buttons and the textView (which all have content) in the "linearLayout_BelowImage".
In the middle you see the layout after the picture has been loaded (image indictated in orange) in code. Depending on the aspect ratio of the android device the black colored gaps differ. I can't get the image to resize to the whole available height and adjusting its width accordingly. The "linearLayout_BelowImage" adjusts itself to the image size (the textView in it is getting wider).
On the bottom is the layout which shows the ideal state. The image always should use the whole available space in height and resize accordingly in width. The "linearLayout_BelowImage" adjusts itself to the image size (the textView in it is getting wider).
Question:
How can I get a layout (after the image is loaded in code) that looks like the bottom picture? The image, after loaded in code, has to resize itself, so it uses the whole available height and resizes its width accordingly. The "relativeLayout_Top" and the "linearLayout_BelowImage" have both fixed heights. The "scrollView_BigRight" adjusts itself based on the space that the "imageView_OrangeImage" doesn't need for itself.
I can deal with solutions that adjust the layout in code, after the image has been added, or solutions that makes the layout.xml itself flexilbe enough to deal with this situation.
Any help is highly appreciated. If you need any more information please let me know.
Below is the main content of my layout.xml, that is needed for this problem.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/white">
<RelativeLayout
android:id="#+id/relativeLayout_Top"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/blue" >
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout_Big"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#color/transparent" >
<LinearLayout
android:id="#+id/LinearLayout_BigLeft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/transparent" >
<ImageView
android:id="#+id/imageView_OrangeImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/black" />
<LinearLayout
android:id="#+id/linearLayout_BelowImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/blue_white_blue" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/blue" />
<TextView
android:id="#+id/textView_BelowImageMiddle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/white" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/blue" />
</LinearLayout>
</LinearLayout>
<ScrollView
android:id="#+id/scrollView_BigRight"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/grey" >
</ScrollView>
</LinearLayout>
</LinearLayout>
android:adjustViewBounds="true"
This one’s a manual fix for “optimized” code in scaleType="fitCenter". Basically when Android adds an image resource to the ImageView it tends to get the width & height from the resource instead of the layout. This can cause layouts to reposition around the full size of the image instead of the actual viewable size.
AdjustViewBounds forces Android to resize the ImageView to match the resized image prior to laying everything else out. There are times where this calculation won’t work, such as when the ImageView is set to layout_width="0dip". If it’s not working, wrap the ImageView in a RelativeLayout or FrameLayout which handles the 0dip flexible size instead
get it from this site
OR
Mode android:scaleType="centerCrop" uniformly stretches the image to fill the entire container and trims unnecessary.
You can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself!
get it here

Scale layout items equally on large screens

I've got a 4-item start screen in my app, which looks like the following:
What's important to me there:
- All items do have the same width (not regarding how much text is actually in it)
- Look the same on all devices (small-screen, mdpi, large-screen, etc.)
Im just wondering if there is a easy solution about this problem?
I've tried using 3 LinearLayouts but thats really awkward..
(1 presenting the layout root[vertical] and two which do each contain 2 buttons[horizonal]).
Making this layout ready for multiple screens would require a lot of fixed-width and fixed-margin hacking. Just like "button margin = 30dp on xlarge, 20 on large, 15 on normal,...".
My layout-xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:id="#+id/main_root"
android:orientation="vertical"
android:gravity="center" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center" >
<Button
android:id="#+id/btn_learn"
android:text="#string/mainBtn_learn"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
<Button
android:id="#+id/btn_quiz"
android:text="#string/mainBtn_quiz"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center" >
<Button
android:id="#+id/btn_search"
android:text="#string/mainBtn_search"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
<Button
android:id="#+id/btn_more"
android:text="#string/mainBtn_more"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
</LinearLayout>
Is there a view which "auto-scales" these Buttons or still any other easier solution?
Edit:
So, in special, you need something like
button:
android:layout_width="15%" // 15% of screen width / height depending on the orientation
android:layout_marginBottom="10%" // see above
I'm pretty new to Android development but I can show you what worked for me in a similar case. I defined my layout as follows:
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/outputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:editable="false" />
<Spinner
android:id="#+id/outputSpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:prompt="#string/OutputBaseOptionsPrompt" />
</LinearLayout>
I have a horizontal layout with two items. The LinearLayout has a width of "match_parent" so that it is as wide as the screen. Both items in the layout have the following:
android:layout_width="0dp"
android:layout_weight="1"
Since both items have a layout_weight of 1, they will be drawn at the same width. In this case, each item takes up half of the available space. If you change the weight of one of these items to "2" then it will be twice as wide as the item with a weight of "1".
Do you already have xml that makes it work on one screen size? If so post what you have so far.
I would suggest using a RelativeLayout for your root though. You can use the alignCenter attributes to float your children towards the middle. Then you just have to hard code the inner margins (how far apart you want the buttons) rather than the margin from yourself to the wall.
You could also avoid having to hard code the inner margin by making your own button 9 patch images. You can just add a border of transparent pixels in your image to represent the margin. You'll probably still want to supply an image for each density you wish to support though.
The solution is you dont use hardcoded values any where
Put three images with same name in hdpi mdpi and ldpi folders in drawables
an run the code

Adding blank spaces to layout

I am trying to make empty lines within android. This is what I have been doing:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\n\n"
I want to know if there is a better way? Thanks
Use Space or View to add a specific amount of space. For 30 vertical density pixels:
<Space
android:layout_width="1dp"
android:layout_height="30dp"/>
If you need a flexible space-filler, use View between items in a LinearLayout:
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
or
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1"/>
This works for most layouts for API 14 & later, except widgets (use FrameLayout instead).
[Updated 9/2014 to use Space. Hat tip to #Sean]
If you don't need the gap to be exactly 2 lines high, you can add an empty view like this:
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
View if you need change background color , Space if not .
that dosent mean you have to change view background .
<View
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#color/YOUR_BACKGROUND">
</View>
or Space
<Space
android:layout_width="match_parent"
android:layout_height="20dp"
/>
An updated Answer: Since API 14, you can use "Space" View, as described in the documentation.
Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.
if you want to give the space between layout .this is the way to use space. if you remove margin it will not appear.use of text inside space to appear is not a good approach.
hope that helps.
<Space
android:layout_width="match_content"
android:layout_height="wrap_content"
android:layout_margin="2sp" />
<View
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#80000000">
</View>
I strongly disagree with CaspNZ's approach.
First of all, this invisible view will be measured because it is "fill_parent". Android will try to calculate the right width of it. Instead, a small constant number (1dp) is recommended here.
Secondly, View should be replaced by a simpler class Space, a class dedicated to create empty spaces between UI component for fastest speed.
try this
in layout.xml :
<TextView
android:id="#+id/xxx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/empty_spaces" />
in strings.xml :
<string name="empty_spaces">\t\t</string>
it worked for me
This can by be achieved by using space or view.
Space is lightweight but not much flexible.
View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is more customizable, you can add background, draw shapes like space.
Implementing Space :
(Eg: Space For 20 vertical and 10 horizontal density pixels)
<Space
android:layout_width="10dp"
android:layout_height="20dp"/>
Implementing View :
(Eg: View For 20 vertical and 10 horizontal density pixels including a background color)
<View
android:layout_width="10dp"
android:layout_height="20dp"
android:background="#color/bg_color"/>
Space for string formatting using HTML entity:
  for non-breakable whitespace.
for regular space.
Just add weightSum tag to linearlayout to 1 and for the corresponding view beneath it give layout_weight as .9 it will create a space between the views. You can experiment with the values to get appropriate value for you.
Agree with all the answers......also,
<TextView android:text=""
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="2" />
should work :) I am just messing with others as TextView is my favourite (waste of memory though!)
The previous answers didn't work in my case. However, creating an empty item in the menu does.
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
...
<item />
...
</menu>
there is a better way to do this
just use the code below and change according to what you want the size of the blank area
<Space
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_column="0"
android:layout_row="10" />
if you are using with the grid layoout then only use
android:layout_row="10" />
Below is the simple way to create blank line with line size.
Here we can adjust size of the blank line.
Try this one.
<TextView
android:id="#id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="5dp"/>

Categories

Resources