shape with drawable resource inside programmatically - java

Currently I have a action bar menu with few items inside.
I am trying to have a menu item with shape which has a drawable
The code i get so far:
ShapeDrawable circle = new ShapeDrawable(new OvalShape());
circle.getPaint().setColor(Color.GREEN);
circle.setIntrinsicHeight(120);
circle.setIntrinsicWidth(120);
circle.setBounds(0, 0, 120, 120);
menu.findItem(R.id.menu_item_1).setIcon(circle);
However currently only looks like a green circle, I want inside to have a drawable icon.
for example

just create icon.xml in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid
android:color="#color/colorPrimaryDark"/>
<size
android:width="70dp"
android:height="70dp"/>
</shape>
</item>
<item android:drawable="#android:drawable/ic_dialog_map" android:gravity="center">
</item>
</layer-list>
and use it like this:
menu.findItem(R.id.menu_item_1).setIcon(ContextCompat.getDrawable(this, R.drawable.icon));

Related

Change Card View Elevation Color

How can I change the color of the card view elevation?
Or make it softer
It gets dark when I increase the app:cardElevation value
Add these 2 properties to your CardView
android:outlineAmbientShadowColor="#color/yourShadowColor"
android:outlineSpotShadowColor="#color/yourShadowColor"
here you can change color first you can make a drawable file and paste this code in it. then in your layout inside cardview, you can assign as background file.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:bottomLeftRadius="8dp"
android:topLeftRadius="5dp"
android:bottomRightRadius="8dp"
android:topRightRadius="5dp"/>
<solid android:color="#ddd"/>
</shape>
</item>

Move bitmap items in layer-list programmatically

I would need move-change position of individual bitmap items in layer-list such as this:
background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:centerX="0.5"
android:centerY="0.3"
android:endColor="#color/colorBackgroundEnd"
android:gradientRadius="300dp"
android:startColor="#color/colorBackgroundStart"
android:type="radial" />
</shape>
</item>
<item
android:id="#+id/peas"
android:left="35dp"
android:top="110dp"
>
<bitmap
android:gravity="left|start|top"
android:src="#drawable/peas"
/>
</item>
<item
android:id="#+id/orange"
android:right="-35dp"
android:bottom="110dp">
<bitmap
android:gravity="right|end|bottom"
android:src="#drawable/orange" />
</item>
<item
android:id="#+id/salad"
android:bottom="-40dp"
android:left="-40dp">
<bitmap
android:gravity="left|start|bottom"
android:src="#drawable/salad"/>
</item>
I was able to reference items in java code:
layerDrawable = (LayerDrawable) ContextCompat.getDrawable(MainActivity.this,R.drawable.background);
Drawable salad = (Drawable) layerDrawable.findDrawableByLayerId(R.id.salad);
and get-set some layer properties, such as getAlpha and setAlpha
layerDrawable.findDrawableByLayerId(R.id.salad).setAlpha(0);
down.setText( String.valueOf(layerDrawable.findDrawableByLayerId(R.id.salad).getAlpha()));
, but I need code access to <item> left and top property that would enable setting layer bitmap position, like it can be done from XML file. How do I do this?

How to add a border to a particular side of a view

This is how I go about adding a border to a EditText. How can I go about adding a border only on one side of a EditText, and define the color and width of the border?
EditText editText = new EditText(this);
editText.setText("Find");
editText.setWidth(555);
GradientDrawable border = new GradientDrawable();
border.setColor(0xFFFFFFFF); // white background
border.setStroke(1, 0xFF000000); // black border with full
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
editText.setBackgroundDrawable(border);
} else {
editText.setBackground(border);
}
Vielen dank im voraus.
To get border on one side, you can create your own drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:right="5dp">
<shape android:shape="rectangle">
<solid android:color="#FFFF" />
</shape>
</item>
</layer-list>
And set this drawable as background to your EditText.
If you want to add border only one side of EditText then you should use View tag in your layout file to draw a simple line and place it near your EditText and use background property to set color of line.
To draw horizontal line use this:
<View
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:layout_height="2dp" />

How do I add user configurable buttons with tap states?

I'm trying to make a button in Android that has a border but with a different background tint when it is pressed, and the ability to change the color of the button background. I know adding a border is assigning it a shape and that tap states are through a selector with different items, but the problem is that the button background color is meant to be user configured.
Without tap states, I am able to allow the user to change the background color of my shape by just doing:
GradientDrawable bgShape = (GradientDrawable) btn.getBackground();
//color value is obtained from shared preferences
if (sharedPref.contains(pref_color)) {
String color = sharedPref.getString(pref_color, "");
bgShape.setColor(Color.parseColor(color));
}
But I can't do the first line if my button is going to be assigned a selector. I don't know how I would get the reference to the drawable shape.
For reference, my button border shape is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="5px" android:color="#ffffff" />
</shape>
The selector would look like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_press" android:state_pressed="true"/>
<item android:drawable="#drawable/button_border" />
</selector>
Is there a way to accomplish this?
you can define an id for drawable layer and then change their properties in runtime
LayerDrawable drawSettings = (LayerDrawable)
getResources().getDrawable(R.drawable.sample);
GradientDrawable backSettings = (GradientDrawable)
drawSettings.findDrawableByLayerId(R.id.backtemp);
and after change color reset it to button background
view.setBackground(drawable);
this is a sample drawable sample.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/backtemp">
<shape
android:shape="oval" >
<solid android:color="#color/default_color"/>
</shape>
</item>
<item
android:drawable="#drawable/w_pref">
</item>
</layer-list>

How to use a drawable shape in Android through Java?

If I have a drawable shape:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="2dp" android:color="#FFFFFFFF" />
<gradient android:startColor="#DD000000" android:endColor="#DD2ECCFA"
android:angle="225"/>
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
How would I go about using this as the background for a textview? I tried something like below, but it didn't work.
TextView jObjTv = new TextView(getActivity());
jObjTv.setBackgroundDrawable(findViewById(R.drawable.sample_box));
The code you posted doesn't work because a drawable isn't a view and isn't a part of the layout.
You need to call textView.setBackgroundResource(R.drawable.sample_box) if you want to use the resource. If you want to use the Drawable, you need to create the Drawable using context.getResources().getDrawable(R.drawable.sample_box).

Categories

Resources