Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – How to change the style of a single view within a fragment

Posted on November 14, 2022

Solution 1 :

In your case you can use:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    app_tint="@null"
    app_srcCompat="@drawable/flag_united_states_of_america"
    ..>

The FloatingActionButton in the Material Components uses the colorOnSecondary attribute defined in your app theme to tint its icon (and the default value is #000000). If you don’t want to tint the icon you have to use app:tint="@null".

In general if you want to use a custom style in your FAB you can use:

  <com.google.android.material.floatingactionbutton.FloatingActionButton
      style="@style/MyCustomFab"
      ..>

with:

   <style name="MyCustomFab" parent="@style/Widget.MaterialComponents.FloatingActionButton>
      .....
      <item name="backgroundTint">@color/....</item>
      <item name="tint">@color/....</item>
   </style>

If you want to override the attribute defined in your app theme you can use:

  <com.google.android.material.floatingactionbutton.FloatingActionButton
      android_theme="@style/fab_themeoverlay"
      ..>

with:

   <style name="fab_themeoverlay" parent="@style/Widget.MaterialComponents.FloatingActionButton>
      <item name="colorPrimary">@color/...</item>
   </style>

Solution 2 :

If you want to change the theme of the button, which has implications for how it is inflated, you should use android:theme instead of android:style.

Solution 3 :

Both Ben P. and Gabrielle were semi-correct. The answer required both a change in the theme via:

android:theme="@style/SecondTheme"

and setting the tint to null:

app:tint="@null"

Both were required in this case

All together, given two themes, this is the correct XML for a FloatingActionButton if you want to change it’s entire background to a vector while your primary theme is MaterialComponents as referenced in your app’s manifest:

<com.google.android.material.floatingactionbutton.FloatingActionButton
          android_layout_width="wrap_content"
          android_layout_height="wrap_content"
          android_layout_margin="10dp"
          app_borderWidth="0dp"

          app_srcCompat="@drawable/your_vector"
          app_tint="@null"
          android_theme="@style/SecondTheme"
          app_maxImageSize="56dp" />

Thank you Gabrielle and Ben for your help!

Problem :

Goal: I am attempting to apply two different themes in one Fragment. One for the fragment’s overall xml and the other for a particular view within that fragment’s xml.

Reason/Problem: The reason for this is that it doesn’t seem possible to change the entire background of a Floating Action Button to a vector using MaterialComponents, but it does work with appCompat.

READ  [ANSWERED] android - How can I export tiles of OpenStreetMap and use it in Flutter Application?
Powered by Inline Related Posts

Initially, I tried to change the theme in the xml using style=”…” as shown below, but it appears to be reverting back to the theme declared in the manifest which is AppTheme1. I know this because I tested it by switching the theme declared there. That is, when I switched it to AppTheme2, the vector loaded properly in the FAB, but it crashed elsewhere since I have MaterialComponents throughout the app.

Solution: I think the obvious solution for this would be to change the theme for just the Floating Action Button in question, but I don’t know how to do that. Any help much appreciated. Thank you!

         <com.google.android.material.floatingactionbutton.FloatingActionButton
                android_id="@+id/nationality"
                android_layout_width="wrap_content"
                android_layout_height="wrap_content"
                android_layout_alignParentRight="true"
                android_layout_margin="10dp"
                android_scaleType="fitXY"
                app_srcCompat="@drawable/flag_united_states_of_america"
                app_borderWidth="0dp"
                style="@style/AppTheme2"
                app_maxImageSize="56dp" />

Themes:

<style name="AppTheme1"  parent="Theme.MaterialComponents.Light.NoActionBar" >

&&

<style name="AppTheme2"  parent="Theme.AppCompat.Light.NoActionBar" >

With MaterialComponents:

MaterialComponentsVersion

With AppCompat:

enter image description here

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns_android="http://schemas.android.com/apk/res/android"
    package="com.example.fabtest">

    <application
        android_allowBackup="true"
        android_icon="@mipmap/ic_launcher"
        android_label="@string/app_name"
        android_roundIcon="@mipmap/ic_launcher_round"
        android_supportsRtl="true"
        android_theme="@style/AppTheme">
        <activity android_name=".MainActivity">
            <intent-filter>
                <action android_name="android.intent.action.MAIN" />

                <category android_name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

So when the theme is changed from AppCompat to Material Components, the image resource no longer applied properly to the Floating Action Button. So I just want to apply AppCompat for Floating Action Button but keep the Material Components as the main style.

Comments

Comment posted by Gabriele Mariotti

The style of a fab is not a theme. Really it is not clear what you are trying to achieve

Comment posted by M A F

@GabrieleMariotti Alright, I will reword.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress