Set gravity of dynamic textview - android

I have created a text view dynamically.I want to set its gravity to right.I have used textview.setGravity(Gravity.RIGHT) but its not working.When I am using it the text view is aligned to left direction only.How can I set the gravity of textview?I dont want to use setPadding().
textMore[i]=new TextView(this);
textMore[i].setGravity(Gravity.Right);
textMore[i].setText("more....");
textMore[i] .setTextColor(Color.BLUE);

you can't change alignment of TextView by setGravity , it changes the TextView contents alignment.

You can do
LinearLayout.LayoutParams textParams = (LinearLayout.LayoutParams) textMore[i].getLayoutParams();
textParams.gravity = Gravity.RIGHT;
textMore[i].setLayoutParams(textParams);
and then add the view in your dynamic LinearLayout

If the parent of your text view is RelativeLayout(Prefer this)
set : alignParentRight
If the parent of your text view is LinearLayout
set : setLayoutGravity(Gravity.Right)
or
LinearLayout.LayoutParams textParams = (LinearLayout.LayoutParams)textMore[i].getLayoutParams();
textParams.gravity = Gravity.RIGHT;
textMore[i].setLayoutParams(textParams);
setGravity(Gravity.Right) ==> This property will align the text inside your textView, rather than aligning the view in its parent.

Firstly set your textView Width property to fill parent then you can set it gravity to right
if you are using linear layout

Related

TextView and EditText cut off in LinearLayout

I have a LinearLayout with horizontal orientation in which I add some TextView and EditText dynamically. My problem is when the TextView is long, it is cut off (by height) and the EditText is invisible.
Adding padding to the views didn't solve it. When setting the LinearLayout minHeight value, the TextView is displayed correctly but the EditText is still invisible.
<LinearLayout
android:id="#+id/content_content"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"></LinearLayout>
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < cntCount; i++) {
TextView cntView = new TextView(getActivity());
cntView.setLayoutParams(params);
cntView.setText(cnt.get(i));
contentContentView.addView(cntView);
EditText answerView = new EditText(getActivity());
answerView.setLayoutParams(params);
answerView.setId(i);
answerView.setHint("......");
contentContentView.addView(answerView);
}
EDIT
I have found the solution and created a library. For those interested you can download it here
Your LinearLayout has an orientation of "horizontal" - it will add the next view to the right of the current view. This means that if the TextView wraps (fills the parent) there is no room for any additional views in the LinearLayout that will be visible on the screen (they will be added to the right of the visible layout).
If you are trying to place an EditText in the same line with a series of TextViews, then you will need to create a custom TextView and layout. This requires doing measurements about the placement and size of the EditText that are not standard with Android.
This post may help: How to overlay EditText on TextView just after the end of text
You can set up layout_weights for the EditText and the TextView instead of using wrap_content for the layout_width. That would make sure that the EditText will always be shown.

add drawable in relative layout programmatically

I have Image in my layout and I want when user clicked on image, 4 drawable (corner button) added in corner of the image. How can I do this?
Add id field to the relative layout in the xml. Use that id in the class to create view dynamically.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.YourID);
imageView.setImageDrawable(R.drawable.image);
//Add View to Layout:
relativeLayout.addView(imageView);
well there are many ways to that.
Using layoutparams you can set layout params programatically.
like
LayoutParams param = new LayoutParam(LayoutPara.width, LayoutParam.height);
//your rules...
imageView.setlayoutParams(param);
here you can set margintop, bottom, left and right and alignParentLeft.... so on

What is difference between .setGravity(...) method and getLayoutParams().gravity=x?

If you want to set the gravity of a View like LinearLayout programmatically, you have 2 ways:
1)
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
MyLinearLayout.setLayoutParams(params);
2)
MyLinearLayout.setGravity(Gravity.RIGHT);
What is difference between these 2 ways?
It is important you know the difference.
In the first way you are setting that layout gravity of your LinearLayout. It means you are setting the position of your layout in its parent View. It is equivalent to android:layout_gravity="right" in xml layouts.
But in the second way you are setting the position of child views in your Linearlayout and it is equivalent to android:gravity="right" in xml layouts. For example if you put a TextView in your LinearLayout and its width was wrap_content, the TextView will be place in the right side of your LinearLayout.

Setting RelativeLayout.ALIGN_PARENT_LEFT to LinearLayout

How can I set the RelativeLayout.ALIGN_PARENT_LEFT to LinearLayout in code? I have a RelativeLayout which child is LinearLayout and I need to set the RelativeLayout.ALIGN_PARENT_LEFT in the child LinearLayout instance. The problem is that when I set it, I will lose the child LinearLayout parameters (I will lose the weight). My understanding is that the only way to set the RelativeLayout.ALIGN_PARENT_LEFT to child element is to use RelativeLayout.Layoutparams when setting the child view layout parameters.
Thanks in advance!!
You have to set the width height parms and weight along with your aligh left rule in programtically
layoutparms = new LayoutParams(WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT, weightthere);
layoutparms.addRule(RelativeLayout.LEFT_OF, leftisdeview);
yourview.setLayoutParams(layoutparms);
You just have to give android:gravity="left" to the parent linear layout.Remember that it is gravity not layout_gravity.

Why my edit text is not taking gravity...?

I am adding one edit text pro-grammatically, in that i am setting the gravity but its not reflecting.
code:
EditText bcc = new EditText(getApplicationContext());
LayoutParams para = new LayoutParams(LayoutParams.FILL_PARENT, 45);
//bcc.setBackgroundColor(Color.parseColor("#00000000"));
bcc.setTextColor(Color.parseColor("#000000"));
bcc.setSingleLine(true);
para.setMargins(0, 0, 0, 5); // left, top, right, bottom.
bcc.setTextSize(15);
bcc.setGravity(Gravity.BOTTOM);
bcc.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
bcc.setId(100);
bcc.setLayoutParams(para);
This gravity bcc.setGravity(Gravity.BOTTOM); marks only how text should lay inside EditText.
If parent of EditText is RelativeLayout you can provide rules inside RelativeLayout.LayoutParams.
set gravity of parent of the view. If view parent is layout then the code will be like the following
((LinearLayout) bcc.getParent()).setGravity(Gravity.CENTER_VERTICAL);
Eventually you would be adding this EditText bcc to a view group? Depending on what type of ViewGroup the parent is, you would need to do the following:
LinearLayout:
via XML:
You have to set android:layout_gravity="center_vertical".
via code:
LinearLayout.LayoutParams lp = viewGroup.getLayoutParams();
lp.gravity = Gravity.CENTER_VERTICAL;
viewGroup.setLayoutParams(lp);
The code will be different for different parent layout types.
When you creating EditText programmatically, you must at first set setKeyListener(TextKeyListener.getInstance());
Otherwise your view will always be aligned with Gravity.TOP.
I don't know real reason, but before you specifiy any other parameter to EditText, you must set setKeyListener(TextKeyListener.getInstance());
Correction : It only work if you create you custom widget by extending EditText and defining your widget in XML. Only tested on Android 5.0.1

Categories

Resources