MenuItem actionView aligns right - android

As the title says, I'm setting using setActionView on a MenuItem, here's the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="4dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
The issue is that the ProgressBar appears aligned to the right. (It should be aligned to the center, same as the MenuItem icon. Icons are 32dp
How can I achieve this?

I faced similar issue today and coudn't find any good QA on SO. Hence I did some research. Here are my findings on the matter:
Android Menu is implemented in this way:
If you have inflated an actionView to have:
icon (optional)
title (optional, or to say it can be made optional by setting it as "")
a custom view
and you don't set icon, set title as "" and set actionView to make the menuItem appear to contain only the custom view which you inflated using setActionView, then:
Android forces the custom view's width to be wrap_content and remaining area gets occupied by icon+title of the menuItem.
We can think of this implementation as:
Think of the entire view after margin as ParentLinearLayout which is divided into LinearLayout1 and LinearLayout2 (depicted in the image above as Layout 1 and Layout 2 respectively)
Now LinearLayout1 has width=0dp and weight=1
And LinearLayout2 has width=wrap_content
Hence no matter what we set, currently, we cannot right align the inflated custom view (action view) in menu.
To be technically accurate, you can right align if you really need this done by checking out the internal implementation code for the navigation menu by Android. But not really worth the effort. Any feature is a tradeoff of time required vs outcome.

Try this android:layout_gravity="center"

You should try android:gravity="center" in RelativeLayout.

Related

Linear Layout Overlapping Navigation Bar

I can not figure out for the life of me why and how to stop the Linear Layout from overlapping the navigation bar. I am familiar with Android Studio, just not version 2+.
This problem was never a thing and I have tried multiple searches and attempts to fix this. (Screenshot below).
Put this in your LinearLayout code in the content_main.xml file.
android:layout_width="match_parent"
android:layout_height="match_parent"
In your LinearLayout set padding top 50dp
android:paddingTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
You can change margin values depending on layout position.

unwanted side margins of my custom listview item layout

I'm getting some weird left and right side margins when using a custom listview item layout. They are margins (or at least not padding of the container), since the background doesn't extend to the edge.
In this layout, I'm using a simple vertical LinearLayout with a bunch of textviews and a progressbar. If I switch back the built-in simple_list_item_activated_1.xml, the margins disappear. The linear layout itself doesn't have any layout margins. I specifically stripped it of any attributes, leaving only the id, the layout_width="match_parent" and layout_height="wrap_content", and the margins were still there.
Is there anything I'm missing here?
A screenshot of the problem can be seen here:
Edit 1: #Grishu: As I've said earlier, these margins appear even with a very simple layout, such as this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/some_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I just typed this from memory, so it might contain syntax errors. But you get the idea.
Edit2: I just went over all my layouts. The problematic margin was set on one of the parent containers, thus it has nothing to do with list items. Sorry for the spam.
i had a similar problem with a listview item layout, it looked fine in the graphical layout in eclipse, i solved it by setting:
android:layout_margin="0dp"
android:padding="0dp"
in the main relative layout, hope this helps.

How to show an icon in the middle of the screen for an empty listview?

As you can see the picture below that when there is no item in the list it shows nice icon in the middle of the screen with a text. I can get only a text shown if the listview is empty with this code in my custom layout :
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
adding this element in my layout show a text when the list is empty. I would like to show an icon instead and also it should be in the middle of the screen. Instead of a textview if I use a imageView again it goes top of the screen. what is the best approach to this? I might be able to get it as I like using imageview and textview and both with android:id="#android:id/empty attribute. or is that possbile to have layout with the android:id="#android:id/empty attribute and place imageview and textview inside?
Sure, just use a layout. LinearLayout might work well in this case. You'll probably have to set the gravity to center. It seems you already had the right idea, I'm not sure why you didn't just try it out? You would have had it!

Using layout_gravity="bottom" to place at bottom of LinearLayout

I would like to place a layout on the bottom of a LinearLayout, but I can't seem to get it to work. I know that I can use RelativeLayout to do this, but I should be able to use LinearLayout, shouldn't I?
EDIT: Actually this is more confusing than I thought. The layout below is simplified. In reality, I'm using fragments on a pre-3.0 device, with the compatibility layer.
I used Hierarchy Viewer to examine what's going on, and found that an android.support.v4.app.NoSaveStateFrameLayout was added to my layout, and that layout has layout_height set to wrap_content. That seems to be what's causing my problem, but I haven't yet figured out how to fix it.
Specifically, why doesn't this work? Shouldn't the layout_gravity place it at the bottom?
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
... stuff here ...
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
... more stuff here ...
</LinearLayout>
</LinearLayout>
BTW, changing layout_height to fill_parent or setting layout_weight don't seem to work either. I just want to better understand what is going on, because clearly I'm missing something important. Thanks.
First of all nice question.
Android behaves we can say weird in the situation like this.
if you have selected your parent linear layout's orientation horizontal then you can set its child component at bottom by setting its layoug_gravity=bottom. suppose you have added 2 text views in that horizontal linear layout and second textview's layout_gravity is bottom then it will set to bottom but it work like it is set at bottom in other column then the first text view. NOTE : you can set textview's layout_gravity = "left" or "right" when its parent linearlayout is horizontal but you cant see its result.
Oppositely, if you have selected parent linearlayout's orientation vertical then you can set its child component at left or right by using layout_gravity. but the second textview will shown in you can say next row with left or right gravity as you have set. NOTE you can set textview's layout_gravity = "top" or "bottom" when its linear layout is vertical but you can not see its result.
Try to make sample xml design as i have stated above so you get better idea.
Strange but True!!! Try to understand this behavior. :)
Just add space between what you want at the bottom and all the rest:
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
So I resolved the problem. It's a two-part solution:
First, the way to do this without using LinearLayout is to provide weight to the element above so that it takes up all of the empty space. BTW, you can see this example in the API demos: http://developer.android.com/resources/samples/ApiDemos/res/layout/linear_layout_3.html
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
... stuff here ...
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weight="1"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
... more stuff here ...
</LinearLayout>
</LinearLayout>
This by itself didn't solve my problem, as I had a NoSaveStateFrameLayout with layout_width="wrap_content" as a parent view, and so I needed to get that fixed first. I'm using code based on the wonderful Google I/O App, and when I searched the code for NoSaveStateFrameLayout, I found this:
// For some reason, if we omit this, NoSaveStateFrameLayout thinks we are
// FILL_PARENT / WRAP_CONTENT, making the progress bar stick to the top of the activity.
mRootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
Thanks for an awesome comment Google!!! I added this into my source and everything worked great!
The moral of the story: Hierarchy Viewer and comments are your friends.
LinearLayout will just stack things as they are placed in there. Since it is vertical, it will keep placing items one after the next in a vertical manner. Can you change the android:gravity of the linearLayout and not the layout_gravity of the nested one and see if that works.
RelativeLayout of course should be the first way but you stated you didnt want to do that. Is there reason for that?
It could be that, as per https://stackoverflow.com/a/13366783/513038, you need to set the parent LinearLayout to have android:baselineAligned="false". Worked in my case.

android child view ignore parent padding

I have many layouts that have different ViewGroup roots (LinearLayouts, RelativeLayouts, etc). These views all work as I expect, but now I am trying to add a top navigation bar. The problem is that the roots can have different paddings, and if I simply include my layout within each of these layouts, the navigation bar is limited in width by the parent's padding. I'd like this navigation bar to ignore the root's left/right/top padding and be completely full width at the very top of the layout. Is there anything I can do within the navigation bar's layout to achieve this, or am I doomed to have to modify all of the existing layouts to accommodate this?
While this seems like a pretty bad idea to begin with, you might be able to accomplish what you're looking for by setting the clipToPadding attribute of the parent ViewGroup to false and then set negative margins on the child view.
Example:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#EEE"
android:clipToPadding="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:background="#333"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-10dp"/>
</LinearLayout>
The above example works, but I would suggest that you should just put your top navigation bar outside of these ViewGroup that contain the paddings you're trying to avoid.
Why would you need to change all of your layouts? If it's only your navigation bar that needs to ignore the padding, you could just take the nav bar out of it's current parent layout, and then put both layouts inside another layout that has no padding.

Categories

Resources