layout problem raise - android

This is my layout file is ::
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/relativeLayout1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/bg1"
>
<!-- <RelativeLayout android:id="#+id/relativeLayo"
android:layout_width="480dp" android:layout_height="320dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginLeft="20dp"
android:background="#drawable/bg1"> -->
<ImageView android:src="#drawable/lets_play"
android:layout_width="wrap_content" android:layout_marginLeft="260dp"
android:id="#+id/imageView1" android:layout_height="wrap_content"
android:layout_marginTop="40dp"
></ImageView>
<ImageView android:src="#drawable/show_me"
android:layout_width="wrap_content" android:layout_marginLeft="220dp"
android:id="#+id/imageView1" android:layout_height="wrap_content"
android:layout_marginTop="60dp"
></ImageView>
<ImageButton android:id="#+id/anim_btn_play"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="260dp" android:layout_marginTop="150dp"
android:src="#drawable/play">
</ImageButton>
</RelativeLayout>
And mobile output is like ::
![this][1]
now my problem is :: i would like to set 480 * 320 size screen and layout is starting from left. At right side there is black portion remain.i have approx ten activity and all activity are going to display same direction. now i would like to show in center like ::
![this][2]
so can you tell me solution?

Easiest method is to wrap your Relative layout with something else, probably linear layout would be fine, and then:
<linearlayout gravity=center>
<relativelayout width=480dp height=320dp>
...
It should quick fix your problem on particular 480x320 screen.
BUT, I would suggest you rethink about your background image to make it possible to adopt to screens with different aspect ratio, consider looking nine-patch and make the background 'strecthable' or make a custom drawable that will stay in middle of screen.

Related

How to resize RelativeLayout/LinearLayout and content inside those layers programmatically, based on screen size

I am developing an Android application, for very first time.I am coming from web environment, and i am not sure if i can transfer my skills over Android UI as well.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_light"
android:gravity="center_vertical|center_horizontal">
<LinearLayout
android:id="#+id/SignUpHolder"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_light"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp">
<ImageView
android:id="#+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#string/Yeah"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/someTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="#string/sign_up_with_string_sign_up_with_facebook"
android:textSize="25sp" />
</LinearLayout>
</RelativeLayout>
As you can see
android:layout_width="350dp"
layout_width is set hardcoded by 350dp.
By this, this LinearLayer is not touching the screen of the phone and will be in every kind of mobile screen.
I am looking for a way to cut the width of this LinearLayer by 20-50dp, but do not be way to small so ImageView and TextView to be cutted off.
Is there a way to get the phone screen width, calculate it and set this Layer dynamically by some dp?
An easy way to achieve what I think you want would be to combine a match_parent width with horizontal margins. Something like this:
<LinearLayout
android:id="#+id/SignUpHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#android:color/holo_blue_light"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp">
This will cause your LinearLayout to be as wide as whatever parent it is inside of... minus 20dp on each side.
You can add margin. Instead of hard coding the width, use this in your linear layout tag
android:layout_margin="30dp"
android:layout_width="wrap_content"

How to position image1 on right top of the image2 in android?

How can i achieve a layout with 2 images in android, one image overlaying another image at the top right?
an image overlapping another image at the top right
http://i.imgur.com/uT3L2wl.jpg
I tried using relative layout, but i could position the overlay image by giving layout margin, but if i do that i cant support all screen sizes with that design.
and also i couldnt position the overlay image with respect to the underimage.
i want to design my layout like the pic i have shared.
I could get this working from the below code,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageButton
android:id="#+id/addnewcustomer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/c1"
android:layout_toRightOf="#id/c1" >
</RelativeLayout>
<ImageButton
android:id="#+id/hover1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/c1"
android:layout_alignLeft="#+id/c1"
android:layout_marginBottom="23dp"
android:layout_marginLeft="22dp"
android:background="#null"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Try look at FrameLayout. It will do the job.
http://www.learn-android-easily.com/2013/05/frame-layout-in-androis.html

Using RelativeLayout, LinearLayout and layout_weight at the same time - weird behaviour

I've got an activity layout specified in an XML file - activity_intro.xml - and I'm trying to create another one that is similar but slightly different - that's going to be activity_instructions.xml.
The Intro activity has a 9patch image at the bottom of the screen that is supposed to stay there and only adjust to different widths of the screens.
The Instructions activity is supposed to contain the same image but above 2 more buttons - all three of these views need to be always located at the bottom of the screen.
activity_intro.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/home_background" >
<LinearLayout
style="#style/Activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/introAnimationImageView"
android:layout_width="152dip"
android:layout_height="176dip"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/intro_animation_content_description"
android:src="#drawable/animation_intro01" />
<TextView
android:id="#+id/introTextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/intro_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/introTextViewSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/intro_subtitle"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/introButtonLoginSignup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/intro_button_label_login_signup" />
<Button
android:id="#+id/introButtonInstructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/introButtonLoginSignup"
android:text="#string/intro_button_label_instructions" />
<Button
android:id="#+id/introButtonReportAnonymously"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/introButtonLoginSignup"
android:layout_centerHorizontal="true"
android:text="#string/intro_button_label_report_anonymously" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:contentDescription="#null"
android:scaleType="fitXY"
android:src="#drawable/footer_cityscape" />
</LinearLayout>
Result:
Since I've got working code for Intro, I wanted to make Instructions follow its example but the layout_weight property isn't behaving as expected. First of all, I was only trying to put in the 2 buttons and leave out the image:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.1"
android:background="#drawable/home_background" >
<LinearLayout
style="#style/Activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/instructionsTextViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/instructions_title_1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<ImageView
android:id="#+id/instructionsImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/instructions_image_content_description"
android:src="#drawable/forms" />
<TextView
android:id="#+id/instructionsTextViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/instructions_description_1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/instructionsButtonPrevious"
style="#style/ButtonPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/instructions_button_label_previous" />
<Button
android:id="#+id/instructionsButtonNext"
style="#style/ButtonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/instructions_button_label_next" />
</RelativeLayout>
</LinearLayout>
This only worked when I set the layout_weight of the bottom RelativeLayout to 1 (instead of 0) and for the ScrollView 0.1 (instead of 1). If I used the original values the RelativeLayout would take up all the screen. Could anyone explain to me why that is?
I also tried googling the issue and noticed people would suggest to set layout_height to 0dip which I tried but it also didn't work as expected.
Secondly, I tried adding the already mentioned ImageView to the bottom RelativeLayout. This, however, basically displays only the ImageView and not the buttons - or one of the buttons is on top of the image (hiding it). Why is that? Don't I specifically set the buttons to be placed below it?
What should the code look like in order for it to be doing what I expect it?
Further explanation:
Below are images that should help indicate what exactly I want to achieve. The green bits are the ScrollViews - I added them because Android devices tend to have diverse screen sizes. Their purpose is to present the content properly independently of the screen size, i.e. if the screen is small, the user will be able to scroll that part to read the entire text and view the image.
The red bit on the left (Intro) shows the ImageView that is supposed to always be at the bottom of the screen; it'll always be there, visible, and it's the green bit above it that will be movable.
If you take a look at the red bit on the right (Instructions), there's a Next button that's covering the image with the lorry/truck that was visible in the Intro screenshot. Now that's wrong - there should be 2 buttons BELOW the image, as seen on the last screenshot (the 2 blue rectangles).

LinearLayout with 4 images--i cant see them hole

I have a linear layout with 4 images with horizontal orientation. My problem is that the first image is cropped and i cant see both of them in my galaxy s,only in some emulators..any help please?this is my linear layout code:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/header_new_3"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/header_btn1"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/header_btn2"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/header_btn3"
/>
</LinearLayout>
this is what i want
and this is what i get
#Jamo: image1:
image2,3,4:
Your containing layout is fill_parent, meaning it's restricted to the width of its parent (probably the whole screen). Each ImageButton is set to width wrap_content, meaning it just fits the width of its content. If the widths of the backgrounds of the images you're using sum up to more than the total parent width, you're going to have problems. This seems to be what's happening. You can't fit more pixels into a screen than what you have.
Update:
With your provided graphics, the "hello" image is 138px and the star is 60px. 138 + 60 * 3 = 318. If your screen is 480px wide (I think the Galaxy S is 480 wide) then you should be able to fit. If you put these into the default or mdpi folder (and the Galaxy S is hdpi) then these will be scaled to 1.5x that, which is 477px. That should still fit.
The only way I know to create something similiar is to use RelativeLayout like this:
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent">
<ImageView android:layout_alignParentRight="true"
android:id="#+id/iv4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/icon"/>
<ImageView android:layout_toLeftOf="#id/iv4"
android:id="#+id/iv3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/icon"/>
<ImageView android:layout_toLeftOf="#id/iv3"
android:id="#+id/iv2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/icon"/>
<ImageView android:layout_toLeftOf="#id/iv2"
android:id="#+id/iv1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="#drawable/icon"/>
</RelativeLayout>
We chatted about this in a previous question. Please post the whole layout file. The layout_weight won't do much, as hinted at in other posts, if the total width is being constrained by whatever is the parent of the outer LinearLayout.
Also, some thoughts.
You have "fill_parent" for the ImageView heights. I think those should be "wrap_content". You're setting the images with 'background' rather than 'src', which is kind of weird.
Also, that first image should be in another layout container, to allow stretching that doesn't screw up the 'hello'.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/header_new_3"
android:layout_gravity="center"
/>
</LinearLayout>
Not 100% sure on that being exactly what you want, but the ultimate goal would be to stretch the first image without shearing it in any way.

Android - Two ImageViews side-by-side

I am trying to create an Activity for an Android app with two imageViews aligned side-by-side. my current layout config is as follows:
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingTop="15dip" android:paddingBottom="15dip"
android:background="#drawable/dark_bg">
<ImageView android:id="#+id/numberDays"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:scaleType="fitStart"
android:src="#drawable/counter_01" />
<ImageView android:src="#drawable/counter_days"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:scaleType="fitStart"
android:id="#+id/daysText"></ImageView>
</LinearLayout>
The first image will be a square (lets say 100x100) and the second image will be rectangular (300x100) - and I want them to be aligned next to each other but always be scaled to fit within the width of the device - is this possible just with layout config?
The current config just shows the first image the entire width (and almost height) of the screen and the second image is not shown at all. I have tried changing wrap_content with fill_parent and hardocding widths but that has just resulted in the both images being shown but the first image on top of the second image (both anchored left).
Thanks
UPDATED AGAIN:
I have updated my layout to look like this now including the ScrollView as recommended but no joy:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="top"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<!-- Header for activity - this has the countdown in it -->
<ScrollView android:id="#+id/ScrollView01" android:layout_height="wrap_content" android:layout_width="fill_parent">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="right"
android:background="#drawable/dark_bg" android:orientation="horizontal">
<ImageView android:id="#+id/numberDays"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="2"
android:src="#drawable/counter_01" />
<ImageView android:src="#drawable/counter_days"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/daysText"/>
</LinearLayout>
</ScrollView>
<!-- main body for the rest of the info -->
<LinearLayout android:orientation="horizontal" android:gravity="center"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="#drawable/light_bg">
</LinearLayout>
</LinearLayout>
Using the layout_weight as suggested has given both the images the right ratios and appear to be scaled perfectly, however, I am still having the problem whereby they are both anchored to the far left of the screen, so the first image is actually overlaid on top of the second image, rather than having them side by side.
Below is a screenshot of the Eclipse display:
try using layout_weight for both of the ImageView components. So something like:
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:background="#drawable/dark_bg">
<ImageView android:id="#+id/numberDays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:layout_weight="1"
android:src="#drawable/counter_01" />
<ImageView android:src="#drawable/counter_days"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="fitStart"
android:layout_weight="1"
android:id="#+id/daysText"></ImageView>
</LinearLayout>
i added android:layout_weight="1" to each of them. Read up on layout_weight for LinearLayout definitions, it's very useful!
You can try creating a horizontal ScrollView with a layout_width of a pixel value greater than the two ImageViews combined, rather than fill_parent. Then place your LinearLayout inside of it.

Categories

Resources