change the appearance of button into round button - java

I want to made the present rectangular button into round shape and add the picture of share button in it. I mean the button should be of round share and it should have only image(fit) of "share"
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginTop="8dp"
android:text="03"
android:textStyle="bold"/>

Create drawable file as bg_share.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/colorPrimaryDark">
</solid>
<corners
android:radius="500dp">
</corners>
<padding
android:top="0dp"
android:right="10dp"
android:left="10dp"
android:bottom="0dp">
</padding>
</shape>
And in your main.xml create imageview instead of button like
<ImageView
android:id="#+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bcshare"
android:padding="10dp"
android:src="#drawable/ic_share_black_24dp" />
In MainActivity.java do something like this
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your code...
}
});

You can find the answer Here
create a resource drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#FF404040" />
<corners android:radius="6dp" />
<gradient android:startColor="#FF6800" android:centerColor="#FF8000" android:endColor="#FF9700" android:angle="90" />
</shape>
then set to the button background .
android:background="#drawable/button_background"

you can create a separate drawable and implement round shape. Then set the drawable to the background of the button as below
Create a drawable file as 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="#008577"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
Then change the layout as follows
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle_button"
android:text="Button"/>

Create an xml in drawable folder and name it like: "round.xml" and use this xml in your given button background.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#eeffffff" />
<corners android:bottomRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:topRightRadius="8dp"
android:topLeftRadius="8dp"/>
</shape>
and to your button in layout xml you can use the last line to set background
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginTop="8dp"
android:text="03"
android:textStyle="bold"
android:background="#drawable/round"/>

You can use the standard MaterialButton with the Widget.MaterialComponents.Button.Icon style.
Something like:
<com.google.android.material.button.MaterialButton
android:layout_width="48dp"
android:layout_height="48dp"
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/ic_share"
app:iconSize="24dp"
app:iconPadding="0dp"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
/>
Use the app:shapeAppearanceOverlay to get rounded corners. In this way you will have a circle.
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">32dp</item>
</style>

Related

Button color from drawable doesn't change

I have some issue with styling of my buttons. I use drawable to change shape and style my app buttons. When I add background in activity xml my buttons change shape but doesn't change background color but when I add background programmatically with "setBackgroundResource" method everything is ok.
There is my piece of code:
activity xml:
`<Button
android:id="#+id/button_traditional"
android:background="#drawable/button_default_main_app"
android:textColor="#color/mustard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="32dp"
android:onClick="newGame"
android:text="#string/standard_game"
app:layout_constraintEnd_toStartOf="#+id/button_extended"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_gameChose" />
`
res/drawable:
<solid
android:color="#color/black"
/>
<corners
android:topLeftRadius="10dp"
android:topRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:bottomRightRadius="10dp"
/>
Thanks in advance for your help.
Edit:
Whole drawable 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/black"
/>
<corners
android:topLeftRadius="10dp"
android:topRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:bottomRightRadius="10dp"
/>
</shape>
By default the button tint is the primary color. You need to set it to null for the drawable to take effect by setting.
app:backgroundTint="#null"
So your whole button would look like this:
<Button
android:id="#+id/button_traditional"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="32dp"
android:background="#drawable/button_default_main_app"
android:onClick="newGame"
android:text="Standard game"
android:textColor="#a00"
app:backgroundTint="#null" />
The result:

Button Border Not Showing

I try to put a border for a button in android but unfortunately I can't see the button border.
Drawable File 👇
<?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="12dp" />
<stroke android:width="3px" android:color="#7E8082" />
</shape>
</item>
</selector>
XML 👇
<Button
android:id="#+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="122dp"
android:background="#drawable/bg_signup"
android:fontFamily="#font/poppins_semibold"
android:text="Create an Account"
android:textAllCaps="false"
android:textColor="#color/black"
app:layout_constraintTop_toBottomOf="#+id/btn_login" />
You don't need to use a custom drawable, just use a MaterialButton:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:strokeColor="#7E8082"
app:strokeWidth="1dp"
app:cornerRadius="12dp"/>
Don't use a selector for single option use shape directly
bg_signup
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#cdcdcd"/>
<corners
android:radius="4dp"
/>
<stroke
android:width="1dp"
android:color="#000"
/>
</shape>
use Style attribute instead of android:background="#drawable/bg_signup"
make a custom style then call it android:style="#style/bg_signup"

How do I add animation / styling to a button? Android Studio

I have a button
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="45dp"
android:onClick="loginIsClicked"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:background="#drawable/button"
android:text="#string/Auth"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="16sp"
android:enabled="false" />
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/red"/>
<corners android:radius="10dp"/>
</shape>
How can I add animation to the button used in the default buttons?
Example:
enter image description here
If I understood correctly, what you are looking for is a ripple. Wrap your shape in a ripple as follows and provide your ripple color:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/rippleColor">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/red"/>
<corners android:radius="10dp"/>
</shape>
</item>
</ripple>
Add MaterialButton.
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Button" />
Also do not forget to change theme Theme.AppCompat.Light.DarkActionBar to Theme.MaterialComponents.Light.DarkActionBar in styles.xml file.

How to create custom button view that can accept corner radius and color dynamically and change accordingly

How to create custom button view that can accept corner radius and background color dynamically rather than making shape file for every button.
I know that i can extend to the Button class and also create attribute set to accept the values, but just dont know how to change corner radius of the button.
You can use MaterialButton
<com.google.android.material.button.MaterialButton
android:id="#+id/btnWithdraw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:padding="15dp"
android:text="#string/withdraw"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="15sp"
android:textStyle="bold"
app:backgroundTint="#color/colorLightRed"
app:cornerRadius="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/edtAgentPin" />
To change background color use this
btnWithdraw.backgroundTintList = ColorStateList.valueOf(Color.BLUE)
To change cornerRadius use this
rootView.btnWithdraw.cornerRadius = 20
NOTE : make sure you have added below dependencies in your Build.Gradle file
implementation 'com.google.android.material:material:1.0.0'
1.Create a xml file in your drawable folder like mybutton.xml and paste the following markup:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
here button code below
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:background="#drawable/mybutton"
android:text="Buttons" />
Please try this
val shape = GradientDrawable()
shape.cornerRadius = 18f
// add some color
// You can add your random color generator here
// and set color
shape.setColor(Color.RED);
// now find your view and add background to it
view.btn.background = shape
You can use the CardView to create custom button with corner radius and color dynamically.
XML Code:
<android.support.v7.widget.CardView
android:id="#+id/main_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="20dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#color/orange"
android:layout_centerInParent="true">
<LinearLayout
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:textColor="#000"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Change Color dynamically :
CardView main_card=findViewById(R.id.main_card);
main_card.setCardBackgroundColor(getResources().getColor(R.color.colorAccent));
I hope it works for you.
Button btn = new Button(this);
btn.setText("Submit");
final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
btn.setBackgroundDrawable(ContextCompat.getDrawable(context,R.drawable.ready) );
} else {
btn.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
}
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams buttonlayout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(btn, buttonlayout);
add drwaable R.drawable.library_cirecle
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/outerRectangle">
<shape android:shape="oval" >
<solid android:color="#FCD366" />
<stroke
android:width="1dp"
android:color="#android:color/darker_gray" />
</shape>
</item>
Change color in code
Drawable tempDrawable = getResources().getDrawable(R.drawable.library_cirecle);
LayerDrawable bubble = (LayerDrawable) tempDrawable; (cast to root element in xml)
GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle);
solidColor.setColor(colorToPaint);
imageView.setImageDrawable(tempDrawable);

mysterious line arround editText from searchview

I have a searchview and i changed the edittext box of them with a drawable (with java code). However I have a mysterious border that i can't remove. If you see the screenshot you will see the red line that is a border defined in drawable 'editext.xml' My problem is I doesn't know what is the black line arround the edittext. Anybody know what is and how i remove it?
drawable (edittext):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dip" android:color="#c66262" />
<solid android:color="#FFFFFF" />
</shape>
drawable (searchview):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#FFFFFF"/>
<stroke android:width="1dip" android:color="#000000" />
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
acitivty 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:fitsSystemWindows="true"
tools:context=".SearchActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="160sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/search_background"
android:orientation="vertical" >
<SearchView
android:id="#+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp"
android:layout_marginTop="60sp"
android:actionViewClass="android.widget.SearchView"
android:background="#drawable/searchview"
android:fitsSystemWindows="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:iconifiedByDefault="false"
android:paddingLeft="10sp"
android:paddingTop="5sp"
android:showAsAction="collapseActionView" >
</SearchView>
</LinearLayout>
</RelativeLayout>
ScreenShot: http://imageshack.com/a/img842/6100/ebsb.jpg
thanks in advance
I think your red color is set in this line in your edittext shape:
<stroke android:width="1dip" android:color="#c66262" />
maybe set here:
<stroke android:width="1dip" android:color="#000000" />
for a black stroke
Edit: this is for making red line black. I misunderstood question a bit. Anyhow I think would look nicer this way. My few cents ;-)
Anyhow, if you don't want the black line, then just set the black line (#000000) to let's say white:
<stroke android:width="1dip" android:color="#ffffff" />
or simply get rid of it at all, by just deleting this code here, which looks like it is your black stroke:
<stroke android:width="1dip" android:color="#000000" />
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
Just remove following line from drawable(searchview):
stroke android:width="1dip" android:color="#000000"

Categories