I have two activities with two layouts and a action bar.
The minimum API I am using is 14.
I go from one activity to another with no problem using intent, and I want to make a home button in the action bar to return to the activity that I choose.
I implemented a menu and called it from xml using the guides on the android developers site, but when I click on the button/menu from the action bar, it crashes with uncaught exception.
The main activity class:
package com.example.quiz;
import android.app.ActionBar;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button new_b;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
actionBar.show();
new_b=(Button)findViewById(R.id.new_button);
new_b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent);
};
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.home_button:homeClick();
return true;
case R.id.info_button:homeClick();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void homeClick() {
Intent intent_home = new Intent(this,HomeActivity.class);
intent_home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent_home);
}
}
}
The home activity class:
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class HomeActivity extends Activity {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
ActionBar actionBar = getActionBar();
actionBar.show();
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.home_button:homeClick();
return true;
case R.id.info_button:homeClick();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void homeClick() {
Intent intent_home = new Intent(this,HomeActivity.class);
intent_home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent_home);
}
}
}
Can I make it another way, or what can I do to make it work?
04-20 19:31:57.610: I/Adreno200-EGL(5708): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build:
AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107_msm8625_JB_REL_2.0.3_CL3357771_release_AU (CL3357771)
04-20 19:31:57.610: I/Adreno200-EGL(5708): Build Date: 02/25/13 Mon
04-20 19:31:57.610: I/Adreno200-EGL(5708): Local Branch:
04-20 19:31:57.610: I/Adreno200-EGL(5708): Remote Branch: quic/jb_rel_2.0.3
04-20 19:31:57.610: I/Adreno200-EGL(5708): Local Patches: NONE
04-20 19:31:57.610: I/Adreno200-EGL(5708): Reconstruct Branch AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107 + NOTHING
04-20 19:31:57.850: W/ResourceType(5708): getEntry failing because entryIndex 14 is beyond type entryCount 1
04-20 19:31:57.850: W/ResourceType(5708): Failure getting entry for 0x7f0b000e (t=10 e=14) in package 0 (error -2147483647)
04-20 19:32:09.390: E/dalvikvm(5708): GC_CONCURRENT freed 2965K, 22% free 12424K/15751K, paused 12ms+26ms, total 75ms
04-20 19:32:10.600: W/dalvikvm(5708): threadid=1: thread exiting with uncaught exception (group=0x41d02438)
You can use this code as an example:
MainActivity.java
package com.example.quiz;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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.activity_main_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.go_home:
Intent intentHome = new Intent(this, HomeActivity.class);
startActivity(intentHome);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
/res/menu/activity_main_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/go_home"
android:showAsAction="ifRoom"
android:title="#string/go_home" />
</menu>
It turned out that I was trying to format my strings with html code.
Example:
I was using:
<string name="title"> <b>App title</b> </string>
When I was supposed to use:
<string name="title"> App title <string>
Related
I am following a tutorial 11. Exercise: Using the contextual action mode
But I am having this error :
mActionMode = Display.this.startActionMode(mActionModeCallback);
view.setSelected(true);
Error: The method startActionMode(ActionMode.Callback) in the type Activity is not applicable for the arguments (ActionMode.Callback)
I checked this stackoverflow answer
they said to add
ActionBarActivity activity=(ActionBarActivity)getActivity();
activity.startSupportActionMode(modeCallBack);
I had this error
The method getActivity() is undefined for the type Display
what I am doing wrong ? the below is my code.
package com.example.sqlfirst;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class Display extends ActionBarActivity {
private final static String TAG = "MainActivity";
protected Object mActionMode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_main);
//have to use getSupportActionBar from android.support.v7.app
// ActionBar actionBar = getSupportActionBar();
//getActionBar().setDisplayHomeAsUpEnabled(true);
ActionBarActivity activity=(ActionBarActivity)getActivity();
activity.startSupportActionMode(modeCallBack);
View view = findViewById(R.id.gridview);
view.setOnLongClickListener(new View.OnLongClickListener() {
// called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
// start the CAB using the ActionMode.Callback defined above
mActionMode = Display.this.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
} });
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.ic_action_person:
Toast.makeText(this, "Create a new account please", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Register.class);
startActivity(intent);
return true;
case R.id.ic_action_search:
Toast.makeText(this, "Search for new images", Toast.LENGTH_SHORT).show();
Intent isearch= new Intent(this,Search.class);
startActivity(isearch);
return true;
case R.id.ic_action_picture:
Toast.makeText(this, "Search for new photos", Toast.LENGTH_SHORT).show();
Intent iphotos= new Intent(this,Display.class);
startActivity(iphotos);
return true;
}
return true;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
// assumes that you have "contexual.xml" menu resources
inflater.inflate(R.menu.activity_main_actions, menu);
return true;
}
// called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// called when the user selects a contextual menu item
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_action_picture:
Toast.makeText(Display.this, "Selected menu",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// called when the user exits the action mode
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
Your Display class is extending ActionBarActivity, that means that it´s an Activity so there´s no need to use getActivity(), you can directly make use of the methods like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* this method is available within your ActionBarActivity*/
startSupportActionMode(modeCallBack);
setContentView(R.layout.grid_main);
// The rest of your code comes here
}
This is probably extremely simple, but I have two activities that both need the same items in the action bar, but when I start the second activity (the first is main) the action overflow menu disappears. I understand that the action bar is unique to the activity its created in, but I want to know how to use the same action bar in various activities. Thanks
Edit: here is some code that might help with my second issue of action bar items not working out of the first activity:
package com.example.wfhsregistry;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MenuActivity extends Activity{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#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.menuSettings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And here is my first class that starts my first activity:
package com.example.wfhsregistry;
import java.lang.reflect.Field;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewConfiguration;
public class Main extends MenuActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionbar = getActionBar();
setContentView(R.layout.splash);
Thread logotimer = new Thread() {
public void run() {
try {
sleep(5000);
Intent menuIntent = new Intent(
"com.example.wfhsregistry.MENU");
startActivity(menuIntent);
} catch (Exception e) {
e.printStackTrace();
} finally {
finish();
}
}
};
logotimer.start();
}
public void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
You could use inheritance.
Create a new Activity lets call it MenuActivity and place all the options logic in.
MenuActivity extends Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
}
And now your 2 activities just need to extend it (so they will inherit MenuActivity's logic unless you override its method without calling super)
MainActivity extends MenuActivity
And
Activity2 extends MenuActivity
I'm using Studio 0.8.9
I have an android-sdks\extras\android\support\v4\android-support-v4.jar
but I still run into the import issue.
I'm using NavUtils.navigateupfromsametask method.
what I'm trying to achieve is simply to use the back button to get to my previous activity.
is it the right way of doing?
when I'm using my email app (for ex yahoo) I'm using the back button to get back to my inbox when I'm viewing an email.
how do they do it? using activities and the back button?
I'm using the standard code:
package com.example.bernard.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.NavigableMap;
import android.support.v4.app.NavUtils;
public class Statistic extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statistic);
getActionBar().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.statistic, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id)
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed(){
super.onBackPressed();
}
}
i'm a noob developer...I hope that someone can help me with this problem.
I'm going to add a share button to my actionbar (ABS) but I've some problems
my actionbar menu XML
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/menu_item_share"
android:title="Share"
android:showAsAction="ifRoom"
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider" />
my activity
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.widget.ShareActionProvider;
public class DisPlayWebPageActivity extends SherlockActivity {
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.actionmenu, menu);
ShareActionProvider myShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_TEXT, "page_url");
myIntent.setType("text/plain");
myShareActionProvider.setShareIntent(myIntent);
return true;
}
relevant errors
Error:(52, 8) java: com.actionbarsherlock.internal.view.menu.MenuBuilder is not abstract and does not override abstract method getItem(int) in com.actionbarsherlock.view.Menu
Error:(663, 34) java: getItem(int) in com.actionbarsherlock.internal.view.menu.MenuBuilder cannot implement getItem(int) in com.actionbarsherlock.view.Menu
return type android.view.MenuItem is not compatible with com.actionbarsherlock.view.MenuItem
Error:(605, 34) java: findItem(int) in com.actionbarsherlock.internal.view.menu.MenuBuilder cannot implement findItem(int) in com.actionbarsherlock.view.Menu
return type android.view.MenuItem is not compatible with com.actionbarsherlock.view.MenuItem
Error:(443, 33) java: addSubMenu(int,int,int,int) in com.actionbarsherlock.internal.view.menu.MenuBuilder cannot implement addSubMenu(int,int,int,int) in com.actionbarsherlock.view.Menu
return type android.view.SubMenu is not compatible with com.actionbarsherlock.view.SubMenu
Error:(435, 33) java: addSubMenu(int,int,int,java.lang.CharSequence) in com.actionbarsherlock.internal.view.menu.MenuBuilder cannot implement addSubMenu(int,int,int,java.lang.CharSequence) in com.actionbarsherlock.view.Menu
return type android.view.SubMenu is not compatible with com.actionbarsherlock.view.SubMenu
...
ask me if you need full error log!
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_setting:
Intent next = new Intent(Home.this, Setting.class);
next.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityForResult(next, 2);
return true;
}
return true;
}
I have made an app with a lot of buttons and activities. I'm having trouble though understanding how to start a new activity through a button that is in my menu (when the menu button is clicked on phone) (inflatable menu). This is my code for the menu connected to my activity:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item1" android:title="#string/menu_home"></item>
</menu>
Here is my activities in Java:
package com.gmail.derekcraigsmith.nanaimobus;
import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
public class Route1TimesCcMonfriAActivity extends Activity implements
OnMenuItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.route1_times_cc_monfri_a);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.route1_times_cc_monfri_a, menu);
return true;
}
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
return false;
}
}
int id = item.getItemId();
switch (id) {
case R.id.your_menu_item_id : {
startActivity(new Intent(start_activity, next_activity));
}
where start activity is your main activity and next_activity is the activity you want to start.
Give a look on my blog here for more info.
activity_main.xml in menu folder
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/next"
android:title="Next" />
</menu>
In your activity inflate the menu
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) //get the id which is an int
{
case R.id.next: // check if its the menu item next selected
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(MainActivity.this, "Next Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,secondAct.class));//start activity
break;
default:
return super.onOptionsItemSelected(item);
}
}