In my project I used setcontentview(R.layout.first).here. In a gallery I am displaying second.xml. In second.xml I have one listview.when. I am trying to scroll these listviewitems total screen scrolling, but it is not scrolling properly. Because outside we used scrollview. When I delete outside scrollview we cannot see the bottom of listview. How to resolve this issue?
Use ListView's headers and footers if you want to put some items only at the end/beginning of the ListView.
Watch this video beginning from 42:40 it's really useful.
A list view will scroll automatically once it goes past its parents height or the ListViews maximum height
If you put your ListView/any scrollable View inside the scrollView it will not work properly because when you touch the screen ,main focus of your touch is on parent view(scrollView ) not the child View (ListView).
<?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"
android:background="#ffffff"
>
<ListView android:layout_height="match_parent" android:id="#+id/all_workouts_list"
android:cacheColorHint="#ffffffff"
android:layout_width="match_parent"
android:layout_above="#+id/add_workout_all_workout_list"></ListView>
<Button android:layout_width="wrap_content"
android:id="#+id/add_workout_all_workout_list" android:layout_height="wrap_content"
android:text="Add Workout" android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"></Button>
</RelativeLayout>
ListView inside ScrollView is not possible.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- here the picture info -->
</LinearLayout>
</LinearLayout>
I have no any ready example with me,
So written a raw code like this as you asked in comment,
ListView lv = (ListView) findViewById(R.id.listView);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)));
Button btn = new Button(this);
ll.addView(btn);
lv.addFooterView(ll, null, true);
Related
I would like to make a listview with dynamic size. When I press the button, it will add a row to the listview. And I want the button is always aligned to bottom of the list view.
The problem is listview will push the button out of screen when it have enough rows. So in this case, how to make the button align to bottom of the screen or set the max height for the listview? But when the listview just have 1 or 2 rows, I want the button is right below the listview.
If you want the button to be displayed even though the user is at the top of the list view see other answers, but if you prefer to display the button when the user scrolls to the bottom of the listView use this approach with a footer.
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addFooterView(footer);
The footer layout contains the button.
You can have into Linear Layout a ListView and a button with different weight. Something like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reportCommentsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/reportCommentsListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:paddingTop="5dp" >
</ListView>
<Button
android:id="#+id/reportCommnets_Addbutton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="Button" />
</LinearLayout>
According to your question, you have to dynamically change the position of your button. From my comment, this is the answer.
Attach listener to your ListView : see https://stackoverflow.com/a/4433294/1377145
In the listener callback, check the visibility of your button :
Rect videoBounds = new Rect();
yourButton.getGlobalVisibleRect(videoBounds);
getGlobalVisibleRect will return true if the button is visible > 0%, false either.
To fix button on the bottom : change the LayoutParams of your button and set alignParentButtom to true. Else set alignParentButtomto false.
bis : depending on your layout, you will have to "move" your button in the view tree with removeView/addView. Don't forget to removeview so the view will not have a parent naymore before adding it to another view.
Additional note : to have a % visibility on your button, you can use the videoBounds.height() and a rule of three :
100 * videoBounds.height() / yourButton.getHeight
EDIT : change answer to match accepted one.
You can make a RelativeLayout with the Button using the rule alignParentBottom=true and the ListView using the rule above. Something like this:
<?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="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/button" >
</ListView>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"/>
Something like this should help:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/add"
/>
</RelativeLayout>
Sorry, to inject my own personal opinions, but I think a "add more" or :load more button" always be the last view within a ListView.
To answer your question, to achieve what you need, the Listview and the "add row" button should be siblings within a ViewGroup.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="48dp"
android:minHeight="48dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
The key point here is that the Button has a fixed height and the ListView takes the rest of the space. The button also aligns to the bottom of the parent view.
Use Relative Layout.
Put the button at the bottom of screen, and set the listView position above him:
<?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:layout_width="match_parent"
android:id="#+id/list"
android:layout_above="#+id/button1"
android:layout_height="wrap_content">
</ListView>
<Button
android:layout_width="match_parent"
android:id="#+id/button1"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" />
</RelativeLayout>
I have a data entry type activity and im using a linear layout to space out evenly the sets of textviews and edittexts. Then I have a scroll view that is supposed to make it so the user can scroll while the softkeyboard is up.
If I use android:fillViewport the linearlayout works properly and fills the screen and spreads each item out evenly but then when the keyboard comes up and stops each item being spread out evenly. If i use android:windowSoftInputMode="stateVisible|adjustPan" then the linearlayout remains spread out but the scroll view doesn't work anymore (from all the unsolved posts on this i don't think you can have a working scroll view with adjustPan)
is there any way to have a linearlayout inside a scrollview, with items spread out evenly and still work while the softkeyboard is up with out changing the linearlayout?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport
>
<LinearLayout
android:gravity="left"
android:orientation="vertical"
android:id="#id/tab2"
android:paddingLeft="40.0dip"
android:paddingTop="0.0dip"
android:paddingRight="40.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<TextView
android:id="#id/textViewBrand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/brand" />
<AutoCompleteTextView
android:id="#id/editTextBrand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</AutoCompleteTextView>
</LinearLayout>
...more linearlayouts with textview and edittext to be spaced out evenly
set the property android:fillViewport="true" in your scrollview it will work
Maybe the ScrollView isn't working because your
android:layout_height attribute
is defined to match_parent. You have to put
wrap_content
Follow This Code, I hope it resolves your Problem
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
//Your Main Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100">
// First Sub Layout Under Main Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="10"
android:weightSum="100" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TextView"
android:layout_weight="70" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="30" />
</LinearLayout>// Finishing First Sub layout
// Second Sub Layout Under Main Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="10"
android:weightSum="100" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TextView"
android:layout_weight="70" />
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="30" />
</LinearLayout>// Finishing Second Sub layout
similarly for 3rd,4rth,5th sub layouts and so on........
</LinearLayout> // Finishing Main Layout
</ScrollView> // Finishing ScrollView
you can go over the child views of the LinearLayout and set their size to a fixed size yourself in code using a ViewTreeObserver.OnGlobalLayoutListener.
the following code sets the sizes to not change, so you can use the android:fillViewport option with weights, and then essentially remove the weights, but keep the calculated sizes.
I used this code with some modifications to also make sure child views are distributed evenly in a LinearLayout that is inside a ScrollView, where the LinearLayout is too big for the android:fillViewport option to get the weights to actually work. this was done by doing 2 iterations over the children. first getting the max view's height, and then setting all child views to that max height.
private void distributeViewsEvenlyInLinearLayout() {
ViewTreeObserver observer = linearParentView.getViewTreeObserver();
final ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressWarnings("deprecation")
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
linearParentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
linearParentView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
for (int i = 0; i < linearParentView.getChildCount(); i++) {
View child = linearParentView.getChildAt(i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, child.getHeight());
child.setLayoutParams(layoutParams);
}
}
};
observer.addOnGlobalLayoutListener(globalLayoutListener);
}
Basically here is a mspaint demonstraiton of what I want to accomplish:
I do not want the outline to be visible to the user, it was just to give you an idea of what I want to do. I want new textviews to take position under each other whenever a new goal is added from the edittext-button combo on the bottom part of the GUI.
I know how to save the data - SQLite. But I have no idea how to make new textviews be on the bottom of the last one - and if there is no more room on the screen I want that textview part to be scrollable.
I don't know what I need to use to accomplish this.
Thanks in advance!
create two xml files.
One consisting of list view
<?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"
android:background="#drawable/bkgrnd_320x285">
<ListView
android:id="#+id/List1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="1dp"
android:fastScrollEnabled="true"/>
</LinearLayout>
and another xml for just the textView eg. row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Name"
android:textSize="20dp"
android:layout_marginTop="20dp" />
</LinearLayout>
Then in the main activity u need to refer to both the xml files as follows
ListView ll = (ListView) findViewById(R.id.List1);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.row,R.id.textView2, arraylist_you_want_to_display);
ll.setAdapter(arrayAdapter);
where R.layout.row is the row.xml consisting of textView
And R.id.textView2 is the id of the textView present in the row.xml
You may create a LinearLayout, setting the orientation to vertical.
Adding a ScrollView or ListView to show the upper part, with android:weight=1
Add the EditText to the LinearLayout, and this is done.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/List1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weight="1"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
I am creating some TextView, ImageView, HtmlView dynamically and adding it in following layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ad_space"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#drawable/navigation_bar"
android:visibility="gone" >
</LinearLayout>
<ViewFlipper
android:id="#+id/viewflipper"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ScrollView
android:id="#+id/quiz_scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/quizView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/answer_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
</ScrollView>
<ScrollView
android:id="#+id/quiz_scroll_viewTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/quizViewTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/answer_layoutTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
</ScrollView>
</ViewFlipper>
</LinearLayout>
In QuizView I am adding some question related views at runtime, and for answers I am creating one list view runtime and adding to answer_layout. I am using BaseAdapter to create ListView runtime based on situation.
But my ListView is not showing all content, its showing only first cell.
I have adding ListView like this.
ListView ansList = new ListView(Test.this);
ansList.setAdapter(adapter);
ansList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ansCellLayout.addView(ansList);
it display only one list cell.
if I set some int value for height then its show all list contains.
ansList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 600));
But I cant Hardcode list view height as my contant is dynamic.
How to make list view as a WRAP_CONTENT in this scenario ?
According to the Android developers as seen in this Google I/O 2010 video , the ListView's height cannot be set to wrap_content. And when it is , the first three child elements are considered for the height. The others are simply not included.
Try setting the ListView's height at runtime. Or even better set it to FILL_PARENT.
From the documentation a ListView is
A view that shows items in a vertically scrolling list
So it's not supposed to be used with WRAP_CONTENT but rather to have a fixed height and scroll content inside it.
You'd better use a vertical LinearLayout for that purpose.
I have a scrollview with a linear layout in it. Within that linear layout there is a list view. The problem I am having is that my list view only has a certain amount of space and then becomes scrollable. I don't want this to happen. I want the entire list to be shown. Does anyone know how I might achieve this? I have tried using this link however my layout does not appear when I add the footer. ListView in ScrollView potential workaround This is my code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="vertical"
android:background="#drawable/darkimg"
android:weightSum="1">
<LinearLayout
android:id="#+id/numbersLL"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/round_rectangle"
android:layout_margin="4px"
android:paddingLeft="6px"
android:paddingTop="3px">
<TextView
android:id="#+id/numberPrint"
android:layout_height="wrap_content"
android:textSize="14px"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_width="fill_parent"
android:text="#string/numberPrint">
</TextView>
<ListView
android:id="#+id/numberListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
This is my footer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/formLL">
<TextView
android:id="#+id/numberFormTextViewt"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20px"
android:text="Add a number:"
android:textColor="#color/white" />
<Button
android:id="#+id/numberFormButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_add"></Button>
</LinearLayout>
And this is the Java code to add the footer to the list:
LayoutInflater factory = getLayoutInflater();
LinearLayout numberFooter = (LinearLayout) factory.inflate(R.layout.number_form, null);
// List of numbers
numberListView = (ListView) this.findViewById(R.id.numberListView);
numberListView.addFooterView(numberFooter);
What is going wrong here? The footer does not appear when the activity is displayed. One thing I should mention is the footer is used to add items to the list above it. I'm not sure if that makes a difference but when I am testing this the list adapter is empty. Even still should the footer show up anyway? Any help would be appreciated thanks.
List view under scroll view is not working(means List view is not scrollable).
So u can put empty Linear Layout instead of List view in ur xml file. In ur code, first u can get the list view size, based on this size value, for loop can rotated. In that for loop u can call the get view method of the adapter class(that class is not extending any class.) this will solve the list view problem.
try this in your listview:
android:layout_height="fill_parent"