Android - Change inflated layout height programatically - android

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)

Related

Android not showing programmatically created ImageView

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

Image inside FrameLayout not shrinking though scaletype set to fitcenter

I have an ImageView that must be wrapped inside Framelayout. But when I use FrameLayout, the image is clipped within the bounds of the ImageView and not made "fit" even if I use setScaleType(ScaleType.FitCenter); but when I use otherViewGroupsuch asRelativeLayout`, the result is ok. why? what can I do? Here is my code:
FrameLayout profileimage_fl = new FrameLayout(
getSherlockActivity());
layoutWidth = WindowSize.convertedHeight(150);
layoutHeight = WindowSize.convertedHeight(150);
p_ll = new LinearLayout.LayoutParams(layoutWidth, layoutHeight);
leftMargin = WindowSize.convertedWidth(1);
topMargin = WindowSize.convertedHeight(1);
rightMargin = WindowSize.convertedWidth(1);
bottomMargin = WindowSize.convertedHeight(1);
p_ll.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
profileimage_fl.setLayoutParams(p_ll);
profileimage_fl.setBackgroundColor(Color.LTGRAY);
ImageView profileimage = new ImageView(getSherlockActivity());
layoutWidth = WindowSize.convertedHeight(150);
layoutHeight = WindowSize.convertedHeight(150);
p_fl = new FrameLayout.LayoutParams(layoutWidth, layoutHeight);
profileimage.setLayoutParams(p_fl);
profileimage.setScaleType(ScaleType.FIT_CENTER);
profileimage.setPadding(WindowSize.convertedWidth(0),
WindowSize.convertedHeight(0), WindowSize.convertedHeight(0),
WindowSize.convertedWidth(0));
profileimage_fl.addView(profileimage);
then I add profileimage_fl to a viewgroup that I inflated in a layout. That layout xml contains only that viewgroup. You see, I don't usually use xml.

Set GestureOverlayView width and height programmatically?

How can I set an GestureOverlayView's width and height programmatically?
I tried the below code. But I am getting null pointer exception in setting the width and height.
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
ImageView imageView = (ImageView) inflate.findViewById(R.id.imageView);
gestureOverlayView.addView(inflate);
gestureOverlayView.getLayoutParams().height = 50;
gestureOverlayView.getLayoutParams().width = 50;
setContentView(gestureOverlayView);
You need to add the view first before you can read out the height and width. Also then you meight get some trouble only after the second or third call of onMeasure(...) the dimensions are final.
If you want to set the width and height by code use this:
ViewGroup.LayoutParams lp = gestureOverlayView.getLayoutParams();
if(lp == null) {
// not yet added to parent
lp = new ViewGroup.LayoutParams(desiredWidth, desiredHeight);
} else {
lp.width = desiredWidth;
lp.height = desiredHeight;
}
gestureOverlayView.setLayoutParams(lp);
yourparent.addView(gestureOverlayView, lp);
Please note that you need to choose also the right LayoutParams implementation. Here I use that one of ViewGroup.

Android grid view recycle issue

I get an error when i try to use the a recycled gridview every time if convertView != null then i get an error here is my source code.
it will give me an error right at text = (TextView) convertView; in the else statment. I am really lost here, I would just stop recycling the views but then its to heavy on memory and its choppy scrolling
$ here is the imageadapter.java
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout lay;
ImageView image;
TextView text;
if (convertView == null) {
Log.d("height", "Width = " + width);
lay = new RelativeLayout(mContext);
image = new ImageView(mContext);
text = new TextView(mContext);
//text.setText("This is a test");
text.setTextSize(14);
text.setTextColor(Color.WHITE);
text.setGravity(Gravity.LEFT | Gravity.TOP);
text.setPadding(2, 2, 2, 2);
text.setBackgroundColor(Color.parseColor("#80000000"));
RelativeLayout.LayoutParams textLayout = new RelativeLayout.LayoutParams(
(int) Math.round(width / 2.0),
(int) Math.round(width / 8.3));
textLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
text.setLayoutParams(textLayout);
MarginLayoutParams textMarginFix = (ViewGroup.MarginLayoutParams) text
.getLayoutParams();
textMarginFix.setMargins(0, 0, 0, (int) Math.round(width / 45.0));
text.setLayoutParams(textMarginFix);
image.setLayoutParams(new LayoutParams((int) Math
.round(width / 2.0), (int) Math.round(width / 2.0)));
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
//image.setImageResource(mThumbIds[position]);
lay.setLayoutParams(new GridView.LayoutParams((int) Math
.round(width / 2.0), (int) Math.round(width / 2.0)));
lay.setBackgroundResource(R.drawable.shadowimage);
lay.setPadding(5, 5, 15, 15);
//lay.setId(mThumbIds[position]);
//lay.addView(image);
//lay.addView(text);
}
else
{
text = (TextView) convertView;
image = (ImageView) convertView;
lay = (RelativeLayout) convertView;
}
image.setImageResource(mThumbIds[position]);
text.setText("This is a test");
lay.addView(image);
lay.addView(text);
return lay;
}
$here is where i call the imageadapter from another class
#Override
public Object instantiateItem(View container, int position) {
View contentView;
switch (position) {
case 0:
LayoutInflater mInflater = LayoutInflater.from(mContext);
View contentView = mInflater.inflate(R.layout.image_grid_view, null);
Display display = mContext.getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
int height = display.getHeight();
float scale = mContext.getResources().getDisplayMetrics().density;
GridView gridview = (GridView) contentView.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(mContext, width, height, scale));
gridview.setFocusable(true);
gridview.requestFocus();
gridview.setOnItemClickListener(itemClickListener);
((ViewPager) container).addView(contentView, 0);
break;
...return contentView
convertView is the view that entirely represents one item in your GridView. If that is a TextView, it will be a TextView, if it is an entire layout, you will receive the entire layout and so on.
So how do you know and define what is "the view that represents one item"? Simple, it is whatever you create when convertView == null and then return from getView.
Simply you are receiving a used item and you are just modifying it to update it to the appropriate content. So you should utilize type casting to get this View you receive in a format you want.
Code like below will get you what you want without redoing things you don't need to do (aka you do not need to re-add child Views from a convertView, only a new view):
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout lay;
ImageView image;
TextView text;
if (convertView == null) {
// Setup your 'item view'
lay = new RelativeLayout(mContext);
image = new ImageView(mContext);
text = new TextView(mContext);
// Do all your customizing stuff (aka size, color, format, padding layout params)
lay.addView(image, 0);
lay.addView(text, 1);
} else {
lay = (RelativeLayout) convertView;
image = (ImageView) lay.getChildAt(0);
text = (TextView) lay.getChildAt(1);
}
// Set content for your image and text
return lay;
}
[ADDITION]
Further, you have the code
image.setImageResource(mThumbIds[position]);
text.setText("This is a test");
lay.addView(image);
lay.addView(text);
return lay;
outside the if and the else, what this means is that you are trying to add a subview EVERY TIME a cell's view is asked for. You really only need to add views to lay within the if portion of your routine.
[ORIGINAL]
Most likely convertView is your parent, and all other views are sub to it. In your else clause, all you do is set each one to the SAME EXACT THING. How can the convertView be three completely different things all at once.
Most likely it needs to be:
text = (TextView) convertView.SomeTextViewSubToConvertView;
image = (ImageView) convertView.SomeImageViewSubToConvertView;
lay = (RelativeLayout) convertView.SomeRelativeLayoutSubToConvertView;

Setting margins on RelativeLayout doesnt work on Kindle Fire

This is similar to my previous question but it didnt work with the Kindle Fire (2.3.4).
I used a FragmentTransaction to add a Fragment into a FrameLayout. I want to dynamically change the margin of the RelativeLayout used by the Fragment.
However, the margins are not changing with FrameLayout.layoutParams on the Kindle Fire. However, this works on 3.2.1. I also tried using setMargins() and it didnt work.
Does anyone know how I can dynamically change the margins on the Kindle Fire?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.infocard, container, false);
RelativeLayout infoLayout = (RelativeLayout) view.findViewById(R.id.info);
infoLayout.setOnClickListener(new EmptyClickListener());
final int width = 250;
final int height = 270;
int leftMargin = 0;
int topMargin = 0;
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
if (x - width < 0) {
leftMargin = 0;
} else {
leftMargin = x - width + 40;
}
if (y >= 400 && y <= 480) {
topMargin = 160;
}
params.leftMargin = leftMargin;
params.topMargin = topMargin;
//params.setMargins(leftMargin, topMargin, 0, 0); //this didnt work either
infoLayout.setLayoutParams(params);
TextView titleView = (TextView) view.findViewById(R.id.titleCard);
titleView.setText(title);
return view;
}
Ok I was able to fix this by wrapping my infoLayout in another RelativeLayout, and now it works perfectly

Categories

Resources