Android - RelativeLayout - position a checkbox on the center of an image - android

I would like to know how can I center a checkbox to an image on my RelativeLayout view - there is no property like this..
Here's my code:
http://pastebin.com/tGAxju3Z
Please notice: the image is bigger then the checkbox.

Use FrameLayout for the following
<FrameLayout
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/image1"
>
<Button
android:id="#+id/button2"
android:layout_gravity="center"
android:src="#drawable/image2" />
</FrameLayout>

If you are using a relative layout, it will be better if you take a linearlayout with its background as an image and then place the checkbox on the linearlayout...

Make sure the layout_height of CheckBox is the same as the layout_height of your ImageView, same for the layout_width, and add the gravity. Be sure the ImageView and Checkbox are in another layout (with layout_width and layout_height set to wrap_content), than the TextView.
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp"
android:layout_gravity="center"
android:background="#e2e2e2"
>
<ImageView
android:contentDescription="Icon"
android:layout_width="74dp"
android:layout_height="64dp"
android:id="#+id/imgThumbnail"
android:scaleType="centerInside"
/>
<CheckBox
android:id="#+id/chkEdit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="visible"
/>
</RelativeLayout>
<TextView />
Tip: Add android:background="#FF0000" (=RED) and android:background="#00FF00" (=GREEN) to the different widgets, so you can exactly see what is covered by the item.

Related

How to remove the padding of images into horizontal linearlayout?

I´m using the xml code for the horizontal linearlayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="60dip"
android:orientation="horizontal"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:layout_alignParentBottom="true"
android:background="#drawable/back1"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="60dip"
android:clickable="true"
android:contentDescription="#string/app_name"
android:src="#drawable/ca_18"
android:adjustViewBounds="true"/>
</LinearLayout>
and the ImageView have a padding top and bottom. How can I remove the padding?
Avoid hardcoding the layout_height and layout_width attributes. This may look fine on your device. But may look ugly on another.

Fit imageView and textView in one small layout in Android

I have a tiny linearlayout that I have to fit an imageView and a textView in. I know this is a terrible way to make an app. I'm just trying to test to see if this would work. The code below only shows the imageview and not the textView. Any ideas?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="90dp"
android:layout_height="90dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:src="#drawable/my_icon" />
</LinearLayout>
</LinearLayout>
The answer ended up being to give the textView a weight of 0 and the imageView a weight of 1. That way, textView showed up perfectly but only took as much space as it needed, and the imageView took up the rest of the space. Perfect1
It depends what you want it to look like. If you want the LinearLayout to be split in half, then in your ImageView set
android:layout_weight="1"
The views inside the LinearLayout get space in proportion to their layout_weight so if your ImageView has layout_weight="0" it will get 0 proportion of the View.
You should use weight like that:
android:layout_weight="2" in LinearLayout
and layout_weight="1" in imageView and textView
so that both of your view will occupy half of space
Please try this
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="TextView" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/my_icon" />

layout_centerHorizontal is not centering

I have a relative layout, and inside it i place 3 items, 2 imageviews and one scrollview.
My layout is like this...
<?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="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:baselineAligned="true"
android:background="#drawable/background">
<RelativeLayout
android:id="#+id/logosLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_margin="16dp"
android:gravity="center">
<!-- An image will be placed here -->
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#33ffffff"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/up" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="8dp"
android:fadingEdgeLength="32dp"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="#+id/hotelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/button_background"
android:contentDescription="#string/hotelBtnDesc"
android:gravity="center"
android:src="#drawable/icon1" />
<TextView
android:id="#+id/hotelBtnDescTxtVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="#string/hotelBtnDesc"
android:textColor="#ffffff"
android:textSize="14sp" />
<!-- more scrollview items -->
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/down" />
</RelativeLayout>
</LinearLayout>
The above code produces the view shown here:
You may note that the arrows are not aligned in the center, but are slightly moved to the right. Why is that, and how can I fix it ? Note that i have already used android:layout_centerHorizontal="true" to my imageviews.
Thank you in advance
Its problem with your
android:layout_margin="8dp"
remove it from both scrollview and imageView
and pass it to RelativeLayout direct.
or add
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip"
in your RelativeLayout.
and pass
android:layout_centerHorizontal="true"
to your scrollview
they are aligned to the center if you put the marginRight into consideration, try adding android:layout_margin="8dp" to the arrows.
u need to set image view that is under the Relative layout set this image view width fill parent.
Try removing android:layout_weight="1" from your RelativeLayout and see if it works.
In your LinearLayout, the attribute android:orientation="horizontal" causing your display to be aligned horizontaly. Therefore your RelativeLayout is in center but since its shared with another layout thats why you cant note the difference. If you completely remove the first RelativeLayout (the one with id logosLayout) then i hope you'll see the second layout in center.
So, first you need to define the hierarchy of layout you require and then adjust your views accordingly.
It seems problem with your android:layout_margin="8dp" for your scrollview only and pass it to RelativeLayout direct but instead of using margin use padding = "8dp".

problem with android layout

I have the following layout for a custom title bar. However, the problem is this: both the imageview and the imagebutton are coming at the centre. I was expecting the imagebutton to be at the extreme right. Can anyone kindly let me know what I did wrong here ? Thanks.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#323331">
<ImageView
android:src="#drawable/header"
android:id="#+id/header"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal">
</ImageView>
<ImageButton
android:id="#+id/saveButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/savetap"
android:background="#null"
android:gravity="right"
android:layout_above="#+id/header">
</ImageButton>
</LinearLayout>
Your layout is only 35dip tall, so pressumibly if you show your ImageView the ImageButton gets positioned outside the screen. Consider changing your layout_height to wrap_content, if appropiate.
You're using a vertically oriented LinearLayout, all View will be presented in a vertical fashion. Use a RelativeLayout so that you'll have more control over where Views are positioned. If you'd still like to use a LinearLayout, you'll have to use horizontal orientation so that the Views can be on the same "line".
Use layout_gravity to layout the item to the right, just gravity is used by the contents of the view.
At first if width is set to fill parent it really fills parent so it use all the width.
Second: why not RelativeLayout? It provides more options and control.
Maybe this is what you looking for:
`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#323331">
<ImageView
android:src="#drawable/header"
android:id="#+id/header"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left">
</ImageView>
<ImageButton
android:id="#+id/saveButton"
android:layout_height="wrap_content"
android:src="#drawable/savetap"
android:background="#null"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content" android:layout_alignParentRight="true">
</ImageButton>
</RelativeLayout>
`

ListView footer not center

Any idea why the footer of my list view is always on the left? Here's my xml layout. Footer just added and removed as more data needs loaded. I want theTextView and ProgressBar to be center
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
android:textColor="#color/author_text"
android:text="LOADING DATA..."
/>
<ProgressBar
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
Okay, so the problem was NOT with the footer view shown in the OP; it indeed works just fine. The problem was my ListView had the layout_width attribute set to wrap_content. This works just fine for all the list renderers, but not for the footer. Changing the ListView to fill_parent fixed it
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/MyList"
/>
thanks gang
[SOLVED]
A horizontal LinearLayout will lay its views out left-to-right. It ignores any gravity or layout_gravity attributes when determining horizontal positioning (although these will affect vertical positioning). You'll need to use a RelativeLayout or else nest a vertical LinearLayout to get horizontal centering of views.
Alternatively, you could give the TextView a layout_weight of 1, which will cause it to take up any extra space. Since it will center its text, that should give you the effect you want.
EDIT: Now that I understand what you want, this ought to do it:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
android:textColor="#color/author_text"
android:text="LOADING DATA..."
/>
<ProgressBar
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</LinearLayout>
You can maybe avoid the nested LinearLayouts by using a RelativeLayout in some way, but this came to mind first.
try setting its layout_width to match_parent

Categories

Resources