I have a rectangle box that I set the spinner background as but I would like to keep the down arrow that comes along with the spinner. Here is my code for the rectangle:
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke android:width="2dp" android:color="#80000000" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />
See the picture below:
On the left is what i currently have, on the right is what I'm trying to achieve or at least have a down arrow in there somewhere. Any ideas on how to create this background?
I checked your code with the help of uiautomator and the spinner dropdown arrow does not show up with the background set.
You could use a LayerList and have a bitmap with gravity right. Now the bitmap is part of the background set to spinner.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<stroke
android:width="2dp"
android:color="#80000000"/>
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp"/>
</shape>
</item>
<item>
<bitmap
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:src="#mipmap/ic_launcher" />
</item>
</layer-list>
Result :
Of course there is no border around the bitmap. You can check this link How to change the spinner background design and color for android? and see if it fits your requirement
Add Spinner inside a RelativeLayout.
In my case, the icon disapper because of parent layout's height and width.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#drawable/startbg"
android:layout_marginLeft="5dp"
>
<Spinner
android:id="#+id/timerSpin"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:spinnerMode="dropdown"
android:backgroundTint="#color/white"
android:layout_centerInParent="true"
android:textColor="#color/white"
android:pointerIcon="arrow"
/>
</RelativeLayout>
Check the below XML code to achieve what you need. It is similar to what Raghunandan posted / answered, but with some minor tweaks. Hope this will help you,
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android = "http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape android:thickness="2dp">
<gradient android:angle = "90" android:endColor = "#ffffff" android:startColor = "#ffffff" android:type = "linear"/>
<stroke android:width = "1dp" android:color = "#80000000"/>
<corners android:radius = "5dp"/>
<padding android:bottom = "3dp" android:left = "3dp" android:right = "3dp" android:top = "3dp"/>
</shape>
</item>
<item>
<rotate
android:fromDegrees = "90"
android:pivotX = "85%"
android:pivotY = "50%"
android:toDegrees = "90">
<shape
android:shape = "line"
android:thickness="2dp"
android:top = "1dp">
<stroke
android:color="#80000000"
android:width = "1dp"/>
</shape>
</rotate>
</item>
<item>
<bitmap
android:gravity = "center|right" android:src = "#drawable/abc_btn_check_to_on_mtrl_015"/>
</item>
</layer-list>
</item>
</selector>
use the required drawable in the placeholder images i added in the code.
Hope this will help you,
Good Luck
Related
i create this xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#color/light_blue"
android:centerColor="#color/dark_blue"
android:endColor="#color/light_purple"
>
</gradient>
<corners
android:radius="30dp">
</corners>
</shape>
</item>
</selector>
and i trying to set this file to my button background.but problem is background dose not accept colors of xml file.it is use backgroundTint colors and not the colors i set above.
THIS IS MY ACTIVITY_XML
<Button
android:id="#+id/temp_l"
android:layout_width="250dp"
android:layout_height="33dp"
android:background="#drawable/temp_shape"
app:layout_constraintBottom_toTopOf="#+id/scrollView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.143" />
i searched in youtube and saw that eveyone did what i did but they get result and im not.
sorry about my english.its not very well.
I guess you are using android:backgroundTint="#null" with android namespace, but you need to set app:backgroundTint="#null" with app namespace instead
Side note:
You should wrap the xml drawable into a layer-list as long as you have no need to the selector, so you might change it to:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:centerColor="#055D93"
android:endColor="#color/light_purple"
android:startColor="#color/light_blue" />
<corners android:radius="30dp">
</corners>
</shape>
</item>
</layer-list>
Preview
I will share the image below that I want to develop my message bubble in Native Android
I have written some code in Custom Drawable XML please have a look and I will share the image what it looks like, first see the code:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="55"
android:pivotX="100%"
android:pivotY="0%"
android:toDegrees="0">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#00A1E4" />
</shape>
</rotate>
</item>
<item android:right="28dp">
<shape android:shape="rectangle">
<solid android:color="#00A1E4" />
<corners android:radius="10dp" />
</shape>
</item>
the above code looks like this
Please help me on how to make like the first image.
Thanks
You can use the ShapeAppearanceModel to apply a custom EdgeTreatment to a widget for eaxmple a CardView:
<LinearLayout
android:padding="8dp"
android:clipChildren="false"
android:clipToPadding="false"
...>
<com.google.android.material.card.MaterialCardView
android:id="#+id/cardview"
android:layout_height="100dp"
app:cardCornerRadius="24dp"
app:cardBackgroundColor="#color/..."/>
</LinearLayout>
Then you can apply a custom ShapeAppearanceModel using the builtin MarkerEdgeTreatment that draws an arrow on the edge given the radius of a circle. Something like:
val markerEdgeTreatment = MarkerEdgeTreatment(cardview.radius)
val offsetEdgeTreatment = OffsetEdgeTreatment(markerEdgeTreatment, 90f)
cardview.setShapeAppearanceModel(
cardview.getShapeAppearanceModel()
.toBuilder()
.setRightEdge(offsetEdgeTreatment)
.build()
)
You can also customize the shape of the edge extending the EdgeTreatment and overriding the getEdgePath method.
Note: it requires at least the version 1.2.0 of the Material Components Library.
I'm in the process of developing a compound view that I'm trying to get to look like the following image. I've marked (with a red arrow) the part that I'm having trouble with):
However, at present I'm unable to get the arc to cleanly start and finish (as shown below):
My code for this inner (dashed) progress drawable is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/secondaryProgress">
<rotate
android:fromDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="90">
<clip>
<shape
android:shape="oval"
android:useLevel="true">
<size
android:width="60dp"
android:height="60dp" />
<stroke
android:width="13dp"
android:color="#android:color/darker_gray"
android:dashWidth="3dp"
android:dashGap="5dp" />
</shape>
</clip>
</rotate>
</item>
<item android:id="#android:id/progress">
<rotate
android:fromDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="90">
<clip>
<shape
android:shape="oval"
android:useLevel="true">
<size
android:width="60dp"
android:height="60dp" />
<stroke
android:width="13dp"
android:color="#android:color/white"
android:dashWidth="3dp"
android:dashGap="5dp" />
</shape>
</clip>
</rotate>
</item>
</layer-list>
The surrounding progress drawables that I've made work fine with android:shape="ring" and removing the <clip> tags, but I can't seem to get the same clean clockwise progress behaviour using an android:shape="oval".
In case it helps, my code for setting the drawable to my ProgressBar is as follows:
<ProgressBar
android:id="#+id/progressbar_dashed_gauge"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="150dp"
android:layout_height="150dp"
android:indeterminate="false"
android:max="100"
android:scaleX="0.75"
android:scaleY="0.75"
android:progress="80"
android:progressDrawable="#drawable/dashed_gauge"
android:secondaryProgress="80"
/>
Any help with getting this dashed ProgressBar to show progress in a manner similar to my other ProgressBar views would be incredibly appreciated!
I have XML progressDrawable file for my ListView. It works well, but I found that if I use one Drawable XML file to make background for few object in one time, it works wrong.
Now I'm just created few XML Drawable files for each item from my ListView.
Now, my solution is like that:
for (int i = 0; i < 3; i++) {
View v1 = getActivity().getLayoutInflater().inflate(R.layout.item_results, null);
ProgressBar bar = v1.findViewById(R.id.MyDonut);
bar.setProgressDrawable(getResources().getDrawable(i==0?R.drawable.circle1:i==1?R.drawable.circle2:R.drawable.circle3));
listView.addHeaderView(v1);
}
Here, the circle1,circle2,circle3 - similar XML Drawable files. I think that my solution is too unflexible.
My circle:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:thicknessRatio="10"
android:useLevel="false">
<solid android:color="#color/orange" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="270"
android:toDegrees="270"
android:pivotX="50%"
android:pivotY="50%">
<shape
android:shape="ring"
android:thicknessRatio="10"
android:useLevel="true">
<solid android:color="#color/green" />
</shape>
</rotate>
</item>
<item android:left="75dp" android:gravity="center">
<rotate android:pivotX="0%" android:fromDegrees="270" android:toDegrees="270">
<shape
android:shape="line"
android:useLevel="false">
<size android:width="75dp" android:height="75dp"/>
<stroke android:width="3dp" android:color="#color/light"/>
</shape>
</rotate>
</item>
<item android:left="75dp" android:gravity="center">
<rotate android:pivotX="0%" android:fromDegrees="270" android:toDegrees="630">
<shape
android:shape="line"
android:useLevel="false">
<size android:width="75dp" android:height="75dp"/>
<stroke android:width="3dp" android:color="#color/light"/>
</shape>
</rotate>
</item>
item-results just contains:
<ProgressBar
android:progress="0"
android:id="#+id/MyDonut"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#color/light"
style="?android:progressBarStyleHorizontal"/>
What about the other solutions of this issue?
Finaly, I found a good solution. We can use drawable.mutate() to set the other Drawable, with all properties from parent Drawable.
Documentation: https://developer.android.com/reference/android/graphics/drawable/Drawable#mutate()
It is possible to use this this way:
progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.circle).mutate());
this is my first time posting. And I've been using this website as a great resource.
But I have come upon an issue. I've tried applying a shadow effect to my ImageButton but it doest work? What I'm trying to achieve is the shadow you can see on the floating bubble (Material Design). I have a circle ImageButton, but I wish to add a shadow around it. How can I achieve that?
This is circle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#C62666"/>
</shape>
This is circle_button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#E92D6C"/>
</shape>
This is my selector which combines both drawable's into one. button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/circle_button"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/circle_button_pressed"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/circle_button_pressed"
android:state_focused="true" />
<item android:drawable="#drawable/circle_button"
android:state_focused="false"
android:state_pressed="false" />
</selector>
And this is how I apply it to my ImageButton
<ImageButton
android:id="#+id/btn_apply"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/scrollView1"
android:layout_marginRight="45dp"
android:layout_marginTop="94dp"
android:adjustViewBounds="true"
android:background="#drawable/button"
android:scaleType="fitCenter"
android:src="#drawable/apply_two" />
So again? How can I add a shadow to my button? Thanks for your time.
may help you :
You can use a Button:
<ImageButton
android:id="#+id/fab"
android:background="#drawable/ripple"
android:stateListAnimator="#anim/anim"
android:src="#drawable/ic_action_add"
android:elevation="4dp"
/>
where the ic_action_add is your icon.
drawable/ripple.xml is:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
anim/anim.xml is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="#dimen/button_elevation"
android:valueTo="#dimen/button_press_elevation"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="#dimen/button_press_elevation"
android:valueTo="#dimen/button_elevation"
android:valueType="floatType" />
</item>
</selector>
Dimens.xml is :
<resources>
<dimen name="fab_size">56dp</dimen>
<dimen name="button_elevation">2dp</dimen>
<dimen name="button_press_elevation">4dp</dimen>
With the elevation attribute you should set the Outline via code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutfab);
//Outline
int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
Outline outline = new Outline();
outline.setOval(0, 0, size, size);
findViewById(R.id.fab).setOutline(outline);
}
}