Recent Posts

Sunday 22 November 2015

Creating Views in android in loops ( Java )

Here goes a small code for looping in android using java.

Layout to which we add views

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent" android:layout_height="wrap_content"
  android:orientation="vertical" android:id="@+id/content">
</LinearLayout>



Lets create views in for loop and add to above layout

LinearLayout content=(LinearLayout)findViewById(R.id.content);

int[5] x={1,2,3,4,5};
for(int i:x)
{
  Button butn=new Button(getApplicationContext());
  butn.setTag(i);       // used for identifying individually each button 
 content.addView(butn);
}

Smart Siva

Author & Editor

In Life every second is important, In programming every line of code is important.

0 comments:

Post a Comment

 
biz.