create a linear layout of overlapping imageviews - android

I need to create a stack of overlapping ImageViews in Android like Spider Solitaire in Windows. Also, I need to be able to dynamically add more ImageViews. Please suggest methods to do so. Can I use a LinearLayout (Its not necessary. Whatever works is fine)? If so, how do I accomplish the overlap?
Note: All the ImageViews will have the same amount of overlap, so no need to worry about the unopened cards in the give screenshot.
Thank You

User LayerList
A LayerDrawable is a drawable object that manages an array of other drawables.
Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="#drawable/android_red"
android:gravity="center" />
</item>
<item android:top="10dp" >
<bitmap android:src="#drawable/android_green"
android:gravity="center" />
</item>
<item android:top="20dp" >
<bitmap android:src="#drawable/android_blue"
android:gravity="center" />
</item>
</layer-list>
Note : the below images uses item attributes as android:left="10dp", android:left="20dp".

You can use FrameLayout. Following layout is an example with 3 ImageViews:
<FrameLayout 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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="300dp"
android:background="#f00" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#0f0" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:background="#00f" />
</FrameLayout>

I don't think you can achieve this using Linearlayout only.
But you can use LayerList
A LayerDrawable is a drawable object that manages an array of other
drawables. Each drawable in the list is drawn in the order of the
list—the last drawable in the list is drawn on top.
with help of LayerList you can achieve the following:
Syntax
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#[package:]drawable/drawable_resource"
android:id="#[+][package:]id/resource_name"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension" />
</layer-list>

Related

How to merge an edittext with spinner in Android?

How can I make an EditText with a drop-down in Android?
For reference i have added an image. I am trying to combine an edittext with spinner but I am not able to achieve the same.
Can anyone add suggestion or xml mockup?
Well, You can use a LinearLayout with a horizontal orientation and create a custom drawable for the rounded border.
Implementation
First, you need to draw the rounded border by creating a new Drawable resource file.
In your res/drawables folder create rounded_border.xml
rounded_border.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="4dp" />
<stroke android:width="2dp" android:color="#2E2E2E" />
</shape>
</item>
</selector>
Then, you can add the LinearLayout to your layout as follows.
your_layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#drawable/rounded_border"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#null"
android:paddingHorizontal="16dp"
android:text="(504) 596-3245"
android:layout_weight="2"/>
<View
android:layout_width="2dp"
android:background="#2E2E2E"
android:layout_height="match_parent"/>
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
tools:listitem="#layout/spinner_item_text"
android:layout_weight="1"/>
</LinearLayout>
Remarks
You can Customize the border width, color, and radius from the rounded_border.xml.
Final Results
could refer this lib demo : https://github.com/z2wenfa/SpinnerEditText
could use the lib directly
dependencies {
compile 'com.github.z2wenfa:SpinnerEditText:1.0.1'
}
or could refer: https://github.com/z2wenfa/SpinnerEditText/blob/master/spinneredittext-lib/src/main/java/com/z2wenfa/spinneredittext/SpinnerEditText.java

Android splash screen vector aspect ratio

How can I keep aspect ratio of a vector drawable (#drawable/ic_splash_screen) and fit horizontally ?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#android:color/white"
android:gravity="fill" />
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/logo" />
</item>
<item
android:drawable="#drawable/ic_splash_screen"
android:gravity="bottom|fill_horizontal" />
</layer-list>
In my code , the SVG drawable vector fill_horizontal at the bottom but keep it's original height
I want to use it in splash screen and want keep aspect ratio
As #DeepThought I also think that your best bet is to go with gravity=center as it is the only way I know to preserve aspect ratio.
however, if you wrap your drawable in a bitmap (as he suggests) it will not work on APIs different from 21,22 and 23. All the while on these same APIs images will always look stretched if you do not wrap them into a bitmap. which is why I would recommend you create 2 versions of your drawable layer-list and put one into drawable-v21 and the other in drawable-v24.
for v24 you can try:
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#android:color/white"
android:gravity="fill" />
<item
android:gravity="center"
android:src="#mipmap/logo" >
</item>
<item
android:gravity="center"
android:src="#drawable/splash" >
</item>
</layer-list>
for drawable-v21 you use
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#android:color/white"
android:gravity="fill" />
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/logo" />
</item>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/splash" />
</item>
</layer-list>
as for making the drawable fill the view horizontally without destroying aspect ratio I really dont know how to achieve this in xml. the only thing I can think of is oversizing your your splash_screen.xml file by setting the android:height and android:width parameters and facing the fact that on some slimmer devices a larger portion of the edges to the left and right of your SplashScreen will be cutt off.
....but if you are really obsessed about getting splash_screen.xml to fit all the width of the screen precisely there is always the total overkill solution. for that I recommend you read the following resource. it would imply you creating a bunch of alternative drawable resources depending on screen dp dimensions i.e. drawable-sw360dp, drawable-sw720dp and so on and then put in them different versions of your splash_screen.xml that have android:height and android:width taylored to each screen.
Can you try with this change :
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#android:color/white"
android:gravity="fill" />
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/logo" />
</item>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/splash" />
</item>
</layer-list>
Try this
First imageView is your background, this will stretch to all side. Having adjustViewBounds will maintain it's height and won't cause stretch image.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:src="#drawable/ic_splash_screen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#mipmap/logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Creating a circle in xml file is not actually a circle

I have a gradient view which animates from left to right. I have a XML that describes the circle inside, but the borders of XML are actually rectangular as you can see, how can I make the overflow hidden of the outside of the XML.
It looks like that only the background is a circle but not the the shape itself ,I thought that the solution is by using PorterDuff.Mode but it doesn't help.
this is my circle.xml file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="2dp"
android:color="#android:color/white" />
<corners android:radius="7.5dp" />
<size android:width="327dp" android:height="211.5dp" />
</shape>
And in my layout i use it like this :
<RelativeLayout
android:id="#+id/white_rectangle"
android:layout_width="327dp"
android:layout_height="211.5dp"
android:layout_gravity="center"
android:clipChildren="true"
android:adjustViewBounds="true"
android:background="#drawable/circle"
>
<View
android:id="#+id/scanner"
android:layout_width="123.5dp"
android:layout_height="211.5dp"
android:layout_gravity="center"
android:background="#drawable/scanner"
android:visibility="gone" />
</RelativeLayout>
i don't want the scanner to go outside the borders
You need to set square dimensions for it to appear as a circle.
Your width is larger than your height. Try this
<RelativeLayout
android:id="#+id/white_rectangle"
android:layout_width="211.5dp"
android:layout_height="211.5dp"
android:layout_gravity="center"
android:clipChildren="true"
android:adjustViewBounds="true"
android:background="#drawable/circle"
>
<View
android:id="#+id/scanner"
android:layout_width="123.5dp"
android:layout_height="123.5dp"
android:layout_gravity="center"
android:background="#drawable/scanner"
android:visibility="gone" />
</RelativeLayout>
The solution for me was to use CardView and give it cardCornerRadius.
found it here How to make a view in android with rounded corners
try this library. Use same height and width for view
https://github.com/hdodenhof/CircleImageView

Showing contact name with initial letter in circle

I need to display the contact name initial letter inside circle like in lollipop contacts. I didn't find any solution.
The circle should different colour for each user.
You can follow this. If you wanna save time you can use this library.
Edit
The link may be invalid any point of time. So I want to add the details below this.
repositories{
maven {
url 'http://dl.bintray.com/amulyakhare/maven'
}
}
dependencies {
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
}
You can add which ever methodology you are following.
Basically you have to add one ImageView with same height and width and add this TextDrawable as a background of your view.
TextDrawable drawable = TextDrawable.builder()
.buildRect("A", Color.RED);
ImageView image = (ImageView) findViewById(R.id.image_view);
image.setImageDrawable(drawable);
The library has support for circle,square and rectangle drawables.
I didn't see any library for this but my solution is draw a circle in run time in a predefined layout in your xml use this :
How to draw circle by canvas in Android?
Then in your list adapter choose first letter of each name string using substring(0,1)
,and using setText() you can set list item textView to first letter.
If you need source code let me know to put it for you.
UPDATE:
I recently used very easy approach for this in one of my apps,
you should make a layout like this in your layout folder:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlWeekDay"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="right"
android:layout_margin="3dp"
android:background="#drawable/circle_shape">
<TextView
android:id="#+id/tvWeekDayFirstLetter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text=" E "
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:textSize="20sp" />
</RelativeLayout>
And your shape in drawable folder as circle_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval"
android:dither="true">
<corners
android:bottomRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp"
android:topRightRadius="100dp"/>
<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="3dp">
<shape android:shape="oval"
android:dither="true">
<solid android:color="#color/white"/> <!-- this one is ths color of the Rounded Button -->
<corners
android:bottomRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp"
android:topRightRadius="100dp"/>
</shape>
</item>
</layer-list>
Then in your listView or recyclerview use this as the item to inflate.like this
Simple and small code to do this -
<com.google.android.material.button.MaterialButton
android:id="#+id/"
android:layout_width="40dp"
android:layout_height="50dp"
android:text="A"
android:clickable="false"
app:backgroundTint="#color/Blue200"
android:padding="0dp"
app:cornerRadius="50dp"
/>
By using a Material Card View and a Textview, it would be easy to create such a view.
<com.google.android.material.card.MaterialCardView
android:layout_width="50dp"
android:layout_height="50dp"
app:cardBackgroundColor="#color/red"
app:cardCornerRadius="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/initialTv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="AB" />
</com.google.android.material.card.MaterialCardView>

How to make a ripple effect over a linear layout, without overriding the background color on its children?

I have a LinearLayout that looks like this.
I want each row to be clickable. The LinearLayout code for a row looks like this:
<LinearLayout
style="#style/home_menu_item_container"
android:id="#+id/home_menu_acronyms_button"
>
<ImageView
style="#style/home_menu_item_left"
android:background="#color/greyLight"
/>
<TextView
style="#style/home_menu_item_right"
android:text="#string/home_menu_option_2"
android:background="#color/grey"
/>
</LinearLayout>
How can I add a ripple effect that expands over the entire row (parent) - not just one child view in the row? The tricky part here is to let the ripple go over the two colored row.
So far, I found out the easiest way to do so is define a <ripple> in your drawable and then set the background of the LinearLayout to this drawable resource.
Define your drawable-v21/item_selector.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/your_background_color">
<item android:id="#android:id/mask"
<!--color here doesn't matter-->
android:drawable="#android:color/white" />
</ripple>
Set the background of your LinearLayout to drawable/item_selector.
<LinearLayout
style="#style/home_menu_item_container"
android:background="#drawable/item_selector"
android:id="#+id/home_menu_acronyms_button" >
...
</LinearLayout>
Besides, if you don't have your own background color, then there is no need to define a item_selector at all. You can simply define background as android:background="?android:attr/selectableItemBackground" for your LinearLayout.
It is a little bit complicated for that what you need but I don't think there is another way,...
You need to put your ImageView's into a ListView so that every ImageView is a ListItem and then you can set the ripple but you also need to set drawSelectorOnTop="true" otherwise it won't work correctly
I too faced this problem, finally found a simple solution
In linear Layout just add android:clickable="true";
and set background with ur ripple effect as
android:background="#drawable/ripple_effect"
code:
<LinearLayout
android:clickable="true"
android:background="#drawable/ripple_effect"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Add your child views here -->
</LinearLayout>
add ripple_effect.xml in drawable
ripple_effect.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#f87d05c2"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f87d05c2" />
</shape>
</item>
</ripple>
<RelativeLayout
android:id="#+id/rll_privacy_policy"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="#+id/txt_privacy_policy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/layout_margin_16"
android:text="#string/privacy_policy"
android:layout_centerVertical="true"
android:textColor="#color/link_acc"
android:textSize="#dimen/txt_title_20" />
</RelativeLayout>
Another option is to make the ripple's background color transparent. This way only the ripple can be seen. The ripple's xml file (in your drawable-v21/ folder) is thus:
<-- Ripple in some ghastly color, like bright red so you can see it -->
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/red"
>
<!-- background color uses a transparent mask set to full #ffffff (white) -->
<item
android:id="#android:id/mask"
android:drawable="#android:color/white"
/>
Note that if you are supporting pre-lollipop devices, you need to have a dummy file with the same name in your drawable/ folder. An empty selector is sufficient for that file. And remember that those older devices will not ripple.
answer above dont work for me,
here is my solution:
ripple.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
>
...put het your design
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ripple" />
</FrameLayout>

Categories

Resources