This question already has answers here:
ImageView without xml definition
(2 answers)
Closed 7 years ago.
I want to create an Imageview programmatically without using XML. Is there any way to create it using the height, width and margin property.
Is it possible to add multiple component like this to different places of the screen using the height, width and margin property?
I wrote some code you can use. It relies on the fact that your container is a RelativeLayout:
ImageView myImageView = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.topMargin = myTopMargin;
params.leftMargin = myLeftMargin;
myImageView.setLayoutParams(params);
myImageView.setImageResource(R.id.my_image_resource);
myRelativeLayout.addview(myImageView);
Hope this helps :)
try this..
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//setting image margin
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
imageView.setLayoutParams(layoutParams);
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
Hope this will help..
Use this:
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.something);
//setting image position
//set the margin to the layoutparams
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
//add view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
Related
I have to set image on Right Top of textview programmatically. But its setting on leftside of textview. Can any one tell me how to deal with this?
My code is below:
FrameLayout frameLayout = KaHOUtility.generateFrameLayout(mContext);
frameLayout.setPadding(5, 5, 5, 5);
LinearLayout linearLayout = KaHOUtility.generateLinearLayout(mContext);
KaHOTextView textView = KaHOUtility.generatePanelHeadingTextViews(mContext);
textView.setText(name);
linearLayout.addView(textView);
frameLayout.addView(linearLayout);
ImageView imageView = KaHOUtility.generateImageView(mContext, 15, 15, R.drawable.cancel_mark);
LinearLayout.LayoutParams rPrams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
rPrams.gravity = Gravity.RIGHT|Gravity.TOP ;
imageView.setLayoutParams(rPrams);
frameLayout.addView(imageView);
You are creating layout params from the LinearLayout.LayoutParams class. However, the imageview is being added to a FrameLayout. This is incorrect because you only apply the layout params of the immediate parent to a view. So, in your case, it should be:
ImageView imageView = KaHOUtility.generateImageView(mContext, 15, 15, R.drawable.cancel_mark);
FrameLayout.LayoutParams rPrams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
rPrams.gravity = Gravity.RIGHT | Gravity.TOP ;
imageView.setLayoutParams(rPrams);
frameLayout.addView(imageView);
I'm having problem when adding the Imageview in my relativelayout if a layoutparams is added to the view, the imageview will not add but if the imageview has no layoutparams, it will show but no image. Imageview is created through code.
TableLayout imageCon = (TableLayout)root.findViewById(R.id.home_screen_table_imagecon);
TableRow imageTableRow = new TableRow(root.getContext());
TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageTableRow.addView(getLove());
imageTableRow.addView(getHappy());
imageCon.addView(imageTableRow,rowSpanLayout);
imageCon.setShrinkAllColumns(true);
private ImageView getLove(){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10,10);
imgLove.setLayoutParams(lp);
imgLove.setImageResource(R.drawable.img_emo_love);
return imgLove;
}
private ImageView getHappy(){
imgHappy.setImageResource(R.drawable.img_emo_happy);
return imgHappy;
}
I'm looking forward for your help guys.
Thanks
Chkm8
You need to use the TableRow.LayoutParams , since your adding the ImageView into a TableRow
Change the LayoutParams as
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
lp.setMargins(10, 10, 10,10);
imgLove.setLayoutParams(lp);
If ImageView is added to a RelativeLayout then use RelativeLayout.LayoutParams
If ImageView is added to a LinearLayout then use LinearLayout.LayoutParams
put below line in xml ImageView tag
android:adjustViewBounds="true"
enjoy...
I have a RelativeLayout with ImageView and TextView. I want to the TextView right below the ImageView(with a small padding) and both the ImageView and TextView, aligned to center in the RelativeLayout.
The RelativeLayout is added Programatically
I have read some questions on SO regarding the alignment, but, I can't get them to work for me. Below is my current code...
Code for RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
Code for ImageView
ImageView image = new ImageView(this);
RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpImage.addRule(RelativeLayout.CENTER_IN_PARENT);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
relativeLayout.addView(image);
Code for TextView
TextView textview = new TextView(this);
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTextView.addRule(RelativeLayout.BELOW, image.getId());
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
relativeLayout.addView(textview);
If I set RelativeLayout.CENTER_IN_PARENT for both image and text, they overlap eachother, which is understandable as RelativeLayout supports overlapping of views.
I thought setting RelativeLayout.BELOW for textview will make it align itself below the image, but it's not the case. I even tried RelativeLayout.ALIGN_BOTTOMfor textview, but even that didn't work.
Try this..
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams lprela = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(lprela);
ImageView image = new ImageView(this);
RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpImage.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
relativeLayout.addView(image);
TextView textview = new TextView(this);
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTextView.addRule(RelativeLayout.BELOW, image.getId());
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
relativeLayout.addView(textview);
Or
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(lp_ineer_ver);
ImageView image = new ImageView(this);
LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
linearLayout.addView(image);
TextView textview = new TextView(this);
LinearLayout.LayoutParams lpTextView = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
linearLayout.addView(textview);
Below process may also work for you.
1) Add Image view & Text view in vertical LinearLayout.
2) Add the linear layout to Center of Relative layout.(using CENTER_IN_PARENT).
I am doing an android app that have images which are located besides each other,
every image has its own width, I want to add these images in one line to fit the screen width,
with the same ratio for every one
I wrote code to add this images but still to know how to set the width programmatically
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
LinearLayout my_root = (LinearLayout) findViewById(R.id.root_layout);
LinearLayout child = new LinearLayout(this);
child.setOrientation(LinearLayout.HORIZONTAL);
child.addView(view);
child.addView(view1);
child.addView(view2);
my_root.addView(child);
only image 1 and 2 appear but the third didn't appear because the width of screen finished
Any help !!
Thank you :)
You must need to use weight parameter to do this.
Try below code:
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
view1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
view2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Use Layout Parameters to set height and Width of views at runtime.
like this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
view .setLayoutParams(layoutParams);
I have a program that generates a rows of 4 buttons, in a horizontal container that gets added to a vertical container.
I cannot figure out how to have it so the buttons are centered, now they are aligns to the right hand side. Code:
for(int i=0; i<cGlobals.mNames.length; i+=2) {
iSoundIdList[i]=soundPool.load(this, cGlobals.mSounds[i], 1);
iSoundIdList[i+1]=soundPool.load(this, cGlobals.mSounds[i+1], 1);
// would like these views to all be centered
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(llp);
favBut[i]=new ImageView(this);
favBut[i].setImageResource(R.drawable.heartunselected);
favBut[i].setId(defStartFavId+i);
favBut[i].setOnClickListener(this);
layout.addView(favBut[i]);
Button but1=new Button(this);
but1.setText( cGlobals.mNames[i]);
but1.setWidth(120);
layout.addView(but1);
but1.setOnClickListener(this);
but1.setId(defStartButId+i);
TextView t=new TextView(this);
t.setText(" ");
layout.addView(t);
favBut[i+1]=new ImageView(this);
favBut[i+1].setImageResource(R.drawable.heartunselected);
favBut[i+1].setId(defStartFavId+i+1);
favBut[i+1].setOnClickListener(this);
layout.addView(favBut[i+1]);
t=new TextView(this);
t.setText(" ");
layout.addView(t);
Button but2=new Button(this);
but2.setText( cGlobals.mNames[i+1]);
but2.setWidth(120);
but2.setId(defStartButId+i+1);
but2.setOnClickListener(this);
layout.addView(but2);
Container.addView(layout);
}
Below snippet will help
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
ImageView imageView = new ImageView(this);
imageView.setImageResource(getResources().getIdentifier(
"sample_image", "drawable", getPackageName()));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(lp);
relativeLayout.addView(imageView);