简体   繁体   中英

I could not find anywhere how to change color of background from item menu to all activities

I want to create something like theme changing day/night.

Here's the code:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId()==R.id.menu_svijetlaTema){
        /**Set background color*/
        View view = this.getWindow().getDecorView();
        view.setBackgroundColor(Color.parseColor("#ffffff"));

        String[] values = getResources().getStringArray(R.array.pitanja);
        final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, values){
            @NonNull
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View a = super.getView(position, convertView, parent);
                TextView boja =  (TextView) a.findViewById(android.R.id.text1);
                boja.setTextColor(Color.BLACK);
                return a;
            }
        };
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_second);
        layout.setBackgroundColor(Color.WHITE);

    } else if(item.getItemId()==R.id.menu_tamnaTema){
        View view = this.getWindow().getDecorView();
        view.setBackgroundColor(Color.parseColor("#4c635c"));

        String[] values = getResources().getStringArray(R.array.pitanja);
        final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, values){
            @NonNull
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View a = super.getView(position, convertView, parent);
                TextView boja =  (TextView) a.findViewById(android.R.id.text1);
                boja.setTextColor(Color.WHITE);
                return a;
            }
        };
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_second);
        layout.setBackgroundColor(Color.BLACK);
    }

return true;
}`

After this when I click item in menu app crashes, adapter is for changing ListView , but RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_second); layout.setBackgroundColor(Color.WHITE); RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_second); layout.setBackgroundColor(Color.WHITE); not working I'm trying to change background color to MainActivity and SecondActivity from menu on MainActivity . Here's error :

--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: hr.itot.vjezbaprvepomoci, PID: 2538 java.lang.NullPointerException: Attempt to invoke virtual method void

android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference at hr.itot.vjezbaprvepomoci.MainActivity.onOptionsItemSelected(MainActivity.java:605) at android.app.Activity.onMenuItemSelected(Activity.java:3204) at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:406) at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195) at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103) at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:667) at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810) at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957) at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:127) at android.widget.AdapterView.performItemClick(AdapterView.jav a:310) at android.widget.AbsListView.performItemClick(AbsListView.java:1155) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3126) at android.widget.AbsListView$3.run(AbsListView.java:4041) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) `

要更改溢出菜单背景,请将其添加到“活动”主题中

<item name="android:itemBackground">@color/overflow_background</item>

Add in manifest

  <activity
        android:name="com.alarm.main.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/MyAppActionBarTheme" />

and create a themes.xml inside values folder and write down it

<resources>

<style name="MyAppActionBarTheme" parent="@android:style/Theme.Holo">
    <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
    <item name="android:itemTextAppearance">@style/TextAppearance</item>
</style>

<!-- The: background color for Action Bar overflow menu -->
<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
    <item name="android:popupBackground">#0b19ba</item>
</style>

<!-- Popup Menu Text Color styles -->
<style name="TextAppearance" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Menu"> 
    <item name="android:textColor">#ffffff</item>
</style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM