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.
Related
the button more information is not visible!!!!
xml of menu :-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/more_information"
android:title="#string/action_mnn"
/>
</menu>
main activity :-
package com.example.yashchaudhary.modernartui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.more_information:
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
screenshot after clickin on 3 dots
In my case it was a Theme specific for the action bar in the activity layout
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
.../>
Removing the theme property solved my problem
<androidx.appcompat.widget.Toolbar android:id="#+id/toolbar" android:theme="#style/ThemeOverlay.MaterialComponents.Dark.ActionBar"/>
Removing the theme attribute will resolve this problem.
Try to add to the item on the menu xml
android:orderInCategory="100"
and add break; after
case R.id.more_information:
return true;
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
Hi I am very new to android development and have gone through many questions in stack over flow but did not get any answer for this.
I want all the share intents to come as a drop down list but i get on button beside and one as a drop down list why is like this. I could explain better by sharing my screen shot but stackoverflow,com does not allow me to.
My mainactivity.java file is:
package com.example.myfirstapp;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#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);
MenuItem shareItem=menu.findItem(R.id.action_share);
ShareActionProvider m=(ShareActionProvider)MenuItemCompat.getActionProvider(shareItem);
m.setShareIntent(getDefaultIntent());
return true;
}
private Intent getDefaultIntent() {
// TODO Auto-generated method stub
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
return intent;
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
`
My main.xml is:
<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.myfirstapp.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom|withText"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="#+id/action_share"
android:icon="#drawable/ic_action_share"
android:orderInCategory="100"
android:title="#string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
</menu>
PLEASE any one help me
The last share provider is shown for the user's convenience. If the user regularly uses the same provider, e.g. Facebook, then they can simply click on a single button to share, rather than having to drop down the list and then select it. Of course they can drop down the list and choose a different one if they want to.
Perhaps you only have 2 share providers installed, so it looks a bit awkward.
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
I am using ActionbarSherlock library to implement action bar for Android 2.1 API 7 platform. The Sherlock library is imported to my own project successfully.
Then, in my project, I have res/menu/action_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/test1"
android:title="#string/test_1"
android:showAsAction="ifRoom"
/>
<item android:id="#+id/test2"
android:title="#string/test_2"
android:showAsAction="ifRoom"
/>
</menu>
My Activity:
import android.support.v4.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
public class MyActivity extends FragmentActivity {
#Override
protected void onStart() {
super.onStart();
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
}
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
}
With the above code, My application has an action bar. But the problem is that all the items defined in the above menu xml file are showing on the right hand side of the action bar, and the left side part of action bar is left blank.
You probably already noticed in my Activity code, I have:
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
The above two line codes are defined in my Activity onStart() callback which disable the action bar default text and icon.
But how can I have my custom items defined in menu xml file to be shown evenly on the action bar instead of only show on the right hand side of the action bar?