how do i remove the switch on my home screen?

2 answers

So currently my home screen is as of such

![enter image description here][1]

however though in my navigation drawer I have already a switch created inside. Hence as such, I would like to ask if there is any way to ensure that the only switch that I would have would be the one inside my navigation drawer?

here is my layout_switch.xml file

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="horizontal" android:layout_width="match_parent"     android:layout_height="match_parent">      <Switch         android:layout_width="fill_parent"         android:layout_height="match_parent"         android:id="@+id/btnSwitch"/>  </LinearLayout> 

here is my drawable_menu file

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto">     <item         android:id="@+id/signIn"         android:icon="@drawable/ic_account_circle_black_24dp"         android:title="Sign In" />     <item         android:id="@+id/Mode"         app:actionLayout="@layout/layout_switch"         android:icon="@drawable/ic_brightness_4_black_24dp"         android:title="Dark Mode" />  </menu> 

and this is my activity_main file

<?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@color/white"     android:id="@+id/drawer"     tools:context=".MainActivity">      <include         android:layout_height="wrap_content"         android:layout_width="match_parent"         layout="@layout/drawabletoolbar"/>     <include         android:layout_height="wrap_content"         android:layout_width="match_parent"         layout="@layout/content_main"/>        <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="horizontal"        >           <Switch             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:id="@+id/btnSwitch"             android:text=""/>        </LinearLayout>        <com.google.android.material.navigation.NavigationView         android:layout_width="match_parent"         android:layout_height="match_parent"         android:id="@+id/navi_view"         app:menu="@menu/drawable_menu"         app:headerLayout="@layout/drawable_header"         android:layout_gravity="start"         android:fitsSystemWindows="true"/>  

this is my MainActivity file

package sg.edu.rp.c346.app4thnewspd;  import androidx.annotation.RequiresApi; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; import androidx.core.view.MenuItemCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.appcompat.widget.Toolbar;  import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Switch; import android.widget.Toast;  import com.google.android.material.navigation.NavigationView;  import java.util.ArrayList; import java.util.List; import java.util.Locale;  import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import sg.edu.rp.c346.app4thnewspd.Model.Articles; import sg.edu.rp.c346.app4thnewspd.Model.Headlines;  public class MainActivity extends AppCompatActivity {      RecyclerView recyclerView;     SwipeRefreshLayout swipeRefreshLayout;     EditText etQuery;     Button btnSearch;     final String API_KEY = "5353a8d609b4415ab6f449f31d46926a";     Adapter adapter;     List<Articles>articles = new ArrayList<>();     private String query;     Toolbar toolbar;     DrawerLayout drawer;     ActionBarDrawerToggle toggle;     private Switch btnSwitch;      public static final String MYPREFERENCES = "nightModePrefs";     public static final String KEY_ISNIGHTMODE = "isNIghtMode";     SharedPreferences sharedPreferences;        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);           toolbar = findViewById(R.id.toolbar);         setSupportActionBar(toolbar);            drawer = findViewById(R.id.drawer);         toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.open,R.string.close);         drawer.addDrawerListener(toggle);         toggle.setDrawerIndicatorEnabled(true);         toggle.syncState();           swipeRefreshLayout = findViewById(R.id.swipeRefresh);         recyclerView = findViewById(R.id.recyclerView);          etQuery = findViewById(R.id.etQuery);         btnSearch = findViewById(R.id.btnSearch);          sharedPreferences = getSharedPreferences(MYPREFERENCES, Context.MODE_PRIVATE);         btnSwitch = findViewById(R.id.btnSwitch);          checkNightModeActivated();          btnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {             @Override             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                 if(isChecked){                     AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);                     saveNightModeState(true);                     recreate();                 }else {                     AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);                     saveNightModeState(false);                     recreate();                 }             }         });              recyclerView.setLayoutManager(new LinearLayoutManager(this));         final String country = getCountry();          swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {             @Override             public void onRefresh() {                 retrieveJson("",country,API_KEY);             }         });          retrieveJson("",country, API_KEY);          btnSearch.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 if (!etQuery.getText().toString().equals("")){                     swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {                         @Override                         public void onRefresh() {                             retrieveJson(etQuery.getText().toString(),country,API_KEY);                         }                     });                     retrieveJson(etQuery.getText().toString(),country,API_KEY);                 }else{                     swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {                         @Override                         public void onRefresh() {                             retrieveJson("",country,API_KEY);                         }                     });                     retrieveJson("",country,API_KEY);                 }             }         });     }      private void saveNightModeState(boolean nightMode) {         SharedPreferences.Editor editor = sharedPreferences.edit();          editor.putBoolean(KEY_ISNIGHTMODE,nightMode);          editor.apply();     }     public void checkNightModeActivated(){         if(sharedPreferences.getBoolean(KEY_ISNIGHTMODE, false)){             btnSwitch.setChecked(true);             AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);         }else {             btnSwitch.setChecked(false);             AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);         }     }       private void restartActivity() {         Intent i = new Intent(getApplicationContext(),MainActivity.class);         startActivity(i);         finish();     }       private String getCountry() {         Locale locale = Locale.getDefault();         String country = locale.getCountry();         return country.toLowerCase();     }         public void retrieveJson(final String query, final String country, final String apiKey){           swipeRefreshLayout.setRefreshing(true);         Call<Headlines>call;         if(!etQuery.getText().toString().equals("")){             call = ApiClient.getInstance().getApi().getSpecificData(query,apiKey);          }else{             call = ApiClient.getInstance().getApi().getHeadLines(country, apiKey);         }         call.enqueue(new Callback<Headlines>() {             @Override             public void onResponse(Call<Headlines> call, Response<Headlines> response) {                 if (response.isSuccessful() && response.body().getArticles() != null){                     swipeRefreshLayout.setRefreshing(false);                     articles.clear();                     articles = response.body().getArticles();                     adapter = new Adapter(MainActivity.this,articles);                     recyclerView.setAdapter(adapter);                  }             }              @Override             public void onFailure(Call<Headlines> call, Throwable t) {                 swipeRefreshLayout.setRefreshing(false);                 Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();             }         });     } } 

this is my drawable_header file

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="#708090"     android:orientation="vertical">       <ImageView         android:id="@+id/imageView2"         android:layout_width="157dp"         android:layout_height="137dp"         android:layout_marginStart="8dp"         android:layout_marginLeft="8dp"         android:layout_marginTop="8dp"         android:elevation="6dp"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:srcCompat="@drawable/news" />      <TextView         android:id="@+id/textView"         android:layout_width="109dp"         android:layout_height="108dp"         android:layout_marginStart="16dp"         android:layout_marginLeft="500dp"         android:layout_marginTop="64dp"         android:layout_marginEnd="68dp"         android:layout_marginRight="68dp"         android:text="News"         android:textColor="@color/white"         android:textSize="25dp"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toEndOf="@+id/imageView"         app:layout_constraintTop_toTopOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout> 

this is my drawable_toolbar file

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="#708090"     android:orientation="vertical">       <ImageView         android:id="@+id/imageView2"         android:layout_width="157dp"         android:layout_height="137dp"         android:layout_marginStart="8dp"         android:layout_marginLeft="8dp"         android:layout_marginTop="8dp"         android:elevation="6dp"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:srcCompat="@drawable/news" />      <TextView         android:id="@+id/textView"         android:layout_width="109dp"         android:layout_height="108dp"         android:layout_marginStart="16dp"         android:layout_marginLeft="500dp"         android:layout_marginTop="64dp"         android:layout_marginEnd="68dp"         android:layout_marginRight="68dp"         android:text="News"         android:textColor="@color/white"         android:textSize="25dp"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toEndOf="@+id/imageView"         app:layout_constraintTop_toTopOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout> 

* Updated so apparently now i could get my switch to be able to switch it to dark mode after making some adjustments to my MainActivity, but however though the switch which could work is the one that you see in the ss but not the one that i have in my navigation drawer.Why?

Here is my Updated MainActivity

package sg.edu.rp.c346.app4thnewspd;  import androidx.annotation.RequiresApi; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; import androidx.core.view.MenuItemCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.appcompat.widget.Toolbar;  import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Switch; import android.widget.Toast;  import com.google.android.material.navigation.NavigationView;  import java.util.ArrayList; import java.util.List; import java.util.Locale;  import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import sg.edu.rp.c346.app4thnewspd.Model.Articles; import sg.edu.rp.c346.app4thnewspd.Model.Headlines;  public class MainActivity extends AppCompatActivity {      RecyclerView recyclerView;     SwipeRefreshLayout swipeRefreshLayout;     EditText etQuery;     Button btnSearch;     final String API_KEY = "5353a8d609b4415ab6f449f31d46926a";     Adapter adapter;     List<Articles>articles = new ArrayList<>();     private String query;     Toolbar toolbar;     DrawerLayout drawer;     ActionBarDrawerToggle toggle;     private Switch btnSwitch;      public static final String MYPREFERENCES = "nightModePrefs";     public static final String KEY_ISNIGHTMODE = "isNIghtMode";     SharedPreferences sharedPreferences;        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)      @Override     protected void onCreate(Bundle savedInstanceState) {          if (AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES){             setTheme(R.style.DarkTheme);         }else{             setTheme(R.style.LightTheme);         }         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          btnSwitch = findViewById(R.id.btnSwitch);         if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES){             btnSwitch.setChecked(true);         }         btnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {             @Override             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                 if(isChecked){                     AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);                     restartActivity();                 }else{                     AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);                     restartActivity();                 }             }         });           toolbar = findViewById(R.id.toolbar);         setSupportActionBar(toolbar);            drawer = findViewById(R.id.drawer);         toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.open,R.string.close);         drawer.addDrawerListener(toggle);         toggle.setDrawerIndicatorEnabled(true);         toggle.syncState();           swipeRefreshLayout = findViewById(R.id.swipeRefresh);         recyclerView = findViewById(R.id.recyclerView);          etQuery = findViewById(R.id.etQuery);         btnSearch = findViewById(R.id.btnSearch);            recyclerView.setLayoutManager(new LinearLayoutManager(this));         final String country = getCountry();          swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {             @Override             public void onRefresh() {                 retrieveJson("",country,API_KEY);             }         });          retrieveJson("",country, API_KEY);          btnSearch.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 if (!etQuery.getText().toString().equals("")){                     swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {                         @Override                         public void onRefresh() {                             retrieveJson(etQuery.getText().toString(),country,API_KEY);                         }                     });                     retrieveJson(etQuery.getText().toString(),country,API_KEY);                 }else{                     swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {                         @Override                         public void onRefresh() {                             retrieveJson("",country,API_KEY);                         }                     });                     retrieveJson("",country,API_KEY);                 }             }         });     }      private void saveNightModeState(boolean nightMode) {         SharedPreferences.Editor editor = sharedPreferences.edit();          editor.putBoolean(KEY_ISNIGHTMODE,nightMode);          editor.apply();     }     public void checkNightModeActivated(){         if(sharedPreferences.getBoolean(KEY_ISNIGHTMODE, false)){             btnSwitch.setChecked(true);             AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);         }else {             btnSwitch.setChecked(false);             AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);         }     }       private void restartActivity() {         Intent i = new Intent(getApplicationContext(),MainActivity.class);         startActivity(i);         finish();     }       private String getCountry() {         Locale locale = Locale.getDefault();         String country = locale.getCountry();         return country.toLowerCase();     }         public void retrieveJson(final String query, final String country, final String apiKey){           swipeRefreshLayout.setRefreshing(true);         Call<Headlines>call;         if(!etQuery.getText().toString().equals("")){             call = ApiClient.getInstance().getApi().getSpecificData(query,apiKey);          }else{             call = ApiClient.getInstance().getApi().getHeadLines(country, apiKey);         }         call.enqueue(new Callback<Headlines>() {             @Override             public void onResponse(Call<Headlines> call, Response<Headlines> response) {                 if (response.isSuccessful() && response.body().getArticles() != null){                     swipeRefreshLayout.setRefreshing(false);                     articles.clear();                     articles = response.body().getArticles();                     adapter = new Adapter(MainActivity.this,articles);                     recyclerView.setAdapter(adapter);                  }             }              @Override             public void onFailure(Call<Headlines> call, Throwable t) {                 swipeRefreshLayout.setRefreshing(false);                 Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();             }         });     } } 

All answers to this question, which has the identifier 61201212

The best answer:

To solve toolbar news overlap replace toolbar xml with below code.If solved accept this answer too please.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent">  <androidx.appcompat.widget.Toolbar     android:id="@+id/toolbar_top"     android:layout_height="wrap_content"     android:layout_width="match_parent"     android:minHeight="?attr/actionBarSize"     android:background="@color/action_bar_bkgnd"     app:theme="@style/ToolBarTheme" >        <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Toolbar Title"         android:layout_gravity="center"         android:id="@+id/toolbar_title" />      </androidx.appcompat.widget.Toolbar>  </LinearLayout> 

First remove switch from activity_main layout

 <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="horizontal"        >           <Switch             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:id="@+id/btnSwitch"             android:text=""/>        </LinearLayout> 

Then add this to MainActivity adjust what you need.It will definitely work.I have tested

Menu menu = navigationView.getMenu(); MenuItem menuItem = menu.findItem(R.id.btnSwitch); View actionView = MenuItemCompat.getActionView(menuItem); actionView.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View v) {    } }); 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data