Custom Shape to PopupWindow - android

How to implement PopupWindow shape like on the screenshot below ?
I understand how to create Rectangle shape with stroke/solid color.
I know how to play with corners, but how can I implement an arrow < near Close label ?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="#dimen/edittext_border"
android:left="#dimen/edittext_border"
android:right="#dimen/edittext_border"
android:top="#dimen/edittext_border">
<shape android:shape="rectangle">
<stroke
android:width="#dimen/edittext_border_width"
android:color="#color/divider_color_light" />
<solid android:color="#color/white" />
<padding android:bottom="#dimen/awsome_edittext_padding" android:left="#dimen/awsome_edittext_padding" android:right="#dimen/awsome_edittext_padding"/>
</shape>
</item>
</layer-list>

you can set exactly that type of background in custom popup
see this library
https://github.com/kvirair/Quick-Action

U can use .9 patch png as background for PopupWindow.
like below code `
<FrameLayout
android:id="#+id/popupbg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/POPUPBACkGROUND" >
</FrameLayout>
You can refer this site!

Related

How to create small round buttons that contains an icon at the centre?

I want to create buttons in my android application, just like shown in the image below
you can se all the four buttons below.
I want to give them a custom color background and then an icon on the top. There is no way I'm able to adjust the icon at the centre of button.
How can I achieve the desired effect?
You can do this with FrameLayout and for circle background you need to create a shape drawable file. Refer my answer here.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_shape"
android:clickable="true"
android:focusable="true"
android:visibility="visible"
android:foreground="?android:attr/selectableItemBackground"
android:padding="4dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_star_border_black_24dp"/>
</FrameLayout>
round_shape.xml add this file in drawable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:angle="270"
android:startColor="#color/profile_grey"
android:endColor="#color/profile_grey"/>
</shape>
Create Drawable and use that drawable as Button Background.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:color="#e1e1e1"
android:width="0.5dp"/>
<solid android:color="#android:color/white"/>
</shape>
</item>
</selector>
This Code create a circle with white background.

How to Make Custom Shaped Layout in Android

I'm making a bottom navigation bar, of custom shape
Like this =>
So I tried using LinearLayout with a FAB on it, as I dont know much about custom shapes,
but my idea not working as, when I scroll my webView bottombar hides, but fab doesn't. Anyways this is a bad method.
So, What I want is to make a custom shaped bottombar,
please help me make it, or just simple guiding would be helpful,
Thank you so much :)
Create a drawable with the below fields
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="4dp"
android:left="20dp"
android:right="20dp"
android:top="15dp">
<shape android:shape="rectangle">
<solid android:color="#ED7D31" /> //Color of youe choice
</shape>
</item>
<item
android:width="50dp"
android:height="50dp"
android:gravity="top|center">
<shape android:shape="oval">
<solid android:color="#ED7D31" />
</shape>
</item>
</layer-list>
and in the XML file add it as a background like below
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:elevation="4dp"
android:background="#drawable/list" /> //Your drawable name here

ButtonView with different background

I have designed a rounded corners button.
button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="10dp"/>
</shape>
In layout:
<Button
android:background="#drawable/button_selector"
.../>
on every click the color of the should button changes :
In activity, inside onClickListener():
btn1.setBackgroundColor(getAnswerColor(choiceArray[0]));
but if I do this, I am not getting the rounder corners.
How to get rounder corners? Please suggest.
You can make your button from this toolsite ==> Android Button Maker
You can make any kind of button with this tool with Code.
Here i edit this Answer as your requirement.
so,make two different button for with different background color and use selector for changing background onClick.For that use this code.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/click_button" android:state_pressed="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/not_click_button" android:state_pressed="false" />
</selector>
try this way
i checked with below 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="13dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
on button click add this code
StateListDrawable drawable = (StateListDrawable) v.getBackground();
drawable.setColorFilter(Color.RED,PorterDuff.Mode.SRC_ATOP);
button1.setBackground(drawable);
btn1.setBackgroundResource(R.drawable.button_selector);
GradientDrawable drawableGradient = (GradientDrawable) btn1.getBackground();
drawableGradient .setColor(Color.RED);

Custom background with layer-list to show diagonal lines?

I just want to create a custom background but I'm not getting how to do that with xml not with image.
Here is the xml:
<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:background="#layout/ui_shape"
tools:context="${relativePackage}.${activityClass}" >
</RelativeLayout>
and I want like this. Is it possible to create xml like the required image?
here is ui_shape
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<item>
<rotate
android:fromDegrees="45">
<shape
android:shape="line" >
<stroke android:color="#color/ui" android:width="1dp" />
<solid
android:color="#color/ui" />
</shape>
</rotate>
</item>
</layer-list>
and color
<color name="ui">#0095A0</color>
here is what i got
anybody any idea?
I know I am late, but this is my solution if anyone gets here:
1) create a single small diagonal line in photoshop or any other program, it should look like this
*
*
*
*
2) Create the following XML drawable in android studio where "#drawable/diagonal_line" is the previous mentioned image:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/diagonal_line"
android:tileMode="repeat"
android:dither="true"
>
</bitmap>
what this bitmap drawable will do is take that single diagonal line and repeat it all over the selected view.
3) Now use that XML drawable as your view background, as an example, this is how I used it, where "#drawable/tiled_background" is the XML in step 2:
<View
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#drawable/tiled_background"/>
and here is the result:
I hope this helps anyone. Happy Coding!
Use
android:background="#drawable/yourcustombackground"
Define your custom background inside drawable (something like below)
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<stroke android:width="1dp" android:color="#555555"/>
<solid android:color="#111111"/>
</shape>
Update: To display image at background
android:background="#drawable/ic_image"
ic_image is the desired image
Or you can use gradient also
No. it is not possible in android to set diagonal line .You have to use image for it for the diagonal lines

Rectangle shape as background causes black background

I would like to create a border around my RableRow.
I am using the following shape as background.
bg_stroke_dotted_right
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="-4dp"
android:left="-2dp"
android:bottom="-4dp"
android:drawable="#drawable/stroke_dotted">
</item>
</layer-list>
stroked_dotted drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#000000" />
<stroke
android:dashGap="4dp"
android:dashWidth="2dp"
android:width="2dp"
android:color="#color/greyDark" />
</shape>
I do not understand why using "bg_stroke_dotted_right" as bg - files the background with black color.
As soon as I add "bg_stroke_dotted_right" as bg - the bg turns to black.
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg_stroke_dotted_right"
>
in the shape try this :
<solid android:color="#00000000" />
in this case the first 2 "00" are the alpha of the colors, and "00" meand transparent.
enjoy!
The accepted answer suggests to use transparent in the solid element. Where as this is the reason for the bug (missing the solid element falls back to black on android <4.2) the solution is not the most optimal.
For starters instead of hard code the colour you can use #android:color/transparent however it's potentially best to use #null so that it doesn't overdraw. Something like this:
<solid android:color="#null" />

Categories

Resources