Before Clicking:
After Clicking:
I am having an issue with my code where clicking on an actionbar action will bring up the title of the action. If you then click on the title that is how you get to the onclick listener. Very strange... I know it is hard to tell without code but I am away and will post it soon. Just wanted to ask around to see if anyone has experienced similar.
ActionBar:
<?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/action_settings"
android:title="#string/hello1"
app:showAsAction="never"/>
</menu>
onClickListener:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_settings:
onCreateDialog();
return true;
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onCreateDialog() {
String[] string= new String[]{"Add to Calendar", "Share"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Options")
.setItems(string, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:...................}
This is the expected behavior for the way you have the menu setup currently.
<item
android:id="#+id/action_settings"
android:title="#string/hello1"
app:showAsAction="never"/> <!-- Don't show this action until the overflow menu is shown -->
With the showAsAction set to never, the action is not shown until the overflow menu is shown. The available options for showAsAction are:
["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
In your case, because you only have one menu item, you can use always.
Refer to the menu resource documentation for more info.
You should be able to preview this in Android Studio (see screenshot below)
I fixed it by extending the activity to appcompatactivity instead of activity. Weird bug. Then it didn't do it.
Related
Option Menu:
<item
android:id="#+id/home"
android:icon="#drawable/ic_home"
android:title="Home"/>
<item
android:id="#+id/companies"
android:icon="#drawable/ic_companies"
android:title="Companies"/>
<item
android:id="#+id/contact"
android:icon="#drawable/ic_contacts"
android:title="Contact"/>
I want to change this item's icon programmatically depending on the open Fragment and, obviously, have different actions when the user hits this button. I tried several things to do that, but nothing worked.
The last thing I tried was this code in my Fragment onCreateView method:
Menu menu = bottomNavigationView.getMenu();
menu.findItem(R.id.ic_home).setIcon(R.drawable.ic_home_fill);
but its not work for me.
what i tried in selectFragment(MenuItem item)
switch (item.getItemId()) {
case R.id.home:
item.setIcon(R.id.ic_home_fill);
break;
}
I want to change the icon of the bottom navigation of selected position. if user clicked one item then the icon change to another one and when i select another one then 1st icon can set as a default.
refer this link:Android: Bottom Navigation View - change icon of selected item
but it's not work for me
plz give me a other solution.
Thank you
Try this, It worked for me
BottomNavigationView mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
invalidateOptionsMenu();
switch (item.getItemId()) {
case R.id.click_to_use:
addHomeFragment(MainActivity.this);
mTitle.setText("Home");
item.setIcon(R.drawable.device);
break;
case R.id.history:
addNotifyFragment(MainActivity.this);
mTitle.setText("History");
item.setIcon(R.drawable.ios_icon);
break;
case R.id.settings:
addSettingFragment(MainActivity.this);
mTitle.setText("Settings");
break;
}
return true;
}
});
after any modification in the menu, you would have to make an "Invalidateoptionsmenu ()" in the action. I invite you to want to test this feature . see here
I've implemented a long click listener to delete an entry from my recyclerview and I'd like to give the user one last chance to change their mind. I've done something similar in a 'standard' view using an Alert Dialog and i've made a few drawables for the buttons in the alert view that I would like to use again (to keep a constant interface among all my views)
The drawable I'm using is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/minus_button"
android:state_pressed="false"
android:state_selected="false"/>
<item android:drawable="#drawable/add_button"
android:state_pressed="true"/>
</selector>
The alert I'm trying to use is :
public void deleteRequestCheck(final int tempIDval){
AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
builder.setMessage("Are you sure you want to reset all your climbs back to zero?");
builder.setCancelable(false);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
deleteEntry(tempIDval);
}
});
AlertDialog alert = builder.create();
alert.show();
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
nbutton.setTextColor(Color.WHITE);
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
pbutton.setTextColor(Color.WHITE);
}
and I'm calling this from inside my Adapter class (where the longclick action is handled).
The issue I'm having is that
nbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
pbutton.setBackgroundColor(getDrawable(R.drawable.button_tap));
are giving me an error that it Cannot resolve method 'getDrawable(int)'. This has me stumped as this is identical to how I've created and called this alert dialog in my previous activities.
There are actually 2 ways you can do this:
1) Programmatically like:
nbutton.setBackground(ContextCompat.getDrawable(this.context, R.drawable.mypositiveButtonDrawable));
But what if you want same button color for other dialogs? Why to duplicate the effort?
2) through styles.xml
Create a theme in your styles.xml like:
<style name="DiscardChangesDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">#style/DialogButtonNegative</item>
<item name="buttonBarPositiveButtonStyle">#style/DialogButtonPositive</item>
</style>
<style name="DialogButtonNegative" parent="Widget.AppCompat.Button.Borderless">
<item name="android:textColor">#color/dialog_button_negative</item>
</style>
<style name="DialogButtonPositive" parent="Widget.AppCompat.Button.Borderless">
<item name="android:textColor">#color/dialog_button_positive</item>
</style>
and then use it like:
AlertDialog.Builder(this, R.style.DiscardChangesDialog)
and then you do not need to worry about adapter or anything.
Hope it helps!!!
I am currently creating the actionBar in android studio and I have created action buttons and my aim is to have a dropdown menu everytime I click a button.
In my menu_main.xml, I added the items search and help:
<item
android:id="#+id/menu_search"
android:icon="#drawable/ic_action_search"
android:title="#string/menu_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
<item android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help"
app:showAsAction="ifRoom"/>
In my MainActivity.java, I have added the Strings for my dropdown:
String[] actions = new String[] {
"Bookmark", "Subscribe", "Share", "Like"
};
In my onOptionsItemSelected() method, i have added the case where everytime you click on this button, it will call a function. Here is my code:
#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.
switch (item.getItemId()) {
case R.id.action_help:
helpMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void helpMessage() {
//Toast.makeText(this, "Location button pressed", Toast.LENGTH_SHORT).show();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, actions);
System.out.println("HERE: " + getActionBar());
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ActionBar.OnNavigationListener navigationListener = new ActionBar.OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(getBaseContext(), "You selected : " + actions[itemPosition], Toast.LENGTH_SHORT).show();
return false;
}
};
getActionBar().setListNavigationCallbacks(adapter, navigationListener);
}
When I run the program, my app will crash and I get
NullPointerException: Attempt to invoke virtual method 'void
android.app.ActionBar.setNavigationMode(int)' on a null object
reference
It points to getActionBar() which is NULL.
Any thoughts?
I always have this error....
I found a solution which I put this to my Style it will be work and getActionBar() won't be null anymore...
<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
Hope this help you
Use getSupportActionBar() instead of getActionBar(). Also theme of your activity should be extended of Theme.AppCompat.
I think you should
1.Check themes
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
2.Check your Activity class , it's extend AppcombatActivity or ActionBarActivity
So, I hope it useful for you
I have a refresh icon on my menu which when clicked will refresh the contents of the Activity. But the problem is that the refresh icon is still clickable while it is refreshing which I do not want. Here is my menu.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=".MainActivity">
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:icon="#mipmap/ic_refresh"
android:enabled="true"
android:title="#string/action_settings"
app:showAsAction="always"/>
And here is where I handle the click event for the icon
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
// getData() fetches the data and updates the display
getData();
return true;
}
return super.onOptionsItemSelected(item);
}
Also, is there a way to maybe dim the icon so that the user knows that they have already clicked the icon?
When you click the icon, use the isEnabled() method of your Menuitem to check if it is currently enabled. If it is, then disable it until the data is loaded. Once the data loads successfully, then you can enable it again. Example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(item.isEnabled()) {
// "Dim" the icon like you said. You can use other alpha values if you like.
item.getIcon().setAlpha(130);
// Disable the menu icon
item.setEnabled(false);
}
getData();
return true;
}
return super.onOptionsItemSelected(item);
}
Now, you said that you have a getData() method which updates the display after it fetches the data. Once the data is fetched, check if the Menuitem is disabled. If it is disabled, then enable it again. Example:
if(item != null && item.isEnabled() == false) {
// "Undim" the icon
item.getIcon().setAlpha(255);
// Enable it again
item.setEnabled(true);
}
You can use setEnabled(true) method to enable/disable menu item
You could create a second menu.xml, that is used after the refresh button is clicked. To force a menu redraw call invalidateOptionsMenu();
In this second menu.xml, the refresh button could be disabled, or switched to a diffrent one, indicating the user, that the refresh is on going.
Easiest way to fix this is to hold on to the menu item in your activity.
private MenuItem mRefreshItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater infl = new MenuInflater(this);
infl.inflate(R.menu.menu, menu);
mRefreshItem = menu.findItem(R.id.action_refresh);
return super.onCreateOptionsMenu(menu);
}
/** Call this when you begin or end loading */
private void setRefreshEnabled(boolean enabled) {
mRefreshItem.setEnabled(enabled);
}
I can't find anything on the google and stackoverflow so I had to ask.
I have menu on Toolbar:
<item
android:icon="#drawable/help_button_selector"
android:title="#string/Menu_Toolbar_Help"
android:id="#+id/MENU_HELP"
app:showAsAction="always"/>
When user long press icon, system shows tooltip (popup/context menu) with title. However on most devices it has black border (like in Toast). Is there any way to style this border/background? I have already created style for toolbar theme and popup, but I can't find a proper item name.
1.- You can follow this post to create your ToolTip link
2.-You can do this to show a Toast as a ToolTip, then you can customize it as you want.
view.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "I'm a ToolTip", Toast.LENGTH_SHORT).show();
return true;
}
}
Hope it helps :)
Use below link to customize your Action Bar:
http://romannurik.github.io/AndroidAssetStudio/
open url -> GoTo Android Action Bar Style Generator -> Select the options -> click DOWNLOAD.ZIP -> Copy all the componets into their correspondence folders -> In your styles.xml file remove all the code and copy all the code of styles_**.xml file and delete this file.
Menu tooltip
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_skip) {
View view = findViewById(R.id.menu_skip);
Tooltip.make(this, new Tooltip.Builder(101)
.anchor(view, Tooltip.Gravity.BOTTOM)
.closePolicy(mClosePolicy, 5000)
.text("Tooltip on a TabLayout child...Tooltip on a TabLayout child...")
.fadeDuration(200)
.fitToScreen(true)
.activateDelay(2000)
.withCallback(this)
.floatingAnimation(Tooltip.AnimationBuilder.DEFAULT)
.showDelay(400)
.build()
).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Visit more