Create drawable programmatically - java

How can I create this exact drawable in java code only?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="1dp" />
<solid android:color="#ff0000" />
<padding
android:bottom="-15px"
android:top="-5px" />
</shape>
I need the padding values to be variable so I can't do it as an xml.

If you wanna draw something custom, you should open a Canvas and make your drawing there.
Read this:
https://developer.android.com/training/custom-views/custom-drawing.html
And it seems a simple shape. So you can add a layout view (View,TextView, ImageView, etc.) set background with this drawable and adjust padding through this view.

Related

Android Studio change button background color with custom drawable

I have a custom drawable using <shape> in xml, with the following code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/blue"/>
<corners
android:radius="150dp"/>
</shape>
on a button, and I would like to programmatically change the button's color to another color while keeping the shape it gets from the custom drawable. Is this possible?
If you dont want to use the normal backgrounTint in your xml folder :
android:backgroundTint="yourcolor"
Use this Code in your java code :
yourbutton.setBackgroundTintList(this.getResources().getColorStateList(R.color.colorname));

How to make a fade black background in Android Studio

I would like to put a fade black covering my image, just like this one, with a text, a fade background and then my image. I just want to know how to implement this fade. Thank you!
Just create a drawable file, I'll name it gradient_background (you can set any name)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:startColor="#000000"
android:centerColor="#00000000"
android:endColor="#00000000"/>
</shape>
then inside your target xml, create a View like this one, look that inside it, we're setting the background as your drawable file. In this case is gradient_background
<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/gradient_background"
app:layout_constraintBottom_toBottomOf="post_image"
app:layout_constraintEnd_toEndOf="#+id/post_image"
app:layout_constraintStart_toStartOf="post_image"
app:layout_constraintTop_toTopOf="#+id/post_image" />
So it should look like this:
:)
First, Make bottom_transparent_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:angle="270"
android:endColor="#64000000"
android:startColor="#android:color/transparent" />
</shape>
</item>
</selector>
Then, add that drawable as your view background in which you want to show the fade background effect.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bottom_transparent_gradient"/>
And that's it! You can see the result you wanted. :)

How to set shapedrawable's border and fill colours to different colours

I need to set the border colour and fill colour of a shapedrawable to different colours. How can I set both colours and have the shape as fill and stroke?
Say I have this:
ShapeDrawable border = new ShapeDrawable(new RectShape());
border.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
border.getPaint().setColor(Color.WHITE);
border.getPaint().setStrokeColour(Color.BLACK); //???????? How to do
Edit: My question is not a duplicate of Android ShapeDrawable set Background and Border programmatically because that question's only answer is unclear and does not help me.
You might want to define your drawable in XML instead. It would look something like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/WHITE"/>
<stroke
android:width="2dp"
android:color="#color/BLACK" />
you might define an xml like below and use it where you need:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="7dp"
android:topRightRadius="7dp"
android:topLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp" />
<solid android:color="#android:color/white" />
<stroke android:width="1dip" android:color="#c9d1d7"/>
</shape>

How create border around visible image in ImageView

It is possible to create contour around or shadow under VISIBLE pixels in ImageView.
In case this image in ImageView (outside from green figure is transparent):
I need next pic with border:
Please any code or references.
Dunno you just want to add a color as stroke to the background then you can do like this
If you are using color then you can create an xml file in the drawable in the res of your project
and create a drawable like this
Image.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="2dp"
android:color="#007cea" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
<solid android:color="#007cea" />
<corners android:radius="4dp" />
</shape>
And to your imageview you can call the xml file as
android:background="#drawable/login_button"
In the xml file you specify like this
<stroke
android:width="2dp"
android:color="#007cea" />
the android:color="#007cea" defines which color you want to give as border
I don't think you will find exactly what you want.
If it was drawable possibly you could use layer-list option.
But it won't work with Imageview.

How can I shape my button on android rectangle to circle?

In generally android buttons are rectangular and on the mxl file what should I do if I want to make ring shaped or any other shaped button? or is there any tool that assure these to android developers which is open source (for now I'm seeking a ring-shaped button ).
create a new xml file inside your drawables, and put this code:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="15dp"
android:thickness="10dp"
android:useLevel="false">
<solid android:color="#fff" />
<size
android:height="50dp"
android:width="50dp" />
</shape>
now, use this as your image in an imageButton
You can also take a look at this:
android-state-list-generator

Categories