Android ImageView Layout Issues - java

There very well may be a duplicate question, but I have yet to find it. I am doing thing all programmatically, not using the xml. Basically what I am trying to do is to have an EditText appear below an image. I am using RelativeLayout with an ImageView and and EditText.
These are the parameters that I am setting up for the ImageView and EditText:
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
editTextParams.width=500;
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageParams.addRule(RelativeLayout.ABOVE, editText.getId());
Which I have verified correctly places the EditText in the bottom right corner and the image above. What I run into is if the picture it "too tall" then it covers the EditText. I also tried using
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
and it stretches it over the EditText as well.
The full code that I am using is this
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView imageView = new ImageView(this);
EditText editText = new EditText(this);
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
editTextParams.width=500;
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageParams.addRule(RelativeLayout.ABOVE, editText.getId());
imageView.setLayoutParams(imageParams);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
editText.setLayoutParams(editTextParams);
Bitmap image = getImage();
imageView.setImageBitmap(image);
layout.addView(editText);
layout.addView(imageView);
Thanks. Any suggestions would be greatly appreciated.

Create a LinearLayout and then add the two components in it (ImageVIew and EditText)
Here is what you should do;
Set the orientation to vertical for the horizontal layout and width to whatever you need
Set the with of both components to 0 and weights to 1 each
After that, you should have the two items one above the other;
I hope this helps

One thing you can do is fix the imageview width and height. This way you can control the maximum size without the imaging going over the edit text.
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(256, 256);
Hopefully this helps.

Your editText has a vertical MATCH_PARENT. Shouldn't it be WRAP_CONTENT?
(I am not sure if this helps, but it might help, as the MATCH_PARENT contradicts your planned layout, and your screenshot indicates that the editText is vertically centered.)

Related

Android: Is it possible to set parent of PopupWindow?

I displayed PopupWindow in fragment and it works well except when side menu appears. Side menu is displayed behind the PopupWindow which I don't want.
So I think it would be good to set parent of PopupWindow or maybe zOrder.
LinearLayout contentView = (LinearLayout) inflate(getContext(), R.layout.callout_merge_destination, null);
PopupWindow mCallout = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mCallout.showAsDropDown(view3, x, y);
Please let me know the best solution.

How to set the align parent right attribute of TextViews in relative layout programmatically

I have a relative layout which I am creating programmatically:
RelativeLayout layout = new RelativeLayout( this );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Now I have two TextViews which I want to add in this relative layout. But the problem is both TextViews are being shown on the left of the RelativeLayout overlapping on each other.
textViewContainer.addView(textView1);
textViewContainer.addView(textView2);
Now I want to know how can I programmatically set the the
`android:layout_alignParentRight="true"`
or
`android:layout_toLeftOf="#id/textView"`
attribute of TextViews as we do in the xml?
You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using
RelativeLayout.LayoutParams#addRule(int verb) and
RelativeLayout.LayoutParams#addRule(int verb, int anchor)
You can get to it via code:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)textView.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
textView.setLayoutParams(params); //causes layout updat
Use the method addrule
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
You need to align LayoutParams to each view:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Then add appropriate rule to each view:
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
to the first one and
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
to the second one.
then apply it to the view:
view.setLayoutParam(params)

Set Gravity to TextView with background drawable image programmatically not working

I search for this problem but i didn't find a convenient answer.
I try to add dynamically a TextView to a LinearLayout, but the problem is that the setGravity doesn't work.
This is my code:
TextView textView = new TextView(mContext);
textView.setTextColor(Color.BLACK);
textView.setText(colorKeyword(resources.getString(R.string.tipsbutton_tooltip)));
textView.setTextSize(35);
textView.setTextScaleX(1.1f);
textView.setPadding(textViewPadding, textViewPadding, 0, textViewPadding);
textView.setTextAppearance(mContext, R.style.fontForTooltipsTexts);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textView.setBackgroundResource(R.drawable.tip_box);
tooltipView.setBackgroundResource(android.R.color.transparent);
ImageView arrowView = new ImageView(mContext);
arrowView.setImageResource(R.drawable.tip_arrow_down);
arrowView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
arrowView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tooltipView.setOrientation(LinearLayout.VERTICAL);
tooltipView.addView(textView);
tooltipView.addView(arrowView);
If i set the layout height of the textview to MATCH_PARENT, the gravity knows to center the text horizontal as i need, but the side effect is that all the background/ textview area is stretched to the parent height.
I now for sure is a problem related to the order in which they are laid out. Is probably something that i miss to do.
I use this for parent:
LinearLayout tooltipView = new LinearLayout(mContext);
tooltipView.setOrientation(LinearLayout.VERTICAL);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
tooltipView.setLayoutParams(lp);
tooltipView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
tooltipView.setGravity(Gravity.CENTER_VERTICAL);
I tried to inflate from a layout in which i already added the background, but i have the same problem.
The height of the TextView should match to the background image height.
Thanks for any suggestions!

Text View not displaying (Android Studio)

So I'm having trouble getting a TextView to appear programmatically with Java. Here's the code:
LinearLayout layout=new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
TextView tx = new TextView(this);
tx.setText("Hello World");
layout.addView(tx);
Are you ever setting the content view for the current activity? The LinearLayout you're dynamically creating doesn't appear to ever be displayed.
Do something like this after you create it (or create the linear layout in your XML first, then dynamically add the text box):
setContentView(layout)

Android ImageView - Fill width and Resize to Maintain Aspect Ratio

I have a row View that I am using in a ListView.
This RowView consists of an ImageView on the left and a TextView on the right in a horizontal LinearLayout where the image is given 40% of the space and the text the remaining 60%.
I want the layout to handle the resizing of the image in the ImageView in the following manner:
Stretch the image horizontally so that it fills up given 40% of the LinearLayout
Resize the ImageView vertically to maintain the original aspect ratio
This is my approach to the layouts:
protected class RowView extends LinearLayout {
public ImageView iv = null;
public TextView tv = null;
private LinearLayout.LayoutParams ivlp = null;
private LinearLayout.LayoutParams tvlp = null;
public RowView(Context ct) {
super(ct);
setOrientation(LinearLayout.HORIZONTAL);
// Create the views
iv = new ImageView(ct);
ivlp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.4f);
iv.setLayoutParams(ivlp);
iv.setAdjustViewBounds(true);
iv.setScaleType(ScaleType.FIT_XY);
tv = new TextView(ct);
tvlp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.4f);
tv.setLayoutParams(tvlp);
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
// Add the views
addView(iv);
addView(tv);
}
}
The result I am getting is something like this:
From what I understand:
The MATCH_PARENT is correctly forcing the image to horizontally stretch to fill the 40% space
The ImageView.setAdjustViewBounds(true) is supposed to allow any resizing of the ImageView that is necessary which is what I want. The problem is none of the ImageView.ScaleType's seem to do what I want
ImageView.ScaleType(ScaleType.FIT_XY) is not what I want because it doesn't maintain the aspect ratio. But all the others that do, don't seem to stretch the height of the ImageView (which is making ImageView.setAdjustViewBounds(true) pretty much useless).
What am I doing wrong? It seems wrong that something this simple could be so difficult?
Or do I actually have to go to the effort of extending an ImageView and Overriding onMeasure to force the resizing and aspect ratio?
Or is there a clever trick with Matrix to get the job done?
I believe this is what you are looking for:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/myImage"
android:scaleType="fitCenter"
android:adjustViewBounds="true" />
Remove the scale type completely. Also, I think you might have misunderstood what setAdjustViewBounds does. If an image is scaled, the bitmap in the ImageView is scaled, but not the ImageView. Setting setAdjustViewBounds to true will assure that the ImageView's bounds will always match the drawn bitmap's bounds.

Categories