How do I make UI elements fill the entire Android Screen - android

I am trying to add a bar similar to the "Today" bar here, but I cannot figure out how to force the width to match the screen. I am just using a simple textview (if there is a better way.. that's awesome, please tell it to me, but i cant find anything for it)
Here is what is happening with match_parent or fill_parent I recolored the textview to show the margins (that I dont want)

match_parent should fill the entire screen.
Check your parent activity layout or your fragment layout and you should find your margins there.
BTW, if you have a list of items like the one in the picture and you want it to have sections (like 'today', 'yesterday', etc) you should probably use recyclerview with headers instead of text view. Check this library for this, it's pretty easy:
https://github.com/cymcsg/UltimateRecyclerView

Related

Why is my Android phone layout different then my layout on Android Studio?

The layout of my MainActivity on my phone appears very differently than the layout I see on the code editor. Images are included. I am using a TabbedView. However, the problem still exists on any layout I choose (empty/tabbed).
What can I do to solve this problem?
I am concerned about the text "Hello world" in the middle, not in the top left corner. I follow this tutorial and in the tutorial it is correctly positioned.
It works as expected, because section_label TextView width is wrap_content it changes, depending on what text it displays. And the 2nd TextViews position depends on it, so it changes too. You should be able to verify this by setting various length texts in your layout editor.
For what you want to achieve, RelativeLayout is not the best choice and you may want to use i.e. LinearLayout instead, with its orientation set to vertical and layout_gravity of second TextView set to right
This is happen because you are using the RelativeLayout in the relativelayout we can arrage our views in custom position wise by the help of give the android:id=#+id/some_id like this if you want your second view is shown just below the first view then give them the property android:layout_below="#id/view1" in your second view
Please read all the documentation of RelativeLayout form this link
https://developer.android.com/guide/topics/ui/layout/relative.html

ContextMenu for blank space of an Android ListView

Is there a way I can get a context menu pop up when a user long presses on the blank space of a listview? I know that this can be done by setting wrap_content to the layout_height parameter of the listview. In fact I have been doing that successfully for a while. However, sometimes this wrap_content behaves very strangely and though there is enough space on the screen the listview restricts itself to a % of the screen and items scroll within that space. To avoid that problem I have moved to the path of setting the height as 0dp and weight as 1. However, that has disturbed the functionality I had in terms of long pressing the empty area of a list to add a new item to the list. Any help will be greatly appreciated.
Note: I have looked at multiple similar questions on SO throughout the day today but couldn't find any conclusive and elegant solution.
You can use ListView#addHeaderView() or ListView#addFooterView() to add extra view at the top or bottom of ListView, which you can make it looks like blank space.
Also I suggest you use match_parent to the layout_height attribute of ListView.

Hiding the Header view in ListView

I'm trying to add a header view to my list, and hide it all the time unless we scroll to it (like the pull-to-refresh mechanism). The problem is: if the list is not tall enough to fill the screen - the header view is shown on top of the list.
Is there a way to hide it, and make it visible only when we scroll to it? I've been trying a lot of stuff, but I can't figure out a good and simple way to do so.
Thanks
Here's a blog post describing a simple way of hiding and showing the header view.
The idea is to to put the content you wish to hide in a LinearLayout that wraps it, and hiding the content only. That way, the wrapping LinearLayout will collapse when its content is hidden, resulting in a headerView that is technically still present, but 0dip high.
Note: If you would try to hide the content without the enclosing layout, you would be left with unwanted space in the header view.
An example layout with a spinner representing the content:
<LinearLayout
xmlns:a="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Then you can hide the spinner (content) as follows:
spinnerLayout.findViewById(R.id.spinner).setVisibility(View.GONE);
You could look into ListView.setOverscrollHeader() or ListView.setOverscrollFooter(). Is this the behavior you are looking for?
If not, could you post some code showing what you have so far?
EDIT:
Ok so I looked into Overscrolling headers/footers and you're right, I don't think that's what you want at all.
Instead you should probably look into the Pull to Refresh mechanism from the Twitter app that others have tried to emulate. You can look into the answers from this question.
The most promising answer seems to be the custom ListView written by Johan Nilson, the code for which can be found here:
https://github.com/johannilsson/android-pulltorefresh
EDIT #2:
I took a look at the PullToRefresh custom ListView and what you want to do is probably possible, though not necessarily easy. Allow me to explain.
The PullToRefreshListView is essentially just a hack that exploits the optional Header in standard ListViews. The hidden "Pull To Refresh" that you see is really just the header of the ListView. When the list is displayed, this line is executed:
setSelection(1);
This scrolls the list to the first item on the list, effectively hiding the Header. When the List is short enough to be displayed entirely on screen, no scrolling is necessary, hence the "Tap to Refresh" button.
When this "Tap to Refresh" is visible, the pull to refresh mechanism is disabled, but it's easy enough to fix that. The pull to refresh effect is accomplished by increasing the top padding of the header view so that it appears that you are pulling the list down (when really it's more accurate to say that the Header is pushing the rest of the list down).
The amount of padding added is controlled by the applyHeaderPadding() function on line 199 of the source code. In that function there is an if statement on line 220 that only applies the padding when the list is in RELEASE_TO_REFRESH mode:
if (mRefreshState == RELEASE_TO_REFRESH)
{
//Some code that eventually adds padding to the header...
}
If you eliminate this condition or change it to apply padding no matter what mode you are in you can drag to refresh even if the list is short and the header says "Tap to Refresh"
if (true)
{
//Some code that eventually adds padding to the header...
}
However, this doesn't exactly create the effect you're looking for. If the list is short, you can drag it down to refresh, but the "Tap to Refresh" header is still shown. Now the problem is "How can I hide the header until the dragging motion begins?" This is a difficult problem on it's own, with several Stack Overflow questions dedicated to it.
If you want a header, you must add it BEFORE you set the adapter for the ListView, otherwise you get all sorts of errors.
I had some success with this, but I haven't come up with anything stable, because my solution is a kind of nasty hack on top of the already hacked PullToRefreshListView. I set an empty FrameLayout as the header and added the original pull to refresh header to that Frame Layout. Then, as I dragged the list, I edited the height in the LayoutParameters of the Frame Layout to grow and shrink much like the padding had originally. It sort of worked, but would eventually force close, and I haven't figured out why yet.
Anyway, if I get it to work I'll post the code, otherwise someone wiser than I might propose a solution based on the info I just provided.
Here is a solution for the current PullToRefreshListView (updated November 4, 2011):
https://github.com/johannilsson/android-pulltorefresh
based on the Hiding Header Views article:
http://pivotallabs.com/users/joe/blog/articles/1759-android-tidbits-6-22-2011-hiding-header-views
1) Copy pull_to_refresh_header.xml from the library's res/layout to your app's res/layout.
2) Edit your app's pull_to_refresh_header.xml. Wrap topmost RelativeLayout in a LinearLayout and then wrap the LinearLayout in a RelativeLayout again. Why? Topmost layout must be RelativeLayout because that's what's expected in code, second level layout must be LinearLayout because that's the only layout that collapses with View.GONE. Third level layout must be the same as original top-level RelativeLayout (except id) to preserve look.
3) Preserve same id on top RelativeLayout (pull_to_refresh_header), give second level LinearLayout an id of your choosing, give third level RelativeLayout another id (pull_to_refresh_header2 for example).
4) Move all padding from the topmost RelativeLayout to the second RelativeLayout.
5) In your code use findViewById and your LinearLayout id to set visibility to View.GONE. The LinearLayout will collapse, and if you moved all padding values appropriately to the inner RelativeLayout the header should take no space at all.

How to layout TextViews side-by-side with spacing in android?

I have a very simple layout with two side by side textviews. Both have the same parent layout that fills the screen horizontally.
I need them to have a visible space between them so that they are visually seperated when both have text. I also need the left textview to take up about 2/3 the screen width and let the other have the rest.
This is fairly easy to do with LinearLayout and a few margin settings, but if either one of the views has no text, I need the other one to fill the entire width.
I'm not quite sure how to have the layout do that without setting the empty view's visibility to GONE in code. Is there any good, efficient way to do all of these things at once? Feel free to use any layout you wish to make it work.
have you tried this using a relative layout? there is a property for layout_alignWithParentIfMissing that might give you what you need...

Implementing Android Action Bar

I am working on creating an Action Bar like the one from the new Android UI Patterns and I am running into a bit of trouble. I have a ViewSwitcher with two layouts in it. When the user taps the search button I animate between the two layouts. The problem is that the layouts are different sizes and I can't figure out how to make them take up the same amount of space. Here's what I mean. p.s. forgive the bad art, they are just place holders ;)
The red box is right up against the ViewSwitcher and there is a gap between that and the action bar for this layout:
but not this one:
What I want is to tell the layouts in the ViewSwitcher to be the same size. How can I do this?
You can try setting android:minHeight on the smaller one to be whatever the height of the larger one is. Or maybe setting it on the ViewSwitcher itself.
Idea:
The ViewSwitcher only lets you add two Views and in your case they are two ViewGroups. You can call View#measure on each of the views you're adding to the ViewSwitcher. It looks like you should use ViewGroup.LayoutParams.WRAP_CONTENT for each parameter to measure. Then you can call View.getMeasuredHeight on each view. What ever is the larger set the smaller view using View#setMinHeight.

Categories

Resources