My app has a navigation drawer and I want to add background images to it, but I want them to change automatically.
Currently the background color is basically red:
The whole interface was auto generated by google's navigation drawer template when I created the project and I selected "Navigation Drawer Activity".
What I want to do is replace background color with background image, but I want that image to change periodically(something like Bing's image of the week, google's chromecast backdrops etc.).
Is there any way I can use Bing's image of the day?
Try this.
English is not my first language.
This may help you or you may get some idea to implement your requirement.
It working good. But I am not sure, this is the right way to implement this.
Here is my sample code:
MyApplication.java
public class MyApplication extends Application {
public static NavigationDrawerActivity navigationDrawerActivity;
public static List<Drawable> drawables = new ArrayList<>();
#Override
public void onCreate() {
super.onCreate();
}
public static NavigationDrawerActivity getNavigationDrawerActivity() {
return navigationDrawerActivity;
}
public static void setNavigationDrawerActivity(NavigationDrawerActivity navigationDrawerActivity) {
MyApplication.navigationDrawerActivity = navigationDrawerActivity;
}
public static List<Drawable> getDrawables() {
return drawables;
}
public static void setDrawables(List<Drawable> drawables) {
MyApplication.drawables = drawables;
}
}
NavigationDrawerActivity.java
public class NavigationDrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
List<Drawable> drawables = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);
LinearLayout linearLayout = (LinearLayout) headerView.findViewById(R.id.header_layout);
drawables.add(ContextCompat.getDrawable(this, R.drawable.bear));
drawables.add(ContextCompat.getDrawable(this, R.drawable.falls));
drawables.add(ContextCompat.getDrawable(this, R.drawable.corbis));
linearLayout.setBackground(drawables.get(0));
MyApplication.setDrawables(drawables);
MyApplication.setNavigationDrawerActivity(NavigationDrawerActivity.this);
UpdateUI();
}
public void UpdateUI() {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
LinearLayout linearLayout = (LinearLayout) headerView.findViewById(R.id.header_layout);
Drawable backgroundDrawable = linearLayout.getBackground();
drawables = MyApplication.getDrawables();
int index = drawables.indexOf(backgroundDrawable);
if (index == -1 || index == 2) {
index = 0;
} else {
index++;
}
linearLayout.setBackground(drawables.get(index++));
linearLayout.invalidate();
Intent intent = new Intent(NavigationDrawerActivity.this, UpdateUIReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
NavigationDrawerActivity.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
cancelNotification(100);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (5 * 1000), pendingIntent);
Toast.makeText(this, "Starting alarm in 5 seconds", Toast.LENGTH_SHORT).show();
}
public void cancelNotification(int requestCode) {
try {
Intent notificationIntent = new Intent(getApplicationContext(), UpdateUIReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation_drawer, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public static class UpdateUIReceiver extends BroadcastReceiver {
public UpdateUIReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
if (MyApplication.getNavigationDrawerActivity() != null) {
NavigationDrawerActivity navigationDrawerActivity = MyApplication.getNavigationDrawerActivity();
navigationDrawerActivity.UpdateUI();
}
}
}
}
nav_header_navigation_drawer.xml
Just added id for parent linearlayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />
</LinearLayout>
AndroidManifest.xml
<receiver android:name=".NavigationDrawerActivity$UpdateUIReceiver" />
Related
I´ve set up an android app with navigation drawer activity. There is an image of the activity by default and I want to go back to start page by clicking it.
How can I do that?
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Dialog myDialog;
NodemcuListFragment nodemcuListFragment;
ConstraintLayout content;
FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
content = (ConstraintLayout) findViewById(R.id.mainLayout);
fragmentManager = getSupportFragmentManager();
nodemcuListFragment = (NodemcuListFragment) Fragment.instantiate(this,NodemcuListFragment.class.getName(),null);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}else if(id == R.id.home){
Intent homeIntent = new Intent(this,MainActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
FragmentManager manager = getSupportFragmentManager();
if (id == R.id.nav_sensors) {
transaction.replace(R.id.mainLayout, nodemcuListFragment).commit();
} else if (id == R.id.nav_map) {
GmapFragment gmapFragment = new GmapFragment();
manager.beginTransaction().replace(R.id.mainLayout,gmapFragment).commit();
} else if (id == R.id.nav_settings) {
// Activity which shows settings
} else if (id == R.id.nav_contactUs) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/homeImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/nav_header_desc"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#mipmap/ic_launcher_round" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Team 1337"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ubiquitous Project" />
</LinearLayout>
In my MainActivity in the method onNavigationItemSelected I can switch in the menu, but if I address their the "androidImg" it doesn´t react...
I'm trying to change the background color of my layout to make dark/light theme on button click. But when I click on my button, my app crash and I don't know why.
Layout XML code :
<android.support.constraint.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:id="#+id/layoutParam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bleuNuit"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="mcamus.ihm_smartphone.Parametres"
tools:showIn="#layout/app_bar_parametres">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="24dp"
android:text="Thème de l'application"
android:textColor="#color/texteBlanc"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/thmClair"
android:onClick="thmClair"
android:layout_width="64dp"
android:layout_height="32dp"
android:layout_marginStart="48dp"
android:layout_marginTop="18dp"
android:text="Clair"
android:textSize="20px"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/thmSombre"
android:onClick="thmSombre"
android:layout_width="64dp"
android:layout_height="32dp"
android:layout_marginStart="120dp"
android:layout_marginTop="18dp"
android:text="Sombre"
android:textSize="20px"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
Java code :
public class Parametres extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
LinearLayout paramLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parametres);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.parametres, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.alimentation) {
Intent alimentation = new Intent(Parametres.this,Accueil.class);
startActivity(alimentation);
} else if (id == R.id.capteurs) {
Intent capteurLayout = new Intent(Parametres.this,Capteurs.class);
startActivity(capteurLayout);
} else if (id == R.id.absence) {
Intent absLayout = new Intent(Parametres.this,Absence.class);
startActivity(absLayout);
} else if (id == R.id.parametres) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void thmSombre(View view) {
paramLayout = findViewById(R.id.layoutParam);
paramLayout.setBackgroundColor((Color.parseColor("#FFFFFF")));
}
public void thmClair(View view) {
}
}
I tried different method, but each time my application crashes. Someone would have an idea of the problem? I have to do this on homework for school and I do not know how to solve this problem.
You are trying to cast LinearLayout into a ConstraintLayout
Change line LinearLayout paramLayout; to ConstraintLayout paramLayout; in your activity
I have created a Base Activity that contains a navigation drawer.
The drawer will open on slide, but never through the navigation button on the toolbar. I've been stuck on this for quite a while, and I had it working prior to creating this base class and I don't think I've changed anything logically.
I know this question has been asked before, but I've gone through all other similar posts and have not been able to solve it.
Thanks in advance!
public class DrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
protected DrawerLayout drawerLayout;
private ActionBarDrawerToggle toggle;
private Toolbar toolbar;
private NavigationView navigationView;
private boolean enableToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
enableToolbar = true;
}
#Override
public void setContentView(int layoutResID){
super.setContentView(R.layout.activity_drawer);
setToolbar();
setDrawerLayout();
setNavigationDrawer();
setToolbarOnClickListener();
if(!useToolbar())
toolbar.setVisibility(View.GONE);
inflateToContentFrame(layoutResID);
}
public void setUsesToolbar(boolean _enable){
enableToolbar = _enable;
}
private boolean useToolbar(){
return enableToolbar;
}
private void setToolbar(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
private void setDrawerLayout(){
drawerLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_drawer, null);
toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
DrawerActivity.this.invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
DrawerActivity.this.invalidateOptionsMenu();
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
DrawerActivity.this.toolbar.setAlpha(1 - slideOffset / 2);
}
};
toggle.setDrawerIndicatorEnabled(true);
drawerLayout.addDrawerListener(toggle);
drawerLayout.post(new Runnable() {
#Override
public void run() {
toggle.syncState();
}
});
}
private void setNavigationDrawer(){
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void setToolbarOnClickListener(){
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DrawerActivity.this.drawerLayout.openDrawer(GravityCompat.START);
}
});
}
private void inflateToContentFrame(int layoutResID){
FrameLayout content = (FrameLayout) findViewById(R.id.content_frame);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(layoutResID, content, true);
}
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.drawer, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Intent _intent;
if (id == R.id.nav_classes) {
_intent = new Intent(this, AppointmentsActivity.class);
} else if (id == R.id.nav_appointments) {
_intent = new Intent(this, AppointmentsActivity.class);
} else if (id == R.id.nav_clients) {
_intent = new Intent(this, AppointmentsActivity.class);
} else if (id == R.id.nav_payments) {
_intent = new Intent(this, AppointmentsActivity.class);
} else if(id == R.id.nav_Settings){
_intent = new Intent(this, AppointmentsActivity.class);
} else if (id == R.id.nav_share) {
_intent = new Intent(this, AppointmentsActivity.class);
} else if (id == R.id.nav_send) {
_intent = new Intent(this, AppointmentsActivity.class);
}else{
_intent = new Intent(this, AppointmentsActivity.class);
}
drawerLayout.closeDrawer(GravityCompat.START);
startActivity(_intent);
return true;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include
layout="#layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_drawer"
app:menu="#menu/activity_drawer_drawer"
/>
</android.support.v4.widget.DrawerLayout>
The DrawerLayout instance you're setting up the Toggle with is not the instance that's on-screen. In the setDrawerLayout() method, you're inflating a new layout that's never used.
Instead of inflating there, use findViewById() to get the DrawerLayout instance that's created and added to the Activity in the super.setContentView() call.
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Also, you can omit the toolbar.setNavigationOnClickListener() call, as the ActionBarDrawerToggle will set a listener itself, and handle opening and closing the drawer internally.
I'm using Android Stduio 2.1. I generated a Navigation Drawer Activity.
How can I call other Activitys or Fragments with the Same Navigation Drawer.
But the Navigation Menu must be still there in every Fragment or Activity I call. How can I manage this?
This is the MainActivity:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, GPS_Overview2.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//GPS AN/AUS Überprüfung
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toast.makeText(this, this.getText(R.string.GPS_AN) , Toast.LENGTH_SHORT).show();
}else{
showGPSDisabledAlertToUser();
}
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
//GPS An Message
private void showGPSDisabledAlertToUser(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(getApplicationContext().getString(R.string.GPS_ERROR))
.setCancelable(false)
.setPositiveButton(getApplicationContext().getString(R.string.GPS_SETTINGS),
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton(getApplicationContext().getString(R.string.cancel),
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.gps_overview) {
} else if (id == R.id.tachoscheibe) {
} else if (id == R.id.gps_overview) {
} else if (id == R.id.settings) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.settings) {
} else if (id == R.id.exit) {
System.exit(0);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
This is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="false" />
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
When you need more information only write a comment.
Greetings form Germany
Tim
Use Intent when to want to go go to another activity
Intent intent = new Intent(CurrentActivity.this,newActivity.class);
And
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.gps_overview) {
startActivity(intent);
} else if (id == R.id.tachoscheibe) {
} else if (id == R.id.gps_overview) {
} else if (id == R.id.settings) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.settings) {
} else if (id == R.id.exit) {
System.exit(0);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
For changing fragment you can you
Fragment fragment = null;
fragmentClass = Fragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_fragment, fragment).commit();
I'm trying to resolve this issue for a long time. I have a AppCompatActivity which includes a ViewPager. Everyting works fine unless the fragment containing a scrollview shows on the screen. From that point the listview vertical scroll movement works very jittery as well as the horizontal movement of the viewpager. I really don't know how to make it work together.
My activity code:
public class MyMeeatie extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, RateFragment.OnFragmentInteractionListener,
MyMeeatieFragment.OnFragmentInteractionListener, StatisticsFragment.OnFragmentInteractionListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_meeatie);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setLogo(R.drawable.meeatie_logo_top);
final ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager_my_meeatie);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tab_layout_my_meeatie);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void setupViewPager(final ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(MyMeeatieFragment.newInstance(),getString(R.string.my_meeatie));
adapter.addFrag(StatisticsFragment.newInstance(), getString(R.string.statistics));
adapter.addFrag(RateFragment.newInstance(), getString(R.string.rate));
viewPager.setAdapter(adapter);
viewPager.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
// How far the user has to scroll before it locks the parent vertical scrolling.
final int margin = 10;
final int fragmentOffset = v.getScrollX() % v.getWidth();
if (fragmentOffset > margin && fragmentOffset < v.getWidth() - margin) {
viewPager.getParent().requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_meeatie, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction(Uri uri) {
}
#Override
public void onFragmentInteraction(String id) {
}
}
Example fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="pl.creative.meeatie.mobile.fragments.MyMeeatieFragment">
<ListView android:id="#+id/my_meeatie_lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I'll be really thankful for your help.