Dynamically add imageviews into layout and fit the screen size using java codes

Introduction 

Some times we need to cover screen(horizontally)using imageview so mostly all are used two or more number of imageviews to fill that space but is not a correct way,here some simple method used to fill that space using single imageview into more imageview(both portrait and landscape)                                                           

demo




Using the code

xml:
<LinearLayout
android:id="@+id/relative4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>


java:
LinearLayout relative4;

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

Drawable d = getResources().getDrawable(R.drawable.border_icon);
final int w = d.getIntrinsicWidth();
final int mScreenWidth = this.getWindowManager().getDefaultDisplay().getWidth();
for(int i =0;i<(mScreenWidth/w)-1;i++)
{
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams
 (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imageView.setBackgroundResource(R.drawable.border_icon);
relative4.addView(imageView);
}


Happy Coding...
Previous
Next Post »