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);
}
}
Related
I am showing a dialog made from Activity. The dialog activity has set no title in onCreate() callback:
public class MyDialogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set window feature no title
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_dialog);
...
}
...
}
I defined a style for the activity:
<style name="Theme.MyDialog" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="background">#android:color/transparent</item>
<item name="android:windowBackground">#drawable/my_bg</item>
</style>
And in AndroidManifest.xml:
<activity
android:name=".MyDialogActivity"
android:theme="#style/Theme.MyDialog">
</activity>
But when I show the dialog, there is always a title(which shows my app's name) on the dialog.
It is weird not only because it still shows title, but also shows my app's name as title, as you can see, I haven't set any title. It looks like an Android default behaviour, but...how to get rid of the title?
(I am running on Android 5.1.1)
You can try adding the following style to your Activity for imitating a dialog:
<style name="Theme.AppCompat.TranslucentDialog" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#color/colorTransparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
where colorTransparent is the following:
<color name="colorTransparent">#64000000</color>
Then use it with:
<activity
android:name=".MyDialogActivity"
android:theme="#style/Theme.AppCompat.TranslucentDialog">
</activity>
When I programmatically create a button as shown below, none of the custom styles are considered and I lose all of the default stylings (such as default background color etc..)
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_main);
Button button = new Button(this, null, R.style.LocButtonStyle);
layout.addView(button);
button.setText("Ok");
}
}
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="LocButtonStyle" parent="#android:style/Widget.Button">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:colorBackground">#ff0000</item>-->
<item name="android:textSize">160dp</item>
</style>
If I programatically create a Button without the styles the default styling is OK, but that's obviously not what I want.
Button button = new Button(this);
I have only seen workarounds for this, but I am yet to find the answer why this doesn't work.
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.
<style name="GCTheme" parent="Theme.Sherlock.Light">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#FFFFFF</item>
<item name="actionDropDownStyle">#style/MyDropDown</item>
<item name="popupMenuStyle">#style/PopupMenu.Example</item>
</style>
<style name="MyDropDown" parent="Widget.Sherlock.Spinner.DropDown.ActionBar">
<item name="android:popupBackground">#7D7D7D</item>
</style>
<style name="PopupMenu.Example" parent="#style/Widget.Sherlock.Light.ListPopupWindow">
<item name="android:popupBackground">#7D7D7D</item>
</style>
The following code styles my action bar such that the title of the action bar is white and the action bar is bluish. The problem that I am faced with now is that, since I am using the Theme.Sherlock.Light, the settings on options menu will not display properly.
Setting in the option menu extends PreferenceActivity and consists of the following code,
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.perferences);
}
The main problem here is that, the textColor for the action bar is set to be white and also the background that appears on this page is white, making the text impossible to read ... Does anyone have an idea so as to solve this problem.
I did try using this code
View v = new View(this);
v.setBackgroundColor(Color.GRAY);
in the settings activity, but it did not work.
So instead of using <item name="android:textColor">#FFFFFF</item> to set your title's color to white you should use this :
<style name="Theme.MyApplications" parent="#style/Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/ActionBar.Solid</item>
</style>
<style name="ActionBar.Title" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="ActionBar.SubTitle" parent="TextAppearance.Sherlock.Widget.ActionBar.Subtitle">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="ActionBar.Solid" parent="#style/Widget.Sherlock.Light.ActionBar.Solid">
<item name="titleTextStyle">#style/ActionBar.Title</item>
<item name="subtitleTextStyle">#style/ActionBar.SubTitle</item>
</style>
This should do the trick for you.
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.