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!
Related
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.
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)
I'm trying to display two RelativeLayouts in a LinearLayout, however the best result I'm getting is getting one of the two displayed.
final LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.VERTICAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutAll = (RelativeLayout) inflate.inflate(
R.layout.activity_main, null);
final RelativeLayout menuLayout = (RelativeLayout)inflate.inflate(
R.layout.bottom_menu, null);
menuLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
layoutMain.addView(layoutAll);
layoutMain.addView(menuLayout);
layoutAll is showing, and if I change the order of the addView lines, then menuLayout is showing. So far I've tried different layout parameters (MATCH_PARENT for layoutAll or menuLayout and even both). Any suggestion ? Thanks.
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)
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.)