I am creating a activity for mix text and image input.
Here is the xml for container.
<ScrollView
android:id="#+id/new_question_body_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/new_question_tag_container"
android:layout_above="#+id/oler_keyboard_accessory"
android:layout_alignLeft="#+id/new_question_tag_container"
android:layout_alignRight="#+id/new_question_tag_container"
android:scrollbars="none">
<LinearLayout
android:id="#+id/new_question_body_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/new_question_body_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:hint="Please edit your body here."
android:layout_marginBottom="5dp"/>
</LinearLayout>
</ScrollView>
After dynamically add three (not lag for two) imageViews, it become pretty lag when scrolling.
Even if I make maxHeight and Width for each individual 50dp, the lag still exist. Am I adding imageView in the wrong way or this is the memory limit?
Wouldn't be better to use a ListView with custom layout (for each row) instead of a mix of ScrollView and LinearLayout?
If you want to add tons of images (just like a gallery), you may think about using LRUCache or another algorithm (i.e. lazy loading).
Also, consider the image size you're tring to load into memory. Maybe a smaller size and 'click to zoom' of each item should be a better aproach/UX
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?
I am working on an android project. My first screen consists of one imageview and three textviews everything is clearly visible when the mobile positioned vertically if the mobile positioned horizontally then some text views are not visible how to make my app which fits to screen and supports all positions(vertical/horizontal).
Thank you.
You need to add a ScrollView to your layout. As in landscape the screen height is decreased, so lesser area is left to show elements.
ScrollView will manage all the things for you. If space available is less than required then Scroll bar will be shown otherwise not.
If you are sure that it will fit on any screen size, consider using RelativeLayout, with this you can say where it needs to align on the screen at all times. Of course if it can overflow than using a Scrollview is a much safer option.
You can use a scrollview to fit the screen in length, or you can use PercentRelativeLayout.
How to use a scrollview:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<--yourcodehere-->
</TableLayout>
</ScrollView>
Add this to your gradle to use PercentRelativeLayout:
compile 'com.android.support:percent:23.2.0'
Then you can use it like this:
<Button
android:text="#string/Tesksten"
android:id="#+id/btContent"
android:layout_below="#+id/searchView"
android:drawableTop="#drawable/ic_menu"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_heightPercent="16%"
app:layout_widthPercent="100%"
android:padding="10dp"
android:textColor="#000000"
android:background="#f0f0f0"
android:textStyle="bold"
android:alpha="0.65"
android:layout_marginBottom="10dp"/>
This is how this button looks now, note that there are more lines to get MY specific layout, remove if needed. Make sure you put Width And height to 0 and then use Percent, i had some problems figuring it out at first to. Goodluck.:
Or if you want to fully turn off screen rotation, add this to your manifest activity, that you dont want to rotate:
android:screenOrientation="portrait"
As we all know, you can customize the font size of your android phone. When a user has the font in large or huge (the common one to use is "normal") and they reach an specific fragment (that has a button), the whole content of the fragment is not shown in screen, the font being so big, moves the text from 2 rows to 4, making it to not fit on screen. I added a scrollview to the fragment to be able to show all the content when the users have this font size selected.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayoutHeader1"
android:layout_centerHorizontal="true" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
---CONTENT----
</LinearLayout>
</ScrollView>
However this isn't showing the whole content (as it is supposed to). The scroll appears and allows you to read whole text,but the button which is under the text is not shown.
try to set
for ScrollView
android:fillViewport="true"
for LinearLayout/your container
android:layout_height="wrap_content"
this is proper way to fill ScrollView without cutting top/bottom edge, maybe this is your problem (description isn't clear at all...)
I have a layout as shown below. It is inflated by code and added to a HorizontalScrollView, sometimes a few hundred times, and causing getting memory issues.
I'm wondering if there's anything that can be done to make it more efficient? Originally I used LinearLayouts, and replacing that with RelativeLayout made a huge difference to the scrolling. Now I'm wondering if it can be further improved?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="156dp"
android:layout_height="254dp"
android:paddingLeft="7dip"
android:paddingRight="7dip">
<FrameLayout android:id="#+id/button_frame"
android:layout_width="156dp"
android:layout_height="218dp">
<ImageView android:id="#+id/button_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/image_bg"
/>
<ImageView android:id="#+id/button_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|right"
/>
<TextView android:id="#+id/button_select"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="#drawable/btn_selector_bg_selected"
/>
<TextView android:id="#+id/button_selected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="#drawable/title_bg_selected"/>
</FrameLayout>
<TextView
android:id="#+id/button_title"
android:layout_width="156dp"
android:layout_height="36dp"
android:textColor="#ffffff"
android:singleLine="true"
android:textSize="13sp"
android:textStyle="bold"
android:gravity="center_vertical|left"
android:ellipsize="end"
android:paddingLeft="30dip"
android:paddingRight="5dip"
android:layout_below="#id/button_frame"
android:background="#drawable/title_bg"/>
</RelativeLayout>
The ImageView button_image is populated using AQuery image caching, so I'm not sure if there's much more I can do to improve the way the image is handled. But any tips on improvements greatly appreciated.
In general, a good way to optimize layouts in Android is to minimize the amount of nesting of containers within each other. The deeper you nest layout containers, the more work the framework must do to measure, layout, and process events for the view hierarchy.
But I think you may be asking the wrong question.
The HorizontalScrollView and VerticalScollView are not intended for the use you're putting them to. They're meant to hold a mostly static layout, and allow it to scroll if necessary depending upon the size of the screen it happens to be running on.
You want a repetitive list of mostly identical Views, that the user can scroll. The correct Android view container to use is ListView, or one of the other descendants of AdapterView.
ListView is careful only to create/inflate the necessary child views to fill the space on screen, and reuse them as the user scrolls. This solves the memory problems you're experiencing. It does that by requiring you to pair it up with an Adapter - an object that wraps the actual data being displayed, and creates on-demand the correct view for a given data item.
Since you're trying to do horizontal scrolling, you might also look at Gallery (now deprecated in Android) or the newer ViewPager class, both of which support horizontal movement through a large list of data.
If you can use a ListView instead of a HorizontalScrollView, you could create an Array Adapter that uses the viewHolder pattern which essentially allows you to re-use views per item in your list view. Take a look at this for more details: http://www.jmanzano.es/blog/?p=166
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).