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.
Related
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>
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);
}
I have simple image view app... That is suppose to display the image in viewpager mode. However, my problem is that it is not displaying my app icon at the top of the action bar...Any help will be appreciated.....I know that it is something really small, but I can't spot it..I woulde like to display my icon as shown in this link
Please see my following codes...
Mainactivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.activity_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
#Override
protected void onPause() {
super.onPause();
oursong.release();
}
}
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"
tools:context=".MainActivity"
android:icon="#drawable/icon" >
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:icon="#drawable/icon" />
</RelativeLayout>
Manifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="com.manishkpr.viewpagerimagegallery.SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="com.manishkpr.viewpagerimagegallery.MainActivity"
android:label="#string/app_name"
android:icon="#drawable/icon">
</activity>
</application>
</manifest>
If I understood you only need edit a file in a folder called menu. Your file looks like
<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>
and just change never and put always
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings"/>
</menu>
I've been trying to implement a contextual action bar along with a dialog fragment.
Similar to the downloads widget in android.
I've tried to set android:windowActionModeOverlay to be true in the theme.
But it doesnt seem to work. Is there any way I can achieve it??
The downloads window that you have in your screenshot is actually an Activity using the #android:style/Theme.Holo.Dialog theme which makes it look like a dialog. To achieve the same look as the downloads window, your Activity need only use the same theme.
You can set this theme in your manifest like so:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Example implementation excluding string and drawable resources.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mceley.dialog.example"
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" >
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.mceley.dialog.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
findViewById(R.id.context_button).setOnClickListener(this);
}
#Override
public void onClick(View v) {
ExampleMode mode = new ExampleMode();
startActionMode(mode);
}
public class ExampleMode implements ActionMode.Callback {
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
}
}
main_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<Button android:id="#+id/context_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/show_context_bar" />
</LinearLayout>
main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
Result:
I don't think you can add ActionBar in DialogFragment.
But We can Try that using Activity As Dialog.
I have tried this with ActionBarSherlock and I have added R.style.Sherlock___Theme_Dialog as my Activity's theme but it looks like this:
After doing Above thing i understud, we can't Add ActionBar in Dialog or DialogFragment.
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.