android mobile theme developing - android

I want to develop a theme for android mobile here the code which i found some where in site but it doesn't work.
My code for XML in /res/values/styles.xml.
<resources>
<!-- Base application theme is the default theme. -->
<style name="Theme" parent="android:Theme">
</style>
<!-- Variation on our application theme that has a translucent
background. -->
<style name="Theme.Translucent">
<item name="android:windowBackground">#drawable/translucent_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
<!-- Variation on our application theme that has a transparent
background; this example completely removes the background,
allowing the activity to decide how to composite. -->
<style name="Theme.Transparent">
<item name="android:windowBackground">#drawable/transparent_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
<style name="TextAppearance.Theme.PlainText" parent="android:TextAppearance.Theme">
<item name="android:textStyle">normal</item>
</style>
</resources>
WidgetActivity:
public class WidgetActivity extends Activity {
#Override protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
Log.i(Global.TAG2, "sub-Activity WidgetActivity being creation: start");
setTheme(R.style.Theme_Translucent);
setContentView(Panel2Builder.createWidgetPanel(this));
Log.i(Global.TAG2, "sub-Activity WidgetActivity being creation: end");
}
}//end class WidgetActivity
this is not work as i expected.
can any one have idea to work on theme for android and make it by developing.

Related

How to have a new activity start with the action bar not visible using a theme?

Why doesn't this style I have result in the action bar not being displayed?
My activity is descended from appcompat. I assume these are all the correct properties.
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="Theme.AppCompat.Light">
<item name="android:windowFullscreen">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
it is taking effect, cause the status bar gets removed. But the action bar doesn't seem to get affected.
I can still successfully remove the action bar like this:
public class MainMenuScreen extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main_menu_screen);
but i have to admit I'm a little bit disappointed that I can't do it from xaml.
Can you show your manifest? Where do you associate the theme with the activity?
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="BaseTheme" />
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/cobalt_light</item>
<item name="colorPrimaryDark">#color/cobalt</item>
<item name="colorControlHighlight">#color/cobalt_lighter</item>
<item name="colorAccent">#color/cobalt_light</item>
</style>
</resources>
You dont need to extends from parent Theme.AppCompat.Light

How to Switch theme for an activity

So here is the situation:
I have DogActivity and FavoritesActivity. DogActivity is just a ListView. When you click on a Dog in the list, it takes you to FavoritesActivity.
I want to have a number of themes ready to go. They don’t need to be dynamically generated. They can already exist in XML form.
Depending on which dog the user selects from the list, I want to have the FavoritesActivity shown in one of my pre-existing themes.
I hear talks about ContextWrapper, but I am not sure how to apply it. Any thoughts on how I may accomplish this?
Details:
Here is the usual single theme:
for v21/styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorControlHighlight">#color/colorAccentLight</item>
<item name="android:colorControlNormal">#color/colorAccent</item>
<item name="android:itemTextAppearance">#style/AppTheme.itemTextStyle</item>
<item name="popupMenuStyle">#style/PopupMenu.MyAppTheme</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:colorControlHighlight">#color/colorAccentLight</item>
</style>
<style name="AppTheme.itemTextStyle" parent="#android:style/TextAppearance.Widget.IconMenu.Item">
<item name="android:textColor">#color/colorPrimary</item>
</style>
</resources>
for styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Want I want to do:
Essentially I just want to change the colorPrimary, colorPrimaryDark and colorAccent on the fly and have all the styles and themes and XML layouts that use them to change. So if I can change those colors before I launch FavoritesActivity then that would solve my problems.
You can just send the dog type as an Intent extra, and then use the setTheme() method to set the appropriate Theme.
For this example, suppose you just have two Themes:
<style name="AppThemeOne" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppThemeTwo" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimaryCustom</item>
<item name="colorPrimaryDark">#color/colorPrimaryDarkCustom</item>
<item name="colorAccent">#color/colorAccentCustom</item>
</style>
Then, in DogActivity, set an Intent Extra to the Dog type the user selected from the ListView:
Intent intent = new Intent(DogActivity.this, FavoritesActivity.class);
intent.putExtra("dog_type", "terrier");
startActivity(intent);
Then, in FavoritesActivity, load the correct Theme:
#Override
protected void onCreate(Bundle savedInstanceState) {
String dogType = getIntent().getStringExtra("dog_type");
if (dogType.equals("terrier")) {
setTheme(R.style.AppThemeOne);
} else {
setTheme(R.style.AppThemeTwo);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites_layout);
//.....
}
I've accomplish it quite simply on my latest project, you just have to set the values on the theme via Java. Like the following code:
public class FavoritesActivity extends AppCompatActivity { // it can be Activity too
#Override
public void onCreate(Bundle savedInstanceState) {
if( ... check condition to change theme ) {
// this will replace every value from FavoritesActivity theme by the
// the values on `other_style` theme.
getTheme().applyStyle(R.style.other_style, true);
}
// call super AFTER applying the theme
super.onCreate(savedInstanceState);
.. carry on your normal stuff
}
that's very useful because you can very easily replace just a few values and keep the rest as the original, or change everything from the original. It all depends what argument you pass to the applyTheme method.
Also is great you don't have to mock with ContextThemeWrapper. The values are there on the theme and that's it.
https://developer.android.com/reference/android/content/res/Resources.Theme.html#applyStyle(int, boolean)
You can use this.recreate() method for this.
Based on https://stackoverflow.com/a/29722976/7547681 answer.

Extracting common attributes from a Dark and Light theme

I have a dark theme and a light theme in my app where the dark theme extends Theme.AppCompat.NoActionBar and the light theme extends Theme.AppCompat.Light.NoActionBar.
I have a lot of custom attributes in those light and dark theme that i have to duplicate in both those themes.
I was wondering if there is a way to actually have all those attributes in a common place and be used by both light and dark theme.
Example below to make things even more clear:
<style name="MotherThemeDark" parent="Theme.AppCompat.NoActionBar">
<!-- Base application theme common to all sdk versions -->
<!--Colors-->
<item name="colorPrimaryDark">#color/brand_dark</item>
<item name="colorPrimary">#color/brand</item>
<item name="colorAccent">#color/brand_accent</item>
<item name="colorPositive">#color/positive</item>
<item name="colorPositiveDark">#color/positive_dark</item>
<item name="colorNegative">#color/negative</item>
<item name="colorNegativeDark">#color/negative_dark</item>
<item name="colorNegativeLight">#color/negative_light</item>
<item name="colorMedium">#color/medium</item>
....
...Things specific to this theme goes here
....
</style>
<style name="MotherTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Base application theme common to all sdk versions -->
<!--Colors-->
<item name="colorPrimaryDark">#color/brand_dark</item>
<item name="colorPrimary">#color/brand</item>
<item name="colorAccent">#color/brand_accent</item>
<item name="colorPositive">#color/positive</item>
<item name="colorPositiveDark">#color/positive_dark</item>
<item name="colorNegative">#color/negative</item>
<item name="colorNegativeDark">#color/negative_dark</item>
<item name="colorNegativeLight">#color/negative_light</item>
<item name="colorMedium">#color/medium</item>
....
...Things specific to this theme goes here
....
</style>
One of the ways is to put the common attributes in a theme overlay (assuming you are using the AppCompat library), e.g. -
<style name=“MyCustomOverlay” parent=”ThemeOverlay.AppCompat”>
<item name="colorPrimaryDark">#color/brand_dark</item>
<item name="colorPrimary">#color/brand</item>
<item name="colorAccent">#color/brand_accent</item>
<item name="colorPositive">#color/positive</item>
<item name="colorPositiveDark">#color/positive_dark</item>
<item name="colorNegative">#color/negative</item>
<item name="colorNegativeDark">#color/negative_dark</item>
<item name="colorNegativeLight">#color/negative_light</item>
<item name="colorMedium">#color/medium</item>
</style>
and then apply android:theme=”MyCustomOverlay” to your root views. This will override the attributes set in your base theme (e.g. MotherTheme or MotherThemeDark) for the root views and all child views. Works for API 11+.
Reference: https://plus.google.com/+AndroidDevelopers/posts/JXHKyhsWHAH
Although I made the above post as an answer, since it helped me find a solution i will post the actual solution here :
I made the dark theme extend ThemeOverlay.AppCompat.Dark and then applied the motherThemeDark to the root layout of the fragment/activity that i wanted to be themed dark. Under the motherThemeDark i only had attributes that i wanted to override for a dark theme.
<style name="MotherTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Base application theme common to all sdk versions -->
<!--Colors-->
<item name="colorPrimaryDark">#color/brand_dark</item>
<item name="colorPrimary">#color/brand</item>
<item name="colorAccent">#color/brand_accent</item>
<item name="colorPositive">#color/positive</item>
<item name="colorPositiveDark">#color/positive_dark</item>
<item name="colorNegative">#color/negative</item>
<item name="colorNegativeDark">#color/negative_dark</item>
<item name="colorNegativeLight">#color/negative_light</item>
<item name="colorMedium">#color/medium</item>
....
...Things specific to this theme goes here
....
</style>
<style name="MotherThemeDark" parent="ThemeOverlay.AppCompat.Dark">
....
....
...Things specific to this theme goes here
....
</style>

Using AppCompatDialog Removes titles from the Dialog

Once I switched from ActionBarActivity to AppCompatActivity one of the only changes I did was add this line:
<item name="windowNoTitle">true</item>
Here is my entire styles.xml file:
<style name="gptheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primaryDark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:windowContentOverlay">#null</item>
</style>
<style name="ThemeNoActionBar" parent="gptheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Now, all the new AppCompatDialogs in my code, (which were formerly Dialog d = new Dialog(mContext), all have no titles even though I use setTitle().
Obviously the change I made to the titles specified windowNoTitle but that should only affect the parent activity. I would think, anyway.
How exactly does this new feature work?
// style.xml
<style name="DialogActivity" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="colorAccent">#fff</item>
</style>
// manifest.xml
<activity
android:name=".MyPopupActivity"
android:theme="#style/DialogActivity"/>
// activity
import android.app.Activity;
public class MyPopupActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_popup);
}
}

How can I change the ActionBar color with XML in Lollipop?

I just updated my Android SDK to 22.
When I make an ActionBar, I’m trying to change the background, but it doesn’t work. Here’s my style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<!--<item name="actionBarTheme">#style/ThemeOverlay.AppCompat.ActionBar</item>-->
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<!--ActionBar-->
<style name="ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#bf360c</item>
</style>
I’ve tried many times, but it always shows the dark background. What should I be doing?
in your activity try to do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#bf360c")));
setContentView(R.layout.act_mainactivity);
}

Categories

Resources