What's wrong with this XML file? (keeps crashing aapt.exe) - android

I've had problems with my app and eclipse so I saved my code, deleting eclipse, and re-downloaded and extracted. Immediately after I added the XML file to res/menu, I got a windows message saying aapt.exe has stopped working; I'd been getting this message constantly before. I've researched it before so I know that if an XML file isn't written correctly, aapt.exe will keep crashing. Only thing is, I don't see the problem with the file.
createlgmenu.xml (used as a popup menu for a button event):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/Create_List"
android:title="#string/Create_List"/>
<item
android:id="#+id/Create_Food_Group"
android:title="#string/Create_Food_Group"/>
</menu>
|
|
Other files:
I have practically no code in my mainactivity.java:
package com.example.groceryrunner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void onCreateLGClick(View v) {
final int id = v.getId();
switch (id) {
case R.id.CreateLG:
//findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGPopup(v);
break;
/*case R.id.ListsButton:
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGMenu(v);
break;*/
}
}
public void createLGPopup(View v) {
PopupMenu LGMenu = new PopupMenu(this, v);
LGMenu.getMenuInflater().inflate(R.menu.createlgmenu, LGMenu.getMenu());
LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
String choice = new String((String) item.getTitle());
if (choice == "Create_List") {
//createListDialog();
}
else if (choice == "Create_Group") {
//createListDialog();
}
return false;
}
});
LGMenu.show();
}
}
Only one button so far in my activity_main.xml:
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/CreateLG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="37dp"
android:layout_marginTop="21dp"
android:text="+"
android:textSize="40sp" />
</RelativeLayout>
In case you want to see my menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>

If you are using the support library and a library project, then you need a custom namespace for the "showAsAction" tag for older platform versions.
So change this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
To this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
custom:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
Note that "custom" can be anything you want.

I think I've found my solution.
I had copied the xml and changed the items originally myself. So I deleted it and created it through Eclipse (right click on the res/menu folder > New > Other > Android XML File > Menu). I no longer seem to be getting the appt.exe error. I then added the items using Eclipse instead of writing or copying them myself.
The only difference in the two files is that the old one ended each item with this:
\>
And the one created by Eclipse uses this at the end of an item declaration:
></item>
As for Eclipse not recognizing my xml files, AdamM helped me find the solution to that problem as well:
1) I deleted the R import
2) Commented out the lines that were giving me errors in recognizing my xml files
3) Restarted Eclipse & Built All

Related

Android : BottomNavigationView icons are not visible

Thanks for stopping by. So my problem here is, the icons from bottom_navigation_menu.xml from menu source file are not visible. I tried Google, Stackoverflow, and here are things that I have tried but still they are not visible: Restart Android Studio, Add selector, Change dependencies to androidX, Class to androidX, re-read the documentation.
Hope someone can help me out...thanks!!!!
I'm using Android Studio 3.6.3, Gradle 6.4
Class file
package com.example.todayilearnedbeta;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class AddEntriesActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addentries);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(2);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.ic_main:
Intent intent0 = new Intent(AddEntriesActivity.this, MainActivity.class);
startActivity(intent0);
break;
case R.id.id_search:
Intent intent1 = new Intent(AddEntriesActivity.this, SearchActivity.class);
startActivity(intent1);
break;
case R.id.ic_add_entries:
break;
case R.id.ic_past_entries:
Intent intent3 = new Intent(AddEntriesActivity.this, PastEntriesActivity.class);
startActivity(intent3);
break;
case R.id.ic_profile:
Intent intent4 = new Intent(AddEntriesActivity.this, ProfileActivity.class);
startActivity(intent4);
break;
}
return false;
}
});
}
}
Layout XML codes:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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=".AddEntriesActivity"
android:background="#drawable/add_entry_activity_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="#+id/bottomBar"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bottomNavView_Bar"
app:menu="#menu/bottom_navigation_menu"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemBackground="#color/colorPrimary"
app:itemTextColor="#drawable/nav_item_color_state">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
bottom_navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ic_main"
android:icon="#drawable/ic_main"
android:title="MainLogo"
/>
<item
android:id="#+id/ic_search"
android:icon="#drawable/ic_search"
android:enabled="true"
android:title="Search"
/>
<item
android:id="#+id/ic_add_entries"
android:icon="#drawable/ic_add_entries"
android:enabled="true"
android:title="AddEntries"
/>
<item
android:id="#+id/ic_past_entries"
android:icon="#drawable/ic_past_entries"
android:enabled="true"
android:title="PastEntries"
/>
<item
android:id="#+id/ic_profile"
android:icon="#drawable/ic_profile"
android:enabled="true"
android:title="Backup"
/>
Nav bar item colour selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="ffffff" android:state_checked="true" />
<item android:color="#000000" android:state_checked="false" />
</selector>
Dependencies (here just showing that I implement the latest material dependency.
dependencies {
implementation 'com.google.android.material:material:1.2.0-alpha06'
}
In your buttom_navigatiom_menu.xml , add this to set the visibility status
android:visible="true"
Make sure you add visibility status cod to each of the icon items
Example
<item
android:id="#+id/ic_add_entries"
android:icon="#drawable/ic_add_entries"
android:visible="true"
android:enabled="true"
android:title="AddEntries"
/>
If this does not work for you, try this:
Go-to File Menu in Android Studio
Invalid Caches & Restart
Open existing project

My overflow menu doesn't seen in xml layout

When i run the app overflow menu seen in emulator(i mean that three dots or lines on right top) , but while i was working, it doesn't seen on xml layout why ?? These are codes ;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class OverflowApp extends AppCompatActivity {
protected void onCreate(Bundle bambam){
super.onCreate(bambam);
setContentView(R.layout.overflow_xml);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main , menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
}
and xml codes ;
<?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">
Firstly, there is no such thing as a layout for action overflow menu. The content that is to be displayed is stored in the following form.
<menu ...>
<item android:id="..." android:title="..."/>
<item android:id="..." android:title="..."/>
<item android:id="..." android:title="..."/>
...
</menu>
This file can be accessed from the menu directory in the res folder.
You can add, delete or change the menu items but not how they look from here.
As far as the appearance is concerned, you can change the appearance only in terms of the colors using styles. You can start with the answer given here.

Android - unwanted submenu appearing after screen rotation

I have created very simple demo project to demonstrate the bug:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MenuBugActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.options_bug_demo, menu);
return true;
}
}
Main layout - main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Menu items options_bug_demo.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_bug"
android:title="Bug"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always">
<menu>
<item
android:id="#+id/menu_bug_demo"
android:title="Bug demo">
<menu>
<item android:title="Settings"/>
</menu>
</item>
</menu>
</item>
</menu>
1.Clicking on "Preferences" icon:
2.Clicking on "Bug demo":
3.Clicking on "Settings":
4.Changing screen orientation to the horizontal:
Submenu "Settings" (closed at step 3) appears again after screen rotation! The only way to prevent the submenu from appearing after rotation - to remove string
android:id="#+id/menu_bug_demo"
from resource file options_bug_demo.xml
The question is how to avoid this unwanted behaviour of the submenu?
P.S. The bug was observed on 4.0.4 and 4.1.1 and was not observed on 4.4.2

Android Menu does not inflate

I am pulling my hairs out in trying to get menu to infate on my main activity but to no avail.
I am using Android Studio and the design view showed that the XML menu items are rightfully defined.
However, when I run the codes either in the phone(running Android 4.4.2) or emulator, the menu items are not showing up. Nothing happens when I press on the menu.
Any enlightenment will be most welcome.
Menu XML - my.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1" android:title="Option1"></item>
<item android:id="#+id/item2" android:title="Option2"></item>
<item android:id="#+id/item3" android:title="Option3"></item>
<item android:id="#+id/item4" android:title="Option4"></item>
<item android:id="#+id/item5" android:title="Option5"></item>
<item android:id="#+id/item6" android:title="Option6"></item>
</menu>
Activity Class - myActivity.java
package com.example.mymenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.widget.Toast;
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, " " +
"Option1", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Option2", Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(this, "Option3", Toast.LENGTH_SHORT).show();
return true;
case R.id.item4:
Toast.makeText(this, "Option4", Toast.LENGTH_SHORT).show();
return true;
case R.id.item5:
Toast.makeText(this, "Option5", Toast.LENGTH_SHORT).show();
return true;
case R.id.item6:
Toast.makeText(this, "Option6", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Layout File
<RelativeLayout 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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I have solved the problem.
My phone has a menu button, and pressing that button will display the menu list.
I tried with a newer phone that does not have the menu button, and the menu shows up within the action bar.
Regards
I think the problem is you must have created my.xml in layout folder .Create my.xml in menu folder.
Cross check if the menu.xml is in menu folder and not any other res folder like layout.
Trying to inflate it in action bar?
If yes, then try using the following code in the menu.xml
android:showAsAction="always"
Refer : http://developer.android.com/guide/topics/ui/menus.html
Hi try this if you want to display it in Actionbar.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:icon="#drawable/action_overflow"
app:showAsAction="always"/>
<menu>
<item android:id="#+id/item1" android:title="Option1"></item>
<item android:id="#+id/item2" android:title="Option2"></item>
<item android:id="#+id/item3" android:title="Option3"></item>
<item android:id="#+id/item4" android:title="Option4"></item>
<item android:id="#+id/item5" android:title="Option5"></item>
<item android:id="#+id/item6" android:title="Option6"></item>
</menu>
</menu>

actionbar menu item onclick?

I have an action bar that puts everything in a menu in the top right, which the user clicks and the menu options open up.
I inflate the action bar menu with this on each activity I use it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
And my xml for main2.xml is:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Seach"/>
</menu>
My question is do I put an onclick in the item in the xml and if so where do I put the onclick method it calls? Do I need to put it in every activity I launch this action bar in?
If you add an onClick attribute on your menu item like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:onClick="doThis"
android:title="Seach"/>
</menu>
Then in your activity:
public void doThis(MenuItem item){
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
Note:
ActionBarSherlock is deprecated. Unless you are developing an app for Android 4.0 or older, please don't use it. But if you are using the library, you will have to import
import com.actionbarsherlock.view.MenuItem;
and not
import com.android.view.MenuItem;
In addition, you could do something like this: ActionBar Sherlock Menu Item OnClick
which #adneal mentions.
In my opinion
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onCreateDialog(getTaskId());
}
});
}
<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=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/add_text_id" android:title="Add"
android:icon="#drawable/ic_add_btn"
android:orderInCategory="100" app:showAsAction="ifRoom" />

Categories

Resources