I am just starting with Android and cannot find a way to arrange the images the way I want.
This is what I am looking for:
This is the code I am currently using, to arrange the images, which unfortunately does not work.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.5"
android:orientation="horizontal"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageView1"
android:layout_width="240dp"
android:layout_height="240dp"
android:src="#drawable/delicio"/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="240dp"
android:layout_height="240dp"
android:src="#drawable/logoerrado"/>
</RelativeLayout>
How can I arrange the images how I want?
To get image1 in the upper right hand corner, use
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
To get image 2 in the center horizontally, use android:layout_centerHorizontal="true". Then add enough padding via android:paddingTop to get it down as far as you want.
If you want another layout beneath image2, like I see in your drawing, just put android:layout_below="#+id/img2" to it.
Related
Objective: I am trying to make a MaterialCard with an Image inside it, as well as some text below the Image. This current layout will serve as a "menu" for the application, where the user can select where to navigate to.
Problem: The ImageView is appearing vertically centred in the MaterialCardView. I would like the ImageView to "stick" to the top of the MaterialCardView. There are large sections of MaterialCardView above and below the ImageView.
My XML Code:
<ScrollView
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="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:liftOnScroll="true"
android:id="#+id/appBar">
<Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/design_default_color_primary_dark"
app:layout_scrollFlags="enterAlways">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/application_title"
android:textColor="#color/colorWhite"
android:textSize="24sp"/>
</Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="#+id/mealPlannerCardView"
android:contextClickable="true"
android:focusable="true"
app:cardBackgroundColor="#color/colorWhite">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:src="#drawable/wedding_dinner"
android:contentDescription="#string/wedding_background_cont_desc"/>
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="#string/mealPlannerTextView"
android:textColor="#color/design_default_color_primary_dark"/>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</ScrollView>
output
I tried to use layout_gravity="Top" but it made no difference.
EDIT
android:scaleType="fitStart" made the Image "stick" to the TOP of the MaterialCardView, but now very large space beneath the ImageView that requires scrolling to get to end.
Add the line below in your xml
android:layout_gravity="start|top"
See if this repositions the image for you. Worry about centring later if this works.
You might get the result you want by setting a scaleType for the image view.
Checkout this link: https://thoughtbot.com/blog/android-imageview-scaletype-a-visual-guide
It shows examples of each scaleType, it will help you to choose the right one to achieve the appearance you need.
This is happening because the Android framework is trying to maintain the aspect ratio in the face of your height and width constraints and default scale type.
There are more than one solutions to your problem and its for you to choose based on whose results are acceptable to you. All these solutions revolve around helping the framework with ImageView's aspect ratio. Let me try to put down the possible solutions that I can think of.
First solution can be as simple as changing the scale type on your ImageView as below:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="#drawable/wedding_dinner"/>
That will take away the white spaces but crop your image.
A slight variation of the above, where you can define a fixed height of the image. This is a surprisingly widely used solution as it provides a certain
<ImageView
android:layout_width="match_parent"
android:layout_height="240dp"
android:scaleType="centerCrop"
android:src="#drawable/wedding_dinner"/>
Finally, a more elegant and better solution is to provide a fixed aspect ratio to your images using a ConstrainLayout.
I´ve tried a few thinks to achive the following thing:
For the discription: I want to have a header with a textview which wraps over the howl page and below this header I want to display three images beside each other. It should be like a image gallery. This layout i want to duplicate.
But my problem is, that I the height of the ImageView is to high and that there is a distance between the sized image and the title. See figure.
On the Left is what I want and on the right is what I got...
Here is my layout code:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textStyle="bold"
android:text="titel"
android:id="#+id/titel"
android:gravity="center"
android:padding="3dp"
android:textSize="18sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/titel">
<ImageView
android:layout_width="0dp"
android:layout_weight="0.208"
android:layout_height="match_parent"
android:id="#+id/image1"
android:src="#drawable/1"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="0.389"
android:layout_height="match_parent"
android:id="#+id/image2"
android:src="#drawable/2"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="0.403"
android:layout_height="match_parent"
android:id="#+id/image3"
android:src="#drawable/3"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
my best guess is because your ImageView scales your image in a way that it fits into the center of the view and leaves empty space around the top and bottom of the view. try adding android:adjustViewBounds="true" to your ImageView in the xml.
on a side note, when debugging view hierarchy, it's almost always gonna be helpful to go to Android Device Monitor, select your devices to the right, and click Dump View Hierarchy for UI Automator. You can easily find out which view is taking the extra space by hovering your mouse over different view elements.
I am trying to place two image views side by side with the one on the left having a height matching the one on the right. The problem is, the sizes of the image view files are much larger than how they appear on the screen. When I assign wrap content to the one on the right, it takes up all of the space, though I want it to be scaled so that there is enough room for the one on the left. I don't know if this makes sense, but here is my code.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/boombox"
android:id="#+id/boombox"
/>
<ImageView
android:id="#+id/play_stop_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="#id/boombox"
android:scaleType="fitXY"
android:src="#drawable/play_stop_button
/>
</RelativeLayout>
First try to put
android:layout_width="wrap_content"
android:layout_height="150dp"
in both imageviews to have a fixed image height
My problem born when i create layout in xml file from graphics tool. RelativeLayout is awful! i don't know which layout to use.. When i put an imageview and a imagebutton, in emulator will displayed in a bad way, one apon the other. what can i do? is there a good tutorial on which i can learn how to resize image and layout in percent of different displays size?
i Try this :
<RelativeLayout android:layout_width="match_parent" android:id="#+id/relativeLayout1" android:layout_height="match_parent">
<LinearLayout android:layout_width="wrap_content" android:id="#+id/linearLayout1" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true">
<ImageView android:id="#+id/imageView1" android:src="#drawable/testotrova" android:layout_width="243dp" android:layout_height="48dp"></ImageView>
</LinearLayout>
<ImageButton android:layout_width="wrap_content" android:src="#drawable/info" android:layout_height="wrap_content" android:id="#+id/imageButton1" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></ImageButton>
</RelativeLayout>
but when i display it isn't good
Regarding support for multiple screens, have a look here.
You will need to add different layouts for screen size small, normal and large.
They are displayed one upon the other because you are using RelativeLayour and they are both (Image and text) anchored to the upper left:
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
I strongly suggest you to use LinearLayout if you have only a few items. (http://www.learn-android.com/2010/01/05/android-layout-tutorial/4/)
For your image size, if you want a percent, please use:
android:layout_height="0dip"
android:layout_weight="1"
http://developer.android.com/resources/articles/layout-tricks-efficiency.html
I'm currently working on a game for android.
I have three ImageView's next to each other, they're positioned next to each other using the "android:layout_toRightOf". The views are located within a RelativeLayout tag. Each of these views are centered into the middle of the screen using "android:scaleType="fitCenter"" and they fill the entire screen.
I'm attempting to implement a horizontal scroll from one view to another so that I basically get one view on my screen at a time.
Here is the code that I'm currently using
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="none">
<RelativeLayout android:id="#+id/GameBg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/GameImageContainer3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:layout_toRightOf="#+id/GameImageContainer2"
/>
<ImageView android:id="#+id/GameImageContainer2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:layout_toRightOf="#+id/GameImageContainer1"
/>
<ImageView android:id="#+id/GameImageContainer1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
/>
</RelativeLayout>
</HorizontalScrollView>
What I'm currently getting is three images next to each other -- but sometimes they take up less then an entire screen. I want both the left and right padding to be correct, so that I can only see one image at a time.
Any ideas?
Have you tried the Gallery Widget. It seems to be what you are looking for. Be sure to also check out the Gallery tutorial.
Try using a ViewFlipper
http://www.androidpeople.com/android-viewflipper-example/