Android Question Grdient to All Activity

DonManfred

Expert
Licensed User
Longtime User
You need to use the same code in any Activity to do so.

Alternatively you can use create a Class to do it and use this class in all activities.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Actually it's possible to use the manifest also.
If to take Erel's AppCompat sample ...
Inside <style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> add
<item name="android:windowBackground">@drawable/mygradient</item>

In res\drawable create READ-ONLY mygradient.xml.
For example:
B4X:
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#3B5998"
        android:endColor="#00000000"
        android:angle="45"/>   
</shape>
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
<item name="android:windowBackground">@drawable/mygradient</item>
@Semen Matusovskiy : I think you are a genius, but sometimes it is hard to understand your answers. You need to come down a little bit to our level. Where does the line:
<item name="android:windowBackground">@drawable/mygradient</item>
need to be inserted when the manifest is the default:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="22"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Download well-known Erel's sample
https://www.b4x.com/android/forum/attachments/appcompatexample-zip.56248/

Open the manifest. After the default text you will see the description of AppCompat theme.
B4X:
SetApplicationAttribute(android:theme, "@style/MyAppTheme")

CreateResource(values, theme.xml,
<resources>
    <style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#0098FF</item>
        <item name="colorPrimaryDark">#007CF5</item>
        <item name="colorAccent">#AAAA00</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>
</resources>
)

Insert <item name="android:windowBackground">@drawable/mygradient</item> before </style>.
Then create mygradient.xml inside <your app>\Objects\\res\drawable folder and mark this file as read-only.
 

Attachments

  • GradientBackground.zip
    11.7 KB · Views: 211
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Well, let's look empty project.
Add
B4X:
CreateResource(values-v14, theme.xml,
<resources>
    <style name="DarkTheme" parent="@android:style/Theme.Holo">
    <item name="android:windowBackground">@drawable/mygradient</item>
    </style>
</resources>
)
CreateResource(values-v20, theme.xml,
<resources>
    <style name="DarkTheme" parent="@android:style/Theme.Material">
    <item name="android:windowBackground">@drawable/mygradient</item>
    </style>
</resources>
)
after default text.
 

Attachments

  • GradientBackground2.zip
    8.3 KB · Views: 230
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Briefly, yes - this is possible not for buttons only and not very complex. But it's necessary to look <sdk>platforms\android-xx\data\res\values folder.
I will make a sample a little later.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I made a sample. Doesn't require to create something in drawable.

So, we take empty project and add to manifest.

1) The description of an activity background

B4X:
CreateResource (drawable, mygradient.xml,
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <gradient android:angle="90" android:endColor="#4a80ec" android:startColor="#14316b" android:type="linear" />       
    </shape>
    )

2) The description for a button background.
I described two states - pressed and other. You can add, for example, for "focused".
I made simple colors. Actually it's possible to make very interesting things.

B4X:
CreateResource (drawable, mybutton.xml,
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">
            <shape>
                <gradient android:angle="90" android:startColor="#c25e00" android:endColor="#fb8c00" />
            </shape>
        </item>
        <item>
            <shape>
                <gradient android:angle="90" android:endColor="#ff009f" android:startColor="#1a2b5d" />
            </shape>
        </item>
    </selector>
    )

3) Now time to describe a theme.
For API 14+

B4X:
CreateResource (values-v14, theme.xml,
    <resources>
        <style name="DarkTheme" parent="@android:style/Theme.Holo">   
            <item name="android:windowBackground">@drawable/mygradient</item>   
            <item name="android:buttonStyle">@style/ButtonAppTheme</item>
        </style>
        <style name="ButtonAppTheme" parent="android:Widget.Holo.Button">
            <item name="android:background">@drawable/mybutton</item>
        </style>
    </resources>
    )

For API 20+
B4X:
CreateResource (values-v20, theme.xml,
    <resources>
        <style name="DarkTheme" parent="@android:style/Theme.Material">
            <item name="android:windowBackground">@drawable/mygradient</item>
            <item name="android:buttonStyle">@style/ButtonAppTheme</item>
        </style>   
        <style name="ButtonAppTheme" parent="android:Widget.Material.Button">
            <item name="android:background">@drawable/mybutton</item>
        </style>
    </resources>
    )

The difference in names only. The most important tag here <item name="android:buttonStyle">
This name is fixed inside Theme.Holo and Theme.Material (you can't change it)
 

Attachments

  • test.zip
    8.4 KB · Views: 225
Last edited:
Upvote 0
Top