Android button position changes while the screen rotating - android

Hai all I am new in android programming.....
For my application I am using image button and it successfully installed on the device.
But when I rotate the screen all the button position get changed...
and I need all the button placed at centre of the screen....
And here is my code....
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/testText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFCC99"
android:textSize="24dp" />
<ImageButton
android:id="#+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="124dp"
android:layout_marginTop="80dp"
android:src="#drawable/up" />
<ImageButton
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_below="#+id/up"
android:layout_marginRight="200dp"
android:layout_marginTop="150dp"
android:src="#drawable/left" />
please help.......
<ImageButton
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_below="#+id/up"
android:layout_marginRight="50dp"
android:layout_marginTop="150dp"
android:src="#drawable/right" />
<ImageButton
android:id="#+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/up"
android:layout_alignParentRight="true"
android:layout_marginRight="124dp"
android:layout_marginTop="80dp"
android:src="#drawable/down" />
</RelativeLayout>

well if u are creating your app for both potrait as well as landscape mode then you have to make two different layouts otherwise you can restrict your app to display your app only on the potrait mode by adding a screenOrientation property in your manifest file
android:name=".ListViewImagesActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"

change your layout to relative layout -- and you can also put a different layout for landscape and portrait refer supporting multiple screens by android developers you will understand in a better way http://developer.android.com/guide/practices/screens_support.html

Create layout xml file for both landscape and portrait mode is the good practice. You can also manipulate it through Layout changes, but its recommended to use two layout files.
Then put the landscape layout file in layout-land folder and other in layout-port folder.
For more info : Click here for more info from android developer site

Related

Do I need to use a horizontal layout (android studio 1.5.1)?

I am new in the Android world and I have a little problem with the screen in horizontal position. My android studio version is 1.5.1.
I have already completed the vertical layout, but I have issues when I turn the mobile in horizontal position; all the images, buttons get moving and the VideoView disappears.
This how my layout looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.example.ubuntunyx.apptest3.MainActivity"
android:background="#a987b7"
android:weightSum="1">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#4c335a"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<RelativeLayout
android:id="#+id/LytRelVideoResize"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.94">
<Button
android:id="#+id/btnResize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnResize"
android:layout_gravity="left"
android:onClick="btnResize_click"
android:background="#drawable/btn_redondo"
android:layout_marginBottom="39dp"
android:textColor="#ffffff"
android:layout_alignBottom="#+id/imgResize"
android:layout_toLeftOf="#+id/imgResize"
android:layout_toStartOf="#+id/imgResize"
android:layout_marginRight="41dp"
android:layout_marginEnd="41dp" />
<ImageView
android:id="#+id/imgResize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ocaso"
android:layout_below="#+id/barResize"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="49dp"
android:layout_marginEnd="49dp" />
<VideoView
android:id="#+id/videoPiramides"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_below="#+id/imgResize"
android:layout_marginTop="41dp"
android:layout_alignRight="#+id/barResize"
android:layout_alignEnd="#+id/barResize"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/btnResize"
android:layout_alignStart="#+id/btnResize" />
</RelativeLayout>
<ImageView
android:id="#+id/imgDragDrop"
android:layout_width="118dp"
android:layout_height="100dp"
android:src="#drawable/buho"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btnExit"
style="?android:attr/buttonStyleSmall"
android:layout_width="81dp"
android:layout_height="wrap_content"
android:text="#string/btnExit"
android:onClick="btnExit_click"
android:textColor="#ffffff"
android:background="#124a68"
android:layout_gravity="right"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</LinearLayout>
I was looking at: http://developer.android.com/intl/es/training/basics/supporting-devices/screens.html but it just get me more confused.
you just need to build another layout landscape
just follow the picture below:
and then you design that new layout as you wish.
OR
if you dont need that landscape (forcing the APP to set in one orientation only)
AndroidManifest.xml
and add this one of these code to the application tag
android:screenOrientation="portrait"
android:screenOrientation="landscape"
The best solution for this problem is to create design for landscape mode. You should make new folder called 'layout-land' in your res folder. In new folder create new layout file with the same name but with design for landscape mode. Android os will know when to use landscape and when to use portrait mode.
You have a couple of options here:
1. Rearrange the layout child views so that they look fine both in landscape and portrait mode. This will need trial and error and is not always possible.
2. Make your activity stick to a particular layout, regardless of changes in the device orientation. This can be done in you apps manifest file as:
<activity android:name=".MyActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"/>
3.The best way: Create separate layout files for landscape and portrait orientations. In android studio it is easy. Suppose you have an activity named MyActivity, then the layout files will be something like: my_activity.xml (for normal, portrait mode) and my_activity_land.xml for landscape mode.

Why isnt my Android view element rotating

I have an image button inside a frame layout inside of a relative layout. I add the following to the image button android:rotation="-90" but the image button doesnt appear to change.
View Hierarchy
XML
<ImageButton
android:id="#+id/profile_pic_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/round_profile_pic_btn"
android:contentDescription="profile picture button"
android:padding="20dp"
android:rotation="-90"
android:layout_gravity="center"
android:src="#drawable/ic_face_black_48dp"/>
<com.github.lzyzsd.circleprogress.DonutProgress
android:id="#+id/donut_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:donut_progress="70"
custom:donut_text_size="0dp"
android:layout_alignTop="#+id/profile_pic_btn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/textView5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_gravity="center" />
Could not post as comment as my rep is not there yet. Did you try it in any emulator or actual device? I copied your xml (except the circleprogress.DonutProgress stuff) and it worked in the emulator and device.
The effect is not visible in Design mode though.

Actual position of app design in graphical versus in Emulator

I am very novice in android platform.I am trying to design an application.For the design purpose i have taken RelativeLayout .But the unique thing that i am noticing is while seeing it in graphical Layout in eclipse ,Buttons and EditText are at different position while it is at right position when seeing in emulator.
Here is my code..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/pnr_bg"
android:orientation="vertical"
android:padding="0dp" >
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="220dp"
android:maxHeight="30dp"
android:src="#drawable/pnr_normal" />
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="266dp"
android:layout_marginTop="220dp"
android:maxHeight="30dp"
android:src="#drawable/tenq_normal" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10" >
<requestFocus />
</EditText>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:src="#drawable/submit" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_columnSpan="6"
android:layout_gravity="right|bottom"
android:layout_row="6"
android:layout_height="match_parent"
android:textColor="#0f0f0f" />
Why is this happening.Will it do any impact on the different size of the mobile screens.
Please help me..
Yes that's correct thing. Whatever layout/UI you see, it won't be the same in emulator or real device. Reason being is android comes with multiple and variety of resolutions. The layout which you see while designing is just to preview the design or we can say just a sample preview of UI with controls/widgets you have taken.
You can consider it as an advantage of designing UI/layout in android for different resolutions/multiple screens.
When I say advantage, it means you can design just a single layout and can preview it for multiple screens, and so you would be having better idea.
Update:
Just check the image and do the same thing as depicted in image, to preview all screens. Once again I am repeating, the preview which you are seeing is just a preview, it would looks somewhat different in real devices.

change imagebutton Proportional to size device on android

I am develop xml with some image button,image buttons show properly in emulator but it Disorganization on
devices(for example galaxy fit)
As described in emulator https://www.dropbox.com/s/qeecp868ht61sck/emulator.jpg and galaxy fit https://www.dropbox.com/s/23r9tvtp0tcn7bs/fit.jpg
what change imagebutton Proportional to size device?
what can i do?
this is my xml:
<RelativeLayout 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"
tools:context=".Main"
android:background="#drawable/backmain" >
<ImageButton
android:id="#+id/btnfehrest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp"
android:background="#null"
android:src="#drawable/fehrest" />
<ImageButton
android:id="#+id/btndarbareh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnfehrest"
android:layout_marginLeft="21dp"
android:background="#null"
android:src="#drawable/darbareh" />
<ImageButton
android:id="#+id/btnmahsulat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btndarbareh"
android:layout_marginRight="17dp"
android:background="#null"
android:src="#drawable/mahsulat" />
<ImageButton
android:id="#+id/btnkhoruj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btndarbareh"
android:layout_centerHorizontal="true"
android:background="#null"
android:src="#drawable/khoruj" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/main"
android:scaleType="fitXY" />
</FrameLayout>
</RelativeLayout>
It is all because you are using Relative Layout in your xml
Relative layout always maintains a relationship with other views in your xml either with Parent view or with child views.
so on small screen your FrameLayout with button image trying to fit in the screen as it maintaining the relationship with other views, that's why its overlaping.
Talking about giving the suggestion regarding this.
1) You can use Linear Layout with scroll view, ie if views gets out of screen you can see them by scrolling down.
//If you don't wanna do this
2) You can create another xml in layout-small with same name.
Example:
res> layout> main.xml
and
res> layout-small> main.xml
Second option will help you in creating multi support for different screen sizes.
Note: By Default layout-small folder is not present but you can create it.

Xml layout changes while changing orientation

I have two activities which opens depending on the SD card presence. One activity has three Buttons and a TextView and the other an ImageView, a button and zoom control. When I change the orientation, the buttons get scrambled around while in horizontal direction. How to go around this?
My 'SD' card layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1E1E1E"
android:orientation="vertical" >
<Button
android:id="#+id/button_print"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:background="#drawable/my_button"
android:text="#string/print" />
<TextView
android:id="#+id/text_SDmissing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:text="#string/SDmissing"
android:textSize="20dp" />
<Button
android:id="#+id/button_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_print"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp"
android:background="#drawable/my_button"
android:text="#string/camera" />
<Button
android:id="#+id/button_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_SDmissing"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="#drawable/my_button"
android:text="#string/insert" />
</RelativeLayout>
You have to create separate XML files for portrait and landscape modes and place it in different directories. The device will automatically select the right one. You can use the following directory structure
res/layout/my_layout.xml
res/layout-land/layout.xml
For further reference you can check:
http://developer.android.com/guide/practices/screens_support.html
You can make a folder in your project called "layout-land" (in the "res" directory). In there you can copy all your xml styles from the normal "layout" folder and restyle them for landscape mode. Android will automatically use those layouts for landscape mode.
Your project would look like this:

Categories

Resources