How to show web view with Navigation Drawer in android - android

I am trying to show web view with navigation drawer in android. I am also implementing this but web view listener is not working.
This is my main Activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerLayout = navigationView.getHeaderView(0);
lv = (ListView) headerLayout.findViewById(R.id.listView);
lv.setAdapter(new CustomAdapter(this, prgmNameList, prgmImages));
Button button = (Button) findViewById(R.id.btbn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_SHORT).show();
}
});
webview = (WebView) findViewById(R.id.contaner);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(MainActivity.this, "WebView Example", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " + url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("https://www.google.co.in/maps?source=tldsi&hl=en");
}
#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.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;
}
And this is layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<WebView
android:id="#+id/contaner"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
<Button
android:id="#+id/btbn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Any help, suggestion, tutorials would be highly appreciated. Thank you....

WebView superWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//This is code for Enable Javascript
superWebView.getSettings().setJavaScriptEnabled(true);
//This is code for Enable Webclient to open webview without application
superWebView.setWebViewClient(new WebViewClient());
//This is code for Webchrome
superWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
getSupportActionBar().setTitle(title);
}
});
............................................
..........Same as Above Code................
............................................
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
//THis show web view with Navigation Drawer link as Webview
superWebView.loadUrl("http://www.google.com");
} 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;
}

check it official doc of web vie
https://developer.android.com/guide/webapps/webview.html

Related

I'm getting this error "RecyclerView: No adapter attached; skipping layout" but i'm working with " boolean onCreateOptionsMenu(Menu menu)"

whenever i'm commenting this code, the application works fine, but with this code i'm getting this error
E/RecyclerView: No adapter attached; skipping layout
E/AndroidRuntime: FATAL EXCEPTION: main
Process: online.ds.primeaddict.eorder, PID: 13600
android.content.res.Resources$NotFoundException: Resource ID #0x7f07006f
please help me, i'm new here please be kind, Thank You
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
final MenuItem menuItem = menu.findItem(R.id.action_cart);
View actionView = MenuItemCompat.getActionView(menuItem);
TextView textCartItemCount = (TextView)
actionView.findViewById(R.id.cart_badge);
actionView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(menuItem);
}
});
return true;
}
main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_cart"
android:icon="#drawable/ic_shopping_cart_black_24dp"
android:title="Cart"
app:actionLayout="#layout/custom_action_item_layout"
app:showAsAction="always" />
</menu>
custom_action_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ic_shopping_cart_black_24dp" />
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:padding="3dp"
android:text="0"
android:textColor="#android:color/white"
android:textSize="10sp" />
</FrameLayout>
UPDATE: here is my mainActivity
mainActivity.java
package online.ds.primeaddict.eorder;
public class Home2Activity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrinkShopAPI mService;
CompositeDisposable compositeDisposable = new CompositeDisposable();
RecyclerView mainMenu;
SliderLayout mainSlider;
TextView badgeText;
TextView navName, navPhoto;
CircleImageView circleImage;
Uri uri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mService = Common.getAPI();
mainSlider = findViewById(R.id.home_slider);
mainMenu = findViewById(R.id.main_menu);
mainMenu.setLayoutManager(new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false));
mainMenu.setHasFixedSize(true);
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);
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);
getMenu();
getSliderImages();
setUpHeadder(navigationView);
initDatabase();
getToppingList();
}
private void getMenu() {
compositeDisposable.add(mService
.getMenus()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<Category>>() {
#Override
public void accept(List<Category> categories) throws
Exception {
Log.d("DEBUG", "getMenu()");
setUpMenu(categories);
}
}));
}
private void setUpMenu(List<Category> categories) {
Log.d("DEBUG", "SetupMenu()");
Log.d("DEBUG", String.valueOf(categories));
CategoryAdapter categoryAdapterOne = new
CategoryAdapter(Home2Activity.this, categories);
Log.d("DEBUG", "adapter" + categoryAdapterOne);
mainMenu.setAdapter(categoryAdapterOne);
}
private void getSliderImages() {
compositeDisposable.add(mService
.getBanners()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<Banner>>() {
#Override
public void accept(List<Banner> banners) throws Exception
{
Log.d("DEBUG", "getSlider Images");
setUpSlider(banners);
}
}));
}
private void setUpSlider(List<Banner> banners) {
HashMap<String, String> banner_map = new HashMap<>();
for (Banner item : banners) {
banner_map.put(item.getName(), item.getLink());
}
for (String name : banner_map.keySet()) {
TextSliderView textSliderView = new
TextSliderView(Home2Activity.this);
textSliderView.description(name)
.image(banner_map.get(name))
.setScaleType(BaseSliderView.ScaleType.Fit);
mainSlider.addSlider(textSliderView);
}
}
private void setUpHeadder(NavigationView navigationView) {
View nav_headder = navigationView.getHeaderView(0);
navName = nav_headder.findViewById(R.id.profile_name);
navPhoto = nav_headder.findViewById(R.id.profile_phone);
circleImage = nav_headder.findViewById(R.id.profile_avtar);
//----------NAV NAME
navName.setText(Common.currentUser.getName());
//----------NAV PHONE
navPhoto.setText(Common.currentUser.getPhone());
//----------SETUP AVATAR----------//
if (TextUtils.isEmpty(Common.currentUser.getAvtarUrl())) {
Picasso.with(this)
.load(String.valueOf(new StringBuilder(Common.BASE_URL)
.append("avtar_folder/")
.append(Common.currentUser.getAvtarUrl())))
.into(circleImage);
}
//----------NAV PHOTO UPDATER
circleImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//todo choosePhoto()
// choosePhoto();
}
});
}
private void initDatabase() {
Common.cartDatabase = CartDatabase.getInstance(this);
Common.cartRepository =
}
private void getToppingList() {
compositeDisposable.add(mService
.getDrinksByMenuID(Common.TOPPING_ID)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<Drink>>() {
#Override
public void accept(List<Drink> toppings) throws Exception
{
Common.toppingList = toppings;
}
}));
}
#Override
protected void onResume() {
super.onResume();
// updateCartCounter();
}
#Override
protected void onDestroy() {
compositeDisposable.dispose();
super.onDestroy();
}
#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) {
getMenuInflater().inflate(R.menu.home2, 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_cart) {
// Toast.makeText(this, "Cart activity",
Toast.LENGTH_SHORT).show();
// }
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 = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
android.content.res.Resources$NotFoundException: Resource ID #0x7f07006f
This error means that your app trying to bind view with some id that doesn't exist. Check your ids. If everything is correct - invalidate cache and restart Android studio.
E/RecyclerView: No adapter attached; skipping layout
This warning in most cases means that list size with data in adapter is 0.
Try to call findViewById from menuItem and not from actionView

Progress bar & Loading text not showing up everything looks correct in my code

I'm using local html files which contains links in it but when i click on links it is not showing up progress bar & loading text in webview
this is my Mainactivity.java file
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
WebView wv;
#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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
wv = (WebView)findViewById(R.id.webview);
wv.loadUrl("file:///android_asset/www/index.html");
wv.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
}
#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_exit) {
System.exit(0);
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;
}
private class MyWebViewClient extends WebViewClient {
TextView txt = (TextView) findViewById(R.id.txtload);
ProgressBar pbar = (ProgressBar) findViewById(R.id.pg1);
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
wv.loadUrl("file:///android_asset/www/error.html");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
System.out.println("loading... please wait");
pbar.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("finished loading");
pbar.setVisibility(View.GONE);
txt.setVisibility(View.GONE);
}
}
}
content.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_main">
<TextView
android:id="#+id/txtload"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Loading... Please Wait"
android:textColor="#android:color/holo_orange_dark"
android:textSize="14sp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="209dp" />
<ProgressBar
android:id="#+id/pg1"
style="#android:style/Animation.Dialog"
android:layout_width="match_parent"
android:layout_height="44dp"
android:gravity="center"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="4dp" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_light"></WebView>
</android.support.constraint.ConstraintLayout>
Android studio displays the views in the order that they appear in the XML file. What you need to do is reorder the views. The webview should be first then followed by the Textview and then the progress view.
Thing is, it's in the app and it's showing, just not to the user or to you while running the app. The webview is obscuring the others. See if it will help and mark as correct answer.

Android Webview crashes on android 4.4 while opening external url

I am developing an application where I am using a webview. Everything is working fine but when I try to open external links inside webview it either freezes or crashes.This happens on android 4.4 and below but works well on android 5.0 and above.
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
///initialize webview
private WebView mwebview;
ProgressBar bar;
private GoogleApiClient client;
#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);
mwebview = (WebView) findViewById(R.id.myWebview);
bar = (ProgressBar) findViewById(R.id.progressBar2);
WebSettings webSettings = mwebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mwebview.loadUrl("http://stalls.co.ke/shopping");
// improve webview perfomance
mwebview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
if (Build.VERSION.SDK_INT >= 19) {
mwebview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
mwebview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
mwebview.getSettings().setAppCacheEnabled(true);
mwebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
//force links to open in webview only
mwebview.setWebViewClient(new MyWebViewClient());
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
#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.nav_home) {
// Handle the camera action
mwebview.loadUrl("http://stalls.co.ke/shopping");
} else if (id == R.id.nav_gifts) {
mwebview.loadUrl("http://stalls.co.ke/gifts-coupons");
} else if (id == R.id.nav_men) {
mwebview.loadUrl("http://stalls.co.ke/stallswear-men");
} else if (id == R.id.nav_women) {
mwebview.loadUrl("http://stalls.co.ke/stallswear-women");
} else if (id == R.id.nav_kids) {
mwebview.loadUrl("http://stalls.co.ke/stallswear-kids-baby");
} else if (id == R.id.nav_men_cosmetics) {
mwebview.loadUrl("http://stalls.co.ke/copy-of-stalls-cosmetics");
} else if (id == R.id.nav_women_cosmetics) {
mwebview.loadUrl("http://stalls.co.ke/copy-of-men-footwear");
} else if (id == R.id.nav_kids_cosmetics) {
mwebview.loadUrl("http://stalls.co.ke/copy-of-stalls-cosmetics");
} else if (id == R.id.nav_homes) {
mwebview.loadUrl("http://stalls.co.ke/home-kitchen-products");
} else if (id == R.id.nav_garden) {
mwebview.loadUrl("http://stalls.co.ke/garden-products");
} else if (id == R.id.nav_smarthomes) {
mwebview.loadUrl("http://stalls.co.ke/smart-home-products");
} else if (id == R.id.nav_eatery) {
mwebview.loadUrl("http://stalls.co.ke/stallseatery");
} else if (id == R.id.nav_customer_service) {
mwebview.loadUrl("http://stalls.co.ke/help");
} else if (id == R.id.nav_about_us) {
mwebview.loadUrl("http://stalls.co.ke/about-us");
} else if (id == R.id.nav_kijo) {
mwebview.loadUrl("http://stalls.co.ke/house-of-kinjo");
} else if (id == R.id.nav_Airi) {
mwebview.loadUrl("http://stalls.co.ke/Airi");
} else if (id == R.id.nav_mamapeter) {
mwebview.loadUrl("http://stalls.co.ke/house-of-kinjo");
} else if (id == R.id.nav_swahili) {
mwebview.loadUrl("http://stalls.co.ke/swahili-princess");
} else if (id == R.id.nav_allstalls) {
mwebview.loadUrl("http://stalls.co.ke/kenyan-stores");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
#Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
#Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
// go to previous page when back button is pressed
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mwebview.canGoBack()) {
mwebview.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
bar.setVisibility(view.GONE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mwebview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.loadUrl(request.getUrl().toString());
}
return false;
}
});
} else {
mwebview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
return false;
}
}
}
This is my MainActivity.java file, I would like to know why this is happening.
try using WebChromeClient; instead of WebViewClient; see if it works.

Material design: naviagtion drawer with tabs and recyclerview

I have a problem with the navigation drawer. I want to know how to display recyclerview when the user clicks on different element of each menu.
Here are some source code and an illustrative capture
N.B: different menu items ("Accueil, Contacts Staff, etc ...") are in an .xml file in the layout.
MainActiviy.java
public class MainActivity extends AppCompatActivity {
ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
setupWindowAnimations();
//définir la toolbr en tant qu'actionbar
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, 0, 0);
drawerLayout.setDrawerListener(drawerToggle);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
//on remplit notre viewpager, comme à notre habitude
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
#Override
public Fragment getItem(int position) {
return RecyclerViewFragment.newInstance();
}
#Override
public CharSequence getPageTitle(int position) {
return "Tab " + position;
}
#Override
public int getCount() {
return 1;
}
});
//indique au tablayout quel est le viewpager à écouter
tabLayout.setupWithViewPager(viewPager);
}
private void setupWindowAnimations() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Explode explode = new Explode();
getWindow().setExitTransition(explode);
Fade fade = new Fade();
getWindow().setReenterTransition(fade);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
#OnClick(R.id.fab)
public void onFabClick() {
Snackbar.make(fab, "Here's a Snackbar", Snackbar.LENGTH_LONG)
.setAction("Undo", new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}).show();
}
}
Sample source code for you
public class NavActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav);
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();
}
}
#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;
}
}
Hope this helps you
Not understand exactly what you want. Where do you want to display recycler view? You already have OnNavigationItemSelectedListener, just check which menu you have clicked and do what you want:
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
 case R.id.contact:
// do anything you want, ex
showContactList();
break;
}
the function to show your contact list
private void showContactList(){
List<Contact> data = getYourDataSomeHow();
yourAdapter.setData(data);
yourRecyclerView.setAdapter(yourAdapter);
}

Android AppCompatActivity with ViewPager and Fragments containing scrollview - scroll gestures don't work properly

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.

Categories

Resources