android - ListView layout weight -


i have 2 linearlayouts in main activity. 1 of them used hold buttons , other 1 used hold listviews. add them dynamically clicking button @ bottom of screen.

i want set layout weight (3f 1st , 2nd button , list , 1f third one)

so, layout:

<?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="majewski.ninja.myphonelibrary.listactivity"     android:background="#000000">       <linearlayout         android:orientation="horizontal"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignparenttop="true"         android:layout_alignparentleft="true"         android:layout_alignparentstart="true"         android:id="@+id/fragmentsholder">     </linearlayout>       <linearlayout         android:orientation="horizontal"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_below="@+id/fragmentsholder"         android:layout_alignparentleft="true"         android:layout_alignparentstart="true"         android:id="@+id/listsholder"/>      <button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="add new fragment"         android:id="@+id/addfragmentbutton"         android:layout_alignparentbottom="true"         android:layout_centerhorizontal="true" />   </relativelayout> 

thats how add buttons , listviews:

private void addbuttonandlist(string columnname) {     // returns button setted name , layout params     button button = getbutton(columnname);     // returns list setted name , layout params     listview list = getnewlistview(columnname);     // adds views 2 of layouts     addtolayout(button, list); } 

and that's how set layout weight(same fot button , listview):

float weight = getcolumnweight(text); // returns 3f, 3f, 1f button.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content,                 linearlayout.layoutparams.match_parent, weight)); 

after add 2 columns looks this:

enter image description here

but when add third 1 (with 1f layout width) goes weird (buttons ok though):

enter image description here

what doing wrong?

full code of main activity:

package ninja.majewski.myphonelibrary;  import java.util.arraylist;  import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.linearlayout; import android.widget.listview; import ninja.majewski.myphonelibrary.data.booklist;  public class listactivity extends activity {      private booklist booklist;      private int numberoflists = 0;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_list);          booklist = new booklist();          button addfragmentbutton = (button) findviewbyid(r.id.addfragmentbutton);         addfragmentbutton.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 switch(numberoflists) {                     case 0:                         addbuttonandlist("title");                         break;                     case 1:                         addbuttonandlist("author");                         break;                     case 2:                         addbuttonandlist("no.");                         break;                     case 3:                         addbuttonandlist("date");                         break;                 }                  numberoflists++;             }         });     }      private void addbuttonandlist(string columnname) {         // returns button setted name , layout params         button button = getbutton(columnname);         // returns list setted name , layout params         listview list = getnewlistview(columnname);         // adds views 2 of layouts         addtolayout(button, list);     }      private button getbutton(string text) {         button button = new button(listactivity.this);         button.settext(text);         float weight = getcolumnweight(text); // returns 3f, 3f, 1f         button.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content,                 linearlayout.layoutparams.match_parent, weight));          return button;     }      private float getcolumnweight(string text) {         float value = 0;          switch(text) {             case "title":                 value = 3f;                 break;             case "author":                 value = 3f;                 break;             case "no.":                 value = 1f;                 break;             case "date":                 value = 2f;                 break;         }          return value;     }      private listview getnewlistview(string columnname) {         arraylist<string> values = new arraylist<string>();          switch(columnname) {         case "title":             values = booklist.getnameslist();             break;         case "author":             values = booklist.getauthorslist();             break;         case "no.":             values = booklist.getpageslist();             break;         case "date":             values = booklist.getdateslist();             break;         }          arrayadapter<string> adapter = new arrayadapter<string>(this,                 android.r.layout.simple_list_item_1, values);          listview list = new listview(this);         list.setadapter(adapter);         float weight = getcolumnweight(columnname);         system.out.println(weight);         list.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content,                 linearlayout.layoutparams.match_parent, weight));          return list;      }      private void addtolayout(button button, listview list) {         linearlayout layout1 = (linearlayout) findviewbyid(r.id.fragmentsholder);         layout1.addview(button);          linearlayout layout2 = (linearlayout) findviewbyid(r.id.listsholder);         layout2.addview(list);     }  } 

when using weights should set layout width 0dp. right you're having each listview decide it's own width wrap_content, linearlayout using weights decide how shrink each view fit them all.

new linearlayout.layoutparams(0, linearlayout.layoutparams.match_parent, weight) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -