can not click action bar app icon Android - java

I'm trying to make the action bar app icon (top left image) clickable but it just doesn't work, I've already searched for some answers but nothing works. I've already tried to getSupportActionBar().setHomeButtonEnabled(true); and I can see the icon, but i still can't click on it. I know I can handle the event in the method onOptionsItemSelected(MenuItem item) but the event will never get triggered with my situation. I also don't get why I have to use getSupportActionBar(); instead of getActionBar()...the second one is always null. The minimum sdk is 16 and the maximum is 22. I read this answer -> ActionBarCompat - App icon action (click) not working on 4.0 devices but I don't know how to get in the ActionBarHelperICS.java class or if it apply to my case.

You haven't really posted any code. But there may be 2 problems.
You have to specify which is the parent activity in your manifest file.
Under the activity's tag in manifest, you'll have to specify which activity your home button will point to. Something like this:
android:parentActivityName="com.example.app.MainActivity"
Or Override your onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//add what you want to do here...
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Related

setDisplayHomeAsUpEnabled not working in one of my Activities but everywhere else it does work

For some reason this doesn't work in one of my activities but everywhere else it works fine when i press the back arrow in the toolbar it goes back to my MainActivity.
import androidx.appcompat.widget.Toolbar;
//Initializing toolbar
mToolbar = findViewById(R.id.mToolbar);
setSupportActionBar(mToolbar);
if((getSupportActionBar() != null)){
getSupportActionBar().setTitle(R.string.equalizerTitle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_home_up);
}
Manifest
<activity
android:name=".activities.EqualizerActivity"
android:parentActivityName=".activities.MainActivity"
android:screenOrientation="portrait"/>
EDIT
This activity uses a constraint layout as parent but i don't see why this would be the cause, all other activities use RelativeLayout and there they work fine.
Looks like you are using onOptionsItemSelected() method to handle menu buttons. The issue is that the "back" button is also kind of a menu button.
In order for Android to know that you don't have a specific handler for the button and it should be handled by the framework, you need to let Android know it via returning false when you don't "consume" the event. According to the docs:
onOptionsItemSelected()
boolean Return false to allow normal menu processing to proceed, true to consume it here.
So, you should have smth like this:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case YOUR_BUTTON:
// do something
return true;
}
// By default, allow Android to work on it
return false;

How do I add a new page to an Android app?

Apologies for the basic question, but I can't really find the answer online. Possibly because I'm not sure what I need to be searching for!
I have created a simple one page app following a guide. At the top there is a menu button, under that 'Settings'. I want to make it so that when the Settings button is clicked it takes you to a new settings page and allows the user to change the background colour, font colour or something simple like that.
However, I can't work out how to actually create a new page. I tried creating a new class and linking the intent under the action_settings bit in the main .java file, but that didn't work for me.
Please could someone give me some guidance?
GitHub repo: https://github.com/LewisLebentz/Quoter
If the settings button is on the action bar then follow these steps:
1) Create a new activity by right-clicking on the Java folder > New > Activity > Blank Activity. This will automatically create a layout file and a Java class. It will also automatically register the activity in the manifest.
2) Next you want to go to the Quoter.java file. Add the following code to it.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// This code will start the new activity when the settings button is clicked on the bar at the top.
Intent intent = new Intent(Quoter.this, newActivityName.class);
startActivity(intent)
return true;
}
return super.onOptionsItemSelected(item);
}
That's it! When you click the settings button, it should start a new activity! Let me know if it does not work!
By the way, you can search for "how to switch activity on button press in Android".

Android Back Action Button Result in App Crash

I am building a new application which composed of 3 activities namely:
- Splash Screen
- Activity A
- Activity B, and
- Activity C
From activity A user can go to both activities indicated below:
A -> B -> C (from act A user can go to B then to C).
A -> C (from act A user can go straight to C).
B -> C (from B user can go to C).
i also pass Serializable intent Extra between activities.
The problem that i am having is whenever i pressed the back button on the Action Bar (Top Left Corner) it always makes my app crashes (Error: NULL Pointer Exception).
I have tried to place this code on ALL of my activity.
#Override
public void onBackPressed() {
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
}
I tried to somehow mimic the physical backbutton behaviour since it is working when user press physical backbutton. But somewhat throws error as well.
or
public void onBackPressed(){
super.onBackPressed();
}
or (well this one literally restart the app, which is discourage since it restart the app from splash screen).
public void onBackPressed(){
super.onBackPressed();
finish();
}
Does anyone know the appropriate way to implement back button?
The button on the top left corner is not the "Back" button, it would be the "up" button, and it's just a button in the action bar, onBackPressed refers to the hardware back button being pressed.
Navigation with back and up is not necessarily the same ("back" means go to where I was before, whereas "up" means go to the upper level in the app hierarchy).
Take a look at http://developer.android.com/design/patterns/navigation.html for more information.
(Also, try to avoid a splash screen, they are highly discouraged in android design patterns)
Edit: I forgot to mention how to actually handle the "up" button. You do that on your activity's onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// Handle "up" button behavior here.
return true;
} else {
// handle other items here
}
// return true if you handled the button click, otherwise return false.
}
You can do something like this when the back button is called
public void onBackPressed(){
// Add data to your intent
finish();
startActivity(intent);
}
And similar in your onClick method for your action bar button.
I think, the problems is from passing value using intent.
If the value is intended to be use by all of your activity, I think the best way
is to use a sharedpreferences.
If You are using API level 16 or more than , Really No need to do anythinngggg,
i just Find the solution :
in manifest file with every other activity Except MainActivity Add meta-data like this :
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gsetia.ohama.MainActivity"/>
with in Activity like this , For Example
<activity
android:name=".ViewReports"
android:label="#string/title_activity_view_reports"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gsetia.ohama.MainActivity"/>
</activity>
And In Last , delete public boolean onOptionsItemSelected(MenuItem item) Method. That's it.
You will find your Back Button and will work fine

Setting splitActionBarWhenNarrow from Java on Android

I'm wondering if it would be possible to tell android to split the ActionBar only when I want it to, but on the same Activity. My use case is that by default, the actions I have on the bar are OK to be collapsed, but on a long click on an item, I enter an "Edit Task" mode, where the action bar is used to provide some shorthands to edit a task. I'd like this "edit mode" to use the split action bar, as it has icon's that are better off to be visible right away, and keep the "not split" action bar for the general view - where it's just "settings" etc.
So the question is, can I set android:uiOptions="splitActionBarWhenNarrow" from the code, instead of hardcode it in the Manifest?
PS: I'm using ActionBar Sherlock.
The native action bar can be set into split mode by calling getWindow().setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW).
Window UI options cannot be read after they are set so with ActionBarSherlock you have to call getSherlock().setUiOptions(...). You don't have to call both. ABS will automatically call the above when appropriate.
This must be done before the decor view has been created. The safest place to put this call to ensure that always happens is in your activity onCreate method before you call super.onCreate.
Take a look at the ActionBarSherlockSamples, SplitActionModes.java. In this example, when the button 'Start' is pressed, a split action bar shows up in the bottom of the screen:
Call this to show the split actionbar:
mMode = startActionMode(new AnActionModeOfEpicProportions());
mMode is type of ActionMode and you need to call 'finish()' on it when you want the action bar to go away.
AnActionModeOfEpicPropotions is an implementation of ActionMode.Callback:
private final class AnActionModeOfEpicProportions implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// add your menu here...
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// handles your action menu clicked event
return true;
}
}
I was just trying if I could have an ActionBar in one Activity and a Split one in another.
I added the action bar in onCreateOptionsMenu in both the activities and added
getWindow().setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);
right before onCreate in the activity I wanted the split action and to my surprise it worked. :)
Then I used getActionBar().hide() to hide the split action bar on button click and it worked too.
I guess if you could try this, you can get this working and customized to your need. Hiding it initially and onLongClick showing it up and after the necessary actions are performed hiding it again.
(The only thing I missed is checking this with support libraries. Will do and update you)
Hope, this might help you in some way. Happy coding :)
private final class AnActionModeOfEpicProportions implements ActionMode.Callback
{
#Override
public boolean onCreateActionMode(ActionMode mode,Menu menu)
{
// add your menu here...
}
#Override
public boolean onActionItemClicked(ActionMode mode,MenuItem item)
{
// handles your action menu clicked event
returntrue;
}
}

How do you write an extra class for often used code and call it from everywhere?

In my little app users can have a look at 'more information' or send me feedback when they click on the menu button (of the device) at every xml file of the app.
This is my code (when you click on the menu button):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.impressum:
startActivity(new Intent (this, MoreInformation.class));
break;
case R.id.feedback:
/*
*
*/
break;
}
return super.onOptionsItemSelected(item);
}
Now I put the code into all java classes. This obviously works but I'd like to create a class (Let's call it isMenuButtonPressed.class) which contains the code above and I'd like to remove the code above from all other java classes and just call isMenuButtonPressed.class (in these java classes when the menu button was pressed). How can you do that? How do you pass the information on that the menu button was pressed? How do you receive it in the isMenuButtonPressed.class?
Thanks!
How do you write an extra class for often used code and call it from everywhere?
By letting a custom class define the actual behaviour for your Menu.
Read my Asynchronous programming best practices answer for more information regarding this subject.

Categories