I am working in Android Studio in a new app and I want to create a layout with two independent scrolled listview (each listview scrolling in independent way) in the same activity. I cannot obtain the target that I want. Could you give me some advice?
I am not sure about efficient way but I worked in following way.
First, I have created two dynamic ListView and put in into one activity.
It will calculate the height dynamically rather than match_parent.
Here is the code:
public void setListViewDynamicHeight(ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
int height = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
height += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
layoutParams.height = height + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(layoutParams);
listView.requestLayout();
}
You can build it in your layout xml like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll_view_1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hallo!" />
<!--Other views inside NestedScrollView-->
</android.support.v4.widget.NestedScrollView>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll_view_2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hallo!" />
<!--Other views inside NestedScrollView-->
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
Related
I wish to add a custom tailored layout for each ListView item. I have an empty linear layout in the xml layout file for ListView item layout. It has horizontal orientation.
I wish to add three FrameLayouts dynamically to above mentioned LinearLayout and set custom weight for each FrameLayout.
I found a part of the solution for it, as in the code below. Problem arises when the ListView has to be updated. ListView is initially hidden. When a certain button is pressed, ListView gets updated (hash map cleared and then filled with a new data set) and its visibility set from GONE to VISIBLE. Next thing is to loop through all the items and add designed view programmatically.
If I invoke the method add_designed_views(null); right after updating ListView items, the app loops through items, creates all the new views and adds them to right items. However, they are not added. I know this because of check if the item is already added, and if I invoke this method several times in a row, it will do the same each time, as if it didn't add anything at all.
Next thing that I tried is I postponed adding views dynamically by invoking the method add_designed_views from onClick of a dummy button. When I see the list has loaded (without the custom views) I press the dummy button and the custom views appear. But every 6th is missing. If I press it again, the missing ones appear. Which makes me even more confused.
It's obvious that the views can't be added immediately after changing the visibility of ListView. Probably because it takes the system some time to inflate the ListView, or it does some background task, but I couldn't find any listener or some function that tells me that it has finished loading so that I can add my views then.
Custom ListView item layout (xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="110dp"
android:background="#drawable/textview_border_dark_line"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="21"
android:minHeight="40dp">
<TextView
android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="15dp"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="70"
android:minHeight="90dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/path_model"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:orientation="horizontal"
android:onClick="onClick_listItem"
>
<!-- Path Model. -->
<!-- This is where I add view dynamically. -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:orientation="vertical">
<TextView
android:id="#+id/text2"
android:textSize="12dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="50"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_time"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/clock"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/text4_1"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_time" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_changes"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/change"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/text4_2"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_changes"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_cost"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/coins"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/text4_3"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_cost"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="21"
android:minHeight="38dp">
<TextView
android:id="#+id/text3"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="15dp"
/>
</RelativeLayout>
</LinearLayout>
add_designed_views method (Java):
public void add_designed_views(View view)
{
for(int i=0; i<hashmap_search_results_list.size(); i++)
{
/* Check if it is already added. We have to check,
because we run it twice in consecutive order. */
LinearLayout listView_item = (LinearLayout)getViewByPosition(i, listView_search_results);
View middle_ground = ((ViewGroup) listView_item).getChildAt(1);
LinearLayout path_model = (LinearLayout) ((ViewGroup) middle_ground).getChildAt(0);
LinearLayout already_added_ll_path = (LinearLayout)((ViewGroup)path_model).getChildAt(0);
if(already_added_ll_path != null)
{
Log.d(TAG, "Already has linear layout: " + hashmap_search_results_list.get(i).get("latest_departure_time"));
continue;
}
Log.d(TAG, "Doing: " + hashmap_search_results_list.get(i).get("latest_departure_time"));
Search_Result_Item result_item = (Search_Result_Item)search_results_list.get(i);
/* How to access each view at each item position in list view. */
//LinearLayout listView_item = (LinearLayout) getViewByPosition(i, listView_search_results);
//View middle_ground = ((ViewGroup) listView_item).getChildAt(1);
//LinearLayout path_model = (LinearLayout) ((ViewGroup) middle_ground).getChildAt(0);
/* Setting the proportions of lines regarding walking time and riding time. */
HrMin hrmin_walking_to_entrance_time = result_item.walking_to_entrance_time();
HrMin hrmin_riding_time = result_item.travel_time();
HrMin hrmin_walking_from_exit_time = result_item.walking_from_exit_time();
int int_walking_to_entrance_time = hrmin_get_minutes(hrmin_walking_to_entrance_time);
int int_riding_time = hrmin_get_minutes(hrmin_riding_time);
int int_walking_from_exit_time = hrmin_get_minutes(hrmin_walking_from_exit_time);
int total = int_walking_to_entrance_time +
int_riding_time +
int_walking_from_exit_time;
float weight_walking_to_entrance = (float)int_walking_to_entrance_time/total;
float weight_bus_ride = (float)int_riding_time/total;
float weight_walking_from_exit = (float)int_walking_from_exit_time/total;
/* How to add a new layout, view to existing layout. */
LinearLayout ll_path = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
/* Pedestrian image. */
ImageView pedestrian = new ImageView(this);
pedestrian.setBackground(getResources().getDrawable(R.drawable.runner_rnd));
int dimensionInPixel = 16;
int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams pedestrian_params = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
pedestrian.setLayoutParams(pedestrian_params);
/* Gray Line. */
FrameLayout frameLayout_pedestrian_container = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_walking_to_entrance);
View gray_line = new View(this);
gray_line.setBackground(getResources().getDrawable(R.drawable.horizontal_gray_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams gray_line_params = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
gray_line.setLayoutParams(gray_line_params);
frameLayout_pedestrian_container.addView(gray_line);
frameLayout_pedestrian_container.addView(pedestrian);
frameLayout_pedestrian_container.setLayoutParams(frame_layout_params);
ll_path.addView(frameLayout_pedestrian_container);
/* Bus image. */
FrameLayout frameLayout_bus_container = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_bus_params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_bus_ride);
ImageView bus = new ImageView(this);
bus.setBackground(getResources().getDrawable(R.drawable.bus_rnd));
dimensionInPixel = 17;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams bus_params = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
bus.setLayoutParams(bus_params);
/* Green line. */
View green_line = new View(this);
green_line.setBackground(getResources().getDrawable(R.drawable.horizontal_green_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams green_line_params = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
green_line.setLayoutParams(green_line_params);
frameLayout_bus_container.addView(green_line);
frameLayout_bus_container.addView(bus);
frameLayout_bus_container.setLayoutParams(frame_layout_bus_params);
ll_path.addView(frameLayout_bus_container);
/* Walking from exit path. */
ImageView pedestrian2 = new ImageView(this);
pedestrian2.setBackground(getResources().getDrawable(R.drawable.runner_rnd));
dimensionInPixel = 16;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams pedestrian_params2 = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
pedestrian2.setLayoutParams(pedestrian_params2);
/* Gray Line. */
FrameLayout frameLayout_pedestrian_container2 = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_params2 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_walking_from_exit);
View gray_line2 = new View(this);
gray_line2.setBackground(getResources().getDrawable(R.drawable.horizontal_gray_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams gray_line_params2 = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
gray_line2.setLayoutParams(gray_line_params2);
frameLayout_pedestrian_container2.addView(gray_line2);
frameLayout_pedestrian_container2.addView(pedestrian2);
frameLayout_pedestrian_container2.setLayoutParams(frame_layout_params2);
ll_path.addView(frameLayout_pedestrian_container2);
ll_path.setLayoutParams(params);
ll_path.setOrientation(LinearLayout.HORIZONTAL);
path_model.addView(ll_path);
}
}
Part where I update the ListView and show it:
search_results.setVisibility(View.VISIBLE);
ListView is a child of two layouts:
<LinearLayout
android:id="#+id/search_results"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:visibility="gone"
android:paddingTop="140dp"
android:orientation="vertical"
android:onClick="onClick_nothing">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_options_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="#+id/mapView"
app:layout_constraintTop_toTopOf="#+id/mapView"
android:background="#android:color/white"
android:orientation="vertical"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarThumbVertical="#android:color/darker_gray"
/>
</RelativeLayout>
</LinearLayout>
One thing that helped is Handler.postDelayed():
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
add_designed_views (null);
}
}, 300);
This function delays method call for 300 mS. Programmatically added layouts are visible. But, still every 6th is missing. They also appear after I touch any of the items.
My goal is to create a dialog that wraps its content height as shown in the image below.
My problem is that my dialog wants to match its parent as shown in the image below. The height should stop at the item "asdf" and this white space below "asdf" should not exist.
This is my code for the later:
private void showPopupEventTest(final String Cardid) {
Dialog dialog = new Dialog(getActivity(), R.style.Theme_AppCompat_Dialog_Alert);
dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
dialog.setContentView(R.layout.fragment_event);
SwipeRefreshLayout swipeRefreshLt = (SwipeRefreshLayout) dialog.findViewById(R.id.swipeRefreshLt);
ListView lv = (ListView) dialog.findViewById(R.id.Contacts_list_view);
SearchView SearchET = (SearchView) dialog.findViewById(R.id.SearchET);
LinearLayout layoutLinearLayout = (LinearLayout) dialog.findViewById(R.id.layoutLinearLayout);
EventAdaptor myAdapter = new EventAdaptor(getContext(), ArrayList_EventObject);
lv.setAdapter(myAdapter);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
dialog.show();
}
And this is the layout:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/layoutLinearLayout"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp">
<SearchView
android:id="#+id/SearchET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false" />
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:id="#+id/swipeRefreshLt"
android:layout_height="wrap_content">
<ListView
android:id="#+id/Contacts_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
What am I doing wrong?
try this
Window window = customDialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
customDialog.show();
There are two options for you
Use RecyclerView instead of ListView
OR
use below code after setting the adapter.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre - condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Your problem is in your xml layout. Try this:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/layoutLinearLayout"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp">
<SearchView
android:id="#+id/SearchET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false" />
<!--<android.support.v4.widget.SwipeRefreshLayout-->
<!--android:layout_width="match_parent"-->
<!--android:id="#+id/swipeRefreshLt"-->
<!--android:layout_height="wrap_content">-->
<ListView
android:id="#+id/Contacts_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--</android.support.v4.widget.SwipeRefreshLayout>-->
</LinearLayout>
Explanation:
The problem was the SwipeRefreshLayout. I commented it
I have a layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/map_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="#+id/skip_button"
android:layout_width="match_parent"
android:layout_height="#dimen/button_height"
android:text="#string/skip"/>
</LinearLayout>
My code hides skip_button (via .setVisibility(View.GONE)) when user pick its coordinates in map_view webview. Also user has an ability to reset its coordinates and then button must appear again.
It works on Android 5, but on Android 4 button stay hidden even after .setVisibility(View.VISIBLE). I tried:
mapСontainer.getParent().requestLayout();
mapСontainer.refreshDrawableState();
skipButton.refreshDrawableState();
Wrap webview into LinearLayout
But all this doesn't help.
Beat this. It looks like this is a api16 bug so i have to workaround it...
if (dataOk) {
skipButton.setVisibility(View.GONE);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mapСontainer.getLayoutParams();
layoutParams.height = 0;
layoutParams.weight = 1;
mapСontainer.setLayoutParams(layoutParams);
mapСontainer.getParent().requestLayout();
} else {
int visibility = skipButton.getVisibility();
skipButton.setVisibility(View.VISIBLE);
if (visibility == View.GONE) {
int height = mapСontainer.getMeasuredHeight();
ViewGroup.LayoutParams skipParams = skipButton.getLayoutParams();
LinearLayout.LayoutParams containerParams = (LinearLayout.LayoutParams) mapСontainer.getLayoutParams();
containerParams.height = height - skipParams.height;
containerParams.weight = 0;
mapСontainer.setLayoutParams(containerParams);
}
}
I need to show n number of Edit text added dynamically below a static Edit text.
I am adding the ET but some how it is not visible. What am doing wrong??
Here is my code:
private void addEditTextView(int numberOfViews){
// Using layout params same as above static ET
ViewGroup.LayoutParams layoutParams = staticEditText.getLayoutParams();
for (int index = 0; index <= numberOfViews; index++) {
final EditText newET = new EditText(getActivity());
//Added below 2 lines just to make sure width and height are coming
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
newET.setLayoutParams(layoutParams);
newET.setHint("Select ME");
newET.setFocusable(false);
newET.setId(index);
newET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO set time
Log.d("Clicked...",newET.getId()+"");
}
});
//parentLayout is Linear Layout with Vertical orientation
parentLayout.addView(newET);
}
}
XML code :
<LinearLayout
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/staticEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#drawable/rectangle_bg_dark_gray_border"
android:focusable="false"
android:hint="staticEditText"
android:padding="7dp" />
</LinearLayout>
UPDATE : parentLayout.getChildCount() is coming correctly. New views are getting added but not visible!
Hi try adding a Scrollview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/parentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- EDITTEXT here -->
</LinearLayout>
</ScrollView>
</LinearLayout>
I have layout with controls:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/contact_phones_layout_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/contact_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:inputType="phone" />
<Spinner
android:id="#+id/contact_phone_type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
And I want to inflate it into another layout on the fragment. Quantity of these controls depends on values in a array. Array contains controls objects. So every time onCreateView rises I filled the layout from the array:
private void addLine(PhoneLine line) {
LinearLayout layout = (LinearLayout) mLayout.findViewById(R.id.contact_phones_layout);
View view = line.getParent();
((ViewGroup) view.getParent()).removeView(view);
layout.addView(view, layout.getChildCount() - 1);
setButtonVisible(false);
}
if line is null controls are created this way:
private void addLine() {
LinearLayout layout = (LinearLayout) mLayout.findViewById(R.id.contact_phones_layout);
View view = inflater.inflate(R.layout.contact_phone_line, layout, false);
EditText phoneEditText = (EditText) view.findViewById(R.id.contact_phone);
Spinner phoneType = (Spinner) view.findViewById(R.id.contact_phone_type);
phoneLines.add(new PhoneLine(phoneEditText, phoneType, view));
layout.addView(view, layout.getChildCount() - 1);
}
But after that I get same values in all EditText/Spinner controls, values equal to last element in the array. What can be wrong? May be there is more pure way to add controls dynamically?
I fixed it by adding controls when onResume rises, not onCreateView. But I still don't understand why onCreateView changes all values.