Weird crash when creating menu - java

I'm changing things around inside of my onCreateOptionsMenu and I'm getting a really weird crash. Logcat tells me it crashed:
This is the crash :
at com.mycompany.myapp.MainActivity.onCreateOptionsMenu(MainActivity.java)
at android.app.Activity.onCreatePanelMenu(Activity.java:2476)
And that it's a null pointer exception.
Here's my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.main_menu, menu);
MenuItem shareNew = menu.findItem(R.id.shareNew);
MenuItem shareOld = menu.findItem(R.id.shareOld);
mShareActionProvider = (ShareActionProvider) shareNew.getActionProvider();
mShareActionProvider.setShareIntent(createShareIntent());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
shareOld.setVisible(true);
shareNew.setVisible(false);
} else {
shareOld.setVisible(false);
shareNew.setVisible(true);
}
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setQueryHint("Search articles");
setupSearchView(searchItem);
return true;
}
And here's my main_menu.xml:
<item
android:id="#+id/starred"
android:title="Starred articles" />
<item
android:queryHint="Search articles"
android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
<item
android:actionProviderClass="android.widget.ShareActionProvider"
android:id="#+id/shareNew"
android:title="Share"
android:showAsAction="ifRoom"
android:icon="#drawable/share"/>
<item
android:id="#+id/shareOld"
android:title="Share"
android:showAsAction="ifRoom"
android:icon="#drawable/share"/>
<item
android:id="#+id/star"
android:title="Star article"
android:showAsAction="ifRoom"
android:icon="#drawable/star" />
<item
android:id="#+id/refresh"
android:title="Refresh"
android:icon="#drawable/refresh"
android:showAsAction="ifRoom" />
<item
android:id="#+id/forward"
android:title="Forward"
android:icon="#drawable/forward"
android:showAsAction="ifRoom" />
<item
android:showAsAction="ifRoom"
android:icon="#drawable/copy"
android:id="#+id/copy"
android:title="Copy url" />
<item
android:id="#+id/browser"
android:title="Open in browser" />
<item
android:id="#+id/startPage"
android:title="Use as startpage" />
<item
android:id="#+id/random"
android:title="Random article" />
<item
android:id="#+id/settings"
android:title="Settings" />
Help would be really appreaciated (and needed :p)

Related

Click a string/link on navigation drawer that loads a local HTML page

I'm a beginner with android development and I need your assistance, I have a simple app with a webview to load local HTML pages, I put all my files inside assets, now in my navigation drawer I want to have some text/links that open these pages.
here is my code:
MainActivity.java
package com.example.psalms;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
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;
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
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);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/index.html");
}
#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();
}
}
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:title="#string/home" />
<item
android:title="#string/erminia" />
<item
android:title="#string/psalmos1" />
<item
android:title="#string/psalmos2" />
<item
android:title="#string/psalmos3" />
<item
android:title="#string/psalmos4" />
<item
android:title="#string/psalmos5" />
<item
android:title="#string/psalmos6" />
<item
android:title="#string/psalmos7" />
<item
android:title="#string/psalmos8" />
<item
android:title="#string/psalmos9" />
<item
android:title="#string/psalmos10" />
<item
android:title="#string/psalmos11" />
<item
android:title="#string/psalmos12" />
<item
android:title="#string/psalmos13" />
<item
android:title="#string/psalmos14" />
<item
android:title="#string/psalmos15" />
<item
android:title="#string/psalmos16" />
<item
android:title="#string/psalmos17" />
<item
android:title="#string/psalmos18" />
<item
android:title="#string/psalmos19" />
<item
android:title="#string/psalmos20" />
<item
android:title="#string/psalmos21" />
<item
android:title="#string/psalmos22" />
<item
android:title="#string/psalmos23" />
<item
android:title="#string/psalmos24" />
<item
android:title="#string/psalmos25" />
<item
android:title="#string/psalmos26" />
<item
android:title="#string/psalmos27" />
<item
android:title="#string/psalmos28" />
<item
android:title="#string/psalmos29" />
<item
android:title="#string/psalmos30" />
<item
android:title="#string/psalmos31" />
<item
android:title="#string/psalmos32" />
<item
android:title="#string/psalmos33" />
<item
android:title="#string/psalmos34" />
<item
android:title="#string/psalmos35" />
<item
android:title="#string/psalmos36" />
<item
android:title="#string/psalmos37" />
<item
android:title="#string/psalmos38" />
<item
android:title="#string/psalmos39" />
<item
android:title="#string/psalmos40" />
<item
android:title="#string/psalmos41" />
<item
android:title="#string/psalmos42" />
<item
android:title="#string/psalmos43" />
<item
android:title="#string/psalmos44" />
<item
android:title="#string/psalmos45" />
<item
android:title="#string/psalmos46" />
<item
android:title="#string/psalmos47" />
<item
android:title="#string/psalmos48" />
<item
android:title="#string/psalmos49" />
<item
android:title="#string/psalmos50" />
<item
android:title="#string/psalmos51" />
<item
android:title="#string/psalmos52" />
<item
android:title="#string/psalmos53" />
<item
android:title="#string/psalmos54" />
<item
android:title="#string/psalmos55" />
<item
android:title="#string/psalmos56" />
<item
android:title="#string/psalmos57" />
<item
android:title="#string/psalmos58" />
<item
android:title="#string/psalmos59" />
<item
android:title="#string/psalmos60" />
<item
android:title="#string/psalmos61" />
<item
android:title="#string/psalmos62" />
<item
android:title="#string/psalmos63" />
<item
android:title="#string/psalmos64" />
<item
android:title="#string/psalmos65" />
<item
android:title="#string/psalmos66" />
<item
android:title="#string/psalmos67" />
<item
android:title="#string/psalmos68" />
<item
android:title="#string/psalmos69" />
<item
android:title="#string/psalmos70" />
<item
android:title="#string/psalmos71" />
<item
android:title="#string/psalmos72" />
<item
android:title="#string/psalmos73" />
<item
android:title="#string/psalmos74" />
<item
android:title="#string/psalmos75" />
<item
android:title="#string/psalmos76" />
<item
android:title="#string/psalmos77" />
<item
android:title="#string/psalmos78" />
<item
android:title="#string/psalmos79" />
<item
android:title="#string/psalmos80" />
<item
android:title="#string/psalmos81" />
<item
android:title="#string/psalmos82" />
<item
android:title="#string/psalmos83" />
<item
android:title="#string/psalmos84" />
<item
android:title="#string/psalmos85" />
<item
android:title="#string/psalmos86" />
<item
android:title="#string/psalmos87" />
<item
android:title="#string/psalmos88" />
<item
android:title="#string/psalmos89" />
<item
android:title="#string/psalmos90" />
<item
android:title="#string/psalmos91" />
<item
android:title="#string/psalmos92" />
<item
android:title="#string/psalmos93" />
<item
android:title="#string/psalmos94" />
<item
android:title="#string/psalmos95" />
<item
android:title="#string/psalmos96" />
<item
android:title="#string/psalmos97" />
<item
android:title="#string/psalmos98" />
<item
android:title="#string/psalmos99" />
<item
android:title="#string/psalmos100" />
<item
android:title="#string/psalmos101" />
<item
android:title="#string/psalmos102" />
<item
android:title="#string/psalmos103" />
<item
android:title="#string/psalmos104" />
<item
android:title="#string/psalmos105" />
<item
android:title="#string/psalmos106" />
<item
android:title="#string/psalmos107" />
<item
android:title="#string/psalmos108" />
<item
android:title="#string/psalmos109" />
<item
android:title="#string/psalmos110" />
<item
android:title="#string/psalmos111" />
<item
android:title="#string/psalmos112" />
<item
android:title="#string/psalmos113" />
<item
android:title="#string/psalmos114" />
<item
android:title="#string/psalmos115" />
<item
android:title="#string/psalmos116" />
<item
android:title="#string/psalmos117" />
<item
android:title="#string/psalmos118" />
<item
android:title="#string/psalmos119" />
<item
android:title="#string/psalmos120" />
<item
android:title="#string/psalmos121" />
<item
android:title="#string/psalmos122" />
<item
android:title="#string/psalmos123" />
<item
android:title="#string/psalmos124" />
<item
android:title="#string/psalmos125" />
<item
android:title="#string/psalmos126" />
<item
android:title="#string/psalmos127" />
<item
android:title="#string/psalmos128" />
<item
android:title="#string/psalmos129" />
<item
android:title="#string/psalmos130" />
<item
android:title="#string/psalmos131" />
<item
android:title="#string/psalmos132" />
<item
android:title="#string/psalmos133" />
<item
android:title="#string/psalmos134" />
<item
android:title="#string/psalmos135" />
<item
android:title="#string/psalmos136" />
<item
android:title="#string/psalmos137" />
<item
android:title="#string/psalmos138" />
<item
android:title="#string/psalmos139" />
<item
android:title="#string/psalmos140" />
<item
android:title="#string/psalmos141" />
<item
android:title="#string/psalmos142" />
<item
android:title="#string/psalmos143" />
<item
android:title="#string/psalmos144" />
<item
android:title="#string/psalmos145" />
<item
android:title="#string/psalmos146" />
<item
android:title="#string/psalmos147" />
<item
android:title="#string/psalmos148" />
<item
android:title="#string/psalmos149" />
<item
android:title="#string/psalmos150" />
<item
android:title="#string/psalmos151" />
</group>
</menu>
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.example.psalms.ui.home.HomeFragment"
android:label="#string/home"
tools:layout="#layout/fragment_home">
<action
android:id="#+id/action_HomeFragment_to_HomeSecondFragment"
app:destination="#id/nav_home_second" />
</fragment>
<fragment
android:id="#+id/nav_home_second"
android:name="com.example.psalms.ui.home.HomeSecondFragment"
android:label="#string/home_second"
tools:layout="#layout/fragment_home_second">
<action
android:id="#+id/action_HomeSecondFragment_to_HomeFragment"
app:destination="#id/nav_home" />
<argument
android:name="myArg"
app:argType="string" />
</fragment>
<fragment
android:id="#+id/nav_gallery"
android:name="com.example.psalms.ui.gallery.GalleryFragment"
android:label="#string/erminia"
tools:layout="#layout/fragment_gallery" />
<fragment
android:id="#+id/nav_slideshow"
android:name="com.example.psalms.ui.slideshow.SlideshowFragment"
android:label="#string/psalmos1"
tools:layout="#layout/fragment_slideshow" />
</navigation>
First you need to place IDs in your menu item xml, for you to be able to distinguish and call them (yes, all of them) here is an example:
<item
android:id="#+id/nav_home"
android:title="#string/home" />
You need to add onNavigationItemSelectedListener:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
Then add method:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
String url = "";
switch (item.getItemId()) {
case R.id.nav_home: {
url = "https://google.com";
break;
}
// Add all your items as case
}
// We will make this method later.
openUrl(url);
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
Add this below your navigationView code:
navigationView.setNavigationItemSelectedListener(this);
Create this function:
private void openUrl(final String url) {
if (url!= null) {
// This can also be an intent to your WebView Activity, passing the link in the extras
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
}

How can I implement onCreateOptionsMenu on my bottomNavigationView

Hi I wouldike to know when I click on my bottomnavigationView how to display my search toolbar ?
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
return true;
case R.id.navigation_loope:
toolbar_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Search"
android:id="#+id/action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
Bottom_nav_view.xml
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_loope"
android:icon="#drawable/ic_loope"
android:title="#string/title_loope"
/>
<item
android:id="#+id/navigation_take_photo"
android:icon="#drawable/ic_take_photo"
android:title="#string/title_take_photo" />
<item
android:id="#+id/navigation_public"
android:icon="#drawable/ic_public"
android:title="#string/title_public" />
</menu>
I can't actually use getMenuInflater().inflate(R.menu.toolbar_menu, menu) I don't know how to implement it for doing the same result when i click on my loope (R.id.navigation_loope)
EDIT : Bottom_nav_view it's for my bottom bar and toolbar_menu for my topbar. If I click on the item navigation_loop, I wouldike to show the search bar on the top.

Android Activity Menu

I wanted to create an option menu which shows up similar to Whatsapp even if the device has its own "hardware" option button. The solution I found online worked just fine until I wanted to add another action to the Bar.
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title=""
android:id="#+id/addViewedMovie"
android:icon="#mipmap/ic_add_to_queue_black_24px"
android:showAsAction="always"
></item>
<item
android:title=""
android:id="#+id/menu_overflow"
android:icon="#mipmap/ic_more_vert_white_48dp"
app:showAsAction="always">
<menu>
<item
android:id="#+id/show_settings"
app:showAsAction="never"
android:title="#string/setttings"/>
<item
android:id="#+id/show_help"
app:showAsAction="never"
android:title="#string/help"/>
<item
android:id="#+id/show_about"
app:showAsAction="never"
android:title="#string/about"/>
</menu>
</item>
</menu>
And this in my MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
With this the app forces the addViewedMovie Action into an Overflow-Menu. Does anyone know how to prevent this?
Try this code and tell me if its work.. you should use app:showAsAction not android:showAsAction also you do not need to use submenu to show 3 dots.. just below code works fine also show 3 dots
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Movie"
android:id="#+id/addViewedMovie"
android:icon="#mipmap/ic_add_to_queue_black_24px"
app:showAsAction="always"/>
<item
android:id="#+id/show_settings"
app:showAsAction="never"
android:title="#string/setttings"/>
<item
android:id="#+id/show_help"
app:showAsAction="never"
android:title="#string/help"/>
<item
android:id="#+id/show_about"
app:showAsAction="never"
android:title="#string/about"/>
</menu>
Create a folder name Menu in your res folder.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:icon="#drawable/ic_launcher"
myapp:showAsAction="ifRoom" />
<item android:id="#+id/abc"
android:title="ABC" />
<item android:id="#+id/abc1"
android:title="ABC1" />
<item android:id="#+id/abc2"
android:title="ABC2" />
</menu>
In java file put this:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
return true;
case R.id.abc:
return true;
case R.id.abc1:
return true;
case R.id.abc2;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Simple.
use app instead of android as app is for design support library.
And so, in Gradle build in dependencies, you must have: compile com.android.support:design..... like that. But, I have seen that you already used it below. So,
Just use: app:showAsAction="never" instead of android:showAsAction="always" in your addViewedMovie item.
Remember: never means it will be on overflow menu item. always means is will show in action bar.
If you are using in a fragment, add below line in the onCreate
setHasOptionsMenu(true);
And two more functions
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
Inflate your views in the above method. Listen to the action in the below one.
#Override public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

How to get a thinner font in options menu android

I've tried making a option menu in my activity, and I succeed. The problem now, is that the text style is bold, and I don't know how to make it normal. I tried setting textStyle, but it had no effect.
option_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/AppointmentItem"
app:showAsAction="always"
android:icon="#drawable/ic_notifications_white_24dp"
android:title="Appointments"/>
<item android:id="#+id/WeekItem"
app:showAsAction="never"
android:icon="#drawable/ic_today_24dp"
android:title="Week vooruit/achteruit"/>
<item android:id="#+id/LeerlingItem"
app:showAsAction="never"
android:icon="#drawable/ic_person_24dp"
android:title="Leerling veranderen"/>
<item android:id="#+id/InstelllingenItem"
app:showAsAction="never"
android:icon="#drawable/ic_settings_24dp"
android:title="Instellingen"/>
<item android:id="#+id/OverItem"
app:showAsAction="never"
android:icon="#drawable/ic_work_white_24dp"
android:title="Over ons"/>
</menu>
How I create the options menu in my mainactivity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu); //your file name
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.AppointmentItem:
return true;
case R.id.WeekItem:
getWeek();
return true;
case R.id.LeerlingItem:
getLeerling();
return true;
case R.id.InstelllingenItem:
getSettings();
return true;
case R.id.OverItem:
getAbout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
How it looks (first) and how I want it (last):
Fixed it by changing "TextAppearance.AppCompat.Widget.ActionBar.Title" to "TextAppearance.AppCompat.Widget.ActionBar.Subtitle".
<style name="MyActionBar.MenuTextStyle" parent="style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
<item name="android:textColor">#color/black</item>
<item name="android:textStyle">normal</item>
<item name="android:textSize">16dp</item>
</style>
Thanks to #Rotwang for advising me to check the styles.xml

Android Menus and Sub-Menus

I'm having trouble creating a radio-button sub-menu that is shown when a menu item from the options menu that appears when a user presses the menu button is selected. This is what I have so far:
// Expand the options menu when the user taps their menu button
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch item.getItemId() {
case R.id.expandRadioMenu:
// Show the sub-menu and collapse the initial menu
return true;
}
}
The XML of res/menu/menu.xml is as follows:
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:visible="true"
android:enabled="true"
android:checkable="false"
android:icon="#drawable/ic_menu_mark"
android:id="#+id/expandRadioMenu"
android:title="Select"
android:titleCondensed="select">
<menu>
<group
android:enabled="true"
android:visible="false"
android:checkableBehavior="single"
android:id="#+id/radio">
<item
android:enabled="true"
android:visible="true"
android:title="Foo"
android:titleCondensed="Foo"
android:id="#+id/foo">
</item>
<item
android:enabled="true"
android:visible="true"
android:title="Bar"
android:titleCondensed="Bar"
android:id="#+id/bar">
</item>
</group>
</menu>
</item>
</menu>
I know it's not particularly appealing, but have you tried moving the sub menu into a separate .xml file? I've seen it work before, but it's not a terribly hopeful solution. I suppose it might further identify the problem though.

Categories

Resources