Android Custom button with imageview and textview inside? - java

I'm looking to create a custom button. This button ideally would have an image on the left and a textview on the right. How would I accomplish this?

The fastest way
Create clickable View with Button drawable as background that contains ImageView and TextView.
<RelativeLayout
android:clickable="true"
android:background="#android:drawable/btn_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#android:drawable/ic_input_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/text"
android:text="Sample text"
android:textAppearance="?android:attr/textAppearanceButton"
android:layout_toRightOf="#id/image"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</RelativeLayout>
Other way
Create your own class, extend RelativeLayout, set content view as above, add custom methods to set text, image etc. and that's all.
Use your custom layout.

you may to use such code for that task:
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/btnLogin"
android:text="#string/text"
android:gravity="center"
android:drawableLeft="#drawable/your_image"
android:background="#drawable/login"/> // this is for nice background - *.9.png

Create a xml with the Layout you want, xreate a drawable which look like you want the Button to look, add the drawable as background to Linear Layout and inflate the xml to a custom view.
Example drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid android:color="#f3ae1b" />
<stroke android:width="1dp" android:color="#bb6008" />
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#f3ae1b"
android:endColor="#bb6008"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#bb6008" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Edit: Code to inflate
private LayoutInflater mInflater;
mInflater = LayoutInflater.from(this);
public LinearLayout yourButton;
yourButton = (LinearLayout) mInflater.inflate(R.xml.yourButton, null);

In Xml layout use this drawable layout options and get simple layouts.The following line gives the image at the left side of the text.
android:drawableLeft="#drawable/image"

If you want to write code to do this, use setCompoundDrawables() of the Button class to decide where the drawable should appear relative to text.
Look for the API for more details.

You can do this by using NoboButton
Simple and fast way to create android button with icon, radius, background
Library url https://github.com/alex31n/NoboButton
Screenshot https://raw.githubusercontent.com/alex31n/NoboButton/master/Assets/button_screenshot.jpg

Related

Android Studio, color in selector (in item) doesnt work

<Button
android:id="#+id/sort_time"
android:layout_width="0dp"
android:layout_height="52dp"
android:layout_marginTop="54dp"
android:background="#drawable/roundless_button"
android:backgroundTint="#2A56C6"
android:text="#string/order_by_time"
android:textSize="14sp"
app:layout_constraintEnd_toStartOf="#+id/sort_status"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/IndBlue"
>
<shape android:shape="rectangle">
</shape>
<solid android:color="#color/IndBlue" />
</item>
</selector>
I tried to edit the buttons colors, but for some reason it won't change. Solid android:color / android:drawable doesn't work even though I placed it everywhere.
when you use selector as button background it inherits the color from theme. you can try changing the theme primary color and that will affect the button color.

Why button is displaying the wrong color?

This button is displaying a purple color and its XML code is
android:id="#+id/btnLogin"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_marginStart="141dp"
android:layout_marginEnd="142dp"
android:layout_marginBottom="48dp"
android:background="#drawable/btn_login"
android:fontFamily="#font/poppins"
android:text="Login"
android:textAllCaps="false"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/tvCreateAccount"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
"#drawable/btn_login" is a shape I created -
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#71D775">
</solid>
<corners android:radius="66dp">
</corners>
</shape>
And this is how it looks like,
This is because the default MaterialButton style overrides backgroundTint.
So if we are using a custom android:background, we have to make sure to null out backgroundTint. We can achieve it by any of the following way:
Add any of these line to your button XML.
app:backgroundTint="#null"
OR
app:backgroundTint="#empty"
For some reason, you need to set the app:backgroundTint to null.
Like so:
app:backgroundTint="#null"

Drawable Layer always top

I don't know whether am wrong or not. I just want to add a box to my EmptyActivity in android studio. I create a resource file and access it from the Activity.xml file. But I found it always layered on top of any ImageView component.
image
Resouce file
page_select_bar.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/white" />
<corners android:radius="10dp" />
</shape>
ActivityMain.xml
<ImageView
android:id="#+id/page_select_bar"
android:layout_width="409dp"
android:layout_height="70dp"
android:layout_marginTop="777dp"
android:contentDescription="#string/desc"
android:elevation="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/page_select_bar"
tools:targetApi="lollipop" />
am I missing anything? I'm new to android :)
Try adding
<stroke android:color="#color/Black" android:width="2sp"/>
to your drawable inside <shape>
and use it as background

How to make a button with rounded corners, background image and text in android

I'm trying to create an android button whose text can be changed (to be able to internationalize the app easily), but at the same time have a background, and using rounded corners if possible.
So far, if I use a background image I can not get the corners to be round and vice versa.
The last thing I tried is to create these xml file
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="2dp" android:color="#FF404040" />
<corners android:radius="6dp" />
<gradient android:startColor="#FF6800" android:centerColor="#FF8000" android:endColor="#FF9700" android:angle="90" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="2dp" android:color="#FF404040" />
<corners android:radius="6dp" />
<gradient android:startColor="#FF6800" android:centerColor="#FF8000" android:endColor="#FF9700" android:angle="90" />
</shape>
and this button, but I can not put a background image:
<Button
android:id="#+id/button"
android:layout_width="235dp"
android:layout_height="wrap_content"
android:background="#drawable/background_selector"
android:text="Button"
tools:layout_editor_absoluteX="101dp"
tools:layout_editor_absoluteY="277dp" />
What I'm trying to achieve is a result similar to the Morning Ritual button in this app, for example, if that is a button, which I do not know either.
You can use CardView or If you want to create Button, try this xml drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" android:padding="5dp">
<corners
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp"/>
</shape>
</item>
<item android:drawable="#drawable/yourImage" />
</layer-list>
You can use ImageButton, where android:src - image you need and android:background - rounded shape:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/roundedImageButton"
android:src="#drawable/backgound"
android:text="TEXT"
android:background="#drawable/roundedShape" />
Or CardView:
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp">
<ImageView
android:src="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TEXT"/>
</androidx.cardview.widget.CardView>
you can do all of them dynamically just like this
Button button = findViewById(R.id.button);
button.setText("Test");
button.setBackground(getRoundRect());
and by getRoundRect() you can get rounded corner shape with the color and size you
want
public Drawable getRoundRect() {
RoundRectShape rectShape = new RoundRectShape(new float[]{
10, 10, 10, 10,
10, 10, 10, 10
}, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape);
shapeDrawable.getPaint().setColor(Color.parseColor("#FF0000"));
shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
shapeDrawable.getPaint().setAntiAlias(true);
shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
return shapeDrawable;
}
or if you want to make gradient rounded corner shape you can easily use this code
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(10f);
button.setBackground(gd);
all of my suggestion was for making a button by dynamic text and rounded corners and colors
good luck and have fun (;

Android studio- Adding style to buttons

I want to add a shadow and have rounded edges on my button, like box-shadow and border radius in CSS how can I do this in XML?
XML code:
<Button android:id="#+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="198dp"
android:layout_height="69dp"
android:layout_weight="1"
android:text="#string/dummy_button"
android:fontFamily="trebuchet ms"
android:clickable="true"
android:onClick="printStarter"
android:background="#8c96ff"
android:layout_gravity="center_horizontal|bottom" />
The Android Developer's Guide has a detailed guide on this: Shape Drawbables.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#1a1a1a" />
button with shadow
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shadow"/>
<item
android:drawable="#drawable/button"
android:bottom="4px" />
</layer-list>
</shape>
Refer this tutorial
You can use XML, here are some links:
How to provide shadow to Button ;
http://zrgiu.com/blog/2011/01/making-your-android-app-look-better/ . OR
You can use a 9patch image: http://developer.android.com/tools/help/draw9patch.html

Categories

Resources