In my Activity, dynamically insert a layout that contains an EditText. The layout can have between one and N EditText. How can I get the text of all EditText ?
LinearLayout item = (LinearLayout) findViewById(R.id.rl);
final View child = getLayoutInflater().inflate(R.layout.inflate_ta, null);
item.addView(child);
MaterialEditText ed = (MaterialEditText) child.findViewById(R.id.ed12);
ed.setFloatingLabelText(edNomeCampo.getText().toString());
The best approach would be to go through each view within whatever parent view you're dynamically adding to.
so...
<LinearLayout
android:id="#+id/wrapper"..>
<EditText ... />
<EditText... />
...
...
<!--Nth EditText-->
<EditText... />
</LinearLayout>
Then once you want to get the values in each dynamically added EditText you would iterate through all views within that wrapping LinearLayout
LinearLayout yourLinearLayoutView = (LinearLayout)getView().findViewById(R.id.wrapper);
for(int i=0; i<yourLinearLayoutView.getChildCount(); i++) {
View editText= yourLinearLayoutView.getChildAt(i);
if(editText instanceof EditText){
String str = ((EditText)editText).getText();
//from here you can store str in whatever structure you wish (AWrrayList, etc.)
}
}
Related
In my RecylcerAdapter, I want to duplicate a TextView.
I have a LinearLayout that contains a TexView. All I want is to dynamically duplicate this TextView inside the LinearLayout, so that I have 10 TextViews inside the LinearLayout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tags_ll"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:gravity="right">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tags"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="عمومی"
android:layout_margin="5dp"
android:padding="5dp"
android:background="#drawable/border_with_background"
android:textSize="12dp" />
</LinearLayout>
MyViewHolder in RecyclerView:
public MyViewHolder(View view) {
super(view);
tags = (TextView) view.findViewById(R.id.tags);
tags_ll = (LinearLayout) view.findViewById(R.id.tags_ll);
for (int i = 0; i < 10; i++) {
TextView rowTextView = new TextView(view.getContext());
//Clone the new textview, get all the properties of the existing textview
rowTextView = tags;
rowTextView.setText("This is row #" + i);
tags_ll.addView(rowTextView);
}
}
I get the following error:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I want all the properties of the existing textview to be copied to the newly created TextViews.
The reason for this error is because,
tags = (TextView) view.findViewById(R.id.tags);
TextView rowTextView = new TextView(view.getContext());
rowTextView = tags;
tags_ll.addView(rowTextView); // tag.ll has already a child with id = tag
When you copy the reference of that object to your new TextView "rowTextView" now both tags, and rowTextView refer to same Object which is already present in tag.ll LinearLayout, Which is why its throws an error.
If your need is just to implement a list with recyclerViewAdapter, than you are making it very complex, no need to get a TextView from the context when you are just going to assign a new reference to that TextView variable.
If you have latest AndroidStudio installed you can just create a new fragment with list, and Android Studio will provide you with a good example how to use RecyclerView Adapter to populate List. or view this link
http://hmkcode.com/android-simple-recyclerview-widget-example/
Hope i helped.
What you're doing is wrong, you can't copy the TextView properties with rowTextView = tags;, you're just replacing rowTextView TextView with tags one.
You need to set the new TextView properties from your code:
for (int i = 0; i < 10; i++) {
TextView rowTextView = new TextView(view.getContext());
//set the new TextView properties from code
rowTextView.setText("This is row #" + i);
tags_ll.addView(rowTextView);
}
Or
You can create an xml layout containing just a TextView with the desired attributes, and inflate it in your for-loop.
eg:
my_text_view.xml :
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="عمومی"
android:layout_margin="5dp"
android:padding="5dp"
android:background="#drawable/border_with_background"
android:textSize="12dp" />
In your ViewHolder :
LayoutInflater inflater = LayoutInflater.from(context);
for (int i = 0; i < 10; i++) {
TextView rowTextView= (TextView) inflater.inflate(R.layout.my_text_view, null, false);
rowTextView.setText("This is row #" + i);
tags_ll.addView(rowTextView);
}
Your approach is wrong - this line:
rowTextView = tags;
will not copy the first TextView. This will make the new reference to point to the original textView and then you will find yourself adding the original textView in the layout again, causing the error.
What you should do is create your new textView and add its properties by using the original textView like this:
for (int i = 0; i < 10; i++) {
TextView rowTextView = new TextView(view.getContext());
rowTextView.setText(tags.getText());
rowTextView.setGravity(tags.getGravity());
//set all other properties like this: rowTextView.setOther(tags.getOther())...
tags_ll.addView(rowTextView)
}
I am new to android. Now i am need to know how to get text values of all edittext which has added dynamically when user press add button.
add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
table = (TableLayout) findViewById(R.id.table);
row = new TableRow(Material.this);
table.setGravity(Gravity.CENTER);
edit = new EditText(Material.this);
edit.setWidth(135);
edit.setHeight(35);
edit.setBackgroundColor(Color.WHITE);
row.addView(edit);
table.addView(row);
Here how to get text values of edittext? User may add more than one edittext. At that case, how to get all text?
If EditTexts are dynamically added maintain the references of EditText in an array and when ever you want text from them iterate the array.
EditTexts are created dynamically with a reference use that reference to get the value of it.
For Example
I have created an editText.
EditText myTextBox = new EditText(getBaseContext());
containerLayout.addView(myTextBox);
Here, myTextBox would be the variable which refers to that editText, you could use getText() to get the value of that box or you could use setText() to set the value to that box.
String value = myTextBox.getText().toString();
myTextBox.setText("this is the setted text");`
EDIT
If there are more than one editText then add all the references in an array and iterate over them.
List<EditText> myArray = new ArrayList();
EditText editText1 = new EditText(getBaseContext());
containerLayout.addView(editText1);
myArray.add(editText1);
EditText editText2 = new EditText(getBaseContext());
containerLayout.addView(editText2);
myArray.add(editText2);
EditText editText3 = new EditText(getBaseContext());
containerLayout.addView(editText3);
myArray.add(editText3);
int i = 0;
while (i < myArray.size()) {
Log.i(TAG,myArray.get(i).getText());
i++;
}
If you have created edit text and text is set in edit text then:
String text = edittext.getText().toString();
would be okay.
You will need to keep reference of that dynamically added EditText.
And call getText().toString(); on that reference.
One other approach is to set some ids using setId(); keep record of those ids and whenever required.
((EditText)findViewById(edittextidyouset)).getText().toString();
Take look at this thread setting ids dynamically : How can I assign an ID to a view programmatically?
Hope this can help you-
ArrayList<EditText> editTexts = new ArrayList<>();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 20; i++) {
EditText editText = new EditText(this);
editTexts.add(editText);
editText.setText("I am at " + i);
linearLayout.addView(editText);
}
for (EditText editText : editTexts) {
Log.d("Texts", editText.getText().toString());
}
Your xml file can be like-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/container"
android:orientation="vertical"
tools:context="com.hestabit.activity.Home">
</LinearLayout>
I would like to be able to for loop in my layout and add text to the textviews dynamically, this does not error out but I get the lost row in the display for example
Tw04 One4
I would like to be able to display
One1 Two1
One2 Two2
etc...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rel_layout);
for (int i = 0; i < 5; i++) {
TextView woTxt = (TextView) findViewById(R.id.ticker_price);
woTxt.setText("One"+i);
TextView cusTxt = (TextView) findViewById(R.id.ticker_symbol);
cusTxt.setText("Two"+i);
}
}
You may add TextViews programmatically to your layout as below :
TextView [] txt1 =new TextView[5];
for(int i=0;i<5;i++)
{
txt1[i]=new TextView(YourActivity.this);
txt1[i].setText("One"+i);
txt1[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
linear.addView(txt1[i]);
}
where linear is a LinearLayout from your layout.
You have only two text views, where in fact you need 10 on your example, one text view for each item you want to display. I suggest that you make a ListView instead, where each line of the list will be a couple of text views.
Hi I'm trying to create the layout dynamically based on my data model. But I have some problem doing that. The xml file I have is here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#f9f9ff">
<TextView
android:layout_width="97dp"
android:layout_height="33dp"
android:id="#+id/textView" android:layout_alignParentLeft="true" android:layout_marginLeft="15dp"
android:layout_alignParentTop="true" android:layout_marginTop="19dp"/>
</RelativeLayout>
This is named as dynamic.xml. And I'm trying to inflate this layout at run-time. For that I'm doing:
ViewGroup parent = (ViewGroup) findViewById(R.id.vertical_container);
//getting the TextView and EditText view.
LayoutInflater inflater = this.getLayoutInflater();
View currentView = inflater.inflate(R.layout.dynamic,null);
TextView textView = (TextView) currentView.findViewById(R.id.textView);
//trying to change the attributes. but #fails!
textView.setText("Setting the text from code");
textView.setId(3);
//setting to the view
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.dynamic, null);
parent.addView(view);
but this doesn't seems to be working! The line :
textView.setText("Setting the text from code");
doesn't work. I can see only the empty text in my emulator but not the text Setting the text from code. Not sure where I'm making the mistake. The changes that I made in my code are not seen in my emulator.
I can see only the dynamic xml layout rendered with its attributes specified!
Not sure where I'm making the mistake. Is it possible to achieve what I'm doing here? Thanks in advance.
I think you are setting wrong view for getting result .No need of twice inflation of layout
//getting the TextView and EditText view.
LayoutInflater inflater = this.getLayoutInflater();
View currentView = inflater.inflate(R.layout.dynamic,null);
TextView textView = (TextView) currentView.findViewById(R.id.textView);
//trying to change the attributes. but #fails!
textView.setText("Setting the text from code");
textView.setId(3);
//Now you had customize your Textview .Now just add this customize view to your view
//setting to the view
parent.addView(currentView);
You are currently adding the
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.dynamic, null);
to
parent.addView(view);
But you use textView.setText("Setting the text from code");
from this
View currentView = inflater.inflate(R.layout.dynamic,null);
You are not adding the currentView to the parent
Try this parent.addView(currentView);
If I have a page whose layout is designated by XML code, but I want to possibly create a few radio buttons, say, in the middle, but decide that at runtime, how would I do it? I'm new to Android so I'm taking a stab in the dark. Would something like this work?
In the XML, add a LinearLayout to the middle of the page's XML like this:
<LinearLayout android:id="#+id/LinLayBut"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
And then in the java something like this:
public void setupRadioButtons(){
LinearLayout linLay;
linLay = (LinearLayout) findViewById(R.id.LinLayBut);
RadioGroup radGroup = new RadioGroup(this);
RadioButton radBut = new RadioButton(this);
radGroup.addView(radBut, 0);
radGroup.setText("A button");
}
This is not an efficient way to build dynamic UI. You would be better off defining the optional layout in an XML file and then inflate it when you want to use it:
public void setupRadioButtons() {
final LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout buttons =
(LinearLayout) inflater.inflate(R.layout.LinLayBut, null);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
mainLayout.addView(buttons);
}
The above code assumes that the radio group and buttons are defined inside the LinearLayout with id LinLayBut and you main layout id is main.
OK, thanks to unluddite, I got it to work. For those tortured souls following the thread, here's the code. The XML doesn't have a layout around it. And if I don't call the method, the radio group takes no vertical space:
<RadioGroup android:id="#+id/radButGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
and here's the method:
public void setupRadioButtons(){
RadioGroup radGroup;
radGroup = (RadioGroup) findViewById(R.id.radButGroup);
RadioButton radBut0 = new RadioButton(this);
radGroup.addView(radBut0, 0); //2nd arg must match order of buttons
radBut0.setText("one Button");
RadioButton radBut1 = new RadioButton(this);
radGroup.addView(radBut1, 1);
radBut1.setText("Two Button");
radBut1.setChecked(true); //which button is set