i've got a Activity with a ListView. I wrote an extra xml-file for the ListView-Item.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutPalettenItem"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
tools:context=".PalettenAdapter" >
<TextView
android:id="#+id/textViewPalettenBez"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="left|center_vertical"
android:text="Bezeichnung"
android:textColor="#android:color/black"
android:textSize="35sp" />
<Button
android:id="#+id/buttonPalettenPlus"
android:layout_width="65dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:text="+"
android:textColor="#android:color/white"
android:textSize="30sp" />
<TextView
android:id="#+id/textViewPalettenAnzahl"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/buttonPalettenPlus"
android:layout_alignBottom="#+id/buttonPalettenPlus"
android:layout_toLeftOf="#+id/buttonPalettenPlus"
android:gravity="center"
android:text="0"
android:textColor="#android:color/black"
android:textSize="25sp" />
<Button
android:id="#+id/buttonPalettenMinus"
android:layout_width="65dp"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/textViewPalettenAnzahl"
android:layout_alignBottom="#+id/textViewPalettenAnzahl"
android:layout_toLeftOf="#+id/textViewPalettenAnzahl"
android:text="-"
android:textColor="#android:color/white"
android:textSize="30sp" />
</RelativeLayout>
As you can see the xml includes two Buttons (+ and -). Everything works fine and the item loads in the list. Also the Buttons are clickable but their color is darker. I set the background to #android:drawable/btn_default_holo_light. They look fine in layout but the Buttons stay darker, when i launch the app.
Let me know if you need more code on this.
I would be very glad if you could help me solve this issue
The default button background color of Theme.MaterialComponents is defined in your themes.xml file with the attribute colorPrimary eg: <item name="colorPrimary">#color/gray</item> but changing this color will affect all your widgets.
You can use app:backgroundTint attribute to change the button background color like below:
<Button
android:layout_width="50dp"
android:layout_height="50dp"
app:backgroundTint="#android:color/holo_blue_dark"/>
You have to add also the xmlns:app namespace on your root xml View like below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
Result with the above colour #android:color/holo_blue_dark using Theme.MaterialComponents will be like below:
Related
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.
i tried to change the dropdownlabel / selector of a AndroidAppCombatSpinner before but did not really get useful help see: Make Drowdown label smaller.
So now I am wondering if it is possible to make a Spinner without a dropdown label.
See the Screenshot for what I want to do:
Edit: Added my XML file which is getting inflated:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="35dp"
android:orientation="horizontal"
android:background="#drawable/x_linearlayoutcontainer_inside"
>
<ImageView
android:id="#+id/ivSpinnerImage"
android:layout_marginTop="3dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp"
android:layout_width="0dp"
android:layout_height="29dp"
android:layout_weight="2.5"
android:cropToPadding="true"
android:scaleType="fitCenter"
android:src="#drawable/blue"
/>
<TextView
android:id="#+id/tvSpinnerText"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="7.5"
android:gravity="center"
android:padding="5dp"
android:text="Accessory"
android:autoSizeMinTextSize="12sp"
android:autoSizeMaxTextSize="15sp"
android:textColor="#fff"
/>
</LinearLayout>
Thanks guys for any suggestions!
try to change the style of your spinner to this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style parent="#android:style/Widget.Spinner" name="SpinnerwithNoArrow">
<item name="android:background">#android:drawable/edit_text</item>
</style>
</resources>
I am having a very weird issue in Android Studio. I've got a simple layout with an EditText object, and a button. When I go to assign the attribute android:background="some_color" to the button, the entire layout color becomes faded and looks white. When I delete the attribute, the background color goes back to normal. Screenshots and explanation:
Code before the background color is added to the button:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/clarksonGreen">
<ImageView
android:id="#+id/EnterRoomNumberBackgroundImage"
android:layout_width="437dp"
android:layout_gravity="center"
android:layout_height="526dp"
android:alpha="0.1"
app:srcCompat="#drawable/knight" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:weightSum="1">
<EditText
android:id="#+id/EnterRoomNumber"
android:layout_width="246dp"
android:background="#drawable/enter_room_number_box"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="55dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Enter Room #"
android:textColorHint="#808080"
android:textAlignment="center"
android:inputType="number"
android:layout_weight="0.09" />
<Button
android:id="#+id/GoButton"
android:layout_width="271dp"
android:layout_height="80dp"
android:text="Button" />
</LinearLayout>
</FrameLayout>
Preview before the background color is added to the button.
Code after the background color is added to the button:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/clarksonGreen">
<ImageView
android:id="#+id/EnterRoomNumberBackgroundImage"
android:layout_width="437dp"
android:layout_gravity="center"
android:layout_height="526dp"
android:alpha="0.1"
app:srcCompat="#drawable/knight" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:weightSum="1">
<EditText
android:id="#+id/EnterRoomNumber"
android:layout_width="246dp"
android:background="#drawable/enter_room_number_box"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="55dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Enter Room #"
android:textColorHint="#808080"
android:textAlignment="center"
android:inputType="number"
android:layout_weight="0.09" />
<Button
android:id="#+id/GoButton"
android:layout_width="271dp"
android:background="#color/clarksonGreen"
android:layout_height="80dp"
android:text="Button" />
</LinearLayout>
</FrameLayout>
Preview after background color is added to button.
Any ideas how to fix this weird error?
Could it be that you didn't assign the colour variable "clarksonGreen" in the Resource file Colors? If yes you should go to the Color variables and put in the name of the colour("clarksonGreen") and assign a colour(example:#e55656).
First off, I cannot recreate the issue. Assuming that you have the color in your color file, I see nothing wrong.
You could try rebuilding the project, applying the color to the ImageView too, updating AndroidStudio or launching it on an Emulator to see if it does this there too.
If it doesn't work, please upload the whole code to GitHub so I can check it out more closely.
We have a relatively simply Android item template on a ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:background="#android:drawable/list_selector_background">
<!--Encounter Type and Start-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/mid_grey"
android:paddingTop="12dp"
android:paddingRight="24dp"
android:paddingBottom="12dp"
android:paddingLeft="24dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:textSize="#dimen/text_size_large"
android:text="Main Item Heading"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:45"
android:layout_toLeftOf="#+id/startTimeRelative"
android:textColor="#color/grey1"
android:textSize="#dimen/text_size_large" />
<TextView
android:id="#+id/startTimeRelative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12sp"
android:layout_alignParentRight="true"
android:text="Today"
android:textColor="#color/grey1"
android:textSize="#dimen/text_size_large" />
</RelativeLayout>
<!--Patient Name and Avatar-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingRight="24dp"
android:paddingBottom="8dp"
android:paddingLeft="24dp">
<ImageView
android:id="#+id/patientAvatar"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:scaleType="fitXY"
android:src="#drawable/patient_avatar" />
<LinearLayout
android:layout_toRightOf="#+id/patientAvatar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/patientname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Subsection Heading"
android:textColor="#000000"
android:textSize="#dimen/text_size_large"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="1" />
</LinearLayout>
</RelativeLayout>
<!--</LinearLayout>-->
</LinearLayout>
That displays this layout
I want to set the background of the items when they are pressed or selected. I am trying to do this by setting the item's background to a custom item selector
android:background="#android:drawable/list_selector_background"
The is is the selector xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/debug_blue" />
<item android:state_checked="true" android:drawable="#color/debug_green" />
<item android:state_pressed="true" android:drawable="#color/debug_blue" />
</selector>
The problem is that because I am setting the background colour's of Views within the item the Selector does not show through as expected. I have removed one of the inner colours (used in the "Subsection Heading" in the screenshot) and set that as the colour of the Listview. That means the blue shows through when the item is pressed on the lower part of the template, as seen in this screenshot
All the Android examples showing item selectors working with simple item layouts that have one or no background colour set.
How can I set the whole item's background whilst still having one or more background colours on child view's, when not selected?
I have looked at setting drawSelectorOnTop on the ListView but that did not help
There is simple xml-layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="5dp" >
<Button
android:padding="15dp"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shadowColor="#android:color/black"
android:shadowDy="10"
android:shadowRadius="5"
android:text="Button" />
</LinearLayout>
When I set this as content view for activity, I see black shadow below text of my button. BUT if I change android:shadowColor="#android:color/black" to android:shadowColor="#android:color/white" - I see no shadow anymore. Why?
Just ran into this myself. For me it happened when the background was bright, and the blending of the white shadow on the bright background was not noticable. I changed the android:shadowRadius to be small (0.1) and that solved the problem.