Not able to create proper shape - java

I am trying to create a shape like
But right now what it look like is
Here is my code
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/somecolor" />
<corners
android:radius="10dip"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<padding
android:top="10dp"
android:bottom="10dp" />
</shape>
Any help?

Change android:top to android:left and android:bottom to android:right.
Also you could use negative values for these attributes, so you can finely tune what you would want to achieve.

custom_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="2dp" android:color="#ff207d94" />
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<corners android:radius="5dp" />
<solid android:color="#color/somecolor" />
</shape>
Your View
<View
android:id="#+id/myView"
android:layout_width="60dp" // width and height will decide the shape
android:layout_height="30dp"
android:background="#drawable/custom_layout"/>

What is exactly the problem there (size/hight...).
Have you tried to add some ohter tags to your shape?
I use something like:
<shape>
<gradient
android:startColor="#525251"
android:endColor="#838181"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#525251" />
<corners
android:radius="6dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
(width/hight defined in layout XML)

Related

customize a toggle switch for Android

I tried to customized a toggle switch and after hours on playing with material, I just gave up and re-used the SwitchCompat
<androidx.appcompat.widget.SwitchCompat
android:id="#+id/settingsSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/cell_padding"
android:checked="#{item.enabled}"
tools:checked="true"
android:thumb="#drawable/ios_thumb"
app:trackTintMode="multiply"
app:trackTint="#null"
app:track="#drawable/ios_track"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
and the ios_thumb look like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#FFFFFF"/>
<corners android:radius="15dp" />
<size android:width="30dp" android:height="30dp" />
<stroke android:width="4dp" android:color="#0000ffff" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#FFFFFF"/>
<corners android:radius="15dp" />
<size android:width="30dp" android:height="30dp" />
<stroke android:width="4dp" android:color="#0000ffff" />
</shape>
</item>
</selector>
and ios_track as below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#color/green300"/>
<corners android:radius="25dp" />
<size android:width="60dp" android:height="30dp" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#color/lightness200"/>
<corners android:radius="25dp" />
<size android:width="60dp" android:height="30dp" />
</shape>
</item>
</selector>
the mechanism works and I almost there for the customization... Only thing is that :
When the switch is to false for check or true, it's not exactly reflecting the color.
As an example:
The white is grey-ish. I think the false is also bad but as my background for false state is a transparent white it's not affecting the visual. It's look like the track is displayed over the thumb instead of the opposite. Any idea?

how to make a gradient color and set it on ImageView (android)

gradient.xml:
<gradient
android:angle="135"
android:endColor="#aaFFB5B9"
android:startColor="#aaFFDEB5"
/>
<corners android:radius="0dp" />
<size
android:width="120dp"
android:height="120dp"/>
gradient2.xml:
<gradient
android:angle="135"
android:startColor="#aaECB4FF"
android:endColor="#aaB4CCFF"
/>
<corners android:radius="0dp" />
<size
android:width="120dp"
android:height="120dp"/>
ImageView:
android:foreground="#drawable/gradient"
ImageView2:
android:foreground="#drawable/gradient2"
In your drawable/gradient.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--You have to specify the shape of the gradient-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:angle="135"
android:endColor="#aaFFB5B9"
android:startColor="#aaFFDEB5"
/>
<corners android:radius="0dp" />
<size
android:width="120dp"
android:height="120dp"/>
</shape>
In your Imageview:
<ImageView
...
android:src="#drawable/gradient"
/>
Do the same with ImageView2

Transparent CardView with Elevation issue

When I want a Transparent CardView with some elevation the result will be as follows:
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B3FFFFFF"
app:cardElevation="8dp"
app:cardBackgroundColor="#B3FFFFFF"
app:cardCornerRadius="25dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/folder_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:background="#color/transparent"
android:gravity="center"
android:hint="#string/default_folder_name"
android:importantForAutofill="no"
android:inputType="text"
android:padding="10dp"
android:textAllCaps="false"
android:textColor="#color/folder_title"
android:textColorHint="#color/folder_title_hint"
android:textSize="15sp"
android:textStyle="bold" />
<!--this is only for the cardView to give it more space to display the issue-->
<View
android:layout_width="150dp"
android:layout_height="60dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
The issue is that there is a color difference inside the CardView, it should be like:
When I replace app:cardCornerRadius="25dp" -> app:cardCornerRadius="0dp", the size of the unwanted color difference increased.
Is this issue the way elevation works?
And are there workarounds?
Thanks in advance,
Try to add this to your theme
<item name="elevationOverlayEnabled">false</item>
With CardView it is not possible.
The workaround in this case is to remove CardView and set a android:background="#drawable/shadowfile"to the LinearLayout.
The file contains the [transparency, elevation, radius].
res/drawable/shadowfile.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- every item add a layer of evaluation to the border, with the padding you define how the evaluation is located around the border-->
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#03000000" />
<corners android:radius="20dp" />
</shape>
</item> <item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#03000000" />
<corners android:radius="20dp" />
</shape>
</item> <item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#03000000" />
<corners android:radius="20dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#03000000" />
<corners android:radius="20dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#03000000" />
<corners android:radius="20dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#03000000" />
<corners android:radius="20dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#03000000" />
<corners android:radius="20dp" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#D2FFFFFF" />
<corners android:radius="20dp" />
</shape>
</item>
</layer-list>

Android custom seekBar with "stations"

I'm trying to make my custom seekbar.
This is what i try to do:
and this is what I succeed to do:
How to add this circles behind my progress?
Here is my code:
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="#drawable/progress"
android:thumb="#drawable/unnamed"
android:id="#+id/risk_seek_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
progress.xml
<?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="#00FF0000" />
<padding android:bottom="1dp"/>
</shape>
</item>
<item
android:id="#android:id/background"
android:drawable="#drawable/background_fill"/>
<item android:id="#android:id/progress">
<clip android:drawable="#drawable/progress_fill" />
</item>
</layer-list>
background fill.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:centerColor="#FF555555"
android:endColor="#FF555555"
android:startColor="#FF555555" />
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#50999999" />
<stroke
android:width="1dp"
android:color="#70555555" />
</shape>
progress_fill.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:centerColor="#FFFF4400"
android:endColor="#FFFF4400"
android:startColor="#FFFF4400" />
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#50999999" />
<stroke
android:width="1dp"
android:color="#70555555" />
</shape>
You can use any of the following libraries
range-slider-view
material-range-bar
range-bar

Changing background color on touchevent on relative layout

i am using following code.
<RelativeLayout
android:layout_width="310dp"
android:layout_height="70dp"
android:layout_below="#+id/UIReportView"
android:layout_marginLeft="4dp"
android:layout_marginTop="2dp"
android:background="#drawable/small_corners" >
small_corners
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/UINoCorners"
android:shape="rectangle">
<corners android:radius="10dp"/>
<padding android:left="10dp"
android:right="10dp"
android:top="5dp"
android:bottom="5dp"/>
<solid android:color="#FFFFFF"/>
</shape>
Now , it shows a white bar. I want that, whenever i click on this relative layout its color should change, set by me.
report = (RelativeLayout) findViewById(R.id.UIMainReportViewTimely);
report .setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// My Code
}
});
What should i do here? report.onTouchEvent?
And how can i change the color of background.
Best Regards
Create another drawable with the new background, and in your setOnclickListener put
report.setBackgroundDrawable(R.drawable.newbackground_small_corners);
or
report.setBackgroundColor(Color.parseColor("#00000000"));
you can use this:-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<stroke android:width="1dp" android:color="#999999" />
<padding android:left="10dp" android:top="3dp" android:right="10dp"
android:bottom="3dp" />
<corners android:radius="7dp" android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp" android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
<gradient android:startColor="#449def" android:endColor="#2f6699"
android:angle="270" />
</shape>
</item>
<item>
<shape>
<corners android:radius="10dp"/>
<padding android:left="10dp"
android:right="10dp"
android:top="5dp"
android:bottom="5dp"/>
<solid android:color="#FFFFFF"/>
</shape>
</item>
</selector>
</shape>
I'd rather suggest to use the onTouch() method and check whether the touched coordinates are within the bounds of your relative layout. If yes, then just set background using setBackgroundColor().

Categories