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.
Related
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));
I am trying to add an editText dynamically to a relative layout. the layout contains an editText already. I need to add the new editText below the existing one.
EditText designation1 = new EditText(context);
designation1.setHint("designation");
designation1.setSingleLine(true);
and my layout is
layoutExp =
(RelativeLayout) childView.findViewById(R.id.relative_layout_edit_exp);
and my existing edit Text is
designation = (EditText)childView.findViewById(R.id.editTextDesignatn);
You need to use RelativeLayout.LayoutParams and specifiy the relative position with the addRule(RelativeLayout.BELOW, ...) (this is the programmatic equivalent for android:below XML attribute):
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.editTextDesignatn);
layoutExp.addView(designation1, params);
It will be possible if You set for example a LinearLayout under Your existing EditText. Give an ID to this LinearLayout and then You could put Your dynamic EditText to this LinearLayout like
YourLinearLayout.addView(YourDynamicEditText);
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
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
I am developping an android app which downloads an xml and displays a layout with a number of edittexts, checkboxes, spinners, etc. added dynamically like this:
LinearLayout ll = new LinearLayout(this);
EditText nameField = new EditText(this);
ll.addView(nameField);
ScrollView sv = new ScrollView(this);
sv.addView(ll);
setContentView(sv);
I'm having trouble with setting some properties to an EditText added this way. For examle android:maxLength attribute can easily be set in an xml layout but I found no method to do the same in the java code.
How can I do it when hawing to add dynamically?
Thanks,
Zoltán from Hungary
If you look at the XML attributes in the docs, it lists the corresponding method you can call in your java code for each attribute
So for example setting the maxLength attribute can be accomplished through the setFilters(InputFilter) method.