ListView background image crop - android

I have my ListView with image background (it's actually a fragment) but still i have set the background with: getListView().setBackgroundResource(R.drawable.table); I noticed that every time this list shows up bacground image is allways like "fill_parent" also i noticed that listview is fill_parent too but i can fix that with getListView().setPadding() but i cant crop background image. Is there any other way to do it? I need background view with aditional buttons...

the Method setPadding is just affecting the content of the ListView, but not it's container e.g. the Background. So basically the padding produces unused space inside the ListView. If you want to restrict the ListView you can use the margin attribute:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Another and in my opinion better way to hava a ListView with a Button below is:
Use a LinearLayout with orientation vertical. Add the ListView and the Button to this layout.
The ListViews height is fill_parent and the Button's wrap_content. Now add to both the layout_weight with value 0 for the Button and 1 for the ListView. The result is that the Button is drawn at the bottom of your screen and the ListView just can expand to the rest of the screen (also its background).

Related

Set Custom layout inside ListView as non-scrollable in android

I have a ListView which contains a custom layout.
Each row of the list View look like this:
The row contains 2 LinearLayouts, one for Date and one for Progress details. The progress Details Layout consits of 2 TextViews(Heading and data below it) and 1 Button (View More).
When the user clicks 'View More' button the data below the heading expands to 10-12 lines.
My problem is that when the TextView expands, a scrollbar comes at the edge and the user has to scroll to read. The width of the row does not change i.e the row does not expand.
I want the row width to expand so that the user does not have to scroll to read the text.
I did read a lot and have already tried the following options but they did not work
1. android:scrollbar="none"
2. View.setOverScrollMode(View.OVER_SCROLL_NEVER);
3. View.setScrollContainer(false);
Please help me with this.
You have to use exapanding listview animation for that you can use this lib for that
https://github.com/nhaarman/ListViewAnimations
Use a expandable List View such that the normal view of your list will have the image that you provided and on clicking that view it will expand to reveal the details that you wanted to show by clicking the view more option(in your image)
Here is a tut link for more info.
Try to use LayoutParams and change the height programatically inside the listener for you Button.
LayoutParams should derive from the type of layout containing your LinearLayout. If for instance it is a RelativeLayout, it will look something like that:
int heightForRow = 100;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.height = heightForRow;
yourLinearLayout.setLayoutParams(params);
Please note that your LinearLayout must then be inside another layout (RelativeLayout in the example)

Android layout webview between another two views

What's the best approach to have a layout with the following:
Spinner (default height)
WebView (all the space between the two views (Spinner and Button))
Button (default height)
How is it possible to specify the height of the WebView to take all the space between the two (i.e. if they are resized later, the WebView will automatically be adjusted).
Thanks!
Use a RelativeLayout as the parent layout. Align the Spinner to the top of the parent. Align the Button to the bottom of the parent. For the WebView, set the height as match_parent and then use the android:layout_below and android:layout_above attributes to make sure that it always lies between the two views.
(I would've given the code but I don't want to. You might just copy paste with zero learning.)

Linearlayout programmatically - How to set up a divider?

I am creating TextViews in LinearLayout programmatically and I would like to separate them with a divider (just a simple line). I have googled endlessly, what I have found is that I can use .setDividerDrawable, but I don't want to use external images for this.
Any tips?
How to Add Divider to an Android Layout Programmatically
Create a View 1 or 2 pixels tall and width match_parent and set the background color to whatever color you want the divider to be.
Separate the divider from the items above and below with margin settings.
Example:
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
divider.setLayoutParams(lp);
divider.setBackgroundColor(Color.WHITE);
You could use a simple drawable in xml for the divider (example here), or use a 9-patch image which barely takes anything.
Then, use the LinearLayoutICS in order to show the divider on most of the devices. you can check out this post i've made about it.
For linear layout you can use this attribute to set divider android:divider="some color"
android:showDividers="middle"

how to make the change the background when ListView is Scroll up

i am trying to implement ListView. Problem is that when i am showing items of list less that the scree size the remaining is showing black. I want to set any image so that when items are less than screen size then background should be a picture
put your <ListView /> inside <LinearLayout/> ,
add background to Linear layout, with LL Height as match_parent and hieght of Listview as wrap_content
I have not done this myself, but this is what I think should work.
Place the image in /res/drawable
Then add these lines to activity in manifest or to parent layout of ListView
android:scaleType="fitCenter"
android:background="#drawable/image_name"
Let me know how that goes.

Force GridView to only a single row?

Is there a way to force GridView to only be a single row? Right now by default it will extend its height to accommodate all views supplied by its adapter
You Should combine a horizontal SCROLLVIEW and a LINEARLAYOUT and a GRIDVIEW to achieve what you want!
put grid view in linearlayout and put the linear layout in the horizontal scroll view;
then when setting adapter! count the number of data!
after that you should calculate the desired width to show all your items!
for example you want to show 8 item! each width is 100dp . so the desired width would be 800dp!
then you should add these lines of code
yourGridView.setNumColumns(8);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(800, ViewGroup.LayoutParams.MATCH_PARENT);
yourGridView.setLayoutParams(lp);
dnt forget to set the width of the linear layout as WRAP_CONTENT in the xml!
*** IMPORTANT NOTE
as i know, by doing this, gridview can't garbage collection because Scroll View doesn't support such a thing and your grid view nested in it!
so dnt use this method for lots of images or you will get HEAP SIZE error!

Categories

Resources