I'm a beginner in Android. I use Eclipse to program my app. When I create an .xml class and I want to set the specific position of a button, I use this code for example:
<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"
android:background="#drawable/fm"
android:baselineAligned="false"
android:gravity="end" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollX="100dp"
android:scrollY="30dp"
android:text="#string/edad" />
</RelativeLayout>
... but nothing happens. It sets the button in a corner. How can I get it to appear elsewhere?
you need to use
android:layout_marginRight
android:layout_marginLeft
android:layout_marginTop
android:layout_marginBottom
You need to use positioning parameters such as android:layout_below. See the Relative Layout Guide for details.
You should click the button and go property. That's where the diffrent positioning settings are located.
For example below :#+id/VIEW then set marin top : 10dip. This will set you button 10 dip below VIEW.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="30dp"
android:text="#string/edad" />
Related
I am trying to design the layout of my Android project, but I must have done something wrong. Now when I try to drag the text boxes around, they just would not move on the screen. I am guessing it is one of the format setting, but not sure which one it is.
This is the XML file of how it looks:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.oldimagerevieal.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>
FrameLayouts use gravity to position items on a screen using the
android:layout_gravity="left|top" //whatever you want.
Unless you need the FrameLayout, you can change the root element to a RelativeLayout or LinearLayout (don't forget to specify orientation), and you should be able to drag things around like normal.
I would high recommend getting familiar with building views in XML. It will significantly help you.
Enable "Autoconnection to parent" by clicking on the magnet button in the design tools section.
May i ask how to put icon on my button like the image below?
And also where can i have resources or sample resources of buttons like this?
You'll need to set the drawable to the left of your Button. Either via xml or programmatically.
Either create a button and set a compound drawable or create an ImageButton.
<TextView android:id="#+id/login_with_facebook"
android:layout_width="100dp"
android:height="48dp"
android:text="FACEBOOK"
android:gravity="center"
android:drawableLeft="#drawable/facebook_icon_48_x_48"
android:clickable="true"/>
<Button android:drawableLeft="#drawable/image"
android:text="facebook"/>
If u want space between image and text, then give drawablePadding Like:
<Button android:drawableLeft="#drawable/image"
android:text="facebook"
android:drawablePadding="10dp"/>
using RelativeLayout it can be done easily. here I got snippet for you,
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/fb_logo_button"
android:text=""
android:background="fb_logo"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<Button
android:layout_toRightOf="#id/fb_logo_button"
android:id="#+id/fb_text_button"
android:text="Facebook"
android:textAlignment="center"
android:background="#android:color/holo_blue_bright"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
this will ensure that whatever your images are for respective DPIs, it will align this layout to center horizontally. Within this layout logo will be always to left of text as in picture.
This is applicable when your parent is also a RelativeLayout. If you got LinearLayout as parent then in above RelativeLayout tag change android:layout_centerHorizontal="true" to android:layout_gravity="center_horizontal"
I can't test it in eclipse as I'm out of home. Try this and let me know. Thanks.
I'm posting here in order to know if it was possible to make this design in Android :
If yes, could you help me how to do it (Just Guidelines).
Thank you
EDIT :
if I use only one Relative Layout I have this xml file :
<?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="wrap_content"
android:background="#drawable/background_cross"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="F1\n" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="300dp"
android:layout_marginLeft="50dp"
android:text="Left\nbutton" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right\nbutton"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp"
android:layout_marginRight="50dp"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="380dp"
android:text="Bottom\nbutton" />
</RelativeLayout>
But declaring static dp is very bad and it isn't good to apply on all devices..
My xml code is shit ?
Do I have to get width and height screen of the device programmatically and set my buttons positions programmatically too ?
May be I could use this picture in Background and bind buttons on it ? (But how ?)
You can do this using a RelativeLayout and add the four buttons in it. Android supports adding custom fonts to your app and you will be able to set the button's font as well, you just need the font file. See here or here an example.
You will need the buttons background just as a white/red ring with transparent background.
You can place the buttons inside the RelativeLayout using margin properties (layout_marginLeft/Right/Top/Bottom) see here other properties.
The buttons text should have two lines (see android:lines xml property for a Button).
Good luck! I hope it helps ;).
I am currently toying around with android and making a little bit of a test app just to try coding one. I'm sure there's an easy way to do this but i can't figure it out. I want two buttons to be on the bottom of the screen. But, i want one to be on the left side, and one on the right side, with space in the middle.
Thanks for the help
Like this perhaps?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id"#+id/left_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Left"/>
<Button
android:id"#+id/right_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Right"/>
</RelativeLayout>
You need to give android:layout_alignParentLeft="true" to align left and android:layout_alignParentRight="true" to align right.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id"#+id/left_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Left"/>
<Button
android:id"#+id/right_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Right"/>
</RelativeLayout>
You can do that using either LinearLayout, TableLayout or RelativeLayout. Try it... it's not that hard.
Create a linear layout (inside your linerlayout which is created by default when your creating layout)
Edit your new linear layout
android:layout_gravity="bottom"
android:orientation="horizontal"
android:gravity="center"
Add two buttons inside linearlayout (add text/size also)
Optional: add both button margin_layout: 10dip
android:layout_margin="10dip"
That should do.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" android:background="#drawable/robo">
<TextView android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max Time(Sec)">
</TextView>
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:id="#+id/maximum"
android:inputType="number">
</EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/startbtn"
android:focusable="true">
</Button>
<ImageButton android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/icon"
android:id="#+id/imageButton1">
</ImageButton>
</LinearLayout>
In LinearLayout you can't change positions a whole lot - because the layout is Linear (sequential). But you can use layout_margin to somewhat move the widgets.
I don't know what you want to do, but you should look into FrameLayout (which will let you put the image anywhere!). My personal favorite is RelativeLayout.
Position depends on position of it's parent container/component (Layout) and on it's layout_* properties.
And u did'n tell what u want. If u want change it position - switch it with another view in Layout.
put more buttons in layout and hide or show them according to your needs
views in android are in relationship, not only in RelativeLayout but in Others, so you should choose the best way to describe your app layout, using more than one is commonly used.
to change position of label, you 'd better using AbsoluteLayout then using android:layout_x="", android:layout_y=""
what is your exact requirement???
you can put two buttons and make one visible and other one invisible or vice verse.or you can put layout_margin for all direction.And try to put all your component in different layout.