In my relative layout, I have a spinner and an imagebutton. I placed my image button in the right corner through the code below:
android:layout_alignParentRight="true"
Now, I want my spinner to stretch its width to the left of my image button. I can acheive this by using fixed width of my spinner, but I want it dynamically so it can adjust to any screen size.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/coa_searchAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/ic_youtube_searched_for_black_18dp"
/>
<Spinner
android:id="#+id/coa_parentid"
android:layout_alignParentLeft="true"
android:layout_alignRight="#id/coa_searchAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Spinner>
Click to see Image
Align your ImageView to the right and then align the Spinner to the left of the ImageView
Use something like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/image_view" />
<ImageView
android:id="#id/image_view"
android:layout_width="#dimen/your_dimen"
android:layout_height="#dimen/your_dimen"
android:layout_alignParentRight="true"
android:src="#color/your_drawable" />
</RelativeLayout>
Related
There are no errors that are crashing the program, I just want the image to be aligned. The Image is shown to the left of the Text if the ImageView is placed before the TextView
My Custom Layout for NavigationDrawer
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical"
android:layout_marginLeft="5dp"
android:textColor="#ffffff"
android:text="New Text"
android:id="#+id/bookName" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:id="#+id/bookImageView" />
</LinearLayout>
How to make the image float right of the text and about some points from the right edge of the listview.
One more thing, my layout is being used in a NavigationDrawer, so the ListView covers less than the full width...
Thanks!!!
Change layout_width of TextView from match_parent to wrap_content:
android:layout_width="wrap_content"
I'm new on Android.
How can I align a textView with the center of an ImageView using a relative layout?
I have try the following code:
<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"
tools:context="${relativePackage}.${activityClass}" >
<ImageView
android:id="#+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="15dp"
android:maxWidth="15dp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/item_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/item_icon"
android:text="Counter" />
</RelativeLayout>
but the text is not aligned with the center of the picture.
Thanks!
Use layout_alignTop and layout_alignBottom to align the top and bottom of the textview to the imageview. Then make sure the text has center gravity. This should align it to the middle. ( I believe it should be the right gravity by default, but forcing it never hurts).
You just need to align your textview between the borders of your imageviews top and bottom. And also be sure to center your textview's gravity, so that it will just centered inside your imageview.
<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"
tools:context="${relativePackage}.${activityClass}" >
<ImageView
android:id="#+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="15dp"
android:maxWidth="15dp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/item_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/item_icon"
android:layout_alignTop="#+id/item_icon"
android:gravity="center"
android:text="Counter" />
For a better approach, IMHO, add your imageview to your textview as a drawableLeft, so that it will represent much more to user.
Relative layout children can use:
android:layout_centerInParent="true"
I have ImageView which needs to be shown in full height available. However at top i have created LinearLayout which contains Text and Image. I want it to display at top but also want to image to fit screen. I have linear layout at bottom and its working fine.
This is my code for LinearLayout which i want to show at top. I have hidden actionbar
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#android:color/black"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ImageView
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="10dp"
android:src="#drawable/back_button" />
<TextView
android:id="#+id/titleofscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Testing"
android:textColor="#22c064"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageShown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</FrameLayout>
If i dont set imageView scale type to android:scaleType="fitXY" then it works but image doesnt fit screen.
I want to display image to fit between above bar which contains Text and below it has some icons. But if i make image scaletype ='fitXY' it doesnt display above text. Following is screenshot but i want image to occupy space
If you want the image to be below the actionbar you should not use the FrameLayout as a parent. You either should use vertical LinearLayout with weight or `RelativeLayout". Try following code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#android:color/black"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ImageView
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="10dp"
android:src="#drawable/back_button" />
<TextView
android:id="#+id/titleofscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Testing"
android:textColor="#22c064"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageShown"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/actionbar"
android:layout_alignParentBottom="true"
android:scaleType="fitCenter"
/>
</RelativeLayout>
Also I doubt that you use fitXY as android:scaleType, because it doesn't maintain the aspect ratio of the image - so your image will be deformed (stretched). In your case you should rather use fitCenter, but it depends on how exactly you want to scale your image - you can also play with centerInside or centerCrop.
When I am loading an image(more specifically, an image of portrait shape) in an ImageView that image is sometimes covering the Button which is above the ImageView. The ImageView has layout_width and layout_height set to wrap_content. My requirement is that the ImageView will wrap content but, won't cover up the button. I don't want to hardcode pixel values.
layout:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="mg.colorfilters.ResultActivity$PlaceholderFragment" >
<Button
android:id="#+id/button1"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/red"
android:text="#string/string3"
android:textStyle="bold"
android:textColor="#color/white"
android:onClick="loadImageWithFilterOrSaveImage" />
<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/abc_ab_bottom_solid_dark_holo"
android:contentDescription="#string/string4" />
</RelativeLayout>
You want your image to be at centre?
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
If yes, then may want to put your Button code below ImageView code, like
<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/abc_ab_bottom_solid_dark_holo"
android:contentDescription="#string/string4" />
<Button
android:id="#+id/button1"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/red"
android:text="#string/string3"
android:textStyle="bold"
android:textColor="#color/white"
android:onClick="loadImageWithFilterOrSaveImage" />
This way the button will always be on top. If image is large, button will be visible on top of it. Use this solution only if you are OK with this behavior.
In Your Layout.xml
please insert android:layout_above="#+id/button1 line in <ImageView> tag.
you can use a scrollview inside the relative layout and use a linear layout in vertical orientation in the scrollview...then add your button and imageview in the linear layout.....I havent tried this but i think this will work :D
my layout is very simple. i use a vertical relative layout which contains an imageview on top of 2 textviews (heading and text). In portrait it looks fine. However in landscape it happens, that the imageview will not fill out the whole width of the screen but the text below still does. that doesnt look good. is there a simply way i can make the text width as wide as the imageview above, so all views will have the same right boarder on any screen size?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scaleType="fitStart"
android:adjustViewBounds="true"
android:src="#drawable/picture" />
<TextView
style="#style/DescriptionTitle"
android:id="#+id/textView1"
android:layout_alignLeft="#id/imageView1"
android:layout_below="#id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/heading" />
<TextView
style="#style/DescriptionText"
android:id="#+id/textView2"
android:layout_alignLeft="#id/imageView1"
android:layout_below="#id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text" />
</RelativeLayout>
</ScrollView>
Add android:layout_alignRight="#id/imageView1" to your TextView. You may need to use android:layout_width="0dp"; also on the same TextView.
insted of using android:src="#drawable/picture"
use android:background="#drawable/picture" hope it will work for you