NullPointerException at ActionBarImplICS (using app compat) on long-press - android

NullPointerException when long-press over field
compile 'com.android.support:appcompat-v7:19.1.+'
XML
<EditText
android:id="#+id/field"
style="#style/Field"
android:textSize="20sp"
android:hint="#string/hint"
android:nextFocusDown="#+id/other"/>
Style
<style name="Field" parent="Theme.AppCompat.Light">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#color/blue</item>
<item name="android:singleLine">true</item>
<item name="android:gravity">center_vertical</item>
<item name="android:inputType">textCapCharacters|textNoSuggestions</item>
<item name="android:imeOptions">actionNext</item>
<item name="android:popupBackground">#color/white</item>
</style>
LogCat
java.lang.NullPointerException
at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:302)
at android.support.v7.app.ActionBarImplJB.getThemedContext(ActionBarImplJB.java:20)
at android.support.v7.app.ActionBarActivityDelegate.getActionBarThemedContext(ActionBarActivityDelegate.java:208)
at android.support.v7.app.ActionBarActivityDelegateICS.onActionModeStarted(ActionBarActivityDelegateICS.java:195)
at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onActionModeStarted(ActionBarActivityDelegateICS.java:359)
at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2437)
at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild(PhoneWindow.java:2362)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
at android.view.View.startActionMode(View.java:4554)
at android.widget.Editor.startSelectionActionMode(Editor.java:1551)
at android.widget.Editor.performLongClick(Editor.java:859)
at android.widget.TextView.performLongClick(TextView.java:8373)
at android.view.View$CheckForLongPress.run(View.java:18441)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)

The ActionBar is null. That's because you're using an activity without a title (and thus also without an actionbar):
requestWindowFeature(Window.FEATURE_NO_TITLE);
There are two ways to fix it.
Simple:
Make YourActivity NOT extends ActionBarActivity but normal Activity.
More difficult:
Make transparent ActionBar with no title, no icon, no home button etc. How to do this? First, define ActionBar style and new theme that extends YourAppTheme:
<!-- Your App Theme-->
<style name="YourAppTheme.Light" parent="#style/Theme.AppCompat.Light">
<...>
</style>
<!-- Transparent ActionBar Style -->
<style name="YourAppTheme.Light.ActionBar.Transparent" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">#android:color/transparent</item>
<item name="background">#android:color/transparent</item>
</style>
<!-- Fullscreen Activity Theme -->
<style name="YourAppTheme.FullScreen.Light" parent="#style/YourAppTheme.Light">
<item name="android:actionBarStyle">#style/YourAppTheme.Light.ActionBar.Transparent</item>
<item name="actionBarStyle">#style/YourAppTheme.Light.ActionBar.Transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
Next, set this theme to your fullscreen activity:
<activity
android:name="your.package.name.YourFullscreenActivity"
android:label="#string/app_name"
android:theme="#style/YourAppTheme.FullScreen.Light" />
Finally, add following lines to YourFullscreenActivity#onCreate method:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
I hope it will help.

I think I have a better solution to this issue.
Remove requestWindowFeature(Window.FEATURE_NO_TITLE); from activity and <item name="android:windowNoTitle">true</item> from activity theme.
To your activity code add:
To onCreate() method
View actionBar = getActionBarView();
if (actionBar != null) {
actionBar.setVisibility(View.GONE);
}
To activity class:
public View getActionBarView() {
Window window = getWindow();
View v = window.getDecorView();
int resId;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
resId = getResources().getIdentifier(
"action_bar_container", "id", getPackageName());
} else {
resId = Resources.getSystem().getIdentifier(
"action_bar_container", "id", "android");
}
if (resId != 0) {
return v.findViewById(resId);
} else {
return null;
}
}
#Override
public void onSupportActionModeStarted(ActionMode mode) {
super.onSupportActionModeStarted(mode);
View actionBar = getActionBarView();
if (actionBar != null) {
actionBar.setVisibility(View.VISIBLE);
}
}
#Override
public void onSupportActionModeFinished(ActionMode mode) {
super.onSupportActionModeFinished(mode);
View actionBar = getActionBarView();
if (actionBar != null) {
actionBar.setVisibility(View.GONE);
}
}
This is remove action bar and at the same time allows you to manipulate text in edit text with action mode buttons.
Cheers

The second solution to the above answer works, but causes the top row that would have been covered by the actionbar to be unclickable. Set the height item to zero in the actionbar style to solve this issue:
<item name="android:height">0dp</item>

I think erakitin is a VERY good answer, and quiet simple if you take the "DO NOT extend ActionBarActivity, but in case anyone needs or want to avoid making that change, I'm leaving another answer which I think is also very simple and solves the problem PERFECTLY.
Try adding this to your activity's theme.
<item name="android:windowActionBar">false</item>

public class MainActivity extends ActionBarActivity
Change to:
public class MainActivity extends Activity

Related

the status bar changes it's color to black inside fullscreen dialog fragment android

I'm using dialog fragment. The problem is that the status bar color is changed to black. How to change it to some other color? It's strange cause inside fragment, activity it works fine. Its only black inside DialogFragment
#Override
public void onStart() {
super.onStart(); //super.onStart() is where dialog.show() is actually called on the underlying dialog, so we have to do it after this point
Dialog d = getDialog();
if (d != null) {
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
d.getWindow().setLayout(width, height);
d.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity(), R.style.full_screen_dialog);
return dialog;
}
You have to set FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDSto indicate that this Window is responsible for drawing the background for the system bars.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
dialog.getWindow().setStatusBarColor(yourColor);
}
I just posted the solution to this problem here
Add following theme to res/value-v21/style
<style name="DialogTheme" parent="#style/Base.Theme.AppCompat.Light.Dialog">
<item name="android:windowTranslucentStatus">true</item>
</style>
And then apply Style on DialogFragment in onCreate
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}
GUYS THE BEST SOLUTION IS IN THE WEBSITE LINK I AM POSTING HERE
https://zocada.com/android-full-screen-dialogs-using-dialogfragment/
I'll also upload the code here for reference
1st create a style FullScreenDialogStyle in your style file :
<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorPrimary">#color/colorPrimary</item>
<!-- Set this to true if you want Full Screen without status bar -->
<item name="android:windowFullscreen">false</item>
<item name="android:windowIsFloating">false</item>
<!-- This is important! Don't forget to set window background -->
<item name="android:windowBackground">#color/colorWhite</item>
<!-- Additionally if you want animations when dialog opening -->
<!--<item name="android:windowEnterAnimation">#anim/slide_up</item>-->
<!--<item name="android:windowExitAnimation">#anim/slide_down</item>-->
</style>
inside your dialogFragment class override a method onCreate
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL,R.style.FullScreenDialogStyle);
}
then override Onstart method, through which u can access the getDialog() and set the height and width
#Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
}
}
To change Status Bar Color under DialogFragment, set below style to your dialogFragment under onCreate Method.
like:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NO_TITLE,R.style.FullScreenDialogWithStatusBarColorAccent)
}
Add this style in style.xml
<style name="FullScreenDialogWithStatusBarColorAccent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/white</item>
<item name="android:windowNoTitle">true</item>
<item name="android:statusBarColor">#color/colorAccent</item>
<!--<item name="android:windowAnimationStyle">#style/MateriDocumentAlertsActivityalDialogSheetAnimation</item>-->
</style>
here is the solution i found:
add this to your style.xml file
<style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog">
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<!-- set the rounded drawable as background to your bottom sheet -->
<style name="BottomSheet" parent="#style/Widget.Design.BottomSheet.Modal">
<item name="android:background">#color/transparent</item>
</style>
<style name="BaseBottomSheetDialog" parent="#style/Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="bottomSheetStyle">#style/BottomSheet</item>
</style>
then you should override onCreate() method in BottomSheetDialogFragment class and call:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.BottomSheetDialogTheme);
}
in kotlin
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window?.statusBarColor = Color.parseColor("#0173B7")
}
A little late to the party but setting IS_FLOATING flag worked for me.
<style name="MyTheme.AlertDialog.FullScreen" parent="MyTheme.AlertDialog">
<item name="windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowAnimationStyle">#style/Animation.AppCompat.Dialog</item>
<item name="android:windowBackground">#color/white</item>
</style>

How to change the statusbar color

Hey i want to change my statusbar color i set in my styles.xml and v21/styles.xml this:
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/colorPrimaryText</item>
<item name="android:windowBackground">#color/windowBackground</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowActionBarOverlay">false</item>
</style>
and in my main activity this:
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setElevation(0);
getSupportActionBar().getThemedContext();
Window window = this.getWindow();
// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// finally change the color
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
but it dosent work... can someone help? I use AppCompact in my mainAcitvity
Call this method to change color from Kitkat bellow Kitkat works normally
//Utils.java
public static boolean hasKitKat()
{
return Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT;
}
//Your Activity
public void applyKitKatTranslucency(int color) {
if (Utils.hasKitKat()) {
if (mTintManager == null)
mTintManager = new SystemBarTintManager(this);
mTintManager.setStatusBarTintEnabled(true);
mTintManager.setStatusBarTintColor(color);
}
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
}
Try the below.
window.setStatusBarColor(this.getResources().getColor(R.color.statusbar));
First create a new color within your color resource file. You need to define the colour inside your colour.xml resource file.
<item name="statusbar" type="color">#FFF</item>
I hope this helps..

Popup menu divider for App compat theme

I used app compat theme style .
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:popupMenuStyle">#style/PopupMenu</item>
<item name="android:itemTextAppearance">#style/myCustomMenuTextApearance</item>
<item name="android:listPopupWindowStyle">#style/PopupMenuStyle</item>
</style>
<style name="PopupMenuStyle" parent="Widget.AppCompat.ListPopupWindow">
<item name="android:divider">#drawable/devider</item>
<item name="android:dividerHeight">2dp</item>
</style>
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
<item name="android:popupBackground">#color/search_panel_color</item>
<item name="android:textColor">#color/activity_button_text_color</item>
<item name="android:shadowColor">#color/activity_theam_color</item>
</style>
<style name="myCustomMenuTextApearance" parent="#android:style/TextAppearance.Widget.TextView.PopupMenu">
<item name="android:textColor">#color/activity_theam_color</item>
</style>
I want to add a divider in my menu item.
I've tried so many things, but the divider is not applying...
Is there any way to show the divider?
I also have the same problem. The solution is like this:
<style name="PopupMenuListView" parent="#style/Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#000000</item>
<item name="android:dividerHeight">1dp</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:dropDownListViewStyle">#style/PopupMenuListView</item>
</style>
You can also refer to the following link:
How to add dividers between specific menu items?
I have one solution.you can design popup as per your choice by programming.use below code to display popup menu.
private ListPopupWindow listPopupWindow;
listPopupWindow = new ListPopupWindow(getApplicationContext());
listPopupWindow.setWidth(400);
listPopupWindow.setDropDownGravity(Gravity.CENTER);
listPopupWindow.setAdapter(new listpopupadapter(a, type));
listPopupWindow.setAnchorView(v);
listPopupWindow.show();
Here listpopupadapter is class to design your list as it below.
public class listpopupadapter extends BaseAdapter {
ArrayList<String> a;
String type;
public listpopupadapter(ArrayList<String> a, String type) {
this.a = a;
this.type = type;
}
#Override
public int getCount() {
return a.size();
}
#Override
public Object getItem(int position) {
return getItem(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View root = LayoutInflater.from(parent.getContext()).inflate(
R.layout.raw_filter, null);
}
}
From your theme style, I guessed you used Toolbar. Is your menu popup is showed from Toolbar? If so, you can customize as the following step.
Define the theme
<style name="AppToolbarPopupTheme" parent="Widget.AppCompat.PopupMenu.Overflow">
<item name="android:dropDownListViewStyle">#style/AppDropDownListViewStyle</item>
</style>
<style name="AppDropDownListViewStyle" parent="Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#drawable/line_divider</item>
<item name="android:dividerHeight">1dp</item>
</style>
Then apply the theme to Toolbar
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:popupTheme="#style/AppToolbarPopupTheme">
</android.support.v7.widget.Toolbar>
if you haven't found the answer to this question then this is how it worked for me using the style:
<style name="PopupMenu">
<item name="android:itemBackground">#color/background_medium_gray</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:colorBackground">#color/BackgroundGray</item>
<item name="android:dividerHeight">1dp</item>
</style>
Context context = new ContextThemeWrapper(getActivity(), R.style.PopupMenu);
final PopupMenu popupMenu = new PopupMenu(context, view);
final MenuInflater menuInflater = popupMenu.getMenuInflater();
I suggest you add dummy groups,try this way
<group>
<!--add it like as a separator-->
<item
android:title=""
android:showAsAction="always"
android:enabled="false" />
</group>
Using a Material theme removes dividers.May be this is the simple solution to this problem.
You can try this or any holo theme (i.e #android:style/Widget.ListPopupWindow) to get divider effect in popup
<!-- Change Overflow Menu ListView Divider Property -->
<style name="PopupMenuListView" parent="android:Widget.ListPopupWindow">
<item name="android:divider">#FF0000</item>
<item name="android:dividerHeight">2dp</item>
</style>
I've made a driver app that needed a popup the ride was coming, at that time I used this one. So please try this one. Maybe it will help.
<activity
android:name="driver_activity_name
android:theme="#android:style/Theme.Translucent.NoTitleBar" />

PrimaryDark is not setting StatusBar color in android

I created an app and It has multiple themes and it changes dynamically. I mean in run-time user can choose different themes. so that UI components color changes in runtime. I got stuck in a strange problem. Problem is status bar color is not changing according to theme. statusBar always stays in initial theme color.
values-v21/style.xml is as below
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:icon">#color/icons</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
values-v21/themes.xml is as below
===================================
<resources>
<style name="AppThemeRed" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#F44336</item>
<item name="colorPrimaryDark">#D32F2F</item>
<item name="colorAccent">#FFCDD2</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:icon">#color/icons</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<style name="AppThemeAmber" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#FFC107</item>
<item name="colorPrimaryDark">#FFA000</item>
<item name="colorAccent">#FFEB3B</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:icon">#color/icons</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<style name="AppThemeBlue" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#3F51B5</item>
<item name="colorPrimaryDark">#303F9F</item>
<item name="colorAccent">#448AFF</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<style name="AppThemePurple" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#9C27B0</item>
<item name="colorPrimaryDark">#7B1FA2</item>
<item name="colorAccent">#7C4DFF</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<style name="AppThemeYellow" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#FFEB3B</item>
<item name="colorPrimaryDark">#FBC02D</item>
<item name="colorAccent">#CDDC39</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
when user selects a theme from theme selection dialog i calling a function and setting theme for that activity, also recreating activity.
public static void onActivityCreateSetTheme(Activity activity)
{
switch (sTheme)
{
case 0:
activity.setTheme(R.style.AppThemeRed);
break;
case 1:
activity.setTheme(R.style.AppThemeAmber);
break;
default:
case 2:
activity.setTheme(R.style.AppTheme);
break;
case 3:
activity.setTheme(R.style.AppThemeBlue);
break;
case 4:
activity.setTheme(R.style.AppThemePurple);
break;
case 5:
activity.setTheme(R.style.AppThemeYellow);
break;
}
}
I am using NavigationView in drawer and drawer is coming below status bar no problem with that. But status bar is not picking up current theme's primaryDark-color. It always takes Default theme(startup theme) color only.
Setting the theme via setTheme(int resId) in the onCreate() method won't have any effect on the StatusBar since it is not part of the Activity itself afaik.
So in order to achieve the desired effect you need to do it programatically. Here's a snippet which resolves the R.attr.colorPrimaryDark and sets it for the StatusBar if the SDK-Level is above 21:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
setContentView(R.layout.activity_management);
// Further code
}
private void init(){
// Set the Theme
setTheme(R.style.MyAppTheme);
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
getWindow().setStatusBarColor(getAttributeColor(R.attr.colorPrimaryDark));
}
}
// Resolve the given attribute of the current theme
private int getAttributeColor(int resId) {
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(resId, typedValue, true);
int color = 0x000000;
if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
// resId is a color
color = typedValue.data;
} else {
// resId is not a color
}
return color;
}
Edit
This approach obviously overwrites the color which is set via android:statusBarColor. So if you're using a NavigationDrawer and you want that the Drawer goes "under" the StatusBar you have to set the StatusBar color to transparent manually as soon as the Drawer is opened:
Example
actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
R.string.open_drawer_accessibility_desc,
R.string.close_drawer_accessibility_desc) {
#SuppressLint("NewApi")
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(
getResources().getColor(android.R.color.transparent));
}
}
/** Called when a drawer has settled in a completely open state. */
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
/** Called when a drawer has settled in a completely closed state. */
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
};
}

How to change theme ( light to dark) during run time? [duplicate]

I know it's possible to have it so if I have a setting I can change between Holo.Light and Holo, however, I cannot seem to find out how. All help is appreciated!
I think that you can do it by using the setTheme() method. Just make sure that you call it before you use setContentView, or it won't work.
For example:
if(userChoice ==1){
setTheme(android.R.style.Theme_Holo_Light);
else if(userChoice == 2){
setTheme(android.R.style.Theme_Holo);
}
A list of themes can be found here
As per a comment on the answer posted, if you need to toggle between the default Holo Themes, use this:
if (mThemeId == R.style.AppTheme.Dark) {
mThemeId = android.R.style.Theme_Holo_Light;
} else {
mThemeId = android.R.style.Theme_Holo;
}
this.recreate();
To use your own custom defined themes from your Styles.XML file. For example, something like this:
<style name="ActionBar" parent="#android:style/Widget.Holo.ActionBar" />
<style name="ActionBar.Light" parent="#style/ActionBar">
<item name="android:background">#color/actionbar_background_light</item>
</style>
<style name="ActionBar.Dark" parent="#style/ActionBar">
<item name="android:background">#color/actionbar_background_dark</item>
</style>
<style name="AppTheme.Light" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBar.Light</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="listDragShadowBackground">#android:color/background_light</item>
<item name="menuIconCamera">#drawable/ic_menu_camera_holo_light</item>
<item name="menuIconToggle">#drawable/ic_menu_toggle_holo_light</item>
<item name="menuIconShare">#drawable/ic_menu_share_holo_light</item>
</style>
<style name="AppTheme.Dark" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/ActionBar.Dark</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="listDragShadowBackground">#android:color/background_dark</item>
<item name="menuIconCamera">#drawable/ic_menu_camera_holo_dark</item>
<item name="menuIconToggle">#drawable/ic_menu_toggle_holo_dark</item>
<item name="menuIconShare">#drawable/ic_menu_share_holo_dark</item>
</style>
Define this as a Global Variable in your Activity:
private int mThemeId = -1;
And set your onCreate() method like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
if (savedInstanceState.getInt("theme", -1) != -1) {
mThemeId = savedInstanceState.getInt("theme");
this.setTheme(mThemeId);
}
mTitlesHidden = savedInstanceState.getBoolean("titlesHidden");
}
setContentView(R.layout.main);
}
And the code to toggle between the two themes:
if (mThemeId == R.style.AppTheme.Dark) {
mThemeId = R.style.AppTheme.Light;
} else {
mThemeId = R.style.AppTheme.Dark;
}
this.recreate();
Note: The theme has to be set before your call to setContentView()

Categories

Resources