Having an issue coloring custome xml button - android

I'm having an issue coloring a custom button. For some reason, it seems like no matter what coloring change I want to apply (text or background) the button remains the same.
I notice the button.xml has the desired color and the right shape though it doesn't appear the button background-color property on the activity
the button from an activity
<Button
android:id="#+id/button2"
android:layout_width="162dp"
android:layout_height="53dp"
android:background="#drawable/button"
android:text="#string/button"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.132"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/messagTextView"
app:layout_constraintVertical_bias="0.804" />
custome button shape
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50sp"/>
<solid android:color="#color/redAdobeXD"/>
</shape>

If you are using material components (which is probably the case if this is a relatively new project) then you style the Button in another way:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context=".FirstFragment">
<Button
android:id="#+id/button"
style="#style/Widget.MaterialComponents.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:text="I am a Button!"
android:textColor="#color/white"
app:backgroundTint="#FF0000"
app:cornerRadius="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Things to notice are:
style="#style/Widget.MaterialComponents.Button"
android:textColor="#color/white"
app:backgroundTint="#FF0000" (would be #color/redAdobeXD in your case)
app:cornerRadius="50dp"
There is no need for an extra xml and setting that on the button etc. (only in more custom cases).
You can read more about available options on the ContainedButton here

Related

Android Button shown differently when run

What the title says when I design the button on XML it shows like this
But in the actual application, it looks like this
I expect that when running the app it looks like the XML design at first I though it was because the style wasn't being recognized but that's not the case in other button works just fine
Here's the XML for a better look
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_white"
android:padding="20dp"
tools:context="com.fia2.presentation.tos.application.item.six.fragments.PreSignNotificationFragment">
<ImageView
android:id="#+id/iv_ic_confirmation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_confirmation"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="20dp"
android:text="#string/signature_welcome_title"
android:textColor="#color/color_black"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/iv_ic_confirmation" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="20dp"
android:text="#string/signature_welcome_subtitle"
android:textColor="#color/colorMediumGrey"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv_description" />
<Button
android:id="#+id/btn_start_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/test_background"
android:drawableStart="#drawable/ic_arrow_right"
android:drawableTint="#color/color_white"
android:gravity="center"
android:minWidth="328dp"
android:minHeight="50dp"
android:paddingHorizontal="10dp"
android:text="#string/sign_in_log_in"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/colorWhite"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
You may try to create a drawable with the content like below
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="100dp" />
<solid android:color="#color/your_color" />
</shape>
and set it as the background of your Button.
PS. In case the Button doesn't change the color when you set a new background, you may try to replace it with the androidx.appcompat.widget.AppCompatButton

Drawable file wont change color in Dark Mode

Im currently working on a LoginActivity, I wanted to make an rounded Linear to put the Edittext's inside, I used an XML file to round the corners of them, but the only problem is, that they wont change their color in dark mode.
I already used the colors from the #colors file, but it wont affect at all, I tried to use cardview instead of a Linearview with an drawable file as background, but it breaks the layout.
XML:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/design_default_color_on_primary"/>
<stroke android:width="0dp" android:color="#B1BCBE" />
<corners android:radius="20dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
LoginActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="#drawable/login_bg1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#drawable/loginbg"
android:elevation="25dp"
android:orientation="vertical"
android:gravity="center_horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginVertical="20dp"
android:textStyle="bold"
android:textSize="30sp"
android:textColor="#color/design_default_color_primary"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="44dp"
android:layout_marginStart="44dp"
android:padding="10dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#drawable/edittext_box"
android:elevation="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_email"/>
<EditText
android:id="#+id/Email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="#null"
android:hint="Email"
android:inputType="textEmailAddress"
android:textColorHint="#color/design_default_color_primary"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="44dp"
android:layout_marginStart="44dp"
android:padding="10dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#drawable/edittext_box"
android:elevation="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_password"/>
<EditText
android:id="#+id/Password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="#null"
android:hint="Password"
android:inputType="textPassword"
android:textColorHint="#color/design_default_color_primary"/>
</LinearLayout>
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Login"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Maybe someone knows to fix this issue.
To support dark mode you need a separate colors.xml file for the night version. To create colors file for night mode. Follow these steps.
Right-click on values folder
New > Value Resource File
In the Available Qualifiers search for Night mode and click on the (>>) button.
From the drop-down select Night
Name the file as colors
And define all the night mode colors here
Names of the colors will be the same as the Non-Night colors file
Another way to create colors.xml for night mode is
On the top left corner click on Android and Switch to project,
Go to folder app > src > main > res and create a new folder and name it values-night and inside that folder create an XML file named colors.xml

editText background set but does not show up when application is launched

I have created a login page which has two editText fields for username and password. I have included an ImageView in the activity also.
I have made an XML file to make rounded white background for the editText fields. However, that did not work and was still transparent. On changing the background from the created XML file to simple white colour, I could still not see the editText fields.
Below is the code I have used for the following:
Login.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:onClick="Clicked"
android:padding="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="307dp"
android:layout_height="63dp"
android:layout_marginBottom="17dp"
android:layout_marginEnd="327dp"
android:layout_marginStart="84dp"
android:foreground="#drawable/emailsignup"
android:text="#string/button"
app:layout_constraintBottom_toTopOf="#+id/editTextTextPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.104"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button2"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="#+id/button2"
android:layout_width="293dp"
android:layout_height="57dp"
android:layout_marginEnd="146dp"
android:layout_marginStart="63dp"
android:layout_marginTop="200dp"
android:foreground="#drawable/gmailsignup"
android:text="#string/button2"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toStartOf="#+id/imageView"
app:layout_constraintHorizontal_bias="0.006"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button3"
android:layout_width="88dp"
android:layout_height="48dp"
android:layout_marginBottom="269dp"
android:layout_marginEnd="253dp"
android:layout_marginStart="150dp"
android:layout_marginTop="43dp"
android:background="#drawable/loginbg"
android:fontFamily="monospace"
android:text="#string/LoginText"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/imageView"
app:layout_constraintEnd_toStartOf="#+id/imageView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextTextPassword"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editTextTextPassword"
android:layout_width="228dp"
android:layout_height="51dp"
android:layout_marginBottom="132dp"
android:layout_marginEnd="95dp"
android:layout_marginStart="179dp"
android:layout_marginTop="430dp"
android:autofillHints=""
android:background="#drawable/edittextround"
android:ems="10"
android:fontFamily="sans-serif-medium"
android:hint="#string/PasswordHint"
android:inputType="textPassword"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.978"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="281dp"
android:layout_height="54dp"
android:layout_marginBottom="320dp"
android:layout_marginEnd="153dp"
android:layout_marginStart="58dp"
android:layout_marginTop="88dp"
android:autofillHints=""
android:ems="10"
android:background="#drawable/edittextround"
android:hint="#string/LoginTextHint"
android:inputType="textPersonName"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imageView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:layout_constraintVertical_bias="0.962" />
<ImageView
android:id="#+id/imageView"
android:layout_width="566dp"
android:layout_height="972dp"
android:adjustViewBounds="false"
android:contentDescription="#string/todo"
android:cropToPadding="false"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.344"
app:srcCompat="#drawable/picture1_2" />
</androidx.constraintlayout.widget.ConstraintLayout>
edittextround.xml
<?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"/>
<gradient
android:centerY="0.2"
android:startColor="#D3D3D3"
android:centerColor="#65FFFFFF"
android:endColor="#00FFFFFF"
android:angle="270"
/>
<stroke
android:width="0.7dp"
android:color="#BDBDBD" />
<corners
android:radius="15dp" />
</shape>
I tried putting the edittextbackground.xml on the login button and it worked perfectly.
The images related to this activity are below:
Image of the design
Please help me figure out why my editText fields do not show their backgrounds
ok got it
so the problem is below
app:layout_constraintBottom_toTopOf="#+id/imageView"
app:layout_constraintTop_toBottomOf="#+id/imageView"
you are setting the constraints to imageView and that imageView also renders in the area of Editext but not in the background because it is at same elevation as of Editexts so if you just increase the elevation of of editexts they will show up , i tried it , it works
just add android:elevation="1dp" in both edittexts the imageview is at 0dp so editext will show above it.
Although it works but your constraints are confusing use parent instead
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/button" will also do
also i want to say if you use constraint layout correctly you will not need to set layout_width and layout_height in most cases you can set them to 0 the constraints will determine the correct width and height also you don't need to use margins your constraint will handle them if you use bias, chains, groups ,barrier, guidelines etc correctly , please learn more about constraint layout and you can reduce the lines in above layout file to half.
this tutorial will give you good start, also watch other videos in that series
have a good day

How can I remove button click effect in Android?

Whenever I click on the button it moves in up direction, Like this
After filling the whole layout it behaves normally means if then I remove the text and click again it remains on its position
<Button
android:id="#+id/b1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="0dp"
android:background="#drawable/blackborder"
android:gravity="center"
android:stateListAnimator="#null"
app:layout_constraintBaseline_toBaselineOf="#+id/b2"
app:layout_constraintEnd_toStartOf="#+id/b2">
</Button>
<Button
android:id="#+id/b2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="0dp"
android:background="#drawable/blackborder"
android:gravity="center"
android:stateListAnimator="#null"
app:layout_constraintBaseline_toBaselineOf="#+id/b3"
app:layout_constraintEnd_toStartOf="#+id/b3"></Button>
if you mean the shadow effects that appear in onClick event, you can remove it by making your background solid transparent.
like the example:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/transparent"/>
<stroke android:color="#android:color/black" android:width="1dp"/>
</shape>
This is happening because I used baseline constraint to connect two button placed next to each other but after removing the baseline constraint I got the desired behavior. Now button is remain on its position after a click.
<Button
android:id="#+id/b1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="0dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="#drawable/blackborder"
app:layout_constraintBottom_toTopOf="#+id/b6"
app:layout_constraintEnd_toStartOf="#+id/b2">
</Button>
<Button
android:id="#+id/b2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="0dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="#drawable/blackborder"
app:layout_constraintBottom_toTopOf="#+id/b7"
app:layout_constraintEnd_toStartOf="#+id/b3"></Button>

Android how to set background color on a Button

I am trying to change the background color of a Button. I'm in Kotlin on SDK 21 on emulator.
A View and a Button are declared in the layout XML file
<View
android:id="#+id/myview"
android:layout_width="64dp"
android:layout_height="32dp"
/>
<Button
android:id="#+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12dp"
android:text="test"
/>
The API to set the color doesn't seem to work:
showButton.setBackgroundColor(0xff60a0e0.toInt()) <-- doesnt work
What works is:
myview.setBackgroundColor(0xff60a0e0.toInt()) <-- works, exact background color
showButton.setTextColor(0xff000050.toInt()) <-- works, exact text color
After trying further it seems that I can only set the alpha channel of the button, not the color:
setBackgroundColor( 0xff000000.toInt()) <-- works, opaque
setBackgroundColor( 0x00000000.toInt()) <-- works, transparent
Also same thing with:
showButton.setBackgroundColor(Color.GREEN) <-- doesnt work, button is opaque but not green
showButton.setBackgroundColor(Color.TRANSPARENT) <-- works, button is transparent
Any idea? Did I miss something in the other answers or the documentation?
Here is complete layout, it used to inflate a fragment, if that matters:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:id="#+id/myview"
android:layout_width="64dp"
android:layout_height="32dp"
/>
<Button
android:id="#+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12dp"
android:text="test"
/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/dictionaryEntryRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager"
/>
</LinearLayout>
mButton.setBackgroundColor(ContextCompat.getColor(mContext, R.color.xxx));
Since you are using a Theme.MaterialComponents.Light.DarkActionBar theme, check the doc and just use the MaterialButton with app:backgroundTint attribute:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#color/color_selector"
android:textColor="#FFF"
android:text="BUTTON"
/>
where color_selector can be a color or a selector. Something like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/..." android:state_enabled="true"/>
<item android:alpha="0.12" android:color="#color/..."/>
</selector>
You can change the colour two ways; through XML or through coding. I would recommend XML since it's easier to follow for beginners.
xml
add this attribute to set background color android:background="#000"
<Button
android:id="#+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fgkdjgdjsf"
android:background="#000"
/>
Coding:
showButton.setBackgroundColor(resources.getColor(R.color.colorPrimary))
showButton.setBackgroundColor(Color.BLACK)
In your layout, you are using
<Button
android:id="#+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12dp"
android:text="test"
/>
if you want to set textSize in Button, you should use
android:textSize="12dp"
and for background set in button your layout should be like :-
<Button
android:id="#+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:text="test"
android:background="#ff60a0e0"/>
OR You can also set color in colors.xml as :-
<color name="button_background">#ff60a0e0</color>
and then your button tag in your layout will be as
<Button
android:id="#+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:text="test"
android:background="#color/button_background"/>
Dynamic you can set color as
showButton.setBackgroundColor(ContextCompat.getColor(context!!, R.color.button_background))
now in kotlin(androidx) you can use this,
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/buttonExploreAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:background="#drawable/select_yellow"
android:text="button"
android:textColor="#color/white"
/>
and select_yellow is your style xml.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:color="#color/colorYellow"
android:width="#dimen/_1sdp"/>
<corners
android:radius="#dimen/_200sdp" />
<solid android:color="#color/colorYellow" />
</shape>
I used the "backgroundTint" to change the color, which did not allow the color to change after clicking. The problem was solved when I changed the color by "background" the button.

Categories

Resources