I am trying to load an Icon dinammically into my Bottom Navigation Bar.
Each time, the user clicks a new variable (in my case a new city) the icon for the variable shall be downloaded from the firebase server and be set to the bottom navigation bar.
My code till now looks like this:
// first I load the relevant Icon from firebase
iconRef = mFirebaseDatabase.getReference().child(city);
iconRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
cityIcon = (String) dataSnapshot.getValue();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
// this is for initiating bottom navigation bar
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
Menu menu = bottomNavigationView.getMenu();
// the following shall load the Id of the part in the bottom navigation bar, where the icon shall be passed to
ImageView iconView = findViewById(R.id.CityIcon);
Picasso.get().load(cityIcon).into(iconView);
menuItemCity.setTitle(cCity);
// the problem is, when I start the activity it crashes
Alternatively I thought this could help me:
// but the following gives me an error as I am only allowed to enter an Integer and I am entering a String
MenuItem menuItemCity = menu.getItem(0);
menuItemCity.setIcon(cityIcon);
I was also thinking about first saving all the icons into the drawable folder. But as I want to be able to change the Icons after launch of the App it wouldn't serve my requirements. Though there might be a possibility to save the Icons into the drawable folder after downloading them from the server - maybe some of you people have any ideas how to solve this problem.
Related
I am using this tool to get a toggle button behavior, this is the library:
https://github.com/ceryle/SegmentedButton
I want the left and right buttons to have different colors when selected. When I change the color programmatically, it works fine except from inside the listener, any hin on why this would happen?
SegmentedButtonGroup toggle = dialogView.findViewById(R.id.my_toggle);
toggle.setSelectorColor(activity.getResources().getColor(R.color.green));
toggle.setRippleColor(activity.getResources().getColor(R.color.green));
toggle.setOnPositionChanged(new SegmentedButtonGroup.OnPositionChanged() {
#Override
public void onPositionChanged(int position) {
SegmentedButtonGroup toggle = dialogView.findViewById(R.id.my_toggle);
toggle.setSelectorColor(activity.getResources().getColor(R.color.red));
toggle.setRippleColor(activity.getResources().getColor(R.color.red));
}
});
I am also open to other implementations of this functionality, I am having a hard time with this...
I'm running into a strange issue when adding a Toolbar to my Form in my Codenameone app. If I set a toolbar on my form, it shows another title with the toolbar hamburger and new title below the title of the previous form instead of replacing it like I would expect. It looks like this:
The functionality works fine replacing the old title like I would expect when I run in the Codenameone simulator, but I get this weird behavior shown in the image when I make an Android build and run it on a Nexus 5 (6.0.1). The back arrow and "12 of 12" is the title from the previousForm
This is my code, am I doing anything wrong here with the Toolbar usage?
void goShowResource(final Form previousForm) {
previous = previousForm;
final Toolbar bar = new Toolbar();
final Form rd = new Form("resource details");
final Resource thisResource = this;
rd.setToolbar(bar);
bar.addCommandToSideMenu(new Command("command 1") {
#Override
public void actionPerformed(ActionEvent evt) {
AddResources ar = new AddResources(settings, thisResource);
ar.goAddResources(rd);
}
});
bar.addCommandToSideMenu(new Command("command 2") {
#Override
public void actionPerformed(ActionEvent evt){
UpdateResource ur = new UpdateResource(settings);
ur.goUpdateResource(rd, thisResource);
}
});
rd.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
showDetails(rd);
rd.show();
}
edit: Additonal info, if I open the sidemenu once, the old title bar at the top shrinks away, and I'm left with the the single correct yet incorrectly formatted title area.
You should use the Toolbar for all the forms in the app or disable the default which is native menu bar when working with the toolbar. You can do the latter by editing the theme and selecting the constants tab then pressing "Add" and selecting commandBehavior=Side.
Android currently defaults to the native ActionBar behavior and Toolbar implicitly overrides that, however when a transition occurs from the native to the lightweight component things can get pretty hairy (and might also look unnatural) so we recommend picking one UI paradigm and going with it.
Since the ActionBar is a volatile API we recommend Toolbar going forward as its far more customizable and gives us a lot of control.
This can be fixed by removing all command from the form after setting the toolbar, then add a fresh back command to the toolbar if required.
void goShowResource(final Form previousForm) {
previous = previousForm;
final Toolbar bar = new Toolbar();
final Form rd = new Form("resource details");
final Resource thisResource = this;
rd.removeAllCommands();
rd.setBackCommand(null);
rd.setToolbar(bar);
//Add back command
Command back = new Command("back") {
#Override
public void actionPerformed(ActionEvent evt) {
previousForm.showBack();
}
};
bar.addCommandToSideMenu(back);
bar.addCommandToSideMenu(new Command("command 1") {
#Override
public void actionPerformed(ActionEvent evt) {
AddResources ar = new AddResources(settings, thisResource);
ar.goAddResources(rd);
}
});
bar.addCommandToSideMenu(new Command("command 2") {
#Override
public void actionPerformed(ActionEvent evt){
UpdateResource ur = new UpdateResource(settings);
ur.goUpdateResource(rd, thisResource);
}
});
rd.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
showDetails(rd);
rd.show();
}
I got a Button in my Main Activity which can get two background images.
I got a variable in a class, which is boolean and got the state the button should have.
In the onCreate() I got following:
if(HelperFunctions.itsRinging == true){
stopButton.setBackgroundResource(R.drawable.klingelt);
}else {
stopButton.setBackgroundResource(R.drawable.normal);
}
If the App is closed, a push message comes in, I open the app, the icon got the "klingelt" background. Like it should!
After clicking it, it got the "normal" background. Like it should!
But when the app is in front, the icons not changes.
How can i refresh the background even if the app is in front?
The button itself is in a standard onClick Method:
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SoundManager.stop();
//set Background normal
if(HelperFunctions.itsRinging = true) {
stopButton.setBackgroundResource(R.drawable.normal);
HelperFunctions.itsRinging = false;
}
I hope its clear, what I want to archieve here :)
So the icon should change its background in "live-mode" when the app is on in front and visible.
I have built an Android app using the AppCompat navigation drawers (one on the left and one on the right). My questions is whether and if so, how, one would go about placing one of the "up" buttons on the title bar, so each drawer has its own button (one on each side of the title bar). I have the following code:
new DrawerToggle(this, getLeftDrawerLayout(), R.drawable.imgur_drawer_list_button_shape_left, R.string.open_drawer, R.string.close_drawer, getLeftDrawerListView(), LeftDrawerDrawerArrayAdapter);
new DrawerToggle(this, getRightDrawerLayout(), R.drawable.imgur_drawer_list_button_shape_right, R.string.open_drawer, R.string.close_drawer, getRightDrawerListView(), RightDrawerDrawerArrayAdapter);
where DrawerToggle extends android.support.v4.app.ActionBarDrawerToggle and R.drawable.imgur_drawer_list_button_shape_left as well as R.drawable.imgur_drawer_list_button_shape_right are in the drawable resource directory as required.
Also, the following code is handled in the activity onCreate method:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
Unfortunately this only produces an application with solely the left-hand drawer "up" button icon, which reacts (i.e. slides in) to both, the left and right drawers...
Can anyone please help and provide a way to have the buttons on either sides of the title bar, each corresponding to its respective drawer?
Thank you sincerely! :)
Piotr.
Having toggles at both sides is never stated in the design guidelines. There is a hacky way to do this, it is real mess, but give it a try.
This includes overriding the functionality of the overflow menu item, which means you will never have it working as it supposed to.
Add an menu item that has showAsAction="never" to make sure you have always a overflow menu item.
Style it as your heart desires
Get View reference of the overflow button as following:
public static View getOverflowButton(Activity a){
View homeButton = a.findViewById(android.R.id.home);
ViewParent parentOfHome = homeButton.getParent().getParent(); //ActionBarView is parent of home ImageView, see layout file in sources
try{
parentOfHome = parentOfHome.getParent();//get to ActionBarView;
Class absActionBarView = parentOfHome.getClass().getSuperclass(); //ActionBarView -> AbsActionBarView class
Field menuPresenter = absActionBarView.getDeclaredField("mActionMenuPresenter"); // ActionMenuPresenter is the object that calls openOverflowMenu() closeOverflowMenu()
menuPresenter.setAccessible(true); // and contains the overflow button view.
Object menuPresenterView = menuPresenter.get(parentOfHome);
Field overflowField = menuPresenterView.getClass().getDeclaredField("mOverflowButton");
overflowField.setAccessible(true);
View overFlowButtonView = (View) overflowField.get(menuPresenterView);
return overFlowButtonView;
}
catch(NoSuchFieldException e){
Log.e("getOverflowButton()", e.getMessage());
} catch (IllegalArgumentException e) {
Log.e("getOverflowButton()", e.getMessage());
} catch (IllegalAccessException e) {
Log.e("getOverflowButton()", e.getMessage());
}
return null;
}
Add OnClickListener to the view reference (anonymous declaration or field). Trivial.
Toggle the drawer in the onClick()
Use onDrawerSlide(View drawerView, float slideOffset) to animate the drawable as it is animated in the ActionBarDrawerToggle. Basically, it is off-screened-drawable by half and it is moved back and forth by some factor made up from the slideOffset.
In my app, I want to be able to start my own function when user copy a text to the clipboard.
I'm overriding function onActionModeFinished and as I want to be sure user tapped on Copy I'm getting selected navigation index. Unfortunately if always return -1. Is there something I'm doing wrong here ?
#Override
public void onActionModeFinished(ActionMode mode) {
super.onActionModeFinished(mode);
if (_actionBar != null) {
int index = _actionBar.getSelectedNavigationIndex();
// here index is always -1
}
}
ActionBar can contain tabs and list selector on the left side. So, getSelectedNavigationIndex is used to get the position in this navigation items not in the menu items located in the right side.