How to replace the actionbar by a popup menu? - android

I want to achieve 1 of these options for my EditText :
Replace the actionbar that appear on the top by a popup menu instead. Something like this for exemple:
Or make the actionbar floating and child of my current view (in some way same a first option)
i need this because i add my view via windowManager.addView(view, Layout_Params); and in this way i have some trouble with the actionbar on the top (it is displayed blank)
actually i do this to show the actionbar :
#Override
public ActionMode startActionMode(ActionMode.Callback callback) {
Activity host = (Activity) this.getContext();
return host.getWindow().getDecorView().startActionMode(callback);
}
but it's don't work, it's show me an empty white actionbar on the stop instead :( i think i need to create myself the ActionMode but i don't know how to do it.

Ok. You can hide the actionbar when your edittext is gained focus. Then you need to show the popup menu where ever you want.
EditText t = new EditText(this);
t.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b){
getSupportActionBar().hide();
//now show your popup menu at the top position
}else{
getSupportActionBar().show();
//here you dismiss the menu.
}
}
});

Related

BottomNavigationView showing items problem

I use BottomNavigationView in my app (modified from sample project) and I want to change the items in the container. I have 4 items within the container, 5 including the BottomNavigationView. I only want to display 2 items when apps is first start. Then show another 2 items and hide the previous 2 items when one of the Button in BottomNavigationView is clicked.
How to do it?
to hide any view button,textview or edittext just use this code on the button click
view.setVisibility(GONE)
GONE hides the whole view even the height and the width of the view
view.setVisibility(INVISIBLE)
INVISIBLE only hides the content but preserves the height and the width
boolean state; // define boolean variable
home.setVisibility(View.GONE);
info.setVisibility(View.GONE);
In your botton navigation click listener just use
b2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if ( state )
{
state = false;
home.setVisibility(View.VISIBLE);
info.setVisibility(View.VISIBLE);
}
else
{
state = true;
home.setVisibility(View.GONE);
info.setVisibility(View.GONE);
}
}
}

How to disable all clickable items in AppBarLayout?

I'm trying to disable all clickable items in the app bar layout when the opaque background appears after clicking the floating action button. But I also need to make sure that all the floating action buttons are all still clickable. I'm thinking maybe I can disable all items in the app bar programmatically?
How to achieve this?
UPDATE
Code to set fab visibility and animation. When the Fabs displayed, the tabs and toolbar finally disabled and unclickable. But i want to make my Fabs still clickable. How can i do this? Please advice. Thank you!
public void fabVisibility(){
if (isOpen){
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
fabActivity.startAnimation(fabClose);
textViewActivities.startAnimation(fabClose);
fabPost.startAnimation(fabClose);
textViewPosts.startAnimation(fabClose);
fabMedia.startAnimation(fabClose);
textViewMedia.startAnimation(fabClose);
fabPlus.startAnimation(fabRotateAntiClockwise);
fabActivity.setClickable(false);
fabPost.setClickable(false);
fabMedia.setClickable(false);
shadowView.setVisibility(View.INVISIBLE);
isOpen = false;
}else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
fabActivity.startAnimation(fabOpen);
textViewActivities.startAnimation(fabOpen);
fabPost.startAnimation(fabOpen);
textViewPosts.startAnimation(fabOpen);
fabMedia.startAnimation(fabOpen);
textViewMedia.startAnimation(fabOpen);
fabPlus.startAnimation(fabRotateClockwise);
fabPlus.setEnabled(true);
fabActivity.setClickable(true);
fabActivity.setEnabled(true);
fabPost.setClickable(true);
fabPost.setEnabled(true);
fabMedia.setClickable(true);
fabMedia.setEnabled(true);
shadowView.setVisibility(View.VISIBLE);
isOpen = true;
}
}
To disable the user interaction you just need to add the following code
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
To get user interaction back you just need to add the following code
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
To disable action bar (app bar) buttons on clicking FAB icon, you could set a flag, let's say DisableAppBarButton.
Now call invalidateOptionsMenu() which will trigger onCreateOptionsMenu and will regenerate your menu.
Modify your onCreateOptionsMenu to disable the buttons.
if (DisableAppBarButton) {
menu.someItem(R.id.yourItem).setEnabled(false);
} else {
menu.someItem(R.id.yourItem).setEnabled(true);
}
Two options:
1 - You can set a view group to be disabled by recursively setting all of its children disabled. Example:
public static setEnabled(View view, boolean enabled) {
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
setEnabled(viewGroup.getChild(i), enabled);
}
}
}
Then in your code in response to the FAB showing or hiding:
ViewUtil.setEnabled(mAppBarLayout, true /* or false */);
2 - You can make the opaque background focusable and clickable to intercept clicks while it's overlayed over the app bar, while the FAB buttons that float above that background would still receive clicks.
Hope that helps!

How to get the "up" button used on the Toolbar?

This is a short question:
I'm trying to force the action bar (used by a Toolbar) to use LTR alignment. I've succeeded making the layout itself use LTR, but not the "up" button (as I've done here, before Toolbar was introduced) .
It seems this view doesn't have an ID, and I think using getChildAt() is too risky.
Can anyone help?
The answer
Here's one way I've found to solve this, based on this answer .
I made it so that it is guarranteed to find only the "up" button, and whatever it does, it will revert back to the previous state it was before.
Here's the code:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
public boolean onCreateOptionsMenu(final Menu menu)
{
// <= do the normal stuff of action bar menu preparetions
if(VERSION.SDK_INT>=VERSION_CODES.JELLY_BEAN_MR1&&getResources().getConfiguration().getLayoutDirection()==View.LAYOUT_DIRECTION_RTL)
{
final ArrayList<View> outViews=new ArrayList<>();
final CharSequence previousDesc=_toolbar.getNavigationContentDescription();
for(int id=0;;++id)
{
final String uniqueContentDescription=Integer.toString(id);
_toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if(!outViews.isEmpty())
continue;
_toolbar.setNavigationContentDescription(uniqueContentDescription);
_toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if (outViews.isEmpty())
if (BuildConfig.DEBUG)
throw new RuntimeException(
"You should call this function only when the toolbar already has views");
else
break;
outViews.get(0).setRotation(180f);
break;
}
_toolbar.setNavigationContentDescription(previousDesc);
}
//
return super.onCreateOptionsMenu(menu);
}
It seems this view doesn't have an ID
You're right, the navigation view is created programmatically and never sets an id. But you can still find it by using View.findViewsWithText.
View.findViewsWithText comes with two flags:
View.FIND_VIEWS_WITH_TEXT
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION
The navigation view's default content description is "Navigate up" or the resource id is abc_action_bar_up_description for AppCompat and action_bar_up_description for the framework's, but you can easily apply your own using Toolbar.setNavigationContentDescription.
Here's an example implementation:
final Toolbar toolbar = ...;
toolbar.setNavigationContentDescription("up");
setActionBar(toolbar);
final ArrayList<View> outViews = Lists.newArrayList();
toolbar.findViewsWithText(outViews, "up", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
outViews.get(0).setRotation(180f);
Results

Android Layout button partially appears

So,
I have an activity with a layout, and in this layout I only have one button.
when clicking this button, the activity sets the visibility of the button to invisible, and launches a popup window.
I implemented a simple onDismiss function in this popup, which sets the button to visible
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
MainActivity.packButton.setVisibility(View.VISIBLE);
}
});
the problem is that sometimes, not very often, after the popup is dismissed, the button is shown, but only the top part of it, something like 1/5 of the button.
I suspected that the button became visible before the popup dismissed completely, and a sort of a clash happened between them, but on the other hand I made some checks and the popup window and the button are able to be shown at the same time without a problem, so a "layout clash" cannot be the reaon, right?
You can add delay and run this method on a handler.
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
MainActivity.packButton.setVisibility(View.VISIBLE);
}
}, 1000);
};
});
I would suggest making the button variable non static and instead call a method of your activity from your listener and in this method set the button visibility. Having the button as a static variable could mean that although it is non null the button isn't added to the activities view at the point when you call to set is visibility.

Put text in "TOP BAR" 2 lines + image

I'm trying put text in to bar, I only put (buttons), I don't know if I can put text 2 lines + image.
Here are references as changing the color:
https://developer.android.com/training/basics/actionbar/styling.html
in this other picture is displayed, as you can put more text in the top bar
http://www.whatsapp.com/img/v3/es/s1-chat.png
Simply create your own view and set it for your ActionBar: setCustomView().
You cannot achieve what you want with the standard layout. For plenty of examples look here:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// add the custom view to the action bar
actionBar.setCustomView(R.layout.actionbar_view);
EditText search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield);
search.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Toast.makeText(MainActivity.this, "Search triggered",
Toast.LENGTH_LONG).show();
return false;
}
});
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
}
}
Source: vogella.com
I don't think you can do it.
I'd advise you just to create a layout that would act like a top bar in your application.
This way you would be able to put there anything you want.
If you have more than one activity that needs to have this top bar, then use fragments instead of activities (to avoid your custom top bar from being animated when new activities are being opened).

Categories

Resources