Android ImageView margins - android

Creating layout and can not solve margins up and above the ImageView
(and don't understand why they appear).
Logo itself is without these margins.
I use layout_width="250dp" the same as for the buttons.
And layout_height="wrap content".
Layout code is here:
<?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:background="#drawable/menu_background"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical"
>
<ImageView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:src="#drawable/game_logo"
/>
<Button
android:id="#+id/start_button"
style="#style/menuButtonStyle"
android:layout_margin="5dp"
android:text="#string/start_button" />
<Button
android:id="#+id/result_button"
style="#style/menuButtonStyle"
android:layout_margin="5dp"
android:text="#string/high_score" />
<Button
android:id="#+id/settings_button"
style="#style/menuButtonStyle"
android:layout_margin="5dp"
android:text="#string/settings" />
<Button
android:id="#+id/about_button"
style="#style/menuButtonStyle"
android:layout_margin="5dp"
android:text="#string/about_button" />
</LinearLayout>
Can I manage this without setting ImageView's layout_height in dp?

Set following property in your ImageView:
android:adjustViewBounds="true"

Related

Cannot see second button

I'm trying to create a menu on my home screen but I cannot see the second button that I created. I tried android:layout_below="#+id/score_button" but it gives warning "invalid layout parameter"
Here is my xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/score_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/action_settings" />
</LinearLayout>
Something that I've noticed is that whichever button is placed first in the xml file is the one that is displayed when I run the app on my device.
Thanks in advance for any help, much appreciated.
use RelativeLayout instead of LinearLayout
then you can use android:layout_below="#+id/score_button"
and remove android:orientation="horizontal" when using RelativeLayout
Change the layout_width to wrap_content:
<Button
android:id="#+id/score_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/action_settings" />
Also if you want equal width buttons, give layout_weight:
android:layout_weight="1"
to both the buttons.
try below code:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/score_button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="#string/action_settings" />
</LinearLayout>
Repace your xml code by this code...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/score_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/action_settings" />
</LinearLayout>
User orientation as Vertical. In linear layout there is no layout_below attribute. Use Relative layout instead.
To force same width for both buttons:
<Button
android:layout_width="0dp"
android:layout_weight="1"
... />
<Button
android:layout_width="0dp"
android:layout_weight="1"
... />
To use natural buttons width:
<Button
android:layout_width="wrap_content"
... />
<Button
android:layout_width="wrap_content"
... />
Instead of "match_parent" for width, put weight
<Button
android:id="#+id/score_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/action_settings" />
IF you want to show the menu in horizontal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/score_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:layout_weight="1"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/action_settings" />
</LinearLayout>
And if you want to show one blow the other change orientation to Vertical
Try this..
Change this orientation horizontal to vertical like below
android:orientation="horizontal"
to
android:orientation="vertical"
Adjust the button width and height appropriately and check again .. All the best
The android:layout_below="" attribute can only be used with a Relative Layout. You have chosen a Linear Layout.
Also both the buttons choose the "match_parent" for the android:layout_width attribute.
Either change "match_parent" to "wrap_content", otherwise change the android:orientation tag of the linear layout to "vertical".
Also if you change the layout to relative, it will work.

Why the height of the scroll view is longer than the actual content

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="#drawable/splash_bg" />
<ImageView
android:id="#+id/photo_area"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:adjustViewBounds="true" />
<TextView
android:id="#+id/share_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/photo_area"
android:layout_marginLeft="20dp"
android:text="#string/share_title"
android:textColor="#android:color/white" />
<EditText
android:id="#+id/share_content"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#+id/share_title"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/custom_textarea"
android:gravity="top|left"
android:hint="default message" />
<ImageView
android:id="#+id/share_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/share_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="#drawable/photo_taken_btn_submit" />
</RelativeLayout>
</ScrollView>
This is the xml of the layout. The photo_area by default do not have image set but it will download a picture from internet and set on it. The problem is the height of the content view is longer than it should be, so there is a lot of empty space at the end . I try to removed the background in linearlayout, the height reduced a bit but still there are some empty space how to fix it? Thanks
use android:adjustviewbound = "true" in the imageview xml fix the problem.

is it possible to scale an ImageView such that a LinearLayout below it is always completely visible?

My layout is below. On some mdpi screens the inner LinearLayout is cutoff. What I would like to happen is to make sure that the inner LinearLayout always shows all of it's content. I would like the ImageView to scale down as small as it needs to make sure that the LinearLayout below it show's all of it's content. On large screens, the ImageView should be as large as it's image (i.e. wrap_content).
I tried giving the ImageView a layout_weight of 0.1 and the LinearLayout a layout_weight of 0.9 (and giving both of them a layout_height of 0dp), but then on larger screens the ImageView ends up being at the top of the layout. I would like the contents of the root LinearLayout to be centered.
So is it possible to make the ImageView shrink only if it needs to, such that the LinearLayout below it is always completely visible? I am trying to accomplish this witout creating multiple layout files for different screen sizes/resolutions.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="#style/tab_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".messages.MessagesHomeFragment"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:src="#drawable/elephant_large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<Button
android:id="#+id/button_send_voice_message"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Send a Voice Message" />
<Button
android:id="#+id/button_send_sms_message"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Send an SMS" />
<Button
android:id="#+id/button_view_messages"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Manage Messages" />
<Button
android:id="#+id/button_invite_sms_recipients"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Invite SMS Recipients" />
</LinearLayout>
</LinearLayout>
I am not sure this is possible... if anything, I would try something like the layout described below (I did not try it, I am afraid the image would grow as much as it can with something like that). I only changed the layout attributes of the ImageView
If that still does not work, it should not be too difficult to define your own widget based on ImageView where you would only need to override the onLayout method to scale down only if the measureSpec you get is smaller than the image...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="#style/tab_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".messages.MessagesHomeFragment"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerInside"
android:layout_marginBottom="40dp"
android:src="#drawable/elephant_large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<Button
android:id="#+id/button_send_voice_message"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Send a Voice Message" />
<Button
android:id="#+id/button_send_sms_message"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Send an SMS" />
<Button
android:id="#+id/button_view_messages"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Manage Messages" />
<Button
android:id="#+id/button_invite_sms_recipients"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Invite SMS Recipients" />
</LinearLayout>
</LinearLayout>
I was able to accomplish what I was trying to do by using the layout below. The key was to set the root LinearLayout's height to wrap_content and set the layout_weight of the ImageView to 1.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="#style/tab_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="#+id/elephantImageView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:layout_marginBottom="20dp"
android:src="#drawable/elephant_large"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<Button
android:id="#+id/button_send_voice_message"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:text="Send a Voice Message" />
...
</LinearLayout>
</LinearLayout>

what RelativeLayout properties should i use?

i want to design a layout for my app as shown below in image
but im getting problem which is that the properties im applying for relative layout are not going right as in below my xml code there is horizontal ruler image having centerInParent property and a verticle ruler image also having property of centerInParent by doing this the both rulers come to accurate place but when i put a child above the horizontal ruler or a child toLeftOf verticle ruler they dont appear as the graphical view of xml file shows the reason that although by applying centerInParent the both rulers come to center but their reference remains at edges thats why by using layout_above or layout_toLeftOf the childs go outside of layout
My xml code
<?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="#FFFFFF"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/title"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/icon_img"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dip"
android:layout_marginTop="2.5dip"
android:src="#drawable/smicon"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_toRightOf="#id/icon_img"
android:layout_marginTop="3dip"
android:text="Welcome To JEM"
android:textStyle="bold"
android:id="#+id/wcm_id"
android:textSize="18dip"
android:textColor="#FFFFFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/wcm_id"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:text="APPS BY SAM"
android:id="#+id/app_by"
android:textSize="16dip"
android:textColor="#FFFFFF"
/>
</RelativeLayout>
<!-- This relative layout which is showing the main cross and icon around it -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/center_hr"
android:layout_centerInParent="true"
android:src="#drawable/horizontal"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/center_vr"
android:layout_centerInParent="true"
android:src="#drawable/verticle"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/center_hr"
android:layout_toLeftOf="#id/center_vr"
android:id="#+id/getuser"
android:src="#drawable/adduser"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/center_hr"
android:layout_toRightOf="#id/center_vr"
android:id="#+id/set"
android:src="#drawable/set"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/center_hr"
android:layout_toLeftOf="#id/center_vr"
android:id="#+id/get"
android:src="#drawable/get"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/center_hr"
android:layout_toRightOf="#id/center_vr"
android:id="#+id/ico"
android:src="#drawable/icon"
/>
</RelativeLayout>
</LinearLayout>
so guys please tell me how can i achive this main cross and icon to lef,right and above,below it as shown in image
Try this..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/vertical_ruler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/imageView1"/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/vertical_ruler"
/>
<ImageView
android:id="#+id/horizontal_ruler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/imageView1"/>
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/horizontal_ruler"/>
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/vertical_ruler"
android:layout_below="#id/horizontal_ruler"
/>
May be it helpful for you..
You could use a TableLayout embedded in a RelativeLayout. The RelativeLayout can be used to center the TableLayout on the screen and in the TableLayout you can add your pictures/buttons and state how many rows and colums you want the TableLayout to have.

How do I achieve the following result using RelativeLayout?

Pictures: http://img838.imageshack.us/img838/1402/picse.png
How do I make the layout in Pic. 2 using RelativeLayout ONLY. Below is the xml layout of the first screenshot.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/timer_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dip"
android:textSize="30sp"
android:layout_alignParentTop="true"
android:text="#string/timer_start" />
<ListView
android:id="#+id/questions_listview"
android:layout_below="#id/timer_textview"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<Button android:id="#+id/true_btn"
android:text="#string/true_option"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/false_btn"
android:text="#string/false_option"
android:layout_toRightOf="#id/true_btn"
android:layout_alignBaseline="#id/true_btn"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
You could use LinearLayout at bottom to achieve this. But you want to use RelativeLayout layout only so:
<RelativeLayout android:layout_width="fill_parent" ... >
...
<View android:id="#+id/helper"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button android:id="#+id/true_btn"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/helper"/>
<Button
android:id="#+id/false_btn"
android:layout_alignBaseline="#id/true_btn"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/helper" />
</RelativeLayout>
Can't you really use nested layouts?
If you change the buttons to
<Button android:id="#+id/true_btn"
android:text="#string/true_option"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/false_btn"
android:text="#string/false_option"
android:layout_toRightOf="#id/true_btn"
android:layout_alignBaseline="#id/true_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
you use at least the full width although the false_btn gets all remaining space.
To increase the width of the first true_btn you could set the minWidth attribute - either use a fixed value or get the screen width and make the minWidth the half of it

Categories

Resources