How to go beyond the edge parent element - android

I have a circular image and a button icon
Lets say image is size of 20x
And button is size of 8x
I want to put the button icon in a way that 80% of its portion stays on the image view and 20% goes beyond it.
My xml code-
RelativeLyaout<
andoird.cardview.widget.Cardview<
<ImageView/>
/>
<button_icon>
layout_alignRight:ImageView
</button_icon>
ImageViw and button_icon overlaps but not that way I want.
80% portion of the button stays behind the image view that i cant see and the rest 20% of the button that goes beyond the image view is visible
This is for my Android studio app. I things above code is less time consuming.
If you need full code i can provide that too.

you can't exceed parents bounds. but you can introduce one extra layer which will hold both your Views and second one may cover part of first one. some basic example with FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#097267">
<View
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="40dp"
android:background="#A9C26A">
</FrameLayout>
also be aware that elevation and translationZ XML attributes may change order of drawing (first View will be drawn on top of second and will cover it). e.g. Button have some elevation set by default, also CardView (setCardElevation(float)) - additionally (mainly?) this makes some small shadow under these Views

Try this code... I have added constraint layout and inside it i have added button overlapping the image.
You can change the size according to your requirements !!
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/circleImageView4"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="60dp"
android:src="#drawable/blue1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.057"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_marginTop="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.166"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/circleImageView4" />
</androidx.constraintlayout.widget.ConstraintLayout>

Related

How to add one image over other in Android

I want to make a view in which one image view, say iv_up should be placed above the another image view, say iv_down.
The iv_up should be slighty transparent. One more thing, the iv_down happens to be an QR Code.
Is there any way to implement this?
Below is the code that I have written so far:
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center">
<ImageView
android:id="#+id/iv_down"
android:layout_width="419dp"
android:layout_height="419dp"
android:layout_centerInParent="true"
android:contentDescription="QR CODE"
tools:srcCompat="#tools:sample/avatars"/>
<ImageView
android:id="#+id/iv_up"
android:layout_width="419dp"
android:layout_height="419dp"
android:layout_centerInParent="true"
android:contentDescription="QR CODE"
tools:srcCompat="#tools:sample/avatars"/>
</RelativeLayout>
So take a look at this sample XML I have created:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_down"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:contentDescription="QR CODE"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/iv_up"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_marginStart="50dp"
android:layout_marginTop="50dp"
android:contentDescription="QR CODE"
app:layout_constraintStart_toStartOf="#id/iv_down"
app:layout_constraintTop_toTopOf="#id/iv_down"
tools:srcCompat="#tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
This will create a UI where the iv_up appears on top of the iv_down. Using the ConstraintLayout you can position views relative to one another with ease - it is the "better version" of the RelativeLayout. So here we set the iv_down to be in the top left corner of the view, and iv_up to be 50dp from the top and start of the iv_down view. Since the views are ordered as such in the XML, the iv_up will always sit on top of the iv_down due to its Z positioning. You can change this by setting specific values for the android:translationZ attribute.
Simply setting the android:alpha="0.5" attribute on the iv_up view, will cause the view to be 50% transparent. You can play with this float value from 0-1 to get the desired look for the transparency. Changing the margins on iv_up will also change how far the view moves from the top left corner i.e. how much of the views overlap!

How do I make my android app scale properly accross different devices?

I'm quite new to android development and I'm currently making an app for a nine men's morris game and I'm using a custom layout xml file with 1 image view for the game board and then 23 other image views for all the different points on the board, all inside a constraint layout and then I use the tag to include this layout inside my main activity layout.
All the image views look perfectly aligned on a pixel 4 using the AVD but they dont look aligned when i test on older devices, so what do you guys think is the best way to make the image views align perfectly on all screen sizes? this pic shows how the layout looks and below i will post code for the first 4 image views
<androidx.constraintlayout.widget.ConstraintLayout
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="match_parent"
android:layout_height="match_parent"
android:id="#+id/g_items">
<ImageView
android:id="#+id/the_board"
android:layout_width="376dp"
android:layout_height="374dp"
android:paddingEnd="15dp"
android:paddingStart="15dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/board" />
<ImageView
android:id="#+id/point_0"
android:onClick="onTileClicked"
android:layout_width="65dp"
android:layout_height="56dp"
android:layout_marginStart="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/the_board"
app:srcCompat="#drawable/empty_spot"/>
<ImageView
android:id="#+id/point_1"
android:onClick="onTileClicked"
android:layout_width="65dp"
android:layout_height="56dp"
android:layout_marginStart="104dp"
app:layout_constraintStart_toEndOf="#id/point_0"
app:layout_constraintTop_toTopOf="#+id/the_board"
app:srcCompat="#drawable/empty_spot" />
<ImageView
android:id="#+id/point_2"
android:onClick="onTileClicked"
android:layout_width="65dp"
android:layout_height="56dp"
android:layout_marginStart="104dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/point_1"
app:layout_constraintTop_toTopOf="#+id/the_board"
app:srcCompat="#drawable/empty_spot" />``
You need to use guideline widget with ConstraintLayout to tell your views when to stop when resizing(using app:layout_constraintGuide_percent property).
Google documentation
Set width and height of the ImageViews to 0, this way the layout will use constraints instead of the absolute values.
Also add all constraints to each view (Start, Top, End, Bottom).

How do i keep image on transparent background?

I want to acheive the following. Transparent background with white tick
How do i get the white tick mark on the transparent background.
i tried the following.
viewprev = mgridview.getChildAt(i - mgridview.getFirstVisiblePosition());
viewprev.setBackgroundColor(Color.WHITE);
viewprev.setAlpha(0.5f);
Try this out,
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ivMain"
android:layout_width="200dp"
android:layout_height="200dp"
android:contentDescription="#string/app_name"
android:src="#mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#99676767"
app:layout_constraintBottom_toBottomOf="#id/ivMain"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/ivMain">
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_gravity="center"
android:contentDescription="#string/app_name"
android:src="#drawable/vector_favorite" />
</FrameLayout>
Your tick should be in the FrameLayout which should be constrainted as in the code so its always in the middle of the ImageView. From the image I guess you're trying to create a long click to select items kind of thing. If so just set the visibility of FrameLayout to gone in xml and in LongClick event set it to visible
This is the output of above code:
You can play around with the color to achieve your desired transparency level
You may need to use FrameLayout.The Framelayout contains your image and an empty ImageView above it. When you need to draw a tick - just set its image into that ImageView.
First you need to add an ImageView to your grid item layout. Remember to insert the xml tag below the main grid image in order to appear on top. Then use match_parent as the image view width and height and assign the scaleType to center. This is your check mark view. You can add a semi transparent background to make a shade above the grid item image and the src attribute goes to the check mark.
You can also modify the image view visibility toggle hide and show of the check mark item.

Constraint layout: Cannot separate elements and keep them all on screen

I have a simple constraint layout like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/main_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/rescan"
/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/rescan"
android:layout_width="wrap_content"
android:text="#string/rescan_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
Despite the layout_constraintBottom_toTopOf="#id/rescan", the listview potentially extends halfway through the button.
To try and correct that, I added hardcoded dimensions (which I prefer not to do); to the listview:
android:layout_marginBottom="50sp"
And to the button:
android:layout_height="40sp"
android:textSize="20sp"
android:layout_margin="10sp"
However, I then got this (emulator pic, the design view also corresponds to this):
The button is halfway off the screen.
So I decided to remove the hardcoded dimensions and use a barrier. The developer page is ambiguous about how barrierDirection is supposed to work, but this
"constraintlayout.com" example makes it clear the direction should be the side you want the barrier on in relation to the elements listed in referenced_ids. Based on that, here's what I have inside the constraint layout:
<ListView
android:id="#+id/main_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/bottomBarrier"
/>
<android.support.constraint.Barrier
android:id="#+id/bottomBarrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="top"
app:constraint_referenced_ids="#id/rescan"
/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/rescan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rescan_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5"
/>
But things are not really getting better...
It's impossible to tell, but here the barrier is at the very bottom. Which explains why the listview, with Bottom_toTopOf the barrier, also now extends all the way to the bottom.
However, that makes the whole barrier, who's direction is top and who's constrained id is the button, totally pointless. In no sense is it keeping the listview on one side and the button on the other. It's below both of them.
Even more berserk: If I change the direction to bottom and leave everything else as it, the barrier jumps to the very top, the listview jumps halfway off the screen upward, and the button stays in place.
I'm totally flummoxed. My two biggest questions are:
Why, in the first picture, does the listview extend halfway through the button, when it is set bottom-to-top of the button?
Why, in the last picture, is the barrier, with a direction of "top" and referencing the button id, below the button?
1.
Your ListView's height is set to wrap_content which means the view will compute its own size and constraints will not limit the dimension. ConstraintLayout-1.1.0 introduced new attributes that allow using wrap_content yet keep enforcing the constraints to limit the specified dimension. These attribues are:
app:layout_constrainedWidth="true"
app:layout_constrainedHeight="true"
2.
As for the Barrier, there's an error in the way you're referencing the view's id:
app:constraint_referenced_ids="#id/rescan"
This should be changed to:
app:constraint_referenced_ids="rescan"

How to resize an ImageView within a given Layout in Android?

I have a problem to make a proper layout for a special case. I experimented on that already for a while both in the designer and in code, but I couldn't find a solution, that's why I need your help.
I have to create a layout which should have a structure like pictured in the images below. It is mainly a combination of several linearLayouts. The problem I have is, that the picture can only be added within the code, because this layout is a detail view that displays information about items from a list.
On the top is the layout without an image place holder (no loaded picture - indicated in black), here the width of "linearLayout_BigLeft" is given by the width of the two buttons and the textView (which all have content) in the "linearLayout_BelowImage".
In the middle you see the layout after the picture has been loaded (image indictated in orange) in code. Depending on the aspect ratio of the android device the black colored gaps differ. I can't get the image to resize to the whole available height and adjusting its width accordingly. The "linearLayout_BelowImage" adjusts itself to the image size (the textView in it is getting wider).
On the bottom is the layout which shows the ideal state. The image always should use the whole available space in height and resize accordingly in width. The "linearLayout_BelowImage" adjusts itself to the image size (the textView in it is getting wider).
Question:
How can I get a layout (after the image is loaded in code) that looks like the bottom picture? The image, after loaded in code, has to resize itself, so it uses the whole available height and resizes its width accordingly. The "relativeLayout_Top" and the "linearLayout_BelowImage" have both fixed heights. The "scrollView_BigRight" adjusts itself based on the space that the "imageView_OrangeImage" doesn't need for itself.
I can deal with solutions that adjust the layout in code, after the image has been added, or solutions that makes the layout.xml itself flexilbe enough to deal with this situation.
Any help is highly appreciated. If you need any more information please let me know.
Below is the main content of my layout.xml, that is needed for this problem.
<?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:orientation="vertical"
android:background="#color/white">
<RelativeLayout
android:id="#+id/relativeLayout_Top"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/blue" >
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout_Big"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#color/transparent" >
<LinearLayout
android:id="#+id/LinearLayout_BigLeft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/transparent" >
<ImageView
android:id="#+id/imageView_OrangeImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/black" />
<LinearLayout
android:id="#+id/linearLayout_BelowImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/blue_white_blue" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/blue" />
<TextView
android:id="#+id/textView_BelowImageMiddle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/white" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/blue" />
</LinearLayout>
</LinearLayout>
<ScrollView
android:id="#+id/scrollView_BigRight"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/grey" >
</ScrollView>
</LinearLayout>
</LinearLayout>
android:adjustViewBounds="true"
This one’s a manual fix for “optimized” code in scaleType="fitCenter". Basically when Android adds an image resource to the ImageView it tends to get the width & height from the resource instead of the layout. This can cause layouts to reposition around the full size of the image instead of the actual viewable size.
AdjustViewBounds forces Android to resize the ImageView to match the resized image prior to laying everything else out. There are times where this calculation won’t work, such as when the ImageView is set to layout_width="0dip". If it’s not working, wrap the ImageView in a RelativeLayout or FrameLayout which handles the 0dip flexible size instead
get it from this site
OR
Mode android:scaleType="centerCrop" uniformly stretches the image to fill the entire container and trims unnecessary.
You can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself!
get it here

Categories

Resources