-2

I'm using Android Studio for application development and I have a problem with placing elements in the screen.

For example: I'm trying to place a button to the right of the screen and in the studio its shown to the right of the screen but when I install the app in my smartphone i see it in the left of the screen (my smartphone is right to left configured).

How can i resolve it? (I use RelativeLayout if that matters).

Thanks!

illustration image

Paebbels
  • 15,573
  • 13
  • 70
  • 139
tomerJK
  • 39
  • 7
  • Please provide code for your layout – K.Os Apr 29 '17 at 15:40
  • why you need a code? – tomerJK Apr 29 '17 at 15:41
  • 1
    By "put it on the right" do you mean that you put it on the right in the graphical editor, or that you made it "layout_alignParentRight=true" in your xml? Never trust the graphical editor. I would suggest not even looking at it. Learn to love the xml. – Gabe Sechan Apr 29 '17 at 15:43
  • yes i put it on the right in the graphical editor as you see in the attached picture. not looking in the graphical editor? are you serious? it's absurd to write a gui by code myself.. – tomerJK Apr 29 '17 at 15:47
  • In android development it is possible to have multiple implementation of the graphical view in xml, so it really matters how your xml looks like. Just copy and paste it there. – K.Os Apr 29 '17 at 15:50
  • the code is attached below for your request. – tomerJK Apr 29 '17 at 15:58
  • Don't put your code in the answers. Edit your question and put your code over there. – ray an Apr 29 '17 at 16:00
  • what does it matter??? – tomerJK Apr 29 '17 at 16:03
  • It does matter because it's not an answer but a part of your question. – ray an Apr 29 '17 at 16:15

5 Answers5

0

The code for those who requested:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"><![CDATA[
    android:splitMotionEvents="true"
    tools:context="tomer.newapp.MainActivity"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">



    ]]>


    <Button
        android:id="@+id/btn"
        android:layout_width="145dp"
        android:layout_height="75dp"
        android:layout_column="2"
        android:layout_row="2"
        android:onClick="btnclick"
        android:text="Click!"
        android:textSize="30sp"
        tools:layout_editor_absoluteX="223dp"
        tools:layout_editor_absoluteY="140dp"
        android:layout_marginLeft="11dp"
        android:layout_marginStart="11dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>
tomerJK
  • 39
  • 7
0

It would be easier to answer your question if you included some code. However, assuming your button is inside a relative parent layout which fills the screen, adding this to your button's xml code will make it align to the right:

android:layout_alignParentRight="true" 

ie.

    <Button
        android:id="@+id/btn"
        android:layout_width="145dp"
        android:layout_height="75dp"
        android:layout_alignParentRight="true"
        android:onClick="btnclick"
        android:text="Click!"
        android:textSize="30sp" />

Or to make it really easy, replace your entire XML with:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <Button
        android:id="@+id/btn"
        android:layout_width="145dp"
        android:layout_height="75dp"
        android:onClick="btnclick"
        android:text="Click!"
        android:textSize="30sp" 
        android:layout_alignParentRight="true"/>

</RelativeLayout>
simplegr33n
  • 130
  • 2
  • 10
  • just replace your button's xml with the xml of the button in my sample above and you should be off to the races. – simplegr33n Apr 29 '17 at 16:05
  • but it is not solve my problem, i want to see in my smartphone the same as i see in the graphical editor..it would be very difficult to design an application without it. – tomerJK Apr 29 '17 at 16:09
  • if you look at your XML you'll see a bunch of added attributes like android:layout_alignParentLeft="true" (which is forcing your layout to be to the left) and others which have been added automatically from you trying to build layouts from the graphical editor. It will take a little time to learn, but XML layouts are relatively straight forward, and you will find it's much more efficient and simpler to work on layouts in the Text Editor. Note that there is a preview panel you can display when working in the text editor which shows a preview of what your layout will look like. – simplegr33n Apr 29 '17 at 16:11
  • Ok it's work in your way, so you suggest in fact to work only with the xml editor? – tomerJK Apr 29 '17 at 16:17
  • Yes. I do not know anyone who works on android layouts using the graphical editor. Click the "Preview" tab in the top right corner of the screen when in the XML editor and it will open a preview panel which will update as you work with the XML. It's a good way to fool around and see how everything works. Please mark my answer as correct if it helped. – simplegr33n Apr 29 '17 at 16:21
0

Note that the absolute values that you are using are in the tools namespace - this means they are not compiled into your app.

tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"

That is probably why you are getting this kind of behavior. Remove them and then see if the button moves to the left of the screen.

More about tools namespace - here

Community
  • 1
  • 1
ray an
  • 1,132
  • 3
  • 17
  • 42
0

You mentioned "(my smartphone is right to left configured)".

This could be a dud answer, but...

With the layout_editor, for either the "gravity" or "layout_gravity" properties, there is a "right", "left", or "start" and "end". In English, one starts writing at the left, and ends at the right. In Arabic, for example, one starts at the right and ends left. Any chance your property says "end", instead of "right"?

MarkDubya
  • 309
  • 2
  • 3
0

I solved it with using this line: android:layoutDirection="ltr" in the xml of the layout section. It's keeping the layout order like in the UI.

AGJ
  • 23
  • 5