I'm writing an application and have come across a bit of an issue.
When working with a layout file, I have a 2 RelativeLayouts inside of a ViewFlipper.
The idea is that this page in particular is a welcome screen. The first RelativeLayout "welcomes" you to the app, and upon pushing a button, the user is led to the second RelativeLayout. In this layout there is a search bar that will allow the user to search for a certain criteria (specific to the app, not important) then display the results in a ListView.
Everything works correctly, but displaying the ListView seems to have some problems. The adapter is set properly, and I tested the method on another ListView in a test layout, and it worked fine. However something seems wrong with the ListView in the RelativeLayout. Here is the layout code
activity_welcome.xml
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/welcomeFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:background="#color/white"
tools:context="com.jacemcpherson.announcer.WelcomeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:padding="40dp">
...
<!-- This code irrelevant, all's well :) -->
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="#color/white"
android:padding="40dp">
<TextView
android:id="#+id/textFirstThingsFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textYouNeedToSearch"
android:layout_centerHorizontal="true"
android:text="#string/first_things_first"
android:textColor="#color/app_color"
android:textSize="32sp" />
<TextView
android:id="#+id/textYouNeedToSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:gravity="center"
android:text="#string/you_need_to_search_for_your_school"
android:textColor="#color/black"
android:textSize="18sp" />
<EditText
android:id="#+id/schoolSearchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textYouNeedToSearch"
android:hint="#string/search_hint" />
<ProgressBar
android:id="#+id/searchListProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/schoolSearchEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:visibility="gone" />
<!-- This is the problem area -->
<ListView
android:id="#+id/searchResultsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/schoolSearchEditText" />
</RelativeLayout>
</ViewFlipper>
I'm setting the adapter works like normal, but just in case I missed something, here's the line of code for that...
mListView.setAdapter(new ArrayAdapter<String>(
mContext,
android.R.layout.simple_list_item_1,
result // this is the array of results to be displayed in the list.
));
Thank you for your help, if I've missed something that in some way makes this unanswerable without further information, please let me know.
Change the layout_height from wrap_content to either match_parent or fill_parent.
This is explained very well by this answer.
Related
This is my custom dialog which consist of the following
list view,button and finally a textview used when the list is empty.
My problem is the listview seems to take up more space than it needs to when only 1 item exists.
The image below shows this.
My XML code
<?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="match_parent" >
<ListView
android:id="#+id/lvRaffles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bCloseDialog"
android:layout_alignParentTop="true" >
</ListView>
<Button
android:id="#+id/bCloseDialog"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#drawable/alert_dialog_button_style"
android:text="Close" />
<TextView
android:id="#+id/tvRaffleEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="No Raffle Code Found With That Search"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone" />
</RelativeLayout>
I have tried the following.
List View height on wrap_content, 0dp does not help.
I know you fixed this with LinearLayout but to answer your question, the reason it appears as if your listview is taking up too much space is because you used layout_alignParentBottom="true" if you want the button to appear below the listview you would need to use layout_below="#id/lvRaffles" You also have two elements aligned to parentTop, which can cause the elements to overlap.
I am showing and hiding views on a particular actions using some animation, I used LayoutTransition and it seems to work fine except one thing, while the animation is going, the background of some TextViews changes and reset to the default white background!!
Following is the code I am using to apply animation after hide/show.
mRightTransitioner = new LayoutTransition();
mRightTransitioner.setDuration(1000);
vg_rightContainer.setLayoutTransition(mRightTransitioner);
Edited: even without animation i tried, same problem, the background resets once view's position changes
Edited: here how i declared the views in the XML
<LinearLayout
android:id="#+id/ll_req_reasonwrapper"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:baselineAligned="false"
android:gravity="right"
android:orientation="horizontal"
android:visibility="gone" >
<EditText
android:id="#+id/et_req_reasons"
android:layout_width="400dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:gravity="right|center_vertical"
android:inputType="text"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_preasons" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_req_timewrapper"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:baselineAligned="false"
android:gravity="right"
android:orientation="horizontal"
android:visibility="gone" >
<LinearLayout
android:id="#+id/ll_req_endtimewrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/ftv_req_endtime"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:freezesText="true"
android:gravity="right|center_vertical"
android:inputType="numberDecimal"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_endtime" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_req_starttimewrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="50dp" >
<TextView
android:id="#+id/ftv_req_starttime"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:freezesText="true"
android:gravity="right|center_vertical"
android:inputType="numberDecimal"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_starttime" />
</LinearLayout>
</LinearLayout>
for instance: if i hide 'll_req_reasonwrapper', then the LinearLayout 'll_req_timewrapper' moves up, then the backgrounds of the textViews like 'ftv_req_starttime' becomes white!!
Please help.
Edited Answer:
if your are using linear layout then if the child is not being displayed, it is not going to be laid out and not going to be the part of the measurements ( as it looks like ). I recommend to use the RelativeLayout.
You can try
android:background="#null"
for your buttons. if your are using some custom class for button then add
.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
for this specific views.
Need more code. The fact that disabling the LayoutTransition does not solve your issue, means that the animation is not the issue.
How do you reinstantiate child views or populate your view. Are you removing views with setVisibility(View.GONE) and then setting them back with setVisibility(View.VISIBLE)
OR
Are you removing childAt(i) and then dynamically adding custom views
I'm sorry I couldnt add a comment since I dont have the reputation for that but I think I am able to answer your question if you post more code.
Okay I have a full week of frustration behind me, it is the third time that the layout behaves random.
For example:
<TextView
android:id="#+id/tvGebiedHead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="16dp"
android:text="Geef gebied op"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/lvGebieden"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1" >
</ListView>
produces a result where the listview is displayed on top of the textview (making both items unreadable). The code down here produces the desired result, the listview is rendered under the textview. It is not the eclipse graphical editor, but it happens on a real phone as well. Note that the only difference between the two snippets is the id of the TextView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="16dp"
android:text="Geef gebied op"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/lvGebieden"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1" >
</ListView>
Earlier this week I had a fight with the layout of four togglebuttons which (all!!) changed size, text align on the button, and even location when I clicked one, while the on and off text was exactly the same. Please tell me I am doing something wrong and android is not broken.
In the example on the top, your ListView's layout_below still refers to #id/textView1 which doesn't exist. It does, however, in your example at the bottom, so the bottom is working. FYI, instead of #+id/textView1 you should use #id\textView1 in the layout_below.
This is my .xml file where I have used two scrollview,in Input Edittext and onether in output TextView. What is wrong here...It is not working in android device.
Another problem is that when I turn my device it only shows the input text area. The output text area goes down.I want to see the half screen of input and half screen of output area.
How to fix it??
Thanks
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="35dp"
android:orientation="horizontal" >
<Button
android:id="#+id/test"
android:layout_width="60dp"
android:layout_height="38dp"
android:text="#string/test" />
<Button
android:id="#+id/rdf"
android:layout_width="60dp"
android:layout_height="38dp"
android:text="#string/rdf" />
<Button
android:id="#+id/load"
android:layout_width="75dp"
android:layout_height="38dp"
android:text="#string/load" />
<Button
android:id="#+id/clear"
android:layout_width="60dp"
android:layout_height="38dp"
android:text="#string/clear" />
<Button
android:id="#+id/close"
android:layout_width="fill_parent"
android:layout_height="38dp"
android:text="#string/close" />
</LinearLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/input"
android:layout_width="0dp"
android:layout_height="175dp"
android:layout_weight="1"
android:background="#fff"
android:ems="10"
android:gravity="top|left"
android:textSize="14dp"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/run"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/run" />
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/output"
android:layout_width="match_parent"
android:layout_height="225dp"
android:background="#fff"
android:text="#string/output"
android:textColor="#1e90ff" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Try setting layout_weight=1 and layout_height=0dp for the two scroll views instead of their
contents.
What is wrong here...It is not working in android device.
That's pretty vague. What were your expectations? What isn't working? In other words, please be a little more specific.
However, based on the layout code given, here are some recommendations:
Avoid hardcoding the size of views. You cannot make assumptions about screen size with the large variety of screen sizes, densities and devices out there. Also, even if you're able to make the layout look nice in portrait mode, it'll probably be not even close to that in landscape.
If you're going to put just a single View in a ScrollView, there's no need to wrap it in a ViewGroup container; just set the View directly, without nesting it again and added an extra layer of complexity to the view hierarchy.
There's no need to wrap a TextView or EditText with a ScrollView, as both views are scrollable by itself.
Regarding your second question: you can prevent Android from extracting all UI components when there's little layout estate left with the keyboard popped up. You'll need to set the IME_FLAG_NO_EXTRACT_UI flag on the EditText, or in xml: android:imeOptions="flagNoExtractUi".
I do like to point out that there's a reason Android has this behaviour by default. In most cases it hardly makes sense to force a tiny part of the UI to be visible, even more as whatever is being typed by the user is probably what really matters.
I have 3 lists stacked on top of each other in a single Activity, and I would like to set empty views for each of them. I have tried setting the empty view to both dynamically created views and views defined in the layout, but either way all it seems to do when the list is empty is collapse it to 0 height, regardless of the fact that I have set the minimum height for each list to 60px.
This is the xml definition for the empty view
<TextView android:id="#+id/empty_view"
android:layout_width="fill_parent"
android:layout_height="60px"
android:text="#string/empty_text"
/>
mListView.setEmptyView(findViewById(R.id.empty_view));
and this is how I programmatically created the text view.
mEmptyView = new TextView(this);
mEmptyView.setLayoutParams(new ListView.LayoutParams(60, 60));
mEmptyView.setText(R.string.empty_text);
mListView.setEmptyView(mEmptyView);
My bad, this was pretty silly on my part :P I was under the rather ludicrous impression that if I put a TextView somewhere randomly in the xml layout and then set it as the empty view for a list, it would magically disappear and then reappear in the list when the list was empty. I guess I thought that the list would add it as a row in the list when it's adapter was empty. In the unlikely even that someone else decides to try something similarly ludicrous and wonders what's going on, I will post the full version of my original layout as well as the solution.
The code I used to set the empty view was the standard:
mListView.setEmptyView(findViewById(R.id.empty));
however my original layout looked like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="200sp"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/drafts"
/>
<ListView android:id="#+id/drafts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="60sp"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:text="#string/finished"
/>
<ListView android:id="#+id/finished"
android:layout_width="fill_parent"
android:layout_height="60px"
android:minHeight="60px"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/uploaded"
/>
<ListView android:id="#+id/uploaded"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="60sp"
/>
<TextView
android:id="#+id/empty"
android:text="DUDE WHERE'S MY CAR?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
While my listviews have a minHeight of 60sp, when the list was empty the height seemingly went to 0. As well, I noticed that the TextView was visible below all three of my lists so it did not seem to be "disappearing" and reappearing as a row in my list. How disappointing! In a gross oversight on my part, for testing I set the view as the emptyView for my second list only, ensured that my first and second lists were empty, and populated the third list. My first list had its min-height, my second shrank to 0, my third list was correctly populated, and my empty view appeared below it. I did not try populating the second list, and thus never realized that the empty view would subsequently disappear.
Finally after spending an hour more playing around with dynamically generated empty views and whatnot, I left it for a day and came back tonight and tested populating the second list. Voila, the empty view disappeared and the lightbulb went on. I quickly tested moving the empty view below the second list and above the third list, and of course, it worked perfectly!
Here is the new working layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="200sp"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/drafts"
/>
<ListView android:id="#+id/drafts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:text="#string/finished"
/>
<ListView android:id="#+id/finished"
android:layout_width="fill_parent"
android:layout_height="60px"
android:minHeight="60px"
/>
<TextView
android:id="#+id/empty"
android:text="DUDE WHERE'S MY CAR?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/uploaded"
/>
<ListView android:id="#+id/uploaded"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Long answer, probably just talking to myself, but whatever - asked the question, figured it out, and I never seem to be able to answer anybody else's questions on here so I might as well answer my own :)