Why button is displaying the wrong color? - java

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"

Related

background is not showing up on android studio button [duplicate]

This question already has answers here:
Android button background is taking the primary color
(6 answers)
Closed 10 months ago.
I have set it up to where my button's style is borderless and I'm using a drawable for the button background android:background="#drawable/circle"
This is the code for the #drawable/circle 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="1000dp"/>
<solid android:color="#263238"/>
</shape>
</item>
</selector>
and that color (#26338) does not appear on the button instead the button looks like this
what it looks like in design mode
In case you're wondering heres the XML code for the button
<Button
android:id="#+id/clearBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:backgroundTint="#color/red"
android:fontFamily="#font/oswald_medium"
android:text="#string/clear"
android:textColor="#FFFFFF"
android:textSize="24sp" />
When in reality it should be more like this color
this is the color I'm looking for
Some more additional info is that my button is in a TableRow inside of a tableLayout. Also when I try applying a backgroundTint it also does not work. Am using the latest version of android studio and I'm pretty sure everything else is up to date.
How can I fix this? Thanks.
That is Button bug.
change Button
to androidx.appcompat.widget.AppCompatButton
use bellow xml for create button background.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp"/>
<solid android:color="#263238"/>
</shape>
this code for use drawable with button.#drawable/circle is your background drawable name.
<Button
android:id="#+id/clearBTN"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:fontFamily="#font/oswald_medium"
android:text="#string/clear"
android:textColor="#FFFFFF"
android:textSize="24sp" />
I had the same problem with Buttons.
use AppCompatButton instead using Button.
package : androidx.appcompat.widget.AppCompatButton

How to set the background color of a button using a layout as the background

layout_bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<!--<stroke android:width="3dp" android:color="#B1BCBE" />-->
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
main_activity.xml
<ImageButton
android:background="#drawable/layout_bg"
android:src="#drawable/myicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#android:color/black"
android:minWidth="80.0dp"
android:minHeight="80.0dp"
android:padding="10px"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:id="#+id/btnFoo"
android:layout_marginLeft="91.0dp"
/>
I am setting the background of my ImageButton to a layout so I can have dynamic rounded corners as you can see in this image:
However, I need to change the background color of just that button to another solid color such as red.
If I set the background color with btnFoo.SetBackgroundColor(Color.Red), then the rounded corners are gone.
I assume because it overwrote the background attribute to be a solid color value rather than the layout_bg.xml file.
How can I set the background color of this specific ImageButton and not all the ImageButtons in my main_activity that use layout_bg as the background?
You need to make another drawable with the color Red then use it :
btnFoo.setBackgroundResource(R.drawable.red_drawable);
use material button all functionality is built in already
<com.google.android.material.button.MaterialButton
android:layout_width="size"
android:layout_height="size"
android:backgroundTint="#color/colorPrimary"
android:text="Add Images"
android:textAllCaps="false"
android:textColor="#color/white"
app:cornerRadius="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="20dp"/>
You can use something like:
btnFoo.background.colorFilter =
BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
color, BlendModeCompat.SRC_ATOP)
Note: it requires androidx.core:core:1.2.0
As alternative you can use the MaterialButton :
<com.google.android.material.button.MaterialButton
android:layout_width="80dp"
android:layout_height="80dp"
android:insetTop="0dp"
android:insetBottom="0dp"
app:icon="#drawable/ic_add_24px"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:iconSize="48dp"
android:backgroundTint="#color/..."
android:textColor="#FFFFFF"
app:cornerRadius="10dp"/>
In this case just use:
button1.backgroundTintList = ContextCompat.getColorStateList(this, R.color....)

EditText Insertion Point same color as text (#null not working)

I want to make the EditText Cursor color same as text but:
android:textCursorDrawable="#null"
isn't working.
I changed it using the primeryAccent color in the themes(style) but would prefer if it just takes the color of text(so I wouldn't make many styles for each editText). Am I missing something?
The editText:
<EditText
android:id="#+id/search_value"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent"
android:inputType="text"
android:hint="Search People..."
android:layout_toRightOf="#id/arrow_search_indicator"
android:layout_alignTop="#id/search_container"
android:layout_alignEnd="#id/search_container"
android:layout_alignBottom="#id/search_container"
android:layout_marginStart="12dp"
android:textColor="#color/white"
android:textColorHint="#color/search_hint_blue"
android:textCursorDrawable="#null"
android:maxLines="1"
android:imeOptions="actionDone"/>
Edited here:
Image:
That pink thing(Sorry if i misnamed it)
Edittext cursor color you want changes your color.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="#drawable/color_cursor"
/>
Then create drawalble xml: color_cursor
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="3dp" />
<solid android:color="#FFFFFF" />
</shape>

Android - Implement a Instagram-like Edittext box

I am working on an Android app and I am going to implement a EditBox which will very like the Instagram one showing below:
Like the screen capture below, the area marked by the red line is a whole edit box (which means click anywhere, even the image, the editbox will get focus and keyboard will show up.) but only the right side of the blue line is the input text area.
Instagram Editbox http://snag.gy/CTWsK.jpg
The code I currently have is pretty trivial
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="1" >
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/rounded_edittext"
android:hint="Ho shot man"
android:inputType="textMultiLine"
android:layout_weight="1" />
</LinearLayout>
and drawable is defined as:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
I am not sure whether there is a layout cascade workaround I could do to make the editbox like the Instagram one, or if I need to extend the editbox to change its behavior? Pointers or code sample will be greatly helpful, thank you!
Try like this
<EditText
android:id="#+id/editthis"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/editbox"
android:hint="this this this"
android:padding="5dip"
android:singleLine="true"
android:textColor="#999999"
android:textSize="14dip"
android:drawableLeft="#drawable/ic_launcher" />
Drawable left the icon will be set in left side

Android Custom button with imageview and textview inside?

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

Categories

Resources