Show Action's Toast in a custom view - android

I needed to create a custom view to set an extra Action in a Sherlock ActionBar. Here's the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/edit"
style="#style/Widget.Sherlock.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_edit" />
</LinearLayout>
and here's the code:
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
ActionBar actionbar = getSherlockActivity().getSupportActionBar();
actionbar.setCustomView(getSherlockActivity().getLayoutInflater()
.inflate(R.layout.top_sample_detail, null), lp);
actionbar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP
| ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_TITLE);
I can easily find the edit button by targeting actionbar.getCustomView().findViewById(R.id.edit), but that's not the problem. I'd like to know if there's a default, automatic way to show the button's name in a Toast after a long click, just as it'd do if it was an Action in a non custom view.
tl;dr
To sum up: Is there an attribute/method/property you can set to an ImageButton, to make it behave as an Action item and show a Toast on long click, just the same ways an Action item would do?

i've come up with a similar problem (here's the post about it) , and I have thought of 2 possible solutions:
extend ActionMenuItemView somehow, and make its layout to include what it needs but disable the views from showing. add your own layout to it layout. this is quite a messy solution.
use setOnLongClickListener on the view of the action item, and call the same code that is called for ActionMenuItemView::onLongClick . this is actually what i've written on the post i've created (here)

Instead of doing all of that, I think you can add the item to your menu xml and set showAsAction to "always".

Related

What is this UI control and how can I make one in my app?

Background
In the following screenshot from Gmail, there is some sort of error bar shown just underneath the action bar / app bar. This bar shows permanently, and it pushes the remaining content down rather than overlaying it.
Questions
Is there a name for this type of UI component/control?
Is there something built-in or in the support libraries that I can use to add one of these to an activity of my own?
Material design has an example of this under the App Errors section. It refers to the control as:
Container/component specific error with action
However, I also found an example in the Android Unified Email app, which refers to the control as Tip View:
ConversationTipView.java
conversation_tip_view.xml
I adapted the source code from the above to make my own:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tip_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e5e5e5"
android:orientation="vertical"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
>
<TextView
android:id="#+id/tip_view_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#android:color/primary_text_light"
android:textSize="16sp"
/>
<Button
android:id="#+id/tip_view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right|end"
android:text="#string/preference_accessibility_service_enable"
style="#style/Widget.AppCompat.Button.Borderless.Colored"/>
/>
</LinearLayout>
If it is a Snackbar like #Karan says you can make it appear on the top like this:
Snackbar snack = Snackbar.make(parentLayout, str, Snackbar.LENGTH_LONG);
View view = snack.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snack.show();
This shows an animation of Snackbar sliding from bottom, then switching to top. If you want to avoid this try this library(I haven't tried this myself): https://github.com/AndreiD/TSnackBar
Add a view inside of your current layout as per your design and make visibility gone, and make it visible when required from your activity/fragment.
when this will appear in the screen it pushes the remain content down from the current position.
For elevation you can use CARDVIEW.
hopefully it will help you;

How to make custom view take up all the actionbar space so it will cover menu as well?

I have created app with similar functionality to a browser. This is what I have right now:
Whenever user clicks on the edittext(the one with google.com inside it) i want to to take all of the space in actionbar covering overflow button and other icons(those are menu items as well).
Is there any way way I could do that? So far I have tried setting the layout-weight of the edittext to 1 but it does not work. The custom view inside actionbar is an linear layout like this one here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchfield"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:imeOptions="actionGo"
android:inputType="textWebEmailAddress"
android:text="www.google.com" />
</LinearLayout>
I should also mention that i want to do it programatically.
Thanks guys
You can hide these icons by following topic: How do I hide a menu item in the actionbar?
I think you can easily hide the actionbar when click,
something like:
ActionBar actionBar = getActionBar(); //OR getSupportActionBar();
actionBar.hide();
Then bring it back when you need it by using:
actionBar.show();

ActionBar with custom layout does not occupy full screen width on Android 4.4.2 (KitKat)

I want to display an Action Bar with a custom layout. The custom layout has three ImageViews, one in the centre and the other two on the left & right ends of the action bar. There must be no back button or action items.
Here is what I've used:
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
View actionBarView = LayoutInflater.from(this).inflate(resource, null);
actionBar.setCustomView(actionBarView, lp);
The custom layout is inflated correctly, but does not occupy the full width of the screen. This happens on Android 4.4.2 (KitKat). On Gingerbread, the above arrangement works properly.
I've tried the recommended solutions in the following discussions:
ActionBar - custom view with centered ImageView, Action Items on sides
RelativeLayout/customview doesn't fill tab's width
However the problem remains. There has to be a way to force the layout to occupy the complete screen width.
I am using the Action Bar from the V7 AppCompat library. Does anyone know a way around this ??
EDIT 1:
Here is the XML layout file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal">
<ImageView
android:id="#+id/left_icon"
android:background="#drawable/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:clickable="true"
android:onClick="leftButtonPressed"
android:hapticFeedbackEnabled="true"
/>
<ImageView
android:id="#+id/center_icon"
android:background="#drawable/centertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
/>
<ImageView
android:id="#+id/right_icon"
android:background="#drawable/rightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:clickable="true"
android:onClick="rightButtonPressed"
android:hapticFeedbackEnabled="true"
/>
</RelativeLayout>
Here is what it looks like on Gingerbread (2.3.5):
As you can see, its inflating correctly. And here's how it looks on Kitkat (4.4.2):
You can see that there's a slight gap on the right and the layout is not occupying the complete width.
I dont believe there's a problem with the layout (its inflating correctly on Android 2.3.5), but I could be wrong. Tell me if I'm crazy.
EDIT 2:
Thanks to Doctoror Drive and this post, I was able to get the result I wanted. I know its a bit strange: why use an Action Bar if you are not going to have any navigation or action items, but this is what I need for my particular problem. I have a TabHost with a stack of sliding Fragments in each tab, and the top LinearLayout (i.e. the Action Bar) needs to change its appearance when different Fragments are displayed.
EDIT 3:
Here are some more links for understanding this further:
https://groups.google.com/forum/#!msg/actionbarsherlock/i8JRUkBJjqk/ZzQV9xaM-lkJ
https://groups.google.com/forum/#!topic/android-developers/FGEi72thLzE
In my case I have solved the issue bypassing the invocation of the LayoutInflater.
So one should simply replace this code:
View actionBarView = LayoutInflater.from(this).inflate(R.layout.custom_actionbar, null);
actionBar.setCustomView(actionBarView, lp);
with this code:
actionBar.setCustomView(R.layout.custom_actionbar);
It's not a gap, it's a menu overflow button to the right. If you look closely you can see three dots button.
You've chosen the wrong theme because you can hardly see it.
Use Theme.Compat.Light.DarkActionBar if your ActionBar is dark and you need the light theme.
On devices that don't have physical menu button (mostly new ones) the menu overflow button is shown.
You can't remove it unless you disable menus.
You can use splitActionBarWhenNarrow uiOptions which will split the ActionBar so that the menu part will be at the bottom one, but that will be only for vertical orientation of medium screens and probably not what you need.
Take a look at the ActionBar Patterns doc and Menus UI doc

Customise context menu like pinterest menu

I've been looking for something like Pinterest menu whenever item in GridView is pressed. I know it's too broad question. But little strike on question will provide a correct way to achieve these.
Que:
How one can implement customise context menu like Contacts+ or Pinterest Context menu on GridView item?
Tried:
ArcMenu : But they are replacement of Slider Menu or Pop up Menu for overall Application. I want menu which can be created onFly for GridView Item.
Satellite Menu : Same as ArcMenu, replacement of Slider Menu or Pop up Menu for overall Application.
Please enlighten me to achieve behaviour like these.
I think instead of Context Menu you can use PopupWindow for your requirement.
//Custom popup view
View view= layoutInflater.inflate(R.layout.popupview, null);
PopupWindow popupWindow = new PopupWindow(
view,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
//Display popup window on clicking anything
//Displays pop up window near button with offsets 10 and -10
popupWindow.showAsDropDown(button, 10, -10);
For more info
http://developer.android.com/reference/android/widget/PopupWindow.html
http://android-er.blogspot.in/2012/03/example-of-using-popupwindow.html
Use quick action 3D view. It is the menu which is used in twitter application.
For source: https://github.com/lorensiuswlt/NewQuickAction3D
I'm using a modified version of ArcMenu (just small and mainly visual modifications) for something similar. And it's perfectly adaptable to gridview (i'm using it with StaggeredGridView onitemclick).
You only have to define it in the xml inside the gridview item with Visibility:gone and then in your gridview adapter or in the activity set it to visible when the item is touched or clicked...
don't know why you say it's for overall app, it can be used as an item element also.
You can check out this library which I created:
https://github.com/reyanshmishra/PinMenu
You can clone it and import it as a module to your app and do something like this:
In your XML layout:
<?xml version="1.0" encoding="utf-8"?>
<com.reyanshmishra.pinmenu.PinMenuHolder xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:pin_holder_draw_over_view="true"
app:pin_holder_overlay_color="#90ffffff">
<com.reyanshmishra.pinmenu.PinMenu
android:id="#+id/one"
android:layout_width="48dp"
android:layout_height="48dp"
android:elevation="5dp"
android:padding="5dp"
android:scaleType="centerInside"
android:src="#drawable/ic_close_black_24dp"
app:pin_background_color="#color/white"
app:pin_name="Cancel"
app:pin_selected_color="#BD081C" />
<com.reyanshmishra.pinmenu.PinMenu
android:id="#+id/three"
android:layout_width="48dp"
android:layout_height="48dp"
android:elevation="5dp"
android:padding="5dp"
android:scaleType="centerInside"
android:src="#drawable/share_variant"
app:pin_background_color="#color/white"
app:pin_name="Share"
app:pin_selected_color="#BD081C" />
<com.reyanshmishra.pinmenu.PinMenu
android:id="#+id/four"
android:layout_width="48dp"
android:layout_height="48dp"
android:elevation="5dp"
android:padding="5dp"
android:scaleType="centerInside"
android:src="#drawable/dots_horizontal"
app:pin_background_color="#color/white"
app:pin_name="More"
app:pin_selected_color="#BD081C" />
</com.reyanshmishra.pinmenu.PinMenuHolder>
Now in Java:
PinDialog mPinDialog = new PinDialog(this);
mPinDialog.setContentView(R.layout.layout_pin_menu);
mPinDialog.setPinSelectListener(new PinSelectListener() {
#Override
public void pinSelected(PinMenu pinMenu) {
Toast.makeText(mContext, "" + pinMenu.getPinName(), Toast.LENGTH_SHORT).show();
}
});
mPinDialog.addToRecyclerView(mRecyclerView);
It's still under development so it just supports recyclerview. For depth of the implementation, you can just skim through the classes of the library. I don't think I can put all the code here.
The result it something like this:

Pull down view (menu) from ActionBar

I need to implement a pull-down view that has a "handle" in right-most part of the ActionBar. It should be full width and open with an animation when the handle is clicked, and additionally the handle itself should be draggable. minSdkVersion is 8
Regarding the pull-down functionality itself, I found that SlidingDrawer isn't going to fit the bill, since it has been deprecated as of API v17, and it can only be opened from bottom to top. The control SlidingTray seems to overcome that issue. I haven't tested it thoroughly but but it seems to work as expected.
Now to the main issue. Is it even possible to display the view in such a manner? I have tried to set a custom view for the ActionBar, where the inflated XML looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<my.package.drawer.SlidingTray
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:content="#+id/content"
android:handle="#+id/handle" >
<ImageView
android:id="#+id/handle"
android:layout_width="88dp"
android:layout_height="44dp"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</my.package.drawer.SlidingTray>
</RelativeLayout>
Now, the SlidingTray view itself is functioning as expected when I put it in the activity/fragment layout (can drag the handle and click it to open/close the tray), but when inflating the layout above inside the ActionBar, and upon pressing/dragging the handle, the Tray only moves a few pixels before stopping - it doesn't go beyond the ActionBar bounds. This is the main issue - can the view go beyond the ActionBar itself (over the activity displayed below), and if so - how?
Since no one answered, I'll post how I resolved the issue.
After some inspecting with hierarchyviewer, I saw that the ActionBar was in a LinearLayout and as such, it wouldn't be possible to extend a child of it outside of the ActionBar bounds. So I decided to get the root (decor) view and attach the modified version of SlidingDrawer there. Here is the excerpt:
ViewGroup decor = (ViewGroup) getWindow().getDecorView();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View drawerContainer = inflater.inflate(R.layout.sliding_drawer, null);
drawer = (SlidingDrawer) drawerContainer.findViewById(R.id.drawer);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
decor.addView(drawerContainer, params);
Since adding a view this way displays it behind the Status bar, I also added a container view with top padding of 25dp so that the handle and content are displayed beneath it.
Note: if you are using the SlidingMenu library, you need to do this in onPostCreate(), because the library also does this and will place your view behind all of the other content.

Categories

Resources