Categories
discuss

Warning : Use SwitchCompat from AppCompat or SwitchMaterial from Material library

What is the difference between these 2 objects (SwitchCompat and SwitchMaterial)? I have tried them and visually they are identical.

By the way, why did they remove the Switch class? Do you know which UI element is supposed to replace it in the future?

Answer

The SwitchMaterial:

  • is provided by the Material Components Library
  • extends the SwitchCompat
  • uses Widget.MaterialComponents.CompoundButton.Switch as default style, using the colors defined in the Theme.MaterialComponents (like colorSecondary, colorSurface and colorOnSurface) and applying the Elevation Overlays in dark mode.

The SwitchCompat:

By the way, why did they remove the Switch class?

The Switch class is not removed. It is provided by the android framework like other widgets as Button,TextView.. and the appcompat and material components libraries provide an updated version of them (like AppCompatButton, MaterialButton…).

There is a different with these widgets.
Using an AppCompat theme there is the AppCompatViewInflater that automatically replaces all usages of core Android widgets inflated from layout files by the AppCompat extensions of those widgets (for example a Button is replaced by AppCompatButton).
Using the Theme.MaterialComponents there is the MaterialComponentsViewInflater that replaces some framework widgets with Material Components ones at inflation time, provided a Material Components theme is in use (for example a Button is replaced by MaterialButton).

It is NOT true for the SwitchMaterial and the SwitchCompat.The reason for that is due to the AppCompat SwitchCompat not actually extending from the framework Switch class.

Categories
discuss

Hilt Fragments must be attached to an @AndroidEntryPoint Activity. Found: class androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity

I got following error when i run my unit test code.

Caused by: java.lang.IllegalStateException: Hilt Fragments must be attached to an @AndroidEntryPoint Activity. Found: class androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity
at dagger.hilt.internal.Preconditions.checkState(Preconditions.java:83)
at dagger.hilt.android.internal.managers.FragmentComponentManager.createComponent(FragmentComponentManager.java:75)
at dagger.hilt.android.internal.managers.FragmentComponentManager.generatedComponent(FragmentComponentManager.java:63)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.generatedComponent(Hilt_HomePage.java:70)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.inject(Hilt_HomePage.java:89)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.initializeComponentContext(Hilt_HomePage.java:53)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.onAttach(Hilt_HomePage.java:45)
at androidx.fragment.app.Fragment.onAttach(Fragment.java:1602)
at com.zhixin.wedeep.homepage.ui.Hilt_HomePage.onAttach(Hilt_HomePage.java:35)
at com.zhixin.wedeep.homepage.ui.HomePage.onAttach(HomePage.kt:281)

This is my test code.

@HiltAndroidTest
@UninstallModules(HomePageDataModule::class)
@RunWith(AndroidJUnit4::class)
@LargeTest
class TestHomePageFragment {

    private val c = Composition("cyrus", "background", "description", "downloadUrl", "1000", "url", "1", true, "100", 100, "100", "test", "title", "1", "100", "cover", ArrayList(), "ONCE", null)

    @Inject
    lateinit var cpd: CompositionDao

    @get:Rule
    var hiltRule = HiltAndroidRule(this)




    @Before
    fun init() {
        hiltRule.inject()
        Util.RETROFIT

        Util.enqueueResponse("mainpage.json")

        cpd.createComposition(c)
        cpd.createBrowseRecord(BrowseRecord(c.id, System.currentTimeMillis()))
        val s = launchFragment<HomePage>()
        s.onFragment {
            IdlingRegistry.getInstance().register(it.mIdleResource)
        }
    /*    dataBindingIdlingResourceRul = DataBindingIdlingResourceRule(s)
        dataBindingIdlingResourceRul.starting(null)*/

    }


    @Test
    fun testDataInitial() {

        onView(ViewMatchers.withId(R.id.recycler_view_preview_data))
                .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(1))
    }

    @After
    fun finish(){

    }

}

Any idea for this question?

Answer

As stated on Hilt guide, “Testing” section:

Warning: Hilt does not currently support FragmentScenario because there is no way to specify an activity class, and Hilt requires a Hilt fragment to be contained in a Hilt activity. One workaround for this is to launch a Hilt activity and then attach your fragment.

The error is happening since hilt-managed fragment should be attached to a hilt-managed Activity as well, in short, both should be annotated with @AndroidEntryPoint. Because FragmentScenario uses an EmptyFragmentActivity to hold the underlying fragment being tested, currently there’s no way to integrate Hilt with FragmentScenario. An workaround is to launch an activity and then attach the fragment to it.

Categories
discuss

Gradient color using MaterialButton and shapeAppearance

I want to make a Button like this one : enter image description here

I try it using style with shapeAppearance :

<style name="ShapeAppearance.Button" parent="ShapeAppearance.MaterialComponents">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopLeft">@dimen/button_corner_radius</item>
    <item name="cornerSizeTopRight">5dp</item>
    <item name="cornerSizeBottomRight">@dimen/button_corner_radius</item>
    <item name="cornerSizeBottomLeft">5dp</item>
</style>

<dimen name="button_corner_radius">40dp</dimen>

I apply the style in my MaterialButton like this :

app:shapeAppearance="@style/ShapeAppearance.Button" 

The result is :

enter image description here

Now, I try to put a linear gradient :

  • #DF2D48 @ 0.0
  • #D72D46 @ 0.2
  • #AF223B @ 0.5
  • #AC2139 @ 1.0

For example :

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#DF2D48"
        android:centerColor="#D72D46"
        android:endColor="#AC2139"
        android:angle="360" />
</shape>

and I apply it using in my Shape style :

<item name="android:background">@drawable/test</item>

But the result is the same. Why ?

Answer

Gradient color using Material Button:

Gradient MaterialButton

MaterialButton : To use a custom drawable background with the MaterialButton background tint attribute should be null ref code: app:backgroundTint=”@null”

 <com.google.android.material.button.MaterialButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:text="@string/button_enabled"

                app:backgroundTint="@null"
                android:background="@drawable/gradient_1"
               />

Gradient with shape: res/drawable/gradient_1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="0"
        android:endColor="#DD8E54E9"
        android:startColor="#CC225AE0" />
        <corners android:topLeftRadius="18dp"
            android:bottomRightRadius="18dp"
            android:topRightRadius="5dp"
            android:bottomLeftRadius="5dp"
            />
        <stroke
            android:width="6dp"
            android:color="#007879E8" />
</shape>
Categories
discuss

How can a Discord bot create a hyperlink in a Discord message in an embed or in general? [closed]

Today I did a lot of research on hyperlinks and how to use them. But everything I found didn’t work as they were probably outdated…

How can my Discord.js bot send a message with a hyperlink? I know I can use .setTitle("Titel") and .setURL("https://...") to make a link in the embed title, but how can I use these hyperlinks elsewhere in the embed like in a field?

Also, it is possible to create a hyperlink just in a guild channel as a normal user?

Can someone help me in general with hyperlinks, or provide useful documentation which is not outdated?

Answer

Jakye is right. Only bots can achieve this (but not in field titles, beware). Just do [link text here](url here).

"Track cases globally, or in a country, by supplying 2-letter [country codes](https://countrycode.org/)."

This embed description results in:

this embed description

this in the embed. Clicking on it directs you to countrycode.org. Using .addField():

yourEmbed.addField("Title here, no hyperlinks allowed", "Main text here, so you can put a hyperlink here [like so.](https://example.com)");

If you want a bot’s message to just be a hyperlink, you need to make an embed, and only set the description, hyperlinking as shown above. (you can set the embed color if you want too of course)

Categories
discuss

What is the difference between Clock.systemUTC() and Clock.systemDefaultZone()?

I’m looking at the Clock class from java so that I can inject it as a dependency for better testability. But I don’t understand the difference between the two methods systemUTC and systemDefaultZone. The Instant.now() seems to be internally using systemUTC clock whereas YearMonth.now() uses the systemDefaultZone clock. I already read the javadoc but it wasn’t very clear to me.

I have code that use Instant.now() and YearMonth.now() and would like to understand the difference between the two clocks with some examples where using each the clocks wrongly can lead to a problem.

Answer

In terms of the instant method of the two clocks returned by Clock.systemUTC and Clock.systemDefaultZone, they will do the same thing. Both of their documentation says that the clocks are “based on the best available system clock”. So creating Instants using the clocks will always give you the same instant.

The difference in behaviour comes when you try to create a date or time. systemUTC does this:

Conversion from instant to date or time uses the UTC time-zone.

Whereas systemDefaultZone does this:

converting to date and time using the default time-zone.

So although both will return the same instant if you ask them what the current instant is, they will not necessarily return the same LocalDateTime, or LocalDate, or ZonedDateTime. For example, suppose my system timezone is Asia/Shanghai, which is at an offset of UTC+8 all year round. And it is now 5 am on 2020-07-29 in Shanghai. If I do LocalDate.now(Clock.systemUTC()), it will tell me 2020-07-28, because it’s still the 28th in the UTC timezone. If I do LocalDate.now(Clock.systemDefaultZone()), however, it will tell me 2020-07-29, because it is 2020-07-29 in Shanghai (which is the system timezone).

Now you should see why the parameterless now of date/time related classes use systemDefaultZone. It would be weird if I did LocalTime.now() in Shanghai and saw a time that’s 8 hours earlier!

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..