Handling of pushing menu item. Android - java

I have the following code:
MainActivity.java:
package asus.example.com.fitnessapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.nav_items,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.home:
textView.setText("Home");
return true;
case R.id.notification:
textView.setText("Notification");
return true;
case R.id.profile:
textView.setText("Profile");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.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"
tools:context=".MainActivity">
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="52dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/nav_items"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"/>
</android.support.constraint.ConstraintLayout>
nav_items.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:title="#string/home"
android:icon="#drawable/home" />
<item
android:id="#+id/notification"
android:title="#string/notifications"
android:icon="#drawable/notification" />
<item
android:id="#+id/profile"
android:icon="#drawable/person"
android:title="#string/profile" />
</menu>
When user clicks on any element of the bottom navigation bar the program should add to the textview the name of this element of menu. That's why there's the following lines in code, such as textView.setText("Home"); and other. But when user clicks on any elements, there's nothing shows int textview. What's the matter?

You are using a BottomNavigationView not a menu. You need to implement the BottomNavigationView.OnNavigationItemSelectedListener like this:
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{
and then you need to set the listener in the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
BottomNavigationView bottomNav = findViewById(R.id.bottomNav);
bottomNav.setNavigationItemSelectedListener(this);
}
For this to work you need to add android:id="#+id/bottomNav" to your layout file:
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="52dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/nav_items"/>
And lastly the method to handle the OnNavigationItemSelectedListener event:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()){
case R.id.home:
textView.setText("Home");
return true;
case R.id.notification:
textView.setText("Notification");
return true;
case R.id.profile:
textView.setText("Profile");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You will not need onOptionsItemSelected() or onCreateOptionsMenu() if you just want to use the BottomNavigationView.

Related

Using a toggle button to start and stop a menu item

This is the code inside of my MainActivity.java:
I am trying to use a toggle button to toggle a menu item to be on and off. The menu item when checked sets the background to red. When the item is not selected the background color is default. I am new to java so I am unsure what to put in the if and else statement under onCheckedChange. Any suggestions would help.
Thanks
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button showFragBut = (Button)findViewById(R.id.button);
showFragBut.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
String butText = showFragBut.getText().toString();
FragmentManager fg = getSupportFragmentManager();
FragmentTransaction ft = fg.beginTransaction();
if (butText.equals("Add fragment")){
Frag1 frag = new Frag1();
ft.add(R.id.dyframe, frag, null);
ft.addToBackStack(null);
ft.commit();
showFragBut.setText("Remove fragment");
} else {
getSupportFragmentManager().popBackStackImmediate();
showFragBut.setText("Add fragment");
}
MenuItem redMenuItem = (MenuItem)findViewById(R.id.menu_red);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton4);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
} else {
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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.
ConstraintLayout mainLayout = (ConstraintLayout)findViewById(R.id.content1);
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id){
case R.id.action_settings:
if (item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
Toast.makeText(getApplicationContext(), "Setting is clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.menu_red:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(Color.RED);
return true;
case R.id.menu_green:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(Color.GREEN);
return true;
}
return super.onOptionsItemSelected(item);
}
}
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:id="#+id/content1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="128dp"
android:layout_marginTop="116dp"
android:text="#string/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Add Fragment" />
<FrameLayout
android:id="#+id/dyframe"
android:layout_width="185dp"
android:layout_height="63dp"
android:layout_marginStart="128dp"
android:layout_marginTop="44dp"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button"/>
<ToggleButton
android:id="#+id/toggleButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginBottom="110dp"
android:text="#string/togglebutton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
<menu 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"
tools:context="com.example.brettbanich.menuactivity.MainActivity">
<group android:checkableBehavior="single" >
<item
android:id="#+id/menu_red"
android:title="#string/red"
app:showAsAction="never" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</group>
</menu>
I think that this should work:
public void setColor(int color, boolean checked) {
ConstraintLayout mainLayout = (ConstraintLayout)findViewById(R.id.content1);
mainLayout.setBackgroundColor(color);
redMenuItem.setChecked(checked);
}
and for the toggle:
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
setColor(Color.GREEN, true);
} else {
setColor(Color.RED, false);
}
}
});
I'm not sure if the colors are swapped or not, the idea is something like this. I assume that the code in setColor() that you used in the onOptionsImetSelected() works as you expect.

OnOptionsItemSelected doesn't work on android API 19 (but works on android 24)

I have the following code in my activity class
public class admin_fab_features_grid extends
base_activity_for_admin_home_page implements Parcelable {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity_context = getApplicationContext();
Intent iIntent= getIntent();
Bundle b = iIntent.getExtras();
// display the admin_fab_features_grid that contain the dashboard list of all the admin options
setContentView(R.layout.admin_fab_features_grid);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar_toolbar);
setSupportActionBar(toolbar);
}
The base_activity_for_admin_home_page.java has the following code
public class base_activity_for_admin_home_page extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Tag, " Into base activity for parent menu");
_context = getApplicationContext();
// display the admin_fab_features_grid that contain the dashboard list of all the admin options
setContentView(R.layout.admin_fab_features_grid);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar_toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_admin_home_page, menu);
Log.d(Tag, "inflate done");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionbar_settings:
return true;
}
}
}
The menu_admin_home_page has the following 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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyActivity">
<item android:id="#+id/actionbar_settings"
android:title="#string/actionbar_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<!--
<item android:id="#+id/actionbar_dropoff"
android:title="#string/actionbar_dropoff"
android:orderInCategory="90"
app:showAsAction="never" />
<item android:id="#+id/actionbar_signin"
android:title="#string/actionbar_signin"
android:orderInCategory="85"
app:showAsAction="never" />
-->
<item android:id="#+id/actionbar_logout"
android:title="#string/actionbar_logout"
android:orderInCategory="80"
app:showAsAction="never" />
</menu>
The "Settings" menu is enabled in the Toolbar. I can see ":" in the Toolbar. On a device that has Android API24 I am able to click on it and the OnOptionsItemSelected function is getting called. But the same application on a device that has Android API 19 the OnOptionsItemSelected does not get called. Can someone help?
change onOptionsItemSelected() to this and try
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.actionbar_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Since you set the showAsAction attribute to never, then these menu items will never show as action views. Try this:
showAsAction = ifRoom|withText
switch (item.getItemId()) {
case R.id.actionbar_settings:
doyouraction();
return true;
default: return super.onOptionsItemSelected(item);
} }
Solved it after 4 days of pain.
What occurred is that the CoordinatorLayout overlays the GridView on top of the toolbar and that is why the onOptionsItemSelected icons were not getting called. They're being blocked. Toolbar doesn't overlay on-top of the GridView like an actionbar. Its a view itself. To solve this I removed the CoordinatorLayout add an id to the Toolbar and then add android:layout_below="#id/toolbar".
<?xml version="1.0" encoding="utf-8"?>
<!--
// <android.support.design.widget.CoordinatorLayout
-->
<LinearLayout
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="fab_features.admin_fab_features_grid">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#id/app_bar_toolbar"
style="#style/Widget_v7_Toolbar_style"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.Toolbar
android:id="#id/app_bar_toolbar"
style="#style/Widget_v7_Toolbar_style"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/admin_fab_features_grid"
style="#style/admin_fab_features_grid_gridview"
android:layout_below="#id/app_bar_toolbar"
/>
</LinearLayout>

Add options menu to QR reader application in Android Studio?

I'm learning to code apps for android and I'm trying to code an app that reads QR codes, but I want to add an options menu. I have a small code that does it, but when I try to add it the QR reader app the menu does not show. Can you tell me what is wrong with what I have so far?
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.view.View;
import android.app.AlertDialog;
import android.util.Log;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Here I added the menu to the qr app
#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) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//Here ends the part of the menu
public void onClick(View v){
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
Log.w("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1"
android:title="Item 1"/>
<item android:id="#+id/item2"
android:title="Item 2"/>
<item android:id="#+id/item3"
android:title="Item 3"/>
</menu>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridLayout
android:layout_width="match_parent"
android:layout_height="114dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:onClick="onClick"
android:text="Scan QR" />
</GridLayout>
</LinearLayout>
I know that the options menu works by itself, because I tested alone as a different app, and also the qr reader works by itself, but if I try to combine them the menu is not there. I'm a newbie on coding android apps, so I will appreciate your guidance. Thanks
your menu name you supplied is menu.xml but instead you are giving getMenuInflater().inflate(R.menu.main, menu);
you should replace
getMenuInflater().inflate(R.menu.main, menu);
with
getMenuInflater().inflate(R.menu.menu, menu);

How to create an abstract activity class and extend it to all activities?

I have navigation drawer menu and 6 activities. I want to create an abstract activity class, and move all my drawer menu code to it, and then extend it.
This is my Main activity.java and my drawer's code in this activity.
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
protected DrawerLayout drawerLayout;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
TextView tv = (TextView)findViewById(R.id.textView);
Typeface textfont = Typeface.createFromAsset(getAssets(),"fonts/B Roya_YasDL.com.ttf");
tv.setTypeface(textfont);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.a1);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_more_vert_black_24dp);
navigationView = (NavigationView) findViewById(R.id.a2);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
item.setChecked(true);
drawerLayout.closeDrawer(Gravity.RIGHT);
switch (item.getItemId()){
case R.id.intro:
Intent i = new Intent(MainActivity.this, IntroActivity.class);
startActivity(i);
break;
}
return false;
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.khat) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
}
else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return super.onOptionsItemSelected(item);
}
else {
Toast.makeText(getApplicationContext(),"somthing",Toast.LENGTH_LONG).show();
}
return true;
}
public void onBackPressed(){
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)){
drawerLayout.closeDrawer(Gravity.RIGHT);
}else {super.onBackPressed();}
}
and this is my second activity for example:
public class IntroActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
}
How can I create an abstract activity class and insert my drawer's code in there and call it from any activity that I want?
You have to create mainActivity like
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
public NavigationView navigationView;
public DrawerLayout drawer;
public RelativeLayout content;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
drawer=(DrawerLayout) findViewById(R.id.drawer_layout);
content = (RelativeLayout) findViewById(R.id.content);
navigationView = (NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void setContentView(#LayoutRes int layoutResID) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(layoutResID, null);
// var x = layout.LayoutParameters;
content.addView(layout);
}
public void OpenDrawer()
{
drawer.openDrawer(Gravity.START,true);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navdrawer_item_first:
Toast.makeText(this, "first item clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.navdrawer_item_second:
Toast.makeText(this, "second item clicked", Toast.LENGTH_SHORT).show();
break;
}
drawer.closeDrawers();
return true;
}
}
And activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:drawer="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
drawer:menu="#menu/navigation_drawer_items"
drawer:headerLayout="#layout/drawer_header"
android:background="#343a46"
drawer:itemTextColor="#ffffff" />
</android.support.v4.widget.DrawerLayout>
then you will extend Mail Activity
public class SecondActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_second);
}
}
and in Manifest make second activity is the launcher:
<activity android:name=".MainActivity">
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>s
</activity>
I don't recommend making a abstract activity for this purpose.
Instead you should have just one activity where your navigation drawer is attached.And replace your activities with fragments except for MainActivity which would contain the navigation drawer.
activity_main layout would looks like:
-DrawerLayout
-FrameLayout
-NavigationView
And in the main activity whenever user clicks on any of the item in NavigationDrawer just replace the fragment with the required one.
navigation_drawer_items
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/group_feature"
android:checkableBehavior="single">
<item android:id="#+id/navdrawer_item_first"
android:title="first item"/>
<item android:id="#+id/navdrawer_item_second"
android:title="second item"/>
</group>
</menu>
drawer_header
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#343a46"
android:orientation="vertical"
android:padding="10dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textColor="#dfba6b"
android:textStyle="bold" />
</LinearLayout>

Why isnt my "about" tag recognised in R

I am trying to implement a dialog for an app I am creating for my own educational purposes using this tutorial, http://javatechig.com/android/android-dialog-fragment-example.
In my notepadlistactivity.java I have the section,
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.addNote:
openNote(new Note(noteManager));
break;
case android.R.id.home:
break;
case android.R.id.about:
FragmentManager fm = getFragmentManager();
MyDialogFragment dialogFragment = new MyDialogFragment ();
dialogFragment.show(fm, "Sample Fragment");
default:
return false;
}
return true;
}
For all the other R.id....'s it recognises the word after the dot, but for R.id.about, the about is not recognised. I have checked in the R file and about is defined so I cannot understand what is going wrong.
For your information this is my MyDialogFragment.java,
package com.nicae.Notepad.note_list;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.nicae.Notepad.R;
public class MyDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sample_dialog, container, false);
getDialog().setTitle("Simple Dialog");
return rootView;
Button dismiss = (Button) rootView.findViewById(R.id.dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
}
This is my fragment_sample_dialog.xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp"
android:orientation="vertical"
android:id="#+id/about">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet..."
android:textSize="20dp" />
<Button
android:id="#+id/dismiss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
</LinearLayout>
Edit corresponding to the comments below:
Here is my activity_notepad_list.xml,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/addNote"
android:title="#string/newNote"
android:orderInCategory="1"
app:showAsAction="always"
android:icon="#drawable/content_new"/>
<item
android:id="#+id/about"
android:title="About"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_ab_other"
android:orderInCategory="2"/>
</menu>
Note that android.R.id is different than R.id depending on the current package name where the current class is and/or import statements. android.R.id refers to standard values from the Android API. You probably mean to use R.id instead.
This is because you are using same id for LinearLayout and menu item. Remove android:id="#+id/about" from LinearLayout (or change it to "#+id/aboutLayout").
And use,
case R.id.about: // instead of android.R.id.about
FragmentManager fm = getFragmentManager();
MyDialogFragment dialogFragment = new MyDialogFragment ();
dialogFragment.show(fm, "Sample Fragment");

Categories

Resources