Changing android dynamic image size programmatically - android

I'm trying to resize a dynamic image in android but I can't, maybe someone can help me. I have searched in the web but didn't find anything useful...
This is the code I'm using to create and place the image:
TableRow row = new TableRow(this);
row.setOnClickListener(this);
ImageView iv = new ImageView(this);
iv.setImageBitmap(getImage());
TableRow.LayoutParams lp = new TableRow.LayoutParams(10, 10);
iv.setLayoutParams(lp);
row.addView(iv);

A possible solution, based on your snippet:
//...
ImageView iv = new ImageView(this);
iv.setScaleType(ScaleType.CENTER_INSIDE);
iv.setImageBitmap(getImage());
TableRow.LayoutParams lp = new TableRow.LayoutParams(10, 10);
row.addView(iv, lp);

Related

Android Studio ImageView

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.

Adding images to one line in layout with ratio

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);

Dynamically add ImageView to TableRow

I have a TableLayout in my XML, and I'd like to dynamically add a TableRow with an ImageView to that TableLayout. What I have so far is this:
TableRow tr = new TableRow(this);
ImageView imgView = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imgView.setLayoutParams(lp);
imgView.setImageDrawable(getResources().getDrawable(R.drawable.icon_test));
tr.addView(imgView);
tlCollection.addView(tr);
What am I doing wrong? If I want to add a Textview to the same TableRow, it works, but adding an ImageView doesn't work..
The code for adding a TextView:
TextView myTextView = new TextView(this);
myTextView.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
myTextView.setText("Test");
tr.addView(myTextView);
Any idea?
I fixed it, using an LayoutInflater:
LayoutInflater inflater = getLayoutInflater();
TableRow row = (TableRow)inflater.inflate(R.layout.collection_row, tlCollection, false);
//fill textview
TextView content = (TextView)row.findViewById(R.id.txtCollection);
content.setText("Test");
//fill imageview
ImageView myImgView = ImageView)row.findViewById(R.id.imgCollection);
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.my_icon));
//add row to tablelayout
tlCollection.addView(row);
I think it has to do with the layout params. You're setting both dimensions to WRAP_CONTENT but the image view doesn't have "content" per se, it has a source drawable. Try playing around with adjustViewBounds, setScaleType, and the layout params. Notice that in your example of being able to add a TextView, you're setting the width to FILL_PARENT?
The only example I can find quickly in my open projects of adding an ImageView dynamically in this way was a case in which I was using FILL_PARENT in both directions. That might not work for you since I'm not doing it exactly the same, but it's worth playing around with these settings as well as the two others mentioned above.
try this
ImageView imgView = new ImageView(this);
imgView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
tr.addView(imgView);
I Had the same problem now its fixed using the below code
CandFlag =new ImageView(this);
CandFlag.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
CandFlag.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.flag));
LayoutParams layoutParams = (LayoutParams) CandFlag.getLayoutParams();
layoutParams.width = 75;
layoutParams.height = 75;
CandFlag.setLayoutParams(layoutParams);
Tablerow.addView(CandFlag);

Problem in creating dynamic table layout

I have created a dynamic TableLayout and I have given an image to TableRow's Background.But my problem is the image is taking its actual size not what I am setting.I want that image should take the height and width defined by me.
Please Help Me.
My Code is...
void createRows()
{
for(int i=0;i<5;i++)
{
String name = "Hareesh Kumar Gangadhara";
String time = "2:53 PM";
String date = "06/05/11";
String time_date =time+" "+date;
String message = "Hello Prashant";
TableRow row_message = new TableRow(this);
ImageView friend_img = new ImageView(this);
LinearLayout layout_msg_info = new LinearLayout(this);
LinearLayout layout_name_date = new LinearLayout(this);
LinearLayout layout_message = new LinearLayout(this);
friend_img.setLayoutParams(new LayoutParams(35,35));
friend_img.setBackgroundResource(R.drawable.archit);
TextView tv_name = new TextView(this);
TextView tv_time_date = new TextView(this);
TextView tv_message = new TextView(this);
tv_name.setText(name);
tv_time_date.setText(time_date);
tv_message.setText(message);
tv_name.setTextColor(color_black);
tv_time_date.setTextColor(color_black);
tv_message.setTextColor(color_black);
tv_name.setTextSize(14);
tv_time_date.setTextSize(10);
tv_message.setTextSize(12);
layout_name_date.addView(tv_name);
layout_name_date.addView(tv_time_date);
layout_message.addView(tv_message);
layout_msg_info.setOrientation(LinearLayout.VERTICAL);
layout_msg_info.setPadding(30, 10, 0, 0);
layout_msg_info.addView(layout_name_date);
layout_msg_info.addView(layout_message);
row_message.setBackgroundResource(R.drawable.img_message_back);
row_message.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT,40));
row_message.setPadding(0, 0, 10, 5);
row_message.addView(friend_img);
row_message.addView(layout_msg_info);
table_message.addView(row_message);
}
}
Use TableRow.LayoutParams instead of ViewGroup.LayoutParams.
Hope it Helps
You probably need to set the ScaleType value on your ImageView.
Here's the ScaleType reference: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
You're probably looking to set it to FIT_XY or CENTER_INSIDE.

How to add an ImageView to a TableRow in code

I'm tryin to add an ImageView into a TableRow in Java, but it always shows in blank. The image seems to be there, but it is completely white.
This is my code:
TableRow newRow = new TableRow(this);
newRow.setId(1);
LayoutParams lptr = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
newRow.setLayoutParams(lptr);
newRow.setBackgroundColor(this.getResources().getColor(R.color.white));
ImageView pImg = new ImageView(this);
pImg.setImageDrawable(this.getResources().getDrawable(R.drawable.error));
newRow.addView(pImg);
Why does it not work?
I think you have forgotten two thing:
1) you have forgotten setContentView(view);
2) you have forgotten to add your TableRow to TableLayout

Categories

Resources