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
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>
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>
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
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"?
Related
i am beginner to android programming and trying to build a simple app program.
whenever i try to make more than one views they stack on each other.
I am trying to make a text field and a button, but when i run it, the text field and button overlap each other however, i want them to be separated by some distance.
i am using the following code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.new1.MainActivity" >
<EditText android:id="#+id/edit_message"
android:layout_weight= "1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</RelativeLayout>
i am checking this code on Samsung Galaxy S2.
does anybody knows the solution to this problem and can guide me where i am doing it wrong.
Either use LinearLayout so that elements stack horizontally or vertically, or use attributes such as android:layout_toRightOf="#id/edit_message" to control placement within a RelativeLayout.
Find out more about LinearLayout here: http://developer.android.com/guide/topics/ui/layout/linear.html and RelativeLayout here: http://developer.android.com/guide/topics/ui/layout/relative.html
RelativeLayout positions views relative to each other. So if you do not specify the relationship between view, all the views will be put one above the other. You can either use LinearLayout with orientation attribute or define relationship between views. Following can be your working code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="#string/edit_message"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/edit_message"
android:text="#string/button_send" />
</RelativeLayout>
Here android:layout_toRightOf="#id/edit_message" lets your button to be positioned to right of your edittext
Depending on what you want, a horizontal layout or vertical, you need a fitting layout xml.
You use relativeLayout, where you have to specify the parents to layout like:
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
If your beginner better take a look at a linear layout in vertical or horizontal mode.
You dont have to specify this then.
Like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/bResults"
android:text="Try Command"
android:layout_weight="20" />
<ToggleButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tbPassword"
android:layout_weight="80"
android:checked="true"
/>
</LinearLayout>
This will put your two buttons nexto each other
Today I changed my Toast to Crouton.
But I had these problems :
1)Showing Crouton in center
2)Whenever I want to display new Crouton it is displaying previous Crouton again, though previous one hided long ago
I want to know if there is better way to solve above problems?? Cause I looked at sample and library but could not find"
Currently I'm solving these ways
activity layout:
<RelativeLayout
android:id="#+id/bigcontainer"
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"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DilmancSRActivity"
tools:ignore="MergeRootFrame" />
<RelativeLayout
android:id="#+id/alternate_view_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Creating my custom toast view and centering it horizontally:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/kdrw"
android:orientation="horizontal"
android:padding="8dp" >
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="8dp"
android:contentDescription="#string/strnull"
android:src="#drawable/logo" />
<TextView
android:id="#+id/toasttextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
Showing Crouton:
LayoutInflater inflater = localdsr.getLayoutInflater();
View layout = inflater.inflate(R.layout.toast, null);
TextView text = (TextView) layout.findViewById(R.id.toasttextView1);
text.setText(info);
if (toast != null)
toast.cancel();
Configuration croutonConfiguration=new Configuration.Builder().setDuration(1500).build();
toast=Crouton.make(localdsr, layout ,R.id.alternate_view_group ,croutonConfiguration);
toast.show();
For solving the Second one I had to download library source and use it. .As I do not want to use animations firstly, I disabled animations
//croutonView.startAnimation(crouton.getInAnimation());
issue on removing function that was making things appear again inside
protected void removeCrouton(Crouton crouton)
//sendMessageDelayed(crouton, Messages.DISPLAY_CROUTON, crouton.getOutAnimation().getDuration());
//I changed it with below .plus removed crouton.getOutAnimation().getDuration() so it does not throw exception
sendMessage(crouton, Messages.DISPLAY_CROUTON);
I believe I am missing something. Hope there is already right way to do those .
Making changes within the library itself is not encouraged as it might create issues that only appear within your version of Crouton.
You can change the Animation via Configuration.Builder.setInAnimation(...) and .setOutAnimation(...).
The Configuration can be re-used and does not have to be created every time you want to show the Crouton. The same is valid for your View. It does not have to be inflated every time.
Regarding the duplicate display of a Crouton: This behavior is not intended.
The code above looks like it should display a Crouton with the info text set. If your info text has changed during the display of Croutons it might be a bug within the library. If that is the case, feel free to report it.
I have a long vertical image (224x1600). I want it to fit the image width, and scroll vertically. It needs to be scroll-able. I have tried everything and can't get it to work :\
Here is what I have
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android1:id="#+id/tab3"
android1:layout_width="fill_parent"
android1:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/tiles" />
</LinearLayout>
</ScrollView>
Second question is: Is there a way to PREVENT image scaling? The image is 224x1632 and shows that way on my galaxy nexus. On the Nexus 7 (much larger screen but lower DP) it goes to 199x1440. A weird scaling of 88%. Can I prevent this? I need it to display 224x1632 on all machines and scale the width accordingly.
Many thanks!
If you need to avoid scaling entirely, you should place your drawables in the res/drawable-nodpi folder. Keep in mind that the physical size will be much different on devices of different densities, but as long as you understand the risks, that is the solution you should pursue.
On your first question, I'm inclined to think you'll need to do some manual work in code, but although I can't test it right now, this also might work:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/tiles"
/>
</ScrollView>
Give it a shot and let me know how it goes.
It should work... I think the problem is that you have android1: and it is suppose to be android:
Something like:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/tiles" />
</LinearLayout>
</ScrollView>
As for the second question, you can try and use android:minWidth="" and android:minHeight="" to see if that solves your problem.
EDIT: try android:scaleType="fitCenter" it will display the image as is.
Hope that helps.
I want to align text of a button to the left, I don't know how to do this, please help me how to do this in the xml file. I didnĀ“t find the properties for this.
Maybe this will help you:
<?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="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:text="Button" />
</LinearLayout>
You probably want both
android:gravity="left|center_vertical"
and then a little bit of space
android:paddingLeft="5dp"
to keep the text from running up against the left edge of the button.
(Adjust the amount of padding_left as appropriate for your button art.)
android:gravity="left"
Hope This will help
android:gravity="left|center_vertical"
It will work but now you may need to add some padding left according to your requirement.
android:paddingLeft="10dp"
This is combination from above that worked for me, icon on left and text left aligned with some padding
<Button
android:id="#+id/butt_labs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dt_labjob"
android:paddingLeft="10dip"
android:gravity="left|center_vertical"
android:text="#string/l_labs" />
You probably need BOTH the android:gravity AND the android:layout_gravity to align text to "left".
As everyone else has mentioned, "left|center_vertical" works, but I have found that if left or right is not explicitly stated, most properties default to left.
So putting the following should be enough to get your text left-justified and vertically centered:
android:gravity="center_vertical"
If you have to support many different languages it is better to use start or end to position text. That way for right to left (RTL) languages it will still work.
Example:
<?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="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:text="Button" />
</LinearLayout>
I want to create layout where I have two images at left and at right and text in center.
I have tried to do it with relative layout but unfortunetly it was unsuccessfully. Could anybody provide me an example?
Have you tried something like?
<?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">
<ImageView
android:id="#+id/img1"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:src="#drawable/image1"/>
<ImageView
android:id="#+id/img2"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"
android:src="#drawable/image2"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/img1"
android:layout_toLeftOf="#id/img2"
android:layout_alignTop="#id/img1"
android:text="I'm between!"/>
</RelativeLayout>
If you don't need more things on your view, you could want to use LinearLayout instead, since it's more easier to implement. In that case, you just have to play with the layout_weight attribute.