TextView and EditText cut off in LinearLayout - android

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.

Related

Place 2+ Textfields side-by-side in a LinearLayout (horizontal) programmatically

i know, there are a lot of questions like this. I read a lot on stackoverflow and google about this topic, but nothing help me :(
Ok, here is the problem. I have a small app. In this app i have a fragment. The layout.xml for this fragment includes a placeholder linearlayout like the following
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/placeholderLinLayout">
</LinearLayout>
The fragment has a button. If u click on it a DialogFragmentPopup opens and u can enter some data-stuff. After you enter the data you can click on another button on this dialog and the data will be transfere to the main-fragment. Here i call a method which should generate programmatically a layout to present the data. I use the following code
myRoot = (LinearLayout) view.findViewById(R.id.placeholderLinLayout);
innerLayout = new LinearLayout(view.getContext());
innerLayout.setOrientation(LinearLayout.VERTICAL);
innerLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout productHeaderLayout = new LinearLayout(getContext());
productHeaderLayout.setOrientation(LinearLayout.HORIZONTAL);
productHeaderLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
TextView product_header = new TextView(getContext());
product_header.setText("Produkt");
product_header.setLayoutParams(layoutParams);
TextView amount_header = new TextView(getContext());
amount_header.setText("Menge");
amount_header.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
amount_header.setLayoutParams(layoutParams);
TextView packaging_header = new TextView(getContext());
packaging_header.setText("Verpackung");
packaging_header.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
packaging_header.setLayoutParams(layoutParams);
TextView price_header = new TextView(getContext());
price_header.setText("Preis");
price_header.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
price_header.setLayoutParams(layoutParams);
TextView payment_header = new TextView(getContext());
payment_header.setText("Zahlart");
payment_header.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
payment_header.setLayoutParams(layoutParams);
productHeaderLayout.addView(product_header);
productHeaderLayout.addView(amount_header);
productHeaderLayout.addView(packaging_header);
productHeaderLayout.addView(price_header);
productHeaderLayout.addView(payment_header);
innerLayout.addView(productHeaderLayout);
The problem is, that the first textview push all other textviews out of the visible space, see the screenshot
What i want to do is, that these 5 textviews spread out automatically to the existing width. I googled a lot and the code i post here is the result of which i found many times on the internet.
So i hope someone can help find out the problem in my code :)
Greetings
Set all your TextView layout paramters to this:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
And remove .setWidth(ViewGroup.LayoutParams.MATCH_PARENT); from all the TextViews.
This will guarantee that all the views will have same weight set to them, and that weight gives all the views in LinearLayout same Width (or Height if orientation is set to vertical).
The issue is that your setting the TextView to MATCH_PARENT in its width. So one TextView takes the whole screen and the other starts just out of it. To solve this set the layoutparam width to WRAP_CONTENT.
Better yet, if you want to spread it, you can use the LinearLayout's weight property so they take as much space as they can:
textview.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
The third parameter 1f is the weight. A weight of one means it'll take all the available space without intruding on the other children, hence they will all spread evenly.
If you want to have your TextViews side by side, you must set the orientation of the LinearLayout to horizontal instead of vertical

Set gravity of dynamic textview

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

Formatting Dynamic Edittext Android

ANDROID
I have a layout defined in xml and have a static textview, edittext and checkboxes which are all formatted as below:
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="2"
android:singleLine="true"
android:textSize="14dp"
android:width="180dp"
I add textview, edittext and checkbox dynamically. I need the new added ones to have the same visual display as the ones already present(static ones) on the layout! Could someone guide or point me how to go about it?
You can add the layout by inflating each time you need textview, edittext and chekbox.This way your layout will have same look.Because you are reusing your static layout in xml.
See Layout Inflating for details
You have a layout with textview,edittext and checkbox.Now you will use the layout as a view.Like you said for each row you have to inflate the layout and add it to the row.So for every row you will have the copy of the layout.
TableLayout layout=(TableLayout)findViewById(R.id.tblLayout);
Now you can add the view after inflating the layout
View rowView = inflater.inflate(R.layout.row_layout, null, true);
TableRow tblrow=new TableRow(this);
tblRow.add(rowView);
layout.add(tblRow);
You can create any control Dynamic like this way.
Here i show for Edit Text same way you can do for others.
EditText et = new EditText(this);
et.setText("");
et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Use the following code for dynamic configurations about EditText.
EditText my_edit = (EditText) findViewById(R.id.my_edit);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 2.0f;
my_edit.setLayoutParams(params);
my_edit.setTextSize(20);
my_edit.setWidth(180);
my_edit.setSingleLine(true);
For this you must need LinearLayout as a parent of your EditText.
I hope this helps you somehow.
Thanks.

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

Layout Views in LinearLayout Programmatically

I am trying to position a TextView and an ImageView within a LinearLayout programmatically. The problem is that the textview is always on top of the imageview, and I would like to it be underneath. I cannot find a way to imitate the android:layout_below= xml attribute in the java UI approach.
You should simply switch the order of your two views.
For example:
linear.addView(image);
linear.addView(text);
instead of:
linear.addView(text);
linear.addView(image);
LinearLayout lin = new LinearLayout(context);
lin.setOrientation(LinearLayout.VERTICAL);
ImageView imageView = new ImageView(context);
TextView textView = new TextView(context);
lin.addView(imageView);
lin.addView(textView);
the first component ImageView should be placed in the LinearLayout first, before the TextView.
Edit: oops forgot about "programmatically"

Categories

Resources