Android not showing programmatically created ImageView - android

I am creating an ImageView in my project like this:
RelativeLayout root = (RelativeLayout) ((Activity) GameGuessActivity.context).findViewById(R.id.layout);
ImageView img = new ImageView(GameGuessActivity.context);
img.setImageResource(R.drawable.image);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.leftMargin = x;
params.topMargin = y;
root.addView(img, params);
But the Image is not displaying. I am creating the image after the setContentView(R.layout.layout) is being called, could that be the reason? If yes, how can I 'refresh' the layout to include the image but not change anything to existing Views?

The following could work:
View root = getLayoutInflater.inflate(your_layout, null);
ImageView img = new ImageView(GameGuessActivity.context);
img.setImageResource(R.drawable.image);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.leftMargin = x;
params.topMargin = y;
root.addView(img, params);
setContentView(root);

Use the LayoutInflator to create a view based on your layout template,
and then inject it into the view where you need it.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

Related

Android - Change inflated layout height programatically

I am currently working on changing the height of a layout programatically.
Here's a snippet of the layout initialization :
setContentView(R.layout.activity_main);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pagerItemInflater = (View) inflater.inflate(R.layout.activity_main, null);
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
LayoutResize layoutResize = new LayoutResize();
RelativeLayout relativeViewPager =(RelativeLayout) findViewById(R.id.relativeViewPager);
int relativeViewPagerHeight = layoutResize.height(75, displayMetrics);
ViewGroup.LayoutParams viewPagerParams = relativeViewPager.getLayoutParams();
viewPagerParams.height = relativeViewPagerHeight;
relativeViewPager.setLayoutParams(viewPagerParams);
RelativeLayout viewPagerItem =(RelativeLayout) pagerItemInflater.findViewById(R.id.viewPagerItem);
int viewPagerItemWidth = layoutResize.width(100, displayMetrics);
int viewPagerItemHeight = layoutResize.height(28, displayMetrics);
RelativeLayout.LayoutParams viewPagerItemParams = new RelativeLayout.LayoutParams(viewPagerItemWidth, viewPagerItemHeight);
//ViewGroup.LayoutParams viewPagerItemParams = viewPagerItem.getLayoutParams();
viewPagerItemParams.height = viewPagerItemHeight;
viewPagerItem.setLayoutParams(viewPagerItemParams);
The problem is, I can't get the second one which is viewPagerItem to resize to 28% of screen. When I use log, the viewPagerItemHeight shows the appropiate height I wanted, but the layout doesn't resize as expected.
I've read around and some people said that View pagerItemInflater = (View) inflater.inflate(R.layout.activity_main, null); null is supposed to be the root class right? The R.layout.activity_main? But I can't use it there, it says cannot resolve method inflate(int,int)

RelativeLayout addView LEFT_TO Center View,Then the Center View doesn`t still in the center,why?

as the title,picture as below
code:
textView = new TextView(mContext);
textView.setId(5005);
textView.setText(mShareText);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mShareTextSize);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.alignWithParent = true;
params.addRule(CENTER_IN_PARENT, TRUE);
addView(textView, params);
imageView = new ImageView(mContext);
initImageView();
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params2.addRule(LEFT_OF, 5005);
params2.addRule(CENTER_VERTICAL, TRUE);
params2.rightMargin = mDrawablePaddingDP;
addView(imageView, params2);
setControlAble();
This is Custom View which extends RelativeLayout .
I want the M still in the center, the program left to the M,how should I do?
Create one more text view, give it an ID and set text as " "(space or blank). Now set it in mid. Now you can set your image and text view left to it.Best of luck.

Android: How to set imageView to center horizontal

I want to position a View horizontally centered in a Layout. My code below fails. How can I fix this?
See my code:
LinearLayout LLT = new LinearLayout(context);
LLT.setOrientation(LinearLayout.VERTICAL);
LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//get booktheme by bookID
theme = db.getthemeByID(id);
String themePath = theme.getFilepath();
int resid = getResources().getIdentifier(themePath, "drawable", getPackageName());
//imageView
ImageView imageTheme = new ImageView(context);
imageTheme.setLayoutParams(new LayoutParams(500, 700));
imageTheme.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageTheme.setPadding(0, 20, 0, 10);
imageTheme.setAdjustViewBounds(true);
imageTheme.setImageResource(resid);
LLT.addView(imageTheme);
// add view
VF.addView(LLT);
Use Gravity feature in your LayoutParams as following:
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(500, 700);
layoutParams.gravity=Gravity.CENTER_HORIZONTAL;
imageTheme.setLayoutParams(layoutParams);
Replace your code with this one and try:
LinearLayout LLT = new LinearLayout(context);
LLT.setOrientation(LinearLayout.VERTICAL);
LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//get booktheme by bookID
theme = db.getthemeByID(id);
String themePath = theme.getFilepath();
int resid = getResources().getIdentifier(themePath, "drawable", getPackageName());
//imageView
ImageView imageTheme = new ImageView(context);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(500, 700);
layoutParams.gravity=Gravity.CENTER_HORIZONTAL;
imageTheme.setLayoutParams(layoutParams);
imageTheme.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageTheme.setPadding(0, 20, 0, 10);
imageTheme.setAdjustViewBounds(true);
imageTheme.setImageResource(resid);
LLT.addView(imageTheme);
// add view
VF.addView(LLT);
have you tried using relative layout instead of linear layout ?
EDIT :
if u want to use relative layouts ,
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)imageview.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageview.setLayoutParams(layoutParams);
( did not test the code btw but i used it in one of my app so this shld do the job )

Dynamically adding TextView causes null point exception

I want to add textview dynamically. When i try to do this iam getting null point exception
this is my code snippet:
LinearLayout layout = (LinearLayout) findViewById(R.layout.activity_main);
LayoutInflater Inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View vs = (View) Inflater.inflate(R.layout.serverd_details, null);
TextView textView = (TextView) vs.findViewById(R.id.SerText);
textView.setText("server details");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(textView, p);
Thanks in advance...
Change your LinearLayout like this
LinearLayout layout=(LinearLayout)findViewById(R.Id.your_layout_ID)
or
TextView textView = (TextView) vs.findViewById(R.id.SerText);
is SerText ID is placed in your XML Layout
Hello Karthick Pandiyan,
I think you are doing well but after viewing your code, i want to suggest you
Use below code.
TextView textView = new TextView(context);
textView.setText("server details");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(textView, p);
I think it will work.

HorizontalScrollView in PopupWindow

i try to create popupWindow with preView where user can see images from gallery.
But first and second images are hiden and at the end of scrollView is empty space like in a screenshot.
I am try to use
layout.addView(imageView);
private void showAttachmentPopup() {
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.attachemnts_file_popup, null, false);
PopupWindow pw = new PopupWindow(popupView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, findViewById(R.id.layout_sent).getId());
popupView.setLayoutParams(params);
LinearLayout layout_attachment = (LinearLayout) popupView.findViewById(R.id.layout_attachment);
RoundedImageView roundedImageView = new RoundedImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
lp.setMargins(5, 5, 5, 5);
roundedImageView.setLayoutParams(lp);
roundedImageView.setScaleType(RoundedImageView.ScaleType.FIT_XY);
roundedImageView.setImageBitmap(bitmap);
layout_attachment.addView(roundedImageView);
pw.setOutsideTouchable(true);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.showAtLocation(mBtnAttach, Gravity.BOTTOM | Gravity.LEFT, locateView(mBtnAttach).bottom, locateView(mBtnAttach).right);
}
And if I try to add this bitmap few time this error happens.
In my xml just HorizontalScrollView and LinearLayout into here
I ( guess ) i had the same issue, looked the same.
What helped me out was to set adjustViewBounds:
roundedImageView.setAdjustViewBounds(true);
Hope it suits your case!

Categories

Resources