So I'm trying to add an imageview to my current xml design - and its working decently. Now my main problem is that I cant seem to find a way to set the attributes for the image like where it needs to be displayed etc.
RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.blue_1);
i.setAdjustViewBounds(true);
mRelativeLayout.addView(i);
setContentView(mRelativeLayout);
I tried messing around with setlayoutparams but got absolutely no clue what to do with it.
You actually have to use the class LayoutParams to do that efficiently and easily :
RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);
ImageView i = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
params.leftMargin = 25;
params.topMargin = 25;
i.setImageResource(R.drawable.icon);
i.setAdjustViewBounds(true);
mRelativeLayout.addView(i, params);
This works for me and put my icon in the top left corner of the screen with the specified margin to the sides of the screen.
Does that help?
Related
I have created an ImageView by code and I want it to have a max size of 24x24dp. But the setMaxHeight/Width aren't working. The image I use is a svg Drawable and it's size is 40x40dp.
I have also tried to use imageView.setMinimumHeight/Width and
imageView.setLayoutParams(new ViewGroup.LayoutParams((int) getTypedValueInDP(24), (int) getTypedValueInDP(24)));
without any effect!
Here I am creating the ImageView:
ImageView imageView = new ImageView(context);
imageView.setMaxHeight(getTypedValueInDP(20));
imageView.setMaxWidth(getTypedValueInDP(20));
imageView.setImageDrawable(context.getResources().getDrawable(drawable));
imageView.setPadding(left, 0, left, 0);
and I'm using it with a RelativeLayout also made in code like this:
RelativeLayout toastLayout = new RelativeLayout(context);
toastLayout.setBackground(getBLLShape());
toastLayout.addView(getImageView());
You need to specify the LayoutParams for the ImageView before adding to the ViewGroup.
ImageView imageView = new ImageView(this);
imageView.setMaxHeight(getTypedValueInDP(20));
imageView.setMaxWidth(getTypedValueInDP(20));
imageView.setAdjustViewBounds(true);
imageView.setImageDrawable(context.getResources().getDrawable(drawable));
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(layoutParams);
RelativeLayout toastLayout = new RelativeLayout(this);
toastLayout.addView(imageView);
The padding seems to be messing up with the setMaxWidth(). Make sure that the padding you give at both sides must not be equal to or greater than setMaxWidth argument. In your code, you are giving padding to left and right side. So, change:
imageView.setMaxWidth(getTypedValueInDP(20));
to
imageView.setMaxWidth(2* left + getTypedValueInDP(20));
If your image and imageview have same aspect ratio. Did you try
imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE)
I'm trying to create, resize and add an imageView and I've been looking for a solution to this all over but nothing works properly, every time I set any kind of Param the image doesn't appear anymore or the app crashes, I'm using Android Studio 1.0.1 and this are the methods I've tried:
This one the image disapear
TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);
LinearLayout.LayoutParams imgPar = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
imgPar.width = 50;
imgPar.height = 50;
imgPar.setMargins(5,5,5,5);
imageView.setLayoutParams(imgPar);
tableInfoCont.addView(imageView);
This one the image disapear
TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);
FrameLayout.LayoutParams imgPar = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
imgPar.width = 50;
imgPar.height = 50;
imgPar.setMargins(5,5,5,5);
imageView.setLayoutParams(imgPar);
tableInfoCont.addView(imageView);
This one the app crashes
TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);
ViewGroup.LayoutParams imgPar = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
imgPar.width = 50;
imgPar.height = 50;
//imgPar.setMargins(5,5,5,5); //not possible to set margin
imageView.setLayoutParams(imgPar);
tableInfoCont.addView(imageView);
This one the app crashes
TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);
imageView.getLayoutParams().width = 50;
imageView.getLayoutParams().height = 50;
tableInfoCont.addView(imageView);
That is it, tryed all this methods... Any idea what am I missing here?
Keep in mind that is discouraged to create Layout in code.
Also try to let a simple ImageView show up first before you go with a TableView.
Try this code :
TableLayout tableLayout = new TableLayout(this);
TableRow tableRow = new TableRow(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.simpleAvatar);
tableRow.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
tableRow.addView(imageView);
tableLayout.addView(tableRow);
setContentView(tableLayout);
Take into consideration that in such expressions you specify height and width in pixels, not in dp. Your picture may disappear just because it's wider or higher than your tablerow is (check your tablerow's layout parameters). Set your image's width and height to match_parent and check up how it feels.
Try using imageView.requestLayout() after you commit some changes in LayoutParams. This makes the View re-read the layout for changes.
I want to put some images in an area at the application start.
As the number of images is not a fixed amount, so I have to create images dynamically.
I want to set the position/margin of every images while they're created for good balance.
I have tried follows, but there is no efficacy.
・After Created, use imageview.layout().
・Use LayoutParams.margins().
・Use AsyncTask to set margins.
・activity.runOnUiThread().
This is the code:
// After access server, if needed then add a imageview to the 'c_box'
// c_box: the parent that imageview to add.
FrameLayout c_box = (FrameLayout) findViewById(R.id.c_box);
MarginLayoutParams mlp = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView img1 = new ImageView(this);
img1.setImageResource(R.drawable.c_file);
img1.setLayoutParams(params);
img1.setLongClickable(true);
img1.setId(img_id);
c_box.addView(img1);
img1.setOnTouchListener(listener);
**// it's possible to set the position by layout or margin?
img1.layout(100, 100, 200, 200);**
I don't know where to call the invalidate() method.
// After access server, if needed then add a imageview to the 'c_box'
// c_box: the parent that imageview to add.
FrameLayout c_box = (FrameLayout) findViewById(R.id.c_box);
MarginLayoutParams mlp = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mlp.setMargins(100, 100, 200, 200);
ImageView img1 = new ImageView(this);
img1.setImageResource(R.drawable.c_file);
img1.setLayoutParams(mlp);
img1.setLongClickable(true);
img1.setId(img_id);
c_box.addView(img1);
img1.setOnTouchListener(listener);
Finally I found the point:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.NO_GRAVITY);
the 3rd param(Gravity.NO_GRAVITY) that I missed. use it you can position views at anywhere with setting width/height.
You could use MarginLayoutParams
MarginLayoutParams mlp = (MarginLayoutParams) yourImageViewHere.getLayoutParams();
mlp.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);//all in pixels
yourImageViewHere.setLayoutParams(mlp);
To set your margins dynamically.
In OnCreate:
svMaster = (ScrollView) findViewById(R.id.scroller); //Only layout in XML file
svMaster.setVerticalFadingEdgeEnabled(false);
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
height = display.getHeight();
...
initializeGUI();
svMaster.removeAllViews();
svMaster.addView(llMaster);
In initializeGUI():
llMaster = new LinearLayout(this); //Only direct child of scrollview
LinearLayout llFirstScreen = new LinearLayout(this); //First layout added to llMaster;
//It's size is that of one screen
LinearLayout.LayoutParams lpMaster = new LinearLayout.LayoutParams
(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams lpFirstScreen = new LinearLayout.LayoutParams
(LayoutParams.MATCH_PARENT, height);
llMaster.setLayoutParams(lpMaster);
llMaster.setOrientation(LinearLayout.VERTICAL);
llFirstScreen.setLayoutParams(lpFirstScreen);
llFirstScreen.setBackgroundResource(R.color.blue2);
llFirstScreen.setOrientation(LinearLayout.VERTICAL);
Here's my code pertaining to the ImageView:
ImageView ivWeather = new ImageView(this);
LinearLayout.LayoutParams ivWeatherParams = new LinearLayout.LayoutParams
(Scale(80), Scale(80);
ivWeather.setImageResource(R.drawable.sunny);
Log.d("ImageView loading?", "I hope so");
ivWeather.setScaleType(ScaleType.FIT_XY);
ivWeather.setLayoutParams(ivWeatherParams);
ivWeather.setVisibility(View.VISIBLE); //This is code I tried
ivWeather.setFocusable(true); //when it wouldn't show up
ivWeather.setFocusableInTouchMode(true); //...
ivWeather.invalidate(); //...
...
llFirstScreen.addView(ivWeather);
Log.d("ImageView loading?", "I hope so");
llMaster.addView(llFirstScreen);
...
I have added TextViews with no problem to llFirstScreen, and I am wondering why ImageView
won't show up. I tried even adjusting llFirstScreen's height to WRAP_CONTENT instead of the screen's height. All that did was shrink the layout to the two TextViews. It's as if it never added the ImageView. The Logs I put in check out, so I know the code is running.
Am I missing anything?
Have you tried changing your LinearLayout.LayoutParams from Scale(80), Scale(80) to something like LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT or something? I've never seen Scale(int) used. If that is a custom method can you post it?
friends,
i want to set android:layout_centerVertical="true" property of layout
through java code of an image.
can any one guide me how to achieve this. here is my code.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.height = (int)totalHeight;
img.setLayoutParams(params);
i have tried using setScaleType(ScaleType.FIT_CENTER) but no use.
any help would be appriciated.
Try this Code..
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
img.setLayoutParams(params);
Correct me if I'm wrong but it sounds like you are trying to set the alignment (or positional orientation) of an image inside of a layout? To do that you need to set the gravity property of the layout containing the View you align.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(params);
imageView.setImageBitmap(bitmap);
layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.TOP);
layout.addView(imageView);
In this example I'm programmatically adding an image to a RelativeLayout in my layout resource, adding an ImageView, and aligning it so it will be placed at the top, vertical center position.
In the new SDK there's no Gravity.VERTICAL_CENTER, but maybe you could set this with
layout.setGravity(Gravity.CENTER).