I want to make a perfect square shaped button and the shape looks like this:
Here, xml is :
<Button
android:id="#+id/button3"
android:layout_width="#dimen/box_size"
android:layout_height="#dimen/box_size"
android:layout_weight="50"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.598"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.146" />
I want to delete the blank between the line and blue square and don't know how to.
I tried to make an Image button and it also gave me unwanted result.
One way to solve this (Which I use), is to create a "Layout Resource File" and set it as the background for your button.
You could do this for example: create a "custom_background.xml" file (or any name you like), and write in it:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/colorPrimary" />
</shape>
Then add in the button code:
android:background="#drawable/button_background"
Related
I am trying to input an image in a regular button, however it does not show up. My code looks like this
<Button
android:text="#string/buttonexit"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/buttonexit"
android:layout_marginLeft="50dp"
android:background="#drawable/wood"/>
I also hoped and included src instead of background, however it still did not work.
<Button
android:text="#string/buttonexit"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/buttonexit"
android:layout_marginLeft="50dp"
android:src="#drawable/wood"/>
Lastly I also tried to make an image button, however once I added an image I cannot adjust the corners anymore.
You can use AppCompatButton instead of Button.
For a Button, the primary color defined in the app theme overrides any custom background that you want to apply to a Button, but it is not the case for the AppCompatButton.
For example:
<androidx.appcompat.widget.AppCompatButton
android:text="test"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/buttonexit"
android:layout_marginLeft="50dp"
android:background="#drawable/bg"/>
Besides, you can also use ImageButton to achieve this function.
You can add an image by property android:src and adjust the corners with property android:background by creating a shape xml file.
You can refer to the following code:
1.create file button_shape.xml in folder drawable:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--background color-->
<solid android:color="#20a4e5"/>
<!--radius-->
<corners android:radius="10dip"/>
<!--Sets the width and color of the border line-->
<stroke android:width="2dp" android:color="#fffff0" />
</shape>
2.set it for android:background of ImageButton
<ImageButton
android:text="test"
android:layout_width="300dp"
android:layout_height="80dp"
android:layout_marginLeft="50dp"
android:background="#drawable/button_shape"
android:src="#drawable/grass"/>
Ok so my buttons are not showing gradient as they should be (the gradient is not showing, its just a normal color background), idk why this is happening, everything looks like how it should be to me, can someone please take a look :
activity_main.xml
...
<Button
android:id="#+id/button1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#drawable/button"
app:icon="#drawable/ic_baseline_room_service_24"
android:fontFamily="monospace"
android:padding="15dp"
android:paddingEnd="40dp"
android:text="BROWSE BY INGREDIENTS"
android:textColor="#FFFFFF"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.084"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/viewPager"
app:layout_constraintVertical_bias="0.396" />
...
Here's a photo of the button as seen in the layout :
button (gradient not working)
button.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="#1746D3"
android:endColor="#6D34CF"/>
<corners
android:bottomRightRadius="900dp"
android:radius="90dp"/>
</shape>
</item>
</selector>
Here's a photo of the gradient background : gradient
However, the android:background="..." does show the mini preview at the side : button_preview
Any help is appriciated!
Replace in Button with androidx.appcompat.widget.AppCompatButton in activity_main.xml
It seems that using Button it is not possible to have the complete personalization of the widget.
Button widget handles its own background and you can only use tinting to change its color. This is an open issue and hasn't been fixed yet.
What I want to do:
What I have:
I want to make a circular icon with initials in it. In my scenario initials can have one or two letters. Like you can see my solution work fine for two letters but not to one. How can I solve this for both cases?
There is my code from the XML file:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20dp"
android:textAllCaps="true"
android:textColor="#color/white"
android:background="#drawable/shape_rounded_blue_button"
android:layout_gravity="center_horizontal"
android:gravity="center"
local:MvxBind="Text Initials"/>
And there is shape_rounded_blue_button:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/blue"/>
<corners android:radius="30dp" />
</shape>
As I wanted to implement the same thing, I made some research on GitHub and found this library:
https://github.com/amulyakhare/TextDrawable
It does exactly what you want and has some neat features as auto-coloring. You can use it with an ImageView and create a drawable, e.g. like this:
val drawable = TextDrawable.builder()
.beginConfig()
.width(bubbleSize)
.height(bubbleSize)
.endConfig()
.buildRound("FH", MATERIAL.getColor("FH"))
theImageView.setImageDrawable(drawable)
And that looks like this:
Your text will automatically be centered within the bubble, so it's no problem to have one or two characters.
You need to create a drawable resource file under res>drawable and save it as bg_circle.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#424242"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
Now make icon_container in your layout.xml file as:
<RelativeLayout
android:id="#+id/icon_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="#dimen/icon_width_height"
android:layout_height="#dimen/icon_width_height"
android:src="#drawable/bg_circle" />
<TextView
android:id="#+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#android:color/white"
android:textSize="#dimen/icon_text" />
</RelativeLayout>
For complete code implementation visit Implementing Gmail like icon with text and random colors
You need to create a circle shape.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/base30"/>
For better effects(shadow and appearance) you can use TextDrawable.
I am looking for a way to style my button like an example from an older question (How to create standard Borderless buttons (like in the design guidline mentioned)?) but some things were left out from the answer.
How can I add more than one value to an XML element, particularly android:background ="";?
I figured out how to make my buttons borderless, but I want them to have a really thin border, and a different background color. I have seen lots of tips online, but I can't find a way to put all my items together properly in code. Below is a copy pasted image with the top part representing the layout I want to achieve with the button name and a thumbnail image on the right hand side on the button with the button being a different color then the background of the app, and below that is a copy pasted image of the border style I'm trying to achieve, thin, touching borders between buttons. I have looked everywhere and tried many ideas but none seem to work properly, or some require me to have
android:background="?android:attr/selectableItemBackground"
but this interacts with
android:background="#22272D"
I need to keep the text in the button because my app is going to translate the buttons text to the language of the users phone, so I can't make the whole button just an image. Below is my XML and the output, any recommendations to how I should change it would be of massive help!
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="6"
tools:context="com.aid.travelers.myapplication.Transportation">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/transportation_page"
android:textSize="40sp"
android:textColor="#000000"/>
<Button
android:layout_width="match_parent"
android:text="#string/airport"
android:id="#+id/AirportButton"
android:layout_height="60dp"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="?android:attr/selectableItemBackground"/>
<Button
android:layout_width="match_parent"
android:text="#string/bicycle"
android:id="#+id/BicycleButton"
android:layout_height="60dp"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="?android:attr/selectableItemBackground"/>
I'm not sure that I get your question, but I guess you want to custom the background of your button, and have a touch effect when you press it.
Have a look at this answer :
https://stackoverflow.com/a/7176006/5446285
You should create your own resource file background.xml (for example) that you create in your drawable folder.
The code should be like :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/background_selected"/> <!-- pressed -->
<item android:drawable="#drawable/background_unselected" /> <!-- default -->
</selector>
You should now create 2 other files in your drawable folder : background_selected.xml, and background_unselected.xml.
To do so, I advise you something like this if you want a thin border :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#7c7c7c" />
</shape>
</item>
<item android:bottom="2dp" android:top="2dp" android:right="2dp" android:left="2dp">
<shape android:shape="rectangle" >
<solid android:color="#a0a0a0" />
</shape>
</item>
Then you set the background of your button in your xml :
android:background="#drawable/background"
I need to display the contact name initial letter inside circle like in lollipop contacts. I didn't find any solution.
The circle should different colour for each user.
You can follow this. If you wanna save time you can use this library.
Edit
The link may be invalid any point of time. So I want to add the details below this.
repositories{
maven {
url 'http://dl.bintray.com/amulyakhare/maven'
}
}
dependencies {
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
}
You can add which ever methodology you are following.
Basically you have to add one ImageView with same height and width and add this TextDrawable as a background of your view.
TextDrawable drawable = TextDrawable.builder()
.buildRect("A", Color.RED);
ImageView image = (ImageView) findViewById(R.id.image_view);
image.setImageDrawable(drawable);
The library has support for circle,square and rectangle drawables.
I didn't see any library for this but my solution is draw a circle in run time in a predefined layout in your xml use this :
How to draw circle by canvas in Android?
Then in your list adapter choose first letter of each name string using substring(0,1)
,and using setText() you can set list item textView to first letter.
If you need source code let me know to put it for you.
UPDATE:
I recently used very easy approach for this in one of my apps,
you should make a layout like this in your layout folder:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlWeekDay"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="right"
android:layout_margin="3dp"
android:background="#drawable/circle_shape">
<TextView
android:id="#+id/tvWeekDayFirstLetter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text=" E "
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:textSize="20sp" />
</RelativeLayout>
And your shape in drawable folder as circle_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval"
android:dither="true">
<corners
android:bottomRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp"
android:topRightRadius="100dp"/>
<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="3dp">
<shape android:shape="oval"
android:dither="true">
<solid android:color="#color/white"/> <!-- this one is ths color of the Rounded Button -->
<corners
android:bottomRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp"
android:topRightRadius="100dp"/>
</shape>
</item>
</layer-list>
Then in your listView or recyclerview use this as the item to inflate.like this
Simple and small code to do this -
<com.google.android.material.button.MaterialButton
android:id="#+id/"
android:layout_width="40dp"
android:layout_height="50dp"
android:text="A"
android:clickable="false"
app:backgroundTint="#color/Blue200"
android:padding="0dp"
app:cornerRadius="50dp"
/>
By using a Material Card View and a Textview, it would be easy to create such a view.
<com.google.android.material.card.MaterialCardView
android:layout_width="50dp"
android:layout_height="50dp"
app:cardBackgroundColor="#color/red"
app:cardCornerRadius="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/initialTv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="AB" />
</com.google.android.material.card.MaterialCardView>