How to get ImageView left-aligned with java coding? - android

I have searched the solution about ImageView alignment. But most of the solutions are using layout.xml. To make it flexible, I'm using java code to add ImageView to layout. Following is my code. It always shows in the center. I need it go to the left.
String uri="/xxx/xxx.png";
File f = new File(uri);
ImageView iv=new ImageView(cntxt);
if (f.exists())
iv.setImageURI(Uri.parse(uri));
else
iv.setImageResource(R.drawable.missing);
iv.setMaxWidth(400);
iv.setScaleType(ScaleType.CENTER_INSIDE);
LinearLayout.LayoutParams p22 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
p22.gravity=Gravity.LEFT;
linenearlayout.addView(iv, p22);

Related

FlowTexView dynamically added text appear in single line height issue

I have added flowtextview jar file in my project lib folder and its working perfectly when i add it in my layout manualy.
I need to add flowtextview dynamically to my LinearLayout and add text and images dynamicaly into it. I can add text and images but the text appearing in sinle line, i thing its because of the flowtexview height setting issue.
please check the attached image , you can see the issue correctly.
code used for adding dynamic views as follows,
contentHolder = (LinearLayout)rootview.findViewById(R.id.contentOuter);
//add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//params.setMargins(10, 20, 10, 20); // llp.setMargins(left, top, right, bottom);
contentHolder.setOrientation(LinearLayout.VERTICAL);
ftv = new FlowTextView(getActivity());
FlowTextView.LayoutParams fp = new FlowTextView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
fp.setMargins(0, 10, 0, 10);
ftv.setLayoutParams(params);
ftv.setId(110);
ftv.setText("Currently it is placing that image as the background to the view and not the actual image in it.Currently it is placing that image as the background to the view and not the actual image in it.Currently it is placing that image as the background to the view and not the actual image in it..");
ImageView im = new ImageView(getActivity());
im.setBackgroundResource(R.drawable.av1);
im.setId(5);
RelativeLayout rt = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rparams.setMargins(0, 10, 0, 10);
rt.setLayoutParams(rparams);
rt.addView(im);
ftv.addView(rt);
ftv.invalidate();
//add the textView and the Button to LinearLayout
contentHolder.addView(ftv);
at last i found the issue, its because of the jar file. its not an updated one. use flowtextview as a library project and add it to your project or use or Gradle:
compile 'uk.co.deanwild:flowtextview:2.0.2#aar'

Changing android dynamic image size programmatically

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

Align image and text on textview

In my app, I populate a linearlayout with a text and a little icon (like a listview with a custom adapter).
I need the text and the picture to have the same size. Is there a way to do that?
Here's the code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout LayoutPadre = new LinearLayout(this);
TextView Tag = new TextView(this);
ImageView img = new ImageView(this);
Tag.setText(data);
if(stato.equalsIgnoreCase("presente")){
img.setImageResource(R.drawable.ic_presente);
}else{
img.setImageResource(R.drawable.ic_assente);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(75, LayoutParams.WRAP_CONTENT); //Here I need the img to have same size as the text, but Tag.getHeight() returns 0
layoutParams.setMargins(50, 0, 0, 0);
img.setLayoutParams(layoutParams);
LayoutPadre.setLayoutParams(params);
LayoutPadre.addView(Tag);
LayoutPadre.addView(img);
The problem is that the image is not horizontally centered like the img.
Look at the screen
Thanks to all.
keep the layout_height of text and image as Match_Parent and not Wrap_Content... By that
If you are preserving the android:layout_height="wrap_content" or it did not work, you can also do android:gravity="center"

ImageView will not show up

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?

how to center layout to vertical in android through java code?

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

Categories

Resources