I have image and text that I want to put on my app.
I need the text to be centered over the image.
Everything is done programmatically with RelativeLayout Object.
For some reason, I only succeed to center the TextView over the ImageView vertically.
This is the function that creates the RelativeLayout:
private void buildPost(Context context){
Random rnd = new Random();
SquareImageView postBgImgIV = new SquareImageView(context);
postBgImgIV.setBackgroundColor(Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
postBgImgIV.setImageResource(R.drawable.clear);
TextView postContentTV = new TextView(context);
postContentTV.setText(this.postContent);
postContentTV.setTextSize(42);
//postContentTV.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
postContentTV.setGravity(Gravity.CENTER);
post = new RelativeLayout(context);
post.addView(postBgImgIV);
post.addView(postContentTV);
}
I sure that I missing something and it can't solve with few lines of code.
I will love to get help :)
Problem Solved!
Actully I needed to define new RelativeLayout.LayoutParams and then difiene the textView LayoutParams as the new RelativeLayout.LayoutParams.
The code it:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
postContentTV.setLayoutParams(params);
You need to add layout params to the textview
TextView textView = new TextView(context);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);;
Related
I am adding some View to the LinearLayout dynamically:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(100, 100, 100, 100);
view.setLayoutParams(params);
parent.addView(view);
However, the margins are not getting applied. The following:
view.invalidate();
view.requestLayout();
parent.invalidate();
parent.requestLayout();
didn't work. However, if I force the activity to recreate (e.g. turn off and on my mobile phone), the margins get applied. Calling activity.recreate() also works, but it's too slow.
How to force layout to recalculate margins? Probably, there's something wrong in the flow? I tried to add my views to the root after creation, add children before and after applying properties, but this didn't work for me.
UPD:
I tried to repeat the bug programmatically and got it with this code:
LinearLayout base = new LinearLayout(context);
LayoutParams params1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
base.setLayoutParams(params1);
base.setBackgroundColor(Color.CYAN);
root.addView(base);
LinearLayout another1 = new LinearLayout(context);
LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
another1.setLayoutParams(params2);
another1.setOrientation(LinearLayout.VERTICAL);
another1.setBackgroundColor(Color.BLUE);
base.addView(another1);
TextView tv1 = new TextView(context);
tv1.setText("SOME TEST TEXT 1");
tv1.setTextColor(Color.BLACK);
LayoutParams params4 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params4.setMargins(100, 100, 100, 100);
tv1.setLayoutParams(params4);
another1.addView(tv1);
TextView tv2 = new TextView(context);
tv2.setText("SOME TEST TEXT 2");
tv2.setTextColor(Color.BLACK);
LayoutParams params5 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params4.setMargins(100, 100, 100, 100);
tv2.setLayoutParams(params5);
another1.addView(tv2);
I expect margins to be applied, but they are not. What is the correct order of initializing such a view?
So, my trouble was in DP to PX converter that just wasn't initialized at that moment. However, to clarify everything, my example above is just an error (I didn't change params4 to params5 when adding the second TextView.
Also while testing everything, I found out that the order of adding layouts and their parameters doesn't matter at all.
You probably added child views to the parent view in onCreate().
In onCreate(), parent view does not have actual size.
If you have to do it in onCreate(), try following code.
final ViewTreeObserver observer = parent.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(100, 100, 100, 100);
TextView view = new TextView(context);
view.setLayoutParams(params);
parent.addView(view);
observer.removeOnGlobalLayoutListener(this);
}
};
Hope this will help!
Try this:
LinearLayout ll = findViewById(R.id.linearLayout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30,20,30,0);
Hope this will help!
I need to set TextView centered on a ImageButton, but using code. I can set a text on, but I can't center it.
code:
RelativeLayout rLayout = new RelativeLayout(this);
TextView textView = new TextView(this);
textView.setText("text");
textView.setTextSize(25);
textView.setGravity(Gravity.CENTER);
final ImageButton iButton = new ImageButton(this);
iButton.setImageResource(R.drawable.button);
iButton.setBackgroundColor(Color.TRANSPARENT);
rLayout.addView(iButton);
rLayout.addView(textView);
linear.addView(rLayout);
This code set text on imageButton, but set it in left-top.
It's kind of unclear what you're asking...but if I'm understanding correctly, you have to set the RelativeLayout.LayoutParams on the TextView before adding it to the parent. Also, you have to add the rule RelativeLayout.CENTER_IN_PARENT.
So, something like:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(size, size)
params.addRule(RelativeLayout.CENTER_IN_PARENT)
textView.setLayoutParams(params)
Or some other combination of rules that fit your usecase.
so try
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;
iButton.setLayoutParams(params);
I need to be able to add a layout to my main layout from xml file many times programatically.
The problem is handling assigning ids of the new layout's views. See the code:
MainFragment.java
private int belowOfWhat = R.id.layout_header;
...
onActivityResult:
LayoutInflater myInflater = LayoutInflater.from(getContext());
RelativeLayout layout = (RelativeLayout) myInflater.inflate(R.layout.assignment_layout, null, false);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//here problems start
//i want to position the new layout below the previously created layout
params.addRule(RelativeLayout.BELOW, belowOfWhat);
layout.setLayoutParams(params);
mRelativeLayout = rootView.findViewById(R.id.to_do_layout);
mRelativeLayout.addView(layout);
belowOfWhat = generateViewId();
idsOfToDos.add(belowOfWhat);
CheckBox assignmentCheckbox = (CheckBox)
//assignment_checkbox is an id of checkbox in the xml layout I add
rootView.findViewById(R.id.assignment_checkbox);
assignmentCheckbox.setId(belowOfWhat);
assignmentCheckbox.setText(mToDoInfo);
I don't know where the problem is, so here is how the app works now: I add a new layout, it gets positioned correctly below layout_header.
But when I add the second or more layouts they overlap each other on top of the app instead of being positioned one below the other.
I'd appreciate if you guided me to the solution of the problem.
If you don't need views'ids for other purposes you could solve more simple.
If I've understood what you are trying to do, what you need is a vertical LinearyLayout instead of a RelativeLayout, then subviews will be added one below each other
You have to use Relative Layout and use the following properties with that to align the views.
RelativeLayout.BELOW
RelativeLayout.RIGHT_OF
RelativeLayout.ALIGN_BOTTOM
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("textView1");
TextView tv2 = new TextView(this);
params2.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
tv2.setId(2);
tv2.setText("textView2");
TextView tv3 = new TextView(this);
params3.addRule(RelativeLayout.BELOW, tv1.getId());
tv3.setId(3);
tv3.setText("textView3");
TextView tv4 = new TextView(this);
params4.addRule(RelativeLayout.RIGHT_OF, tv3.getId());
params4.addRule(RelativeLayout.ALIGN_BOTTOM, tv3.getId());
tv4.setId(4);
tv4.setText("textView4");
layout.addView(tv1, params1);
layout.addView(tv2, params2);
layout.addView(tv3, params3);
layout.addView(tv4, params4);
hope so it gives you an idea how to align views pragmatically.
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.
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).