Resize Height Listview when Move button under ListView - android

In my app I have a listview and a button under Listview(button in footer listview or set under list in relative layout)
Now i want when i move button (up or down) height of listview will change.
How i do?
Thank for any ideal or solution!

I will assume that you are referring to a resize when you say move. For this, it is better to use a LinearLayout instead of a RelativeLayout.
Just set the android:layout_weight property of the ListView to 1.
Example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
When you do this, the Button will occupy the amount of space it requires and the ListView will take the remaining space and also scale accordingly.

Related

HorizontalScrollView's child not matching it's parent view's width even on setting android:layout_width="match_parent"

I want to display a horizontal list of scrollable buttons at top of the phone screen containing 30 items, for this purpose I am using a HorizontalScrollView with a LinearLayout with "horizontal" orientation as it's child but the linear layout is not taking up the entire phone width even on setting its width as "match-parent". Here's the code :
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center">
<GridView
android:id="#+id/gridView_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchMode="columnWidth" />
</LinearLayout>
</HorizontalScrollView>
Here is shown in the image that linear layout is not taking up the entire space and LinearLayout is only covering some of the space. Also on changing the size to a fixed size, I noticed that the HorizontalScrollView was actually behaving like a vertical Scroll View only.
NOTE: Also if there is an alternative way to display a horizontal list of buttons with 30 items with numbers from 1 to 30 on it, please suggest it.
To achive this easily you may use Recyclerview with Horizotanl Layout Manager.For Example
recycler_view.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
I don't think you need a HorizontalScrollView with a nested LinearLayout for what you are trying to achieve. It would be a better idea to simply use a horizontal RecyclerView if your button layouts are similar and the functionality of the buttons is similar too. Refer to this answer for help https://stackoverflow.com/a/40584425/9119277

Position textview below list & reposition it as size of list increases

In my layout, I have a textview which I need to be fixed at the top of the screen, a button to be fixed at the bottom of the screen.
Between these two, I have a list with a textview(with a drawable) below it. (please see image). I want the textview positioned below the list;i.e.; as list items grow, the textview should reposition itself. As I click on the textview, I add items to the list. I am able to achieve this, but as my list items increase, the textview goes below the list & is not visible. So what I see on screen is the top textview with the list below it & the bottom button. The textview to add items in list view is not visible, so I cannot add items to my list. I need that even if the list items increase, I want the list to be visible along with the textview.i.e.; as list items grow, the textview should reposition itself. I tried various approaches suggested with several combinations of linear & relative layouts & positioning techniques, but I am not able to achieve this.
Try 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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc xyz"
android:gravity="center"
android:layout_alignParentTop="true" />
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button"></ListView>
<TextView
android:layout_below="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Save"
android:id="#+id/button" />
</RelativeLayout>
You have to nest the layouts here. You could for example use a LinearLayout with vertical orientation as parent and place your textview and listview (or whatver you are using for your item list) inside the LinearLayout. Here's the idea:
-TopView: set to wrap content
-ListView: set to fill parent
-TextView: Set to wrap content
If I'm not mistaken this should set the top and bottom view to fixed sizes and make the ListView fill the remaining space.
You can try adding that button with image as footer to the listview it will work with less noumber of items when it exceed the screen hight remove footer and make the bottom layout visible(Have to add the same footer item in bottom layout also).

Fill Parent on Horizontal List Row with Margin

Okay so I have a horizontal Relative Layout on a ListView and I want a margin between the different Rows on the ListView but I have one problem. I have a button that is being pushed to the far right and I want the click zone to be as big as possible. If there was no margin on the root element of relative layout I would be fine and my button would take up the entire height of the row. But the problem is because there is margin there is an opportunity for the user to click above and below the button where the margin is to trigger the OnItemClick event which isn't the desired functionality of the button which could get frustrating at times.
Example:
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp">
<ImageButton
android:id="#+id/list_button"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/btn"
android:background="#android:drawable/list_selector_background" />
</RelativeLayout>
Any help would be greatly appreciated obviously its possible I just can't see it sadly.
One way around this is to wrap the ImageButton in a transparent layoutview (relative, linear, frame - depending on your needs) that does take up the whole space (i.e. android:layout_width and android:layout_height are fill_parent). Then add the same click handler to the outer layout as you do for the button.

Which of layout should i use?

I'm confused. I want to show the map and on below of map show 5 buttons. I use RelativeLayout, but the program just show Product button. Why? I'm confused which layout i use (Linear,Relative,Frame or absolute )!! Please help me. and How can i correct this code?
location.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:apiKey="0cPRv243zM1_S3ydsNg8MJP9_6BfCp642jOhPvQ"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:layout_gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/button_home"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/home_icon"
android:text="#string/button_home"
android:textColor="#color/text_home" />
<Button
android:id="#+id/button_product"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/product_icon"
android:onClick="Product"
android:text="#string/button_product"
android:textColor="#color/text_product" />
</LinearLayout>
</LinearLayout>
To answer your specific problem: Instead of saying that the home button is to the left of the product button, you should say that the product button is to the right of the home button. When a RelativeLayout is inflated, the layout is parsed in a linear way so if view A it positioned relative to view B, view B must come first.
<Button
android:id="#+id/button_home"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/home_icon"
android:text="#string/button_home"
android:textColor="#color/text_home"/>
<Button
android:id="#+id/button_product"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/button_home"
android:drawableTop="#drawable/product_icon"
android:onClick="Product"
android:text="#string/button_product"
android:textColor="#color/text_product" />
Add this to the product button and delete the layout_toLeftOf from the home button.
android:layout_toRightOf="#id/button_home"
You can use gravity and alignment to position the home button and then have the other four buttons following it, each one positioned to the right of the one before it.
Good luck
RelativeLayout by default places these 2 buttons together, so you can just see the latter.
And the line
android:layout_toLeftOf="#+id/button_product"
is wrong. #+id creates an id, use #id in such cases.
I would recommend LinearLayout for the situation. Place these buttons in it, and adjust them with some margin.
LinearLayout : LinearLayout is used when we need to arrange the
widgets/views in a horizontal or vertical manner.
The direction of arrangement can be set to horizontal or vertical,
by default it is being horizontal.
TableLayout : If the Layout's widgets/views need to be arranged
in the form of rows and columns, we use this layout object.
This is similar to html tables. The cells can span columns.
The TableLayout do not display its border. We can be made to
shrink and stretch by setting the respective properties of the columns,
"TableRow" is another helper widget which should be used in conjunction
with the TableLayout.
RelativeLayout : Here the position of each of the widgets/view is
in relative/dependent to each other. For example, when a layout is needed
such that it has a text view just to the left of an Edit Textbox, and a button
just below the EditText. The relation between the views are taken care in
one iteration, hence if view B’s position is dependent on view A’s position,
view A must come first in the layout.
FrameLayout : This is a very simply layout which is used to hold a section
of the screen blank, for displaying an item or group of items at run time. All the
elements added in the framelayout will be added to the top left of the screen.
AbsoluteLayout : When there is a need is to specify exact x and y co-ordinate
position of the view, then AbsoluteLayout need to be used. This layout is
difficult to maintain.

How to place fixed button below listview?

Is there any way to have a button directly below a listview, so that as the listview grows, the button moves down BUT the button is never pushed off screen. IE, once the listview has outgrown the screen, the button is still always visible, and the listview is scrollable.
I have managed to make the button ALWAYS at the bottom of the screen, but i want it to sit up directly below the listview while the listview is smaller than the screen.
I have tried using various arrangements of relative and linear layouts and using the weight property, and things that seem like they should work simply don't, so it might be worth checking any answers before posting.
CLARIFICATION:
To phrase it in a different way: I want a button to sit below a listview, moving down as it grows, but i dont want the button to be pushed offscreen
This previous post does exactly what you want to do. What it does basically is that it keeps the button at the bottom of the list at all times. But when the list grows out of the screen area, its height gets limited by the weight parameter.
This way, the list's bottom edge is just above the button's LinearLayout and you get the same behavior that you were looking for.
If You Want to show this button in the end of list item. Then use this code
final Button btnAddMore = new Button(this);
btnAddMore.setText(R.string.art_btn_moreIssues);
exArticlesList = (ExpandableListView) this.findViewById(R.id.art_list_exlist);
exArticlesList.addFooterView(btnAddMore);
OR If you show button in your layout end then use this code.
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/btn_New" >
</ListView>
<Button
android:id="#+id/btn_New"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:text="#string/New"
android:width="170dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>

Categories

Resources