android - How to add custom layout to linear layout in scrollView dynamicaly -
i want show screen text area vertically scrolling , image area horizontally scrolling. have created 2 layouts:
activity_display.xml <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".mainactivity" > <scrollview android:id="@+id/displaysv1" android:layout_width="match_parent" android:layout_height="200dp" android:layout_marginbottom="20dp"> <linearlayout android:id="@+id/displayll1" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical"> </linearlayout> </scrollview> <horizontalscrollview android:id="@+id/displaysv2" android:layout_width="match_parent" android:layout_height="200dp" android:layout_marginbottom="20dp"> <linearlayout android:id="@+id/displayll2" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> </linearlayout> </horizontalscrollview> </linearlayout>
and custom_layout.xml
: want add custom layout in linear layout displayll1
.
<framelayout android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/customframe"> <textview android:layout_width="150dp" android:layout_height="wrap_content" android:id="@+id/customtv1" android:layout_gravity="start|top" android:layout_marginstart="10dp" android:layout_marginleft="10dp" android:text="test text" android:layout_margintop="5dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/customtv2" android:layout_gravity="top|center_horizontal" android:text="test value" android:layout_margintop="5dp" /> <button style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="edit" android:id="@+id/editbutton" android:layout_gravity="right|top" /> </framelayout>
display activity calls:
super.oncreate(savedinstancestate); setcontentview(r.layout.activity_display); linearlayout linearlayout2 = (linearlayout) findviewbyid(r.id.displayll2); for(int = 0; < count1; i++) { linearlayout linearlayout = (linearlayout) findviewbyid(r.id.displayll1); framelayout custlayout = (framelayout) findviewbyid(r.id.customframe); textview label = (textview) findviewbyid(r.id.customtv1); textview value = (textview) findviewbyid(r.id.customtv2); label.settext("test"); value.settext("test"); linearlayout.addview(custlayout); } (int x = 0; x < count2; x++) { final imageview image = new imageview(this); image.setbackgroundresource(r.drawable.ic_launcher); linearlayout2.addview(image); }
i able populate image part if text part commented out. above code nothing displayed on screen. idea might going wrong? thank you!
probably have nullpointerexception.
because there no customtv1
, customtv2
ids in activity xml, calling findviewbyid(r.id.customtv1);
return null value, , label.settext("test");
throw nullpointerexception.
actually calling framelayout custlayout = (framelayout) findviewbyid(r.id.customframe);
not add custom xml activity one, need inflate it.
try code:
linearlayout linearlayout = (linearlayout) findviewbyid(r.id.displayll1); linearlayout linearlayout2 = (linearlayout) findviewbyid(r.id.displayll2); layoutinflater inflater = layoutinflater.from(youractivity.this); for(int = 0; < count1; i++){ view custlayout= inflater.inflate(r.layout.custom_layout, null, false); textview label = (textview) custlayout.findviewbyid(r.id.customtv1); textview value = (textview) custlayout.findviewbyid(r.id.customtv2); label.settext("test"); value.settext("test"); linearlayout.addview(custlayout); } (int x = 0; x < count2; x++){ imageview image = new imageview(this); image.setbackgroundresource(r.drawable.ic_launcher); linearlayout2.addview(image); }
Comments
Post a Comment