Retrieve data from dynamically created EditBox in android - java

I have created dynamic EditText boxes and spinner on Add button, but I don't know how to retrieve data from them. The whole code is available on this website.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
mText=(EditText) findViewById(R.id.number_edit_text);
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
parentLinearLayout.getChildAt(counter);
}
public void onDelete(View v) {
parentLinearLayout.removeView((View) v.getParent());
}

Try this, You can simply create loop and get all data which have added dynamically, like this :
Change your layout activity_main.xml because in demo they were doing wrong code for layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/parent_linear_layout"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="phone"/>
<Spinner
android:id="#+id/type_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:entries="#array/types"
android:gravity="right" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"
android:onClick="onDelete"/>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/add_field_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#555"
android:layout_gravity="center"
android:onClick="onAddField"
android:textColor="#FFF"
android:text="Add Field"
android:paddingLeft="5dp"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/result"/>
</LinearLayout>
Code for getting all values of edit Text :
int count = parentLinearLayout.getChildCount();
for(int i=0; i<count; i++) {
LinearLayout linearLayout = (LinearLayout)parentLinearLayout.getChildAt(i);
EditText editText = (EditText)linearLayout.getChildAt(0);
String result = editText.getText().toString();
//You can result get data from edit text.
}
For Spinner Value :
int count = parentLinearLayout.getChildCount();
for(int i=0; i<count; i++) {
LinearLayout linearLayout = (LinearLayout)parentLinearLayout.getChildAt(i);
Spinner spinnerField = (Spinner)linearLayout.getChildAt(1);
String result = spinnerField.getSelectedItem().toString();
//You can result get data from spinner value.
}

mText=(EditText) view.findViewById(R.id.number_edit_text);
Put this in onAddField method.
When you want to get the text from the ediText you need to call
mText.getText().toString()

Related

Android: How to display more views after clicking a button

SOLVED THE CODE BELOW WORKS
If I have a layout similar to the one below:
Those five temp EditTexts represent some info the user can enter (like the price of an item, the order number, etc.) If the user wants to add another item they would click on the Add button and I want another 5 textviews to appear on the screen right above the two buttons but right below the previous set of the 5 EditTexts. Can someone give me a starting point on how I would do this.
My layout of the fragment goes like this:
I have a top level linear layout (Vertical Orientation).
Then a scrollview.
Inside the scrollview I have another linear layout.
In that linear layout I have those five EditText objects and the two buttons
The view above is a fragment (defined in the file below) which I pass to my FragmentAdapter in my MainActivity file:
public class Device extends Fragment {
ScrollView scrollView;
LinearLayout ll;
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.device_view, container, false);
scrollView = (ScrollView) rootView.findViewById(R.id.device_scroll_view);
ll = (LinearLayout) rootView.findViewById(R.id.layout_in_scrollview);
Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View temp = inflater.inflate(R.layout.edit_text_view_objects, container, false);
ll.addView(temp);
}
});
return rootView;
}
}
Here is my layout file for this fragment:
<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:id="#+id/device_fragment_linear_layout"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/device_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp">
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Submit" />
<Button
android:id="#+id/add_another_device_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
edit_text_view_objects.xml:
<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="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name of Master Device"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Device Name"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Max SMS per day"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="15dp"
android:hint="Carrier Name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="OS Version"
android:layout_marginTop="10dp"/>
</LinearLayout>
in your XML take
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/device_scroll_view">
<LinearLayout
android:id="#+id/layoutDynamic"a
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
</LinearLayout>
</ScrollView>
and create one more XML for your Item and design accordingly (It will add dynamically when you click on ADD)
dynamic_item.xml
<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:id="#+id/device_fragment_linear_layout"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">
<TextView
android:id="#+id/etDynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="Color"
android:layout_marginTop="10dp"/>
</LinearLayout>
so come to java Code
// take the reference of LinearLayout
linearLayout = (LinearLayout) romptView.findViewById(R.id.layoutDynamic);
// Take the reference of Add button
Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final View addView = layoutInflater1.inflate(R.layout.dynamic_row, null);
final TextView textView1 = (TextView) addView.findViewById(R.id.etDynamic);
textView1.setText(otheret.getText().toString().trim()); otheret.setText(""); linearLayout.addView(addView);
}
});
#and if you want to remove particular item
removeIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addView.getParent()).removeView(addView);
}
});
You can add these EditText through code at runtime
we can solve this, using List/Recycler view efficiently/easily
when ever you clicking on ADD add the item to list and notify the Recycler view adapter
and and removing also easy you just delete the item from list based on recycler View position.
No need to create Dynamic View here recycler view will do the job
// In Main Layout
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView_Stops"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layoutSaveBottom"
android:layout_below="#+id/ll1"
android:layout_marginTop="#dimen/pad_10dp"
app:layout_behavior="#string/bottom_sheet_behavior" />
<TextView
android:id="#+id/tvAddOther"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="#string/icon_plus"
android:textColor="#color/colorPrimary"
android:textSize="40sp"
app:customFontPath="#string/roboto_regular" />
// design your layout item
---------------------
and when clicking on Add set user entered details
addOther.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RouteElements routeElements = new RouteElements();
routeElements.setLatitude(latitude);
routeElements.setLongitude(longitude);
routeElements.setStopName(otheret.getText().toString().trim());
routeElementsArrayList.add(routeElements);
routeStopsAdapter.notifyDataSetChanged();
});
And in Your Adapter for removing the item
public void remove(int position) {
if (position < routeElementsArrayList.size()) {
routeElementsArrayList.remove(position);
notifyDataSetChanged();
}
}

java.lang.IllegalStateException: HorizontalScrollView can host only one direct child

I want to realize the Gallery using HorizontalScrollView. But it throws java.lang.illegalStateException. The logcat hint that the HorizontalScrollView can host only one direct child. I only set one direct child - LinearLayout to it. I searched from the Internet, but haven't solved the problem. Hope anyone can help me. Belows are the code.
xml
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:scrollbars="none"
>
<LinearLayout
android:id="#+id/gallery_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
item xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="20dp"
>
<ImageView
android:id="#+id/trend_item_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:src="#drawable/gallery0"
/>
<TextView
android:id="#+id/trend_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="权利的游戏"
android:textSize="16sp"
/>
</LinearLayout>
Fragment
public class DiscoverFragment extends Fragment {
private int[] imageResources = {R.drawable.gallery0, R.drawable.gallery1, R.drawable.gallery2,
R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5};
private String[] imageDescriptions = {"权利的游戏", "风吹的风景", "插画背景", "美食君", "吃", "你好四月"};
private List<TrendItem> trendItemList;
private LinearLayout galleryLayout;
public DiscoverFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_discover, container, false);
ButterKnife.bind(this, view);
galleryLayout = ButterKnife.findById(view, R.id.gallery_layout);
initData();
setView();
return view;
}
private void initData(){
trendItemList = new ArrayList<>();
for (int i = 0; i < imageResources.length; i++){
TrendItem trendItem = new TrendItem();
trendItem.setImageResource(imageResources[i]);
trendItem.setText(imageDescriptions[i]);
trendItemList.add(trendItem);
}
}
private void setView(){
for (int i = 0; i < trendItemList.size(); i++){
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.trend_item, galleryLayout, false);
ImageView trendItemImageView = ButterKnife.findById(view, R.id.trend_item_imageview);
TextView trendItemTextView = ButterKnife.findById(view, R.id.trend_item_textview);
trendItemImageView.setImageResource(trendItemList.get(i).getImageResource());
trendItemTextView.setText(trendItemList.get(i).getText());
galleryLayout.addView(view);
}
}
}
Meaning, you have to add the ImageView, TextView to the Linearlayout. when you add the imageview you are adding it to the HorizontalScrollview which also has a LinearLayout in it their by adding 2 child elements to the HorizontalScrollView which you cannot do.
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:scrollbars="none">
<LinearLayout
android:id="#+id/gallery_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/trend_item_imageview"
android:layout_width="your_size"
android:layout_height="your_size"
android:src="#drawable/gallery0"/>
<TextView
android:id="#+id/trend_item_textview"
android:layout_width="your_size"
android:layout_height="your_size"
android:text="权利的游戏"
android:textSize="16sp"/>
</LinearLayout>
</HorizontalScrollView>

Programatically set layout_width of EditText in Android

So I have an application that dynamically adds a table row every time the "Add Row" button is pressed. The code attached is the code that runs when "Add Row" is pressed. What I am trying to do is have 3 EditTexts next to each other on the same row. In order to do that, I need to change the Layout_Width of each EditText so the first EditText doesn't cut the other two off the screen. I can't seem to figure out a way to properly do this and was wondering if anyone would help me out. After I figure out how to do that, the next step is to adjust the layout_width according to the screen size but thats later down the road. It needs to be done programmatically because a user can theoretically have as many rows as they want to.
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
tablerow = new TableRow(getActivity());
ed1 = new EditText(getActivity());
ed1.setInputType(InputType.TYPE_CLASS_TEXT);
ed2 = new EditText(getActivity());
ed2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
ed3 = new EditText(getActivity());
ed3.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
tablerow.addView(ed1);
tablerow.addView(ed2);
tablerow.addView(ed3);
table1.addView(tablerow);
}
};
I believe what you are looking for is LayoutParams and its "weight" value.
Try:
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f);
ed1 = new EditText(getActivity());
ed1.setInputType(InputType.TYPE_CLASS_TEXT);
ed1.setLayoutParams(params);
The third value (1.0f) in the LayoutParams constructor is a weight...all EditTexts in that TableRow should end up the same width.
You can try next approach, it is based on xml. You will be able to play around with layout at table_row.xml.
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.a_mn_button_add_row:
onClickButtonAddRow();
break;
}
}
private void onClickButtonAddRow() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.a_mn_table);
tableLayout.addView(View.inflate(this, R.layout.table_row, null));
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/a_mn_button_add_row"
android:onClick="onClick"
android:text="Add row"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="#+id/a_mn_table"
android:stretchColumns="0,1,2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
table_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
table_row.xml for use case with 3-1-2 proportions.
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:layout_weight="1"
android:inputType="text"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<EditText
android:layout_weight="2"
android:inputType="text"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</TableRow>

Get data from programmatically added view

Could somebody help me?
For example, there is the following layout:
main.xml:
<LinearLayout
android:id="#+id/linearLayout_item"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout_item_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
... Other views, skipped ...
</LinearLayout>
<Button
android:id="#+id/button_add_alias"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_commit"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I use button_add_alias to add views as they needed.
((Button) findViewById(R.id.button_add_alias))
.setOnClickListener(new View.OnClickListener() {
int position = 1;
public void onClick (View v) {
View view = getLayoutInflater().inflate(R.layout.alias, null);
((LinearLayout) findViewById(R.id.linearLayout_item)).addView(view, position);
position++;
}
});
alias.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout_alias"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText_alias"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
... Other views, skipped ...
</LinearLayout>
So, what is the best way to get data from programmatically added editText_alias views?
Firstly, iterate through your LinearLayout using getChildAt().
Then, retrieve the first child (which in your case will be the EditText) from your child view.
LinearLayout parent = (LinearLayout) findViewById(R.id.linearLayout_item);
for (int i = 0; i != parent.getChildCount(); i++) {
if (parent.getChildAt(i).getId() == R.id.linearLayout_alias) {
LinearLayout alias = (LinearLayout) parent.getChildAt(i);
EditText editAlias = (EditText) alias.getChildAt(0);
// do what you want with the EditText
}
}
If I understand what you are trying to do in order to grab the edit text you should be able to call
view.findViewById(R.id.editText_alias)

Change textview dynamically in Viewflipper

pretty easy question here, but I am not a java superuser quite yet.
I am using a ViewFlipper to provide a number of images, and a sliding-drawer to contain text specific to each image. I would like to have the text of the sliding drawer change to a specific string dependent on which child view is currently displayed.
Here is the XML:
<?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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/image1"></ImageView>
<ImageView android:id="#+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/image2"></ImageView>
</ViewFlipper>
</LinearLayout>
here is the java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper = (ViewFlipper)findViewById(R.id.flipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
imageview1 = (ImageView)findViewById(R.id.imageView1);
imageview2 = (ImageView)findViewById(R.id.imageView2);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == next) {
flipper.setInAnimation(inFromRightAnimation());
flipper.setOutAnimation(outToLeftAnimation());
flipper.showNext();
}
if (v == previous) {
flipper.setInAnimation(inFromLeftAnimation());
flipper.setOutAnimation(outToRightAnimation());
flipper.showPrevious();
}
}
I'm sure the answer is really obvious, but that's why I need your help....THANKS!
When you call showNext() and showPrevious(), update your TextView to match.

Categories