XML Views in Android - android

I'm sorry I'm new to Android and this seems like an easy question but I just don't get it. I have two buttons (same size) and two images (different sizes). I want the buttons below the images but exactly centered to it. I searched a couple of forums but I just don't get it how that works. I also don't understand how to declare one of the images as a parent so the button could be the child.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/negativ"
android:background="#drawable/smiley"
android:layout_below="#+id/titlet"
android:layout_marginTop="50dp"
android:layout_marginLeft="500dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textnhelpful"
android:background="#drawable/nothelp"
android:layout_below="#+id/negativ"
android:layout_marginTop="20dp"
android:layout_marginLeft="500dp"
/>

In android SDK you can't add image view to button as child or reverse, my solution make relative layout and add image view as child to layout make button and layout "wrap_content" and add button as child to layout andmake gravity "center".

Here is xml code
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>

Related

Placement of ImageButtons in corners of LinearLayout in Android

I am trying to place two ImageButton in the corners of a LinearLayout as shown in figure below:
I have tried using the android:layout_gravity attribute with the value set to left and right for the two buttons. However, they show just next to each other as shown in image below:
How can I place the two image buttons in the corners of the LinearLayout? I have looked up a lot of previous answers and tried them but nothing seems to work. How can this placement of image buttons be done?
The XML of the layout is in the snippet below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|fill_horizontal"
android:orientation="horizontal">
<ImageButton
android:id="#+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:contentDescription="#string/prev_button"
android:src="#drawable/arrow_left"/>
<ImageButton
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:contentDescription="#string/next_button"
android:src="#drawable/arrow_right"/>
</LinearLayout>
Use a RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:contentDescription="#string/prev_button"
android:src="#drawable/arrow_left"/>
<ImageButton
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:contentDescription="#string/next_button"
android:src="#drawable/arrow_right"/>
</RelativeLayout>
A relativelayout is better for positioning views at the edges of the screen
there is another way to do this instead of using RelativeLayout by using a third View , layout xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<View
android:layout_width="0dp"
android:layout_weigth="1"
android:layout_height="0.5dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
this way is better than using RelativeLayout and makes your layout smoother

TextView displaying one char per line

I'm having a problem whereby a string in textview is displaying each character on seperate lines. My code is as below. I am hoping to show the text on one line.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp" >
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#drawable/button"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/sectionPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginLeft="10dp"
android:text="AM"
android:textSize="18sp"
android:background="#drawable/button"
android:layout_toRightOf="#+id/numberPicker"
android:layout_centerVertical="true" />
</RelativeLayout>
The problem was that like #mbm29414 suggested I had another relative layout that the above was nested in and this layout had a left/right margin that was limiting this layouts size. The parent layout had a horizontal margin set to 40dp. I discovered this by rotating the screen and noticing a change.

Relative layouts children positions

I need to adjust my layout with a button as exit on the right corner of the layout and an image button below the center of the layout. It should be accurately there to support the background for multiple screens.
I used relative layout and inside it the image buttons, but I am unable to set the image button 2 on its position. If i do it, the whole layout is vertically expanded.
<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="#00ffffff"
android:orientation="vertical"
tools:context="com.rabiya.menu.MainActivity"
android:id="#+id/rl01" >
<RelativeLayout
android:id="#+id/rl02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:background="#drawable/background3" >
<ImageButton
android:id="#+id/exit3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:contentDescription="#string/image"
android:layout_alignParentTop="true"
android:background="#drawable/exit"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv01"
android:background="#000"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rl03"
android:background="#00ffffff"
android:layout_below="#+id/tv01"
android:orientation="vertical"
>
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/click"
android:contentDescription="#string/image"
android:paddingTop="20dip" />
<ImageButton
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/click"
android:contentDescription="#string/image"
/>
<ImageButton
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/click"
android:contentDescription="#string/image"
/>
<ImageButton
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/click"
android:contentDescription="#string/image"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I don't understand cleary wath yo want but seems a problems that I have
And i change my layout to the FrameLayout
http://www.tutorialspoint.com/android/android_frame_layout.htm
Mayby a gravity center for one and gravite up aling right for the buton do the works
Create a new custom view extended from LinearLayour or RelativeLayout and place an image inside of it. If I understand correctly, you want to manipulate image inside of the button.
Once you have your own view with image inside of you can override on click to make it behave like button.

Android Relative layout: ToLeftOf gone view

I have a relative layout in which I have a TextView on the Left and a Spinner on the right. Also I need an error image to show if user selected incorrect variant.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="#string/some_text"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="74dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/err"/>
<ImageView
android:id="#+id/err"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_error"/>
</RelativeLayout>
I want my spinner to be on the aligned to the right when error is not shown (visibility = GONE) and move it to be to the left of error image when the error is visible. How do I do that? Now it just ignores this:
android:layout_toLeftOf="#+id/err"
EDIT: thank you, I corrected the typo, but it's not the cause of the problem
Perhaps you want something like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|right"
android:layout_alignParentRight="true"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner"
android:layout_width="74dp"
android:layout_height="40dp" />
<ImageView
android:id="#+id/err"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_error"/>
</LinearLayout>
EDIT: Just put your Spinner and ImageView inside a LinearLayout like this one.
EDIT 2: I just received a +10 and as I am a bit ashamed about this solution, follow this:
So, for future questions related to this one, please ignore my answer and create your own custom view extending ConstraintLayout and include a Spinner and an ImageView on the custom layout, preferably using Kotlin. Thanks!
Adding an anchor view on the right side of the relative layout can solve this issue. Add the anchor view to the RelativeLayout:
<View
android:id="#+id/anchor_top_right"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
Replace the error ImageView relative position from android:layout_alignParentRight="true" to android:layout_toLeftOf="#id/anchor_top_right". Also remove android:layout_alignParentRight="true" from the Spinner, it's aligned to the left of the err.
Here's the full layout with the anchor:
<?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="wrap_content"
>
<View
android:id="#+id/anchor_top_right"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="#string/some_text"
/>
<Spinner
android:id="#+id/spinner"
android:layout_width="74dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/err"
/>
<ImageView
android:id="#+id/err"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/anchor_top_right"
android:src="#drawable/ic_error"
/>
</RelativeLayout>
I think you have a typo in your code.
It has to be android:layout_toLeftOf="#+id/err" instead of android:layout_toLeftOf="#id/err"
Make 2 Spinners, and every time make one of them invisible

android can android:gravity do this?

Searched all day and came up with a poor solution i think.
i want three ImageButton placed on the right sida of the screen.
Not centered but just above center position..
With the code below i get the result i want but,
If the user have a bigger screen they will not have this position right?
I have tried the android:gravityall day and all i can do with it is
to center the three buttons very nicely.
What should i use to make the three buttons always stay at the positions that
they are on the image belove.
i have the button image in 3 different sizes in hdpi,mdpi,xhdpi folder.
<RelativeLayout
android:id="#+id/rightRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:id="#+id/btn_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="A"
android:src="#drawable/drawer_1"
android:background="#android:color/transparent"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="#+id/btn_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="B"
android:src="#drawable/drawer_1"
android:background="#android:color/transparent"
android:layout_alignParentRight="true"
android:layout_below="#+id/btn_A"
/>
<ImageButton
android:id="#+id/btn_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="C"
android:src="#drawable/drawer_1"
android:background="#android:color/transparent"
android:layout_alignParentRight="true"
android:layout_below="#+id/btn_B"
/>
</RelativeLayout>
Picture of the three buttons placed on the right side, and my daughter of course.
One option would be to put all three buttons within a LinearLayout (for simplicity's sake) and then set the layout of this container programmatically.
You can see the size of the screen using getResources().getDisplayMetrics().heightPizels and then set the top margin accordingly.
You could add a LinearLayout inside the RelativeLayout, and then use the following properties on the LinearLayout:
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
EDIT: Ok, I've done some testing and found a way of doing what you want without the use of styling it programatically, here's the xml:
<RelativeLayout
android:id="#+id/rightRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="80dip"
>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true">
<ImageButton
android:id="#+id/btn_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:src="#drawable/icon"
android:background="#android:color/transparent"
/>
<ImageButton
android:id="#+id/btn_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:src="#drawable/icon"
android:background="#android:color/transparent"
/>
<ImageButton
android:id="#+id/btn_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:src="#drawable/icon"
android:background="#android:color/transparent"
/>
</LinearLayout>
</RelativeLayout>

Categories

Resources