Dynamically added TextInputLayout is not shown in LinearLayout - android

I am adding a View in a LinearLayout, this way:
LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayout);
TextInputLayout til=new TextInputLayout(MainActivity.this);
til.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
Log.i("TAG","BEFORE: "+ll.getChildCount());
ll.addView(til);
Log.i("TAG","AFTER: "+ll.getChildCount());
This way, the til object is not shown, but it IS added, because in the second log I see the number has incremented by one, the view I just added.
Why can't I see it? how to show the new view in the layout?
Thank you.

I think what you are forgetting is to add an EditText or a TextInputEditText to the TextInputLayout.
The android documentation says:
[TextInputLayout is a] Layout which wraps an EditText (or descendant) to show a floating label when the hint is hidden due to the user inputting text.
EditText editText = new EditText(this);
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(editTextParams);
editText.setTextSize(16);
editText.setTextColor(getResources().getColor(R.color.black));
editText.setHint("hint");
editText.setHintTextColor(getResources().getColor(R.color.gray));
textInputLayout.addView(editText, editTextParams);

TextInputLayout is nothing without having EditText as child.
Create a layout file text_input_layout.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
Now, to add, simply inflate this layout and addView() to LinearLayout as below:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
TextInputLayout textInputLayout = (TextInputLayout) LayoutInflator.from(this).inflate(R.layout.text_input_layout, null);
linearLayout.addView(textInputLayout);
Hope this helps...
Note: You can also have TextInputEditText as Child of TextInputLayout

The til has zero height. Use this instead:
til.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));

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.

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.

TextView write text vertically when setting its layoutparams to fill_parent

I am trying to add TextView into a view which extends LinearLayout. When I set its width of layoutparams to fill_parent or wrap_content, the text inside TextView will display vertically such like the following.
e.g.
T
E
X
T
However, when I set the width to some fixed number, it will display normally or as I expected.
e.g. TEXT
My question is why this happens and how to solve it, i.e. how I should set programmatically so that TextView can write text horizontally without setting a fixed width to it such as setting fill_parent or wrap_content?
Here is setting of XML code of the parent ilGallery which extends LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.illib.ilgallery.view.ILGallery
android:id="#+id/galleryLayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
The following is the code how i initialize the children inside it:
ilViewPager = new ILViewPager(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
ilViewPager.setLayoutParams(params);
ilgallery = this;
//initialize default header and footer view
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
headerView = new TextView(context);
headerView.setLayoutParams(params2);
((TextView)headerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)headerView).setText("hello");
footerView = new TextView(context);
footerView.setLayoutParams(params2);
((TextView)footerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)footerView).setText("hello2");
There appears to be only a single item inside your LinearLayout. Please set the orientation of the LinearLayout to "horizontal", like this:
android:orientation="horizontal"
Also, whenever possible instead of creating new LayoutParams objects from scratch, you should get the LayoutParams from the current layout and modify that, like this:
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)ilViewPager.getLayoutParams();
lp.<change-any-values>;
ilViewPager.setLayoutParams(lp);

Adding EditText to custom LinearLayout

I recently updated a custom view so I could potentially add an EditText to the center of my canvas (so to speak).
After adding my onDraw code to dispatchDraw my custom LinearLayout works in the same way as my previous custom view did.
Now, how can I add an EditText smack in the middle of the layout?
So far I am trying this:
EditText edit = new EditText(getContext());
edit.setText("My EditText");
edit.setTextSize((int)Math.ceil(thickness/2));
edit.setWidth((int)(diameter*0.07f));
edit.setX(centerX);
edit.setY(centerY);
addView(edit);
Forgive some of the variables, they are not too important but I'm trying to add the EditText using the X and Y coordinates.
Thanks for any help.
UPDATE:
I have updated my LinearLayout constructor to inflate the comment_edit.xml file to see if I could get it to work this way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionDone" />
</LinearLayout>
The LinearLayout constructor extract is as follows:
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout view = (LinearLayout) inflater.inflate(R.layout.comment_edit, this, false);
EditText edit = (EditText) view.findViewById(R.id.edittext_comment);
edit.setText("Add Comment");
edit.setX(112);
edit.setY(117);
addView(view);
I have also tried:
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout view = (LinearLayout) inflater.inflate(R.layout.comment_edit, this, false);
EditText edit = (EditText) view.findViewById(R.id.edittext_comment);
edit.setText("Add Comment");
view.setX(112);
view.setY(117);
addView(view);
The EditText still does not appear
In case the answer is not found yet. Every dynamically created view should have layout parameters (WRAP_CONTENT, FILL_PARENT, etc) that is why is not shown. For more information please edit your question accordingly.
An example might be:
editText.setLayoutParams(new LayoutParams(WRAP_CONTENT,WRAP_CONTENT));
EDIT:
Now your problem is that the edit text is not centered. Ok, I can give you an overview but I believe you must research from now on because there are a lot of things to do. First of all the LinearLayout is what it says, is a layout that it stacks the views vertically or horizontally. The position of the text shouldn't be given with the actual pixels of the screen but with some other specific layout parameters. Take Gravity for example, it aligns the inner view of a layout to the center, left, etc. Add gravity property of the LinearLayout.
I hope it helped

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