How to Remove Background Resource of Button

How to remove button background resource

Give this a try:

btn.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default));

Remove background drawable programmatically in Android

Try this

RelativeLayout relative = (RelativeLayout) findViewById(R.id.widget29);
relative.setBackgroundResource(0);

Check the setBackground functions in the RelativeLayout documentation

How to remove ImageButton's standard background image?

You can use android:background="@null" for your ImageButton.

See also:

    Android Developers official guide on Buttons

Removing deafult background of ImageButton in Android but keep onClick highlight

You really need to use a selector for this.

You can use colors for your backgrounds in your selectors, it doesn't need to be a PNG resource (just set the background to a color, instead of a drawable resource).

You will place this code in your drawable folder, with a specific name (like button_sel.xml).

Then you set this as the background to your button in your XML it would look like this:

android:background="@drawable/button_sel"

The selector would look something like this:

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/ab_background"
android:state_focused="true"
android:state_pressed="true"/>
<item
android:drawable="@color/ab_background"
android:state_focused="false"
android:state_pressed="true"/>
<item
android:drawable="@color/ab_background_on"
android:state_focused="true"
android:state_pressed="false"/>
<item
android:drawable="@color/ab_background_on"
android:state_focused="false"
android:state_pressed="false"/>
</selector>

Remove background color of button pressed and replace with image in Windows Phone 8.1

This 'horrible' blue background comes with default button style. If you take a look at it:

<Style x:Key="ButtonStyle1" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{ThemeResource PhoneForegroundBrush}"/>
<Setter Property="Foreground" Value="{ThemeResource PhoneForegroundBrush}"/>
<Setter Property="BorderThickness" Value="{ThemeResource PhoneBorderThickness}"/>
<Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}"/>
<Setter Property="FontWeight" Value="{ThemeResource PhoneButtonFontWeight}"/>
<Setter Property="FontSize" Value="{ThemeResource TextStyleLargeFontSize}"/>
<Setter Property="Padding" Value="{ThemeResource PhoneButtonContentPadding}"/>
<Setter Property="MinHeight" Value="{ThemeResource PhoneButtonMinHeight}"/>
<Setter Property="MinWidth" Value="{ThemeResource PhoneButtonMinWidth}"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="Grid" Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition From="Pressed" To="PointerOver">
<Storyboard>
<PointerUpThemeAnimation Storyboard.TargetName="Grid"/>
</Storyboard>
</VisualTransition>
<VisualTransition From="PointerOver" To="Normal">
<Storyboard>
<PointerUpThemeAnimation Storyboard.TargetName="Grid"/>
</Storyboard>
</VisualTransition>
<VisualTransition From="Pressed" To="Normal">
<Storyboard>
<PointerUpThemeAnimation Storyboard.TargetName="Grid"/>
</Storyboard>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<PointerDownThemeAnimation Storyboard.TargetName="Grid"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonPressedForegroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonPressedBackgroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledForegroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledBorderThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledBackgroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{ThemeResource PhoneTouchTargetOverhang}">
<ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

you will find those lines, which produce this unwanted effect:

<VisualState x:Name="Pressed">
<Storyboard>
<PointerDownThemeAnimation Storyboard.TargetName="Grid"/> <!-- leave this -->
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonPressedForegroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonPressedBackgroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>

disable them (apart PointerDownThemeAnimation, which produces nice animation when button is pressed) and the VisualStateMenager won't change the background anymore.

Kotlin remove button's background tint

I asume you are using a normal Button and then you set the style, don't do it:

style="@style/Widget.AppCompat.Button.Colored"

Or maybe you are using a MaterialButton.

Anyway, the documentation says to don't change the background:

All attributes from MaterialButton are supported. Do not use the
android:background attribute. MaterialButton manages its own
background drawable, and setting a new background means MaterialButton
can no longer guarantee that the new attributes it introduces will
function properly. If the default background is changed,
MaterialButton cannot guarantee well-defined behavior.

If you want to use a custom background, as I see you have a drawable for this purpose, use just a normal Button.

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/myBackground"/>

Edit:

Your theme style it's making the button to be a MaterialButton so now you can do two things:

-> You can change the full theme to a normal one:

<style name="Theme.TestFragmentUpdate" parent="Theme.AppCompat.Light.DarkActionBar">

This will change the full theme of your app.

-> You can change just the button theme

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.TestFragmentUpdate" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/bluish_grey</item>
<item name="colorPrimaryVariant">@color/dark_teal</item>
<item name="buttonStyle">@android:style/Widget.Button</item>

<!-- <item name="colorOnPrimary">@color/black</item>-->
<!-- Secondary brand color. -->
<!-- <item name="colorSecondary">@color/teal_200</item>-->
<!-- <item name="colorSecondaryVariant">@color/teal_200</item>-->
<!-- <item name="colorOnSecondary">@color/black</item>-->
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>

Also, I think it could work that you set this concret style (The default Style) to the button and after just set the background you need so you don't modify the full theme:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.Button"
android:background="@drawable/myBackground"/>


Related Topics



Leave a reply



Submit