i have problem putting the menu in the upper right corner. even i checked this Link. but not worked.
i have tried with android:showAsAction="always"even tried with more then 6 SDK version but not worked .
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
items.xml:-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/phone"
android:title="#string/phone"
android:icon="#drawable/phone"
android:showAsAction="always"
/>
<item
android:id="#+id/computer"
android:title="#string/computer"
android:icon="#drawable/computer"
/></menu>
Java:-
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.items, menu);
//inflater.inflate(R.menu.items, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
.
please suggest . any suggestion are welcome . Thanks
Solution as below-
In build.gradle(Module:app) add appcompat dependency
dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
// ... other dependencies
}
change
public class MainActivity extends Activity
to
public class MainActivity extends AppCompatActivity.
change your menu.xml file as below
<?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/phone"
android:title="#string/phone"
android:icon="#drawable/phone"
app:showAsAction="always" />
<item
android:id="#+id/computer"
android:title="#string/computer"
android:icon="#drawable/computer" />
</menu>
Related
I am having trouble displaying my menu options on my phone. I got all the code right I think but it is not displaying. Any help would be greatly appreciated.
Here is how my manifest looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.drumloopsequencer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:screenOrientation="landscape"
android:name=".Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the onCreateOptionsMenu function:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
and here is the menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/file"
android:title="#string/fileTab"
android:orderInCategory="1"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/packs"
android:title="#string/packsTab"
android:orderInCategory="2"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/drumRack"
android:title="#string/drumRackTab"
android:orderInCategory="3"
android:showAsAction="ifRoom"
/>
</menu>
If you are using a fragment make sure that you have enabled the options menu with setHasOptionsMenu(true). A good place to call this might be from onCreate.
public class MyFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
}
or for an Activity
public class MyActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
}
Try this :
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow()
openOptionsMenu();
};
If you are using fragment then you have to do this:
//inside constructor
public MyClassFrag(){
setHasOptionsMenu(true);
}
or inside onCreate()
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Yesterday I had a problem with replacing fragments, so I had to change my MainActivity to extend Acivity instead of ActionBarActivity in order to avoid compatibility issues. The problem is, that after I've done that, an actionbar item disappeared. I tried following androids official tutorials on how to set it up without using the support library, but with no avail. This is what I have:
Inside the MainActivity:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction().add(R.id.frag_container, new MainMenuFragment()).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return true;};
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
As you can see, I'm implementing the needed methods for the action bar to be populated with buttons. This is my menu's XML file:
<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.pszczyna.MainActivity" >
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
The manifest is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jam.pszczyna"
android:versionCode="1"
android:versionName="1.0.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.jam.pszczyna.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
which is the minimum SDK version required for this to work. For me everything looks like it's supposed to, whats wrong with it?
try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.pszczyna.MainActivity" >
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
android:showAsAction="always"/>
</menu>
and in your manifest remove android:theme="#style/Theme.AppCompat" and put your own style.
meanwhile I have no idea why my navigation is not working.
In my mainmenu there are 2 icons in order to switch to different activities: searching and settings.
But everytime I am clicking on search, settingsActivity is opened. I checked the IDs of search, activities are entered in the Manifest ...
If I am adding an additional button to a view and trying to start the searchActivity, everything works fine. But even if I am commenting out the case R.id.settingsMenu-thing, the settingsActivity is opened if I am clicking on the search symbol ... Any ideas? :( Thanks in advance!
MainActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainmenu, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item){
Intent intent = null;
switch(item.getItemId()){
case android.R.id.home:
intent = new Intent(this,MainActivity.class);
startActivity(intent);
break;
case R.id.searchMenu:
intent = new Intent(this,SearchActivity.class);
startActivity(intent);
break;
case R.id.settingsMenu:
intent = new Intent(this,SettingsActivity.class);
startActivity(intent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.anothertest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true">
<activity
android:name="android.app.ui.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="android.app.ui.SearchActivity" android:label="#string/search"></activity>
<activity android:name="android.app.ui.SettingsActivity" android:label="#string/settings"></activity>
<activity android:name="android.app.ui.NoArticleInListActivity"></activity>
</application>
</manifest>
mainmenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/searchMenu"
android:title="#string/search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="1"
android:showAsAction="always">
</item>
<item
android:id="#+id/settingsMenu"
android:orderInCategory="2"
android:showAsAction="always"
android:title="#string/action_settings"
android:icon="#android:drawable/ic_menu_preferences">
</item>
</menu>
EDIT
SettingsFragment
public class SettingsFragment extends PreferenceFragment{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
SettingsActivity
public class SettingsActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
protected void onStart(){
super.onStart();
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
default:
return super.onOptionsItemSelected(item);
}
}
}
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/settingsSummaryCalendar">
android:key="pref_calendar_settings">
<CheckBoxPreference
android:key="prefArticleAsCalendarEntry"
android:summary="#string/settingsSummaryCalendarEntry"
android:title="#string/settingCreateEntryForEach"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="prefCreateOwnCalendar"
android:summary="#string/settingsSummaryCalendar"
android:title="#string/settingCreateOwnCalendar"
android:defaultValue="true"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/settingsSummaryNotification">
android:key="pref_notification_settings">
<CheckBoxPreference
android:key="prefNotification"
android:summary="#string/settingsSummaryNotification"
android:title="#string/settingsNotification"
android:defaultValue="true" >
</CheckBoxPreference>
<ListPreference
android:key="prefNotificationTime"
android:entries="#array/notificationTime"
android:entryValues="#array/notificationTimeValues"
android:summary="#string/settingsSummaryNotificationTime"
android:title="#string/settingsNotificationTime"
android:defaultValue="#string/settingsNotificationTime_default"
android:dependency="prefNotification">
</ListPreference>
</PreferenceCategory>
</PreferenceScreen>
The only possibility is that Eclipse is messing up the ids of your elements. This happened to me after I updated the SDK but not ADT. So, here is a suggestion:
1- Try to clean the project. In Eclipse, it is under Project-> Clean . Using the debug statement, check that the ids of the elements you click on (R.id.settings, R.id.search) are the same as the ones defined in R.java
2- If that does not work, update the SDK to the latest (as of this posting, 22). Also, update ADT. Check this answer for how to do so: How can I update my ADT in Eclipse?
3- Final resort: delete the project, and reimport it.
So I have an action bar I am trying to learn and when I implement it I see this white spinning circle left of my first menu item. Why is this being shown here?
Source:
package com.example.lookingfor;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.atcAbout:
break;
case R.id.atcContact:
break;
case R.id.atcRate:
break;
}
return true;
}
}
Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/atcAbout"
android:showAsAction="ifRoom"
android:title="About"/>
<item
android:id="#+id/atcRate"
android:showAsAction="ifRoom"
android:title="Rate this app"/>
<item
android:id="#+id/atcContact"
android:showAsAction="ifRoom"
android:title="Contact Us"/>
</menu>
Main Layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lookingfor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is a screenshot (I cannot post images here yet)
http://imgur.com/RaTTP
How can I get rid of this spinning circle before I publish the app? It is an ugly and annoying distraction to the app itself.
Thanks Y'all!
Try commenting out the line requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); in your onCreate() callback.
I am using this "Tab layout and Listview" from http://www.androidhive.info/2012/05/android-combining-tab-layout-and-list-view/ and "Android Menus" from http://www.androidhive.info/2011/09/how-to-create-android-menus/.
I merge them together, yet it doesn't work. The tab works, but not for the menu.
Here are my codes.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class optionMenuActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Initiating Menu XML file (menu.xml)
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
Intent myList = new Intent();
switch (item.getItemId())
{
case R.id.menu_login:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
// Toast.makeText(optionMenuActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
myList = new Intent(optionMenuActivity.this, LoginActivity.class);
startActivity(myList);
return true;
case R.id.menu_save:
Toast.makeText(optionMenuActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(optionMenuActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(optionMenuActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(optionMenuActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_quit:
// Toast.makeText(optionMenuActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
The main from "Tab layout"
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
And this is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_login"
android:icon="#drawable/icon_bookmark"
android:title="Login" />
<item android:id="#+id/menu_save"
android:icon="#drawable/icon_save"
android:title="Save" />
<item android:id="#+id/menu_search"
android:icon="#drawable/icon_search"
android:title="Search" />
<item android:id="#+id/menu_share"
android:icon="#drawable/icon_share"
android:title="Share" />
<item android:id="#+id/menu_delete"
android:icon="#drawable/icon_delete"
android:title="Delete" />
<item android:id="#+id/menu_quit"
android:icon="#drawable/icon_preferences"
android:title="Quit" />
</menu>
And lastly, my manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".WorkDroid5"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Inbox Activity -->
<activity android:name=".AnnouncementActivity" />
<!-- Outbox Activity -->
<activity android:name=".HomeworkActivity" />
<!-- Profile Activity -->
<activity android:name=".TimetableActivity" />
<activity android:name=".tt_Friday" />
<activity android:name=".ttFinalDetails" />
<activity android:name=".TimetableAdapter" />
<activity android:name=".tt_Thursday" />
<activity android:name=".tt_Monday" />
<activity android:name=".tt_Tuesday" />
<activity android:name=".tt_Wednesday" />
<activity
android:name=".RegisterActivity"
android:label="Register New Account" >
</activity>
<activity
android:name=".LoginActivity"
android:label="Login to your Account" >
</activity>
<activity
android:name=".optionMenuActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.example.OPTIONMENUACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Thanks in advance for anyone's help (':
Here:
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
you pass R.layout.menu to MenuInflater.inflate(..) method, but it should be R.menu.menu. Move your menu.xml file from res/layout dir to res/menu dir.
I ran into similar problem with TabHost. By default, Android creates an options menu for the parent activity, i.e. TabHost in your case. The child tab does not receive the OnCreateOptionsMenu event. Therefore, your menu is not being created.
Put the option menu creation/handling code in the activity extending TabActivity. You can change the preference menu content by checking which tab is currently selected by checking getTabHost.getCurrentTab or getTabHost.getCurrentTabTag and inflating appropriate menu.