I have declared a spinner in my layout file. I have set a custom ArrayAdapter for this. My problem is the spinner height gets unexpectedly larger than defined when the view is rendered.
For example if I declare the spinner like this:
<Spinner
android:id="#+id/categorySpinner"
android:layout_width="match_parent"
android:layout_height="45dip"
android:layout_above="#+id/rewardListView"
android:layout_below="#+id/customerRewardPointsTextView"
android:background="#drawable/btn_dropdown"
android:spinnerMode="dropdown" />
Then its rendered like this:
On the other hand if I declare the spinner like this:
<Spinner
android:id="#+id/categorySpinner"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_above="#+id/rewardListView"
android:layout_below="#+id/customerRewardPointsTextView"
android:background="#drawable/btn_dropdown"
android:spinnerMode="dropdown" />
Then its rendered like this:
So the problem is whether I set the android:layout_height="45dip" or android:layout_height="0dip" the height of the spinner remains the same. How to resolve this issue ?
Although you are defining android:layout_height but it's not actually working because of the restriction you have in your view :
android:layout_above="#+id/rewardListView"
android:layout_below="#+id/customerRewardPointsTextView"
As you can see you placed the view between your two view "rewardListView" and "customerRewardPointsTextView". And those view actually adjusting the height of your "categorySpinner" view, so it doesn't matter whether you give 45dip or 0dip.
Try placing "categorySpinner" in your relatvielayout in a way so other view does not mess up with categorySpinner's height.
Related
Firstly, I am new to android so if I have missed something basic I apologise.
I have a page which has two ListViews side by side, both with varying amounts of content. I also have a TextView above the ListViews and another TextView below the listviews. These text view boxes change based on items selected in either of the two ListViews.
These two ListViews sit side by side, taking up half of the screen each, while a Textview sits directly above and directly below, both centred to the page. An image is shown below.
This is the page looking normal on load.
The problem is when I select an item from either list. I have a feeling I am missing some XML properties, but I am not sure which properties or if this is even the case. When an item is selected, let's say from the ListView on the right, the TextView at the bottom is updated with text taken from an array. The ListView also decides to change the width and I am not sure why this is.... I don't want the ListView to change width. I want it to remain taking up half of the page and half of the page only.
This is the page after an item from the right ListView has been selected.
I would also like to keep things in RelativeLayout. I also believe it is only an XML issue and not to do with the adapter or any other code so I will not include that for now. I can include it if required.
Any help would be great. Thanks in advance.
content_titles.xml my activity xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TitlesActivity">
<ListView
android:id="#+id/unlocked_titles_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:longClickable="true"
android:layout_below="#+id/current_title"
android:layout_alignEnd="#+id/requirements"
android:layout_marginEnd="26dp"
android:layout_above="#+id/requirements">
</ListView>
<TextView
android:id="#+id/current_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:text="Current Title: Novice"
android:textSize="24sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<ListView
android:id="#+id/locked_titles_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignStart="#+id/requirements"
android:layout_marginStart="28dp"
android:layout_above="#+id/requirements"
android:layout_below="#+id/current_title"/>
<TextView
android:id="#+id/requirements"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="34dp"
android:text="temp"
android:textAlignment="center"
android:textSize="24sp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
activity_listview.xml used as the individual rows of the ListViews
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
The problems with layout could be caused by ScrollView to be the wrapper
I stumbled upon some note in http://developer.android.com/reference/android/widget/ExpandableListView.html
"...Note: You cannot use the value wrap_content for the android:layout_height attribute of a ExpandableListView in XML if the parent's size is also not strictly specified (for example, if the parent were ScrollView you could not specify wrap_content since it also can be any length. However, you can use wrap_content if the ExpandableListView parent has a specific size, such as 100 pixels."
I removed wrapping ScrollView and linear layout started working properly. Now its only to understand how to wrap the stuff to ScrollView. God help me
But anyway this is really weird behavior. I think that fill_parent is not really correct wording. When using heirarchyviewer tool I always see WRAP_CONTENT and MATCH_PARENT values for layout_width and leayout_height. So probably fill_parent is actually means match_parent which puts me in cognitive dissonance.
You have your layout_width properties set to wrap_content. This means that they could change as the data changes. I would recommend putting your ListViews in a LinearLayout with orientation:horizontal and set the amount of space that each element takes up with layout_weight. Here is a relevant SO question What does android:layout_weight mean?
This may seem like a duplicate but I can't find an answer anywhere.
It seems everyone has "TextView" available to edit in their activity xml file, whereas I only have the following:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/liv1"
android:layout_below="#+id/lt1"
android:layout_marginTop="29dp"
android:layout_alignRight="#+id/lt1"
android:layout_alignEnd="#+id/lt1"
android:layout_alignLeft="#+id/lt1"
android:layout_alignStart="#+id/lt1"
/>
Using android:gravity = centerdoes nothing when I put it in between the "ListView" brackets.
So what is it I should do?
You should create a custom ListView adapter that fits your needs, and also create an inflater's xml layout and align the text there whatever you want.
More info here:
Custom Adapter for List View
http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/
You should set gravity in your item, that you using in your adapter. And there modify your textView like this way:
<TextView
android:id="#+id/textView"
android:layout_width="match_parent" <= set this
android:layout_height="wrap_content"
android:text="#string/invalid"
android:gravity="center" <=this your gravity
/>
I have a spinner and set the spinnerMode to dialog on it so that text will wrap onto a new line on Android 4.2+, but in doing so it causes the information on the spinner not to fill the width. i.e http://i.imgur.com/TDMXNnZ.png. How can I get it to fill the width but also have it wrapping onto a new line on 4.2+?
<Spinner
android:id="#+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
Make sure that the layout in which the spinner is wrapped in is set to:
android:layout_width="fill_parent"
In essence this effects the properties of all widgets in that layout.
Hope this helps :)
I'm using NumberPicker with String values displayed. Here is the example:
odaberiRazinu.setVisibility(View.VISIBLE);
odaberiRazinu.setMinValue(0);
odaberiRazinu.setMaxValue(3);
odaberiRazinu.setDisplayedValues( new String[] { "FirstAnswer", "SecondAnswer", "ThirdAnswer", "FourthAnswer"} );
I have problems, because from FirstAnswer, only it's only visible "stAnsw" (something like that). When I move it, then it gets wider sometimes, but it's not working well.
Is there a way to make a display area wider or is there a better way to make NumberPicker-like mechanism for Strings?
EDIT (this is XML layout):
<NumberPicker
android:id="#+id/section_numberPicker"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
This code works for me. The NumberPicker is as wide as the longest item text. Maybe you have to choose a different size for the parent layout.
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
android:id="#+id/section_numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
Also you can set a static widht android:layout_width="300dp" but then it will truncate the text ifi it is longer.
Im trying to have a searchbox on the top of my list view. But I want this searchbox to disapear sometimes and the listview to resize to regain space. Is there a way I can do that without reloading another and different layout ???
is there a way to add and remove a component from the current view ?I have been playing with setvisibility but it doesnt resize anything.
Please, if you know, give code example ! :)
I did this with a layout like this
<RelativeLayout android:id="#+id/editFrame"
android:layout_width="wrap_content"
android:layout_below="#id/imageAttachments"
android:layout_height="wrap_content"
>
<EditText android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></EditText>
</RelativeLayout>
<ListView android:id="#+id/ListView01"
android:layout_below="#id/editFrame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></ListView>
Then, in the code, do the following:
findViewById(R.id.editText).setVisibility(View.GONE);
to free up the space, or
findViewById(R.id.editText).setVisibility(View.VISIBLE);
to show the search box.
Instead of the EditText, one can as well use any other single control or a layout for a combination of controls.
Setting its visibility to GONE will make the surrounding editFrame layout (can as well be a FrameLayout) shrink to zero size and reclaim the space for the ListView (which is set to be layout directly below the editFrame layout).