MainActivity cannot run after splashcreen - java

This is my code:
package pi.com.pariwisata;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#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);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_maps) {
Intent peta = new Intent(this, MapsActivity.class);
startActivity(peta);
} else if (id == R.id.nav_list) {
Intent list = new Intent(this, ListActivity.class);
startActivity(list);
} else if (id == R.id.nav_about) {
Intent tentang = new Intent(this, TentangActivity.class);
startActivity(tentang);
} else if (id == R.id.nav_exit) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Keluar Dari Aplikasi?")
.setCancelable(false)//tidak bisa tekan tombol back
//jika pilih yess
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
//jika pilih no
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
and I have an error in setSupportActionBar(toolbar);
can you help me?

Try setting your Activity's parent theme to Theme.AppCompat.Light.NoActionBar
The important part is "NoActionBar"
Then you can use your custom ToolBar at your layout.

Set windowNoTitle and 'windowActionBar` in base theme of styles.xml and apply this theme to your application in AndroidManifest.xml
<!-- 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>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>

Related

How to pass string variable between fragments in navigation drawer activity template

tell me how to pass a string variable between two fragments of the navigation drawer activity template in Android Studio 4.0.1, preferably with examples of program code
I tried to do it using intent, but I don't understand where to specify it
Мой код MainActivity.java:
package com.example.myt;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#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 onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
Communication between fragments should happen through the parent activity.
You can read more about it on the documentation (including code samples):
https://developer.android.com/guide/fragments/communicate

MenuItem on Navigation Drawer does not respond

i am new to java programming, now i am working on a simple app that has an webview integrated that open a particular website.
My style is navigation drawer for the app. When i press an item from the drawer menu it doesn't work at all. I am trying to open a URL using the webview but nothing happens.
I have tried many things i found on here but i cant get any results
package com.virsoft.alfagreece;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import com.virsoft.alfagreece.ui.home.HomeFragment;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
private NavigationView navigationView;
private DrawerLayout drawerLayout;
private WebView myWebView;
private AppBarConfiguration mAppBarConfiguration;
#Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent Email = new Intent(Intent.ACTION_SEND);
Email.setType("text/email");
Email.putExtra(Intent.EXTRA_EMAIL,
new String[]{"sales#test.com"});
Email.putExtra(Intent.EXTRA_SUBJECT,
"testApp"); // Email 's Subject
Email.putExtra(Intent.EXTRA_TEXT, "Hi test," + ""); //Email 's Greeting text
startActivity(Intent.createChooser(Email, "Send message"));
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
myWebView =(WebView) findViewById(R.id.webView);
WebSettings webSettings=myWebView.getSettings();
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowContentAccess(true);
webSettings.setAppCacheEnabled(true);
webSettings.setCacheMode(webSettings.LOAD_CACHE_ELSE_NETWORK);
webSettings.setDomStorageEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://www.test.com");
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#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 onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_home) {
myWebView.loadUrl("https://youtube.com");
Toast.makeText(MainActivity.this,"testing", Toast.LENGTH_LONG).show();
return true;
}
else if (id == R.id.nav_gallery) {
myWebView.loadUrl("https://yahoo.com");
return true;
}
return true;
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
After setting up a small sample and doing some debugging, it seems to me that a NavigationView can be controlled either by setting up navigation with NavController or by setting a NavigationView.OnNavigationItemSelectedListener. You can't have both of them.
So if you want to keep on using Navigation components (which should by all means be possible), the question is how to get notified of clicks on NavigationView items?
NavController will navigate to some destination, nothing more. But using the safeargs plugin, one can pass along argument values to the destination Fragment.
If you use always the same destination (Fragment) but with different URLs, you could associate each NavigationView item with a specific action in the navigation graph which in turn belongs to one specific URL.
In my sample app, NavController replaced an already visible HomeFragment with a new instance of HomeFragment and the new URL was passed in correctly.
MainActivity onCreate() (note that you can remove everything related to NavigationView.OnNavigationItemSelectedListener from MainActivity):
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
Basic declaration of arguments in navigation graph:
<fragment
android:id="#+id/nav_home"
android:name="com.example.navdrawerissue.HomeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home">
<argument
android:name="webUrl"
app:argType="string"
android:defaultValue="https://developer.android.com"/>
</fragment>
Declaration of global action in navigation graph (below fragment, not nested!)
<action android:id="#+id/nav_other" app:destination="#+id/nav_home">
<argument
android:name="webUrl"
app:argType="string"
android:defaultValue="https://youtube.com" />
</action>
NavigationView menu
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/nav_home"
android:icon="#drawable/ic_launcher_background"
android:title="nav_home"/>
<item android:id="#+id/nav_plusone"
android:title="plus one"/>
<item android:id="#+id/nav_other"
android:title="other"/>
</menu>

How to navigate to activity on navigation drawer item click

This is my main activity i want to navigate to an activity on my navigation drawer item click. I have checked the old way of achieving this i.e using switch statement and using onNavigationItemSelected method.
I know this is a question frequently asked, but after reading the many questions and solutions on stack overflow I am confused. I am confused with regards to Fragments and what is required to start an activity from clicking an item in the navigation drawer.
MainActivity.java
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#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 onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="#+id/nav_home">
<fragment
android:id="#+id/nav_home"
android:name="com.vdosol.navigationdrawer.ui.home.HomeFragment"
android:label="#string/menu_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/nav_gallery"
android:name="com.vdosol.navigationdrawer.ui.gallery.GalleryFragment"
android:label="#string/menu_gallery"
tools:layout="#layout/fragment_gallery" />
<fragment
android:id="#+id/nav_slideshow"
android:name="com.vdosol.navigationdrawer.ui.slideshow.SlideshowFragment"
android:label="#string/menu_slideshow"
tools:layout="#layout/fragment_slideshow" />
<fragment
android:id="#+id/nav_tools"
android:name="com.vdosol.navigationdrawer.ui.tools.ToolsFragment"
android:label="#string/menu_tools"
tools:layout="#layout/fragment_tools" />
<fragment
android:id="#+id/nav_share"
android:name="com.vdosol.navigationdrawer.ui.share.ShareFragment"
android:label="#string/menu_share"
tools:layout="#layout/fragment_share" />
<fragment
android:id="#+id/nav_send"
android:name="com.vdosol.navigationdrawer.ui.send.SendFragment"
android:label="#string/menu_send"
tools:layout="#layout/fragment_send" />
</navigation>
you can do the following to navigate:
MenuItem logOutItem = navigationView.getMenu().findItem(R.id.nav_logout);
logOutItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//do your stuff
return true;
}
});
your AppBarConfiguration and Nav Controller should not contain above id ie "nav_logout" (in my case):
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_plan, R.id.navigation_history, R.id.navigation_about, R.id.navigation_feedback, R.id.navigation_notifications)
.setDrawerLayout(drawer)
.build();
your menu should only contain in your menu file....no need to add it to you AppBarConfiguration and your NavController....
hope this helps....happy coding
Implement the listener in your Activity:
public class HomeActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener
setNavigationItemSelectedListener in onCreate of Activity
NavigationView mNavigationView = (NavigationView) findViewById(R.id.account_navigation_view);
if (mNavigationView != null) {
mNavigationView.setNavigationItemSelectedListener(this);
}
Override the method
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_account) {//DO your stuff }
...
}

Action Bar isn't displaying in API16

I'm trying to use a navigation drawer in my Main activity.
when i run it in API 23 Emulator , everything looks fine.
But when I run it in API 16 Emulator, my action bar goes away!
Here is my manifest file :
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here is my styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/line1</item>
<item name="colorPrimaryDark">#color/line1_dark</item>
<item name="colorAccent">#color/isbc</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Here is my v21/styles.xml
<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>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/line1</item>
<item name="colorPrimaryDark">#color/line1_dark</item>
<item name="colorAccent">#color/isbc</item>
</style>
And this is my Mainactivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
changeTypeface(navigationView);
}
#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;
}
private void applyFontToItem(MenuItem item, Typeface font) {
SpannableString mNewTitle = new SpannableString(item.getTitle());
mNewTitle.setSpan(new CustomTypefaceSpan("", font, 16), 0 ,
mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
item.setTitle(mNewTitle);
}
private void changeTypeface(NavigationView navigationView) {
FontTypeface fontTypeface = new FontTypeface(this);
Typeface typeface = fontTypeface.getTypefaceAndroid();
MenuItem item;
item = navigationView.getMenu().findItem(R.id.nav_camera);
item.setTitle("لیست ایستگاه ها");
item.setIcon(R.drawable.ic_action_stations);
applyFontToItem(item, typeface);
item = navigationView.getMenu().findItem(R.id.nav_gallery);
item.setTitle("نقشه");
item.setIcon(R.drawable.ic_action_map);
applyFontToItem(item, typeface);
item = navigationView.getMenu().findItem(R.id.nav_slideshow);
item.setTitle("درباره ما");
item.setIcon(R.drawable.ic_action_us);
applyFontToItem(item, typeface);
}
#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) {
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit(); }
else if (id == R.id.nav_gallery) {
MapFragment fragment = new MapFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_slideshow) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Here is my app_bar_main :
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/fragment_main" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
Here is my activity_main.xml
<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">
<include
layout="#layout/app_bar_main"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
When I run this code in emulator API 21, everything looks fine.
But when I run it in API 16, i get this error:
"This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead."
I guess it's telling me not to use actionbar, but how can i actually display my navigation drawer if i set windowActionBar to false?
i've tried this topic but it didn't work

After clicking on option webview reload again and again

i have create a webapp using webview. Here in this i have a left sidedrawer with different links opening in same web view. Problem i am facing is as soon as i click on any of the option in drawer that web views is open and it gets reload again and again. here i am pasting the xml and java code my application.
MAIN ACTIVITY FILE
package com.example.dr.app3;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.KeyEvent;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
//import java.net.URI;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
// intialize web view
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
//webview control content
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//improve WebProformance
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//load cahe resources if requried otherwise load from network
webView.getSettings().setAppCacheEnabled(true);//enable catch
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);//makes all coloms no wider then screen
webSettings.setUseWideViewPort(true);//
webSettings.setSavePassword(true);//save username password
webSettings.setSaveFormData(true);//save date and time
webSettings.setEnableSmoothTransition(true);// smooth transition
webView.loadUrl("http://school.techjunctionplace.in");
//Force webview to open link in itself
webView.setWebViewClient(new MyWebViewClient());
}
#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;
}
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.dashboard) {
// Handle the camera action
webView.loadUrl("http://www.school.techjunctionplace.in/#/");
} else if (id == R.id.message) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/messages");
} else if (id == R.id.calender) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/calender");
} else if (id == R.id.classes_schedule) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/classschedule");
} else if (id == R.id.attendance) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/attendance");
} else if (id == R.id.attendance_stats) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/attendanceStats");
}else if (id == R.id.staff) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/staffAttendance");
}else if (id == R.id.hostel) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/hostel");
}else if (id == R.id.hostel_category) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/hostelCat");
}else if (id == R.id.library) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/library");
}else if (id == R.id.media_center) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/media");
}else if (id == R.id.teachers) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/teachers");
}else if (id == R.id.students) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/students");
}else if (id == R.id.parents) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/parents");
}else if (id == R.id.grade_level) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/gradeLevels");
}else if (id == R.id.study_material) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/materials");
}else if (id == R.id.assignments) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/assignments");
}else if (id == R.id.exam_list) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/examsList");
}else if (id == R.id.online_exams) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/onlineExams");
}else if (id == R.id.news_board) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/newsboard");
}else if (id == R.id.events) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/events");
}else if (id == R.id.fee_types) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/feeType");
}else if (id == R.id.fee_allocation) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/feeAllocation");
}else if (id == R.id.payments) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/payments");
}else if (id == R.id.expenses) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/expenses");
}else if (id == R.id.transportation) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/transports");
}else if (id == R.id.classes) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/classes");
}else if (id == R.id.sections) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/sections");
}else if (id == R.id.subjects) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/subjects");
}else if (id == R.id.reports) {
webView.loadUrl("http://www.school.techjunctionplace.in/#/reports");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.school.techjunctionplace.in")) {
// open url content in web view
return false;
}
// here open external url in browser
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
//ProgressDialogue
ProgressDialog pd = null;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd=new ProgressDialog(MainActivity.this);
pd.setTitle("Please Wait..");
pd.setMessage("It's Loading..");
pd.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
super.onPageFinished(view, url);
}
}
//goto previous page on back button
#Override
public boolean onKeyDown(int keycode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keycode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keycode, event);
}}
XML FILE OF SIDE DRAWER
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/dashboard"
android:icon="#drawable/ic_menu_camera"
android:title="Dashboard" />
<item
android:id="#+id/message"
android:icon="#drawable/ic_menu_gallery"
android:title="Messages" />
<item
android:id="#+id/calender"
android:icon="#drawable/ic_menu_slideshow"
android:title="Calender" />
<item
android:id="#+id/classes_schedule"
android:icon="#drawable/ic_menu_manage"
android:title="Classes Schedule" />
</group>
<item android:title="Attendance">
<menu>
<item
android:id="#+id/attendance"
android:icon="#drawable/ic_menu_share"
android:title="Attendance" />
<item
android:id="#+id/attendance_stats"
android:icon="#drawable/ic_menu_send"
android:title="Attendance Stats" />
<item
android:id="#+id/staff"
android:icon="#drawable/ic_menu_camera"
android:title="Staff Attendance" />
</menu>
</item>
<item android:title="Hostel Management">
<menu>
<item
android:id="#+id/hostel"
android:icon="#drawable/ic_menu_share"
android:title="Hostel" />
<item
android:id="#+id/hostel_category"
android:icon="#drawable/ic_menu_send"
android:title="Hostel Category" />
</menu>
</item>
<item android:title="School Info">
<menu>
<item
android:id="#+id/library"
android:icon="#drawable/ic_menu_camera"
android:title="Library" />
<item
android:id="#+id/media_center"
android:icon="#drawable/ic_menu_gallery"
android:title="Media Center" />
<item
android:id="#+id/teachers"
android:icon="#drawable/ic_menu_slideshow"
android:title="Teachers" />
<item
android:id="#+id/students"
android:icon="#drawable/ic_menu_manage"
android:title="Students" />
<item
android:id="#+id/parents"
android:icon="#drawable/ic_menu_manage"
android:title="Parents" />
<item
android:id="#+id/grade_level"
android:icon="#drawable/ic_menu_manage"
android:title="Grade Level" />
<item
android:id="#+id/study_material"
android:icon="#drawable/ic_menu_manage"
android:title="Study Material" />
<item
android:id="#+id/assignments"
android:icon="#drawable/ic_menu_manage"
android:title="Assigenments" />
<item
android:id="#+id/exam_list"
android:icon="#drawable/ic_menu_manage"
android:title="Exam List" />
<item
android:id="#+id/online_exams"
android:icon="#drawable/ic_menu_manage"
android:title="Online Exams" />
<item
android:id="#+id/news_board"
android:icon="#drawable/ic_menu_manage"
android:title="News Board" />
<item
android:id="#+id/events"
android:icon="#drawable/ic_menu_manage"
android:title="Events" />
</menu>
</item>
<item android:title="Accounting">
<menu>
<item
android:id="#+id/fee_types"
android:icon="#drawable/ic_menu_share"
android:title="Fee Types" />
<item
android:id="#+id/fee_allocation"
android:icon="#drawable/ic_menu_send"
android:title="Fee Allocation" />
<item
android:id="#+id/payments"
android:icon="#drawable/ic_menu_send"
android:title="Payments" />
<item
android:id="#+id/expenses"
android:icon="#drawable/ic_menu_send"
android:title="Expenses" />
</menu>
</item>
<item android:title="Transportation">
<menu>
<item
android:id="#+id/transportation"
android:icon="#drawable/ic_menu_camera"
android:title="Transportation" />
</menu>
</item>
<item android:title="Classes">
<menu>
<item
android:id="#+id/classes"
android:icon="#drawable/ic_menu_share"
android:title="Classes" />
<item
android:id="#+id/sections"
android:icon="#drawable/ic_menu_send"
android:title="Sections" />
</menu>
</item>
<item android:title="Reports">
<menu>
<item
android:id="#+id/subjects"
android:icon="#drawable/ic_menu_share"
android:title="Subjects" />
<item
android:id="#+id/reports"
android:icon="#drawable/ic_menu_send"
android:title="Reports" />
</menu>
</item>
In above code even
Uri.parse(url).getHost().equals("www.school.techjunctionplace.in"))
is also not working.
I am a beginner in android please help to identify what is the actual problem in my code.
in the onCreate method:
webView.loadUrl("http://school.techjunctionplace.in");
replace it with
webView.loadUrl("http://www.school.techjunctionplace.in");
Some changes in the webview client:
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("www.school.techjunctionplace.in")) {
// open url content in web view
return false;
}
// here open external url in browser
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
//ProgressDialogue
ProgressDialog pd = null;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if(pd == null) {
pd = new ProgressDialog(HomeActivity.this);
pd.setTitle("Please Wait..");
pd.setMessage("It's Loading..");
pd.show();
}
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
if(pd != null && pd.isShowing())
pd.dismiss();
pd = null;
super.onPageFinished(view, url);
}
}

Categories

Resources