I need some guidance regarding the working of Action bar. Actually clicking on a Action bar MenuItem
. Current activity must be moved to another activity. But it's doing nothing. Not even any error. I used following code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mnew1, 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:
try {
openSearch();
} catch (IOException e) {
e.printStackTrace();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSearch() throws IOException{
val=address(location.getLatitude(), location.getLongitude());
Intent intnt=new Intent(getApplicationContext(),SendSms.class);
intnt.putExtra("loct", val);
startActivity(intnt);
}
Use MenuInflater inflater = getSupportMenuInflater();
And if you are using android home button then the id is android.R.id.home
. In that case make sure you have getSupportActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate
.
EDIT- Make sure you have imported these:
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
put this code inside onCreate method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_main_activity_xml);
// creating Actionbar
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
actionBar.setDisplayShowTitleEnabled(true);
actionBar.show();
...
}
and then put this code outside onCreate :
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mnew1, 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:
try {
openSearch();
} catch (IOException e) {
e.printStackTrace();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSearch() throws IOException{
val=address(location.getLatitude(), location.getLongitude());
Intent intnt=new Intent(getApplicationContext(),SendSms.class);
intnt.putExtra("loct", val);
startActivity(intnt);
}
this code is perfect. There is not any problem with this code. Actionbar button was not working due to null value it is receiving..and null value was just because of some hardware issues..nothing wrong with this code thanks