Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the layout margin for an Android ListView Programmatically

I have defined a List View in xml as below

     <ListView android:id="@+id/mylist" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"
           android:layout_weight="1"
           android:layout_marginTop="95dip"/>

And i need to re-define the layout margin upon some result in my programe ,how i can achieve this

like image 947
ganesh Avatar asked Jun 17 '10 12:06

ganesh


Video Answer


2 Answers

More simply, you may be able to modify the existing MarginLayoutParams instance to modify the margin:

ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) mListView
        .getLayoutParams();

mlp.setMargins(adjustmentPxs, 0, 0, 0);

break;
like image 64
Danny Armstrong Avatar answered Sep 30 '22 17:09

Danny Armstrong


If you get hold of the ListView object in your Activity you can use the method setLayoutParams(), passing in an instance of android.view.ViewGroup.MarginLayoutParams.

like image 28
James Avatar answered Sep 30 '22 17:09

James