Add view to another one programmatically inside Fragment - android

Something gone wrong on my code. I have an XML representing layout of my fragment (fragment_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
and another xml file representing a single view that shod go inside fragment layout (element.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:inputType="number"
android:text="0" />
</RelativeLayout>
and this is how i fill Fragment layout inside onCreateView method:
View view = inflater.inflate(R.layout.fragment_layout, container,
false);
some_elements = getActivity().getResources().getStringArray(
R.array.some_array);
LinearLayout root = (LinearLayout) view.findViewById(R.id.root_layout);
for (int i = 0; i < some_elements.length; i++) {
View element = inflater.inflate(
R.layout.element, container, false);
TextView tv= (TextView) element.findViewById(R.id.tv);
prodotto.setText(prodotti[i]);
EditText et= (EditText) element.findViewById(R.id.et);
root.addView(element);
}
return view;
Now happen that only first element of "some_elements" array is showed inside fragment (element contains 10 elements). why other elements is not showed? what's wrong?

The problem is android:layout_height="match_parent", in your RelativeLayout. It should be wrap_content

Related

how to display a layout dynamically with margin

I have two layouts, i inflate one layout into another and want to display this inflated layout including textview as many times as condition is true . I set the textview value as value of i and also set the layout top margin. but it displaying value of i in one row only and rest are blank,also its setMargin() also not working.Below is what i have done yet.
public void displayRows(){
int n= 6;
for(int i=0;i< n;i++){
LinearLayout container = (LinearLayout)findViewById(R.id.layoutContainer);
View hiddenInfo = getLayoutInflater().inflate(R.layout.dynamic, container, false);
container.addView(hiddenInfo);
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.dynamic);
TextView textView = (TextView) findViewById(R.id.text);
textView.setText("Text"+i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 30, 0, 0);
hiddenLayout.setLayoutParams(layoutParams);
hiddenLayout.addView(textView);
}
container.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/layoutContainer"
android:animateLayoutChanges="true"
android:layout_gravity="center|top"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/li1"
android:layout_gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#color/colorAccent"
android:onClick="displayRows"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_gravity="center"
android:text="Rows"/>
</LinearLayout>
</LinearLayout>
dynamic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/dynamic"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:text=""
android:textColor="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I also want to know how to remove all the generated dynamic rows on click remove button.
Replace
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.dynamic);
TextView textView = (TextView) findViewById(R.id.text);
with
LinearLayout hiddenLayout = (LinearLayout) hiddenInfo.findViewById(R.id.dynamic);
TextView textView = (TextView) hiddenInfo.findViewById(R.id.text);
And remove this line
hiddenLayout.addView(textView);

Inflating View into LinearLayout and editing cmponents in this view

I am trying to add multiple LinearLayouts into one declared in xml. Everyone has 3 textView which will be edited in code. My problem is, when i am inflating xml layout to View object in code, all margins are ignored. My second question:
How can i dynamically set ids to textViews and then edit text in it?
LinearLayout xml which is inflating:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/pointsAwaiting"
android:layout_marginTop="40dp"
android:background="#drawable/background_blue"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:textColor="#FFFFFF"
android:textSize="15dp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
</LinearLayout>
He is inflating into this piece of code:
<
ScrollView 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:background="#drawable/background"
tools:context="pl.com.qiteq.zielonomocni_rework.HistoryActivity">
<LinearLayout
android:id="#+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:id="#+id/historyView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</ScrollView>
And finnaly java code: (loop counter is for example)
LinearLayout mainView = (LinearLayout) findViewById(R.id.historyView);
for (int i=0; i<=2; i++){
View layout = View.inflate(this, R.layout.history_bar, null);
mainView.addView(layout);
}
The reason all your margins are being ignored is that you are passing null into your inflater here:
View layout = View.inflate(this, R.layout.history_bar, null);
When you do this all the LayoutParams of your view are thrown away. You should replace it with:
View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
To set text in the TextViews you don't need to set a different id for each one, just give each one an id. Then in your loop you can do something like:
List<TextView> textViews = new ArrayList<>();
LayoutInflater inflater = LayoutInflater.from(this);
for (int i=0; i<=2; i++){
View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
mainView.addView(layout);
TextView textView = (TextView) layout.findViewById(R.id.text1);
textViews.add(textView);
}
You would then have a reference to each of your TextViews in the List

How to update the textview dynamically created textview in childlayout?

LinearLayout lay1 = (LinearLayout) findViewById(R.id.lay1);
for(int i=0;i<list1.size();i++) {
View child = lay1.getChildAt(i);
LinearLayout lay3 = (LinearLayout) child.findViewById(R.id.lay3);
for (j = 0; j <list2.size(); j++) {
View child1 = lay3.getChildAt(j);
// View hiddenInfo11 = getActivity().getLayoutInflater().inflate(R.layout.add_inner_item, lay3 , false);
final TextView name = (TextView) child1.findViewById(R.id.name);
name.setText("new");
}
}
XML Layout:
<LinearLayout
android:id="#+id/lay1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/lay2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="#color/white"
android:orientation="horizontal">
</LinearLayout>
add_item.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="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/lay3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
add_inner_item.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="match_parent"
android:orientation="horizontal"
>
<TextView android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
Added View Dynamically using the following method.
for(int i=0;i<list.size();i++) {
View hiddenInfo = getActivity().getLayoutInflater().inflate(R.layout.add_item, lay, false);
LinearLayout lay3 = (LinearLayout) hiddenInfo.findViewById(R.id.lay3);
lay1.addView(hiddenInfo);
for (j = 0; j < list2.size(); j++) {
View hiddenInfo1 = getActivity().getLayoutInflater().inflate(R.layout.add_inner_item, lay3, false);
final TextView name = (TextView) hiddenInfo1.findViewById(R.id.name);
name.setText(list2.get(j).get("Name"));
lay3.addView(hiddenInfo1);
}
I have one Layout with creating dynamically and I want to update the child textview from outside of the button click view.I have tried this code.But nothing is changed.Can anyone give suggestion to this sample.
You are looking for "lay2" inside the "lay1" container. According to your XML layout, they are not nested, so you won't be able to find it this way. Change it to this:
LinearLayout lay2 = (LinearLayout) findViewById(R.id.lay2);
You should get the actual lay2 container. I'm not sure whether the text views are there though since I have seen your code for the views' creation.
EDIT:
I've not specified a parent - you should use the Activity for this. Something like:
LinearLayout lay2 = (LinearLayout) MyActivity.this.findViewById(R.id.lay2);

Android LinearLayout not displaying correctly if number of items is lesser then screen height

I have a fragment which fetch items from rest api and display the item title in LinearLayout within a scrollview.
Below are my views and codes.
fragment_cards_view.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:fillViewport="true">
<LinearLayout
android:id="#+id/linear_card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/wallet_holo_blue_light"/>
</ScrollView>
list_item.xml
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:layout_marginBottom="10dp">
<TextView
android:id="#+id/list_titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
style="#style/item_header"/>
</RelativeLayout>
</RelativeLayout>
TitleFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "In onCreateView");
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_cards_view, container, false);
mLinearLayout = (LinearLayout)v.findViewById(R.id.linear_card_view);
return v;
}
private void createUpcomingView() {
int counter = 0;
for (User user : mUsers) {
LayoutInflater inflater = null;
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = inflater.inflate(R.layout.list_item, null);
mLinearView.setId(user.getId());
TextView titleTextView = (TextView)mLinearView.findViewById(R.id.list_titleTextView);
titleTextView.setText("title " + counter);
mLinearLayout.addView(mLinearView);
counter++;
}
}
The problem I faced is that if the number of items is lesser than the screen height, the display becomes incorrect.
This screenshot has 4 items, but it is only showing the first item and the margins are not aligned correctly.
This screenshot has many items and the margins are aligned correctly.
This screenshot is the first solution:
Remove following layout from list_item.xml then it will work properly.
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:layout_marginBottom="10dp">
</RelativeLayout>
Try writing
inflater.inflate(R.layout.list_item, mLinearLayout, false);
instead of
inflater.inflate(R.layout.list_item, null);
Also, use wrap_content heights in list_item.xml
I have found the solution based on the answers from #clemp6r and #Jain Nidhi.
fragment_cards_view.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:fillViewport="true">
<LinearLayout
android:id="#+id/linear_card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/wallet_holo_blue_light"/>
</ScrollView>
list_item.xml, add left and right margin, removed nested RelativeLayout, set all layout_height to wrap_content
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:id="#+id/list_titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
style="#style/item_header"/>
</RelativeLayout>
TitleFragment.java, modified the 2nd and 3rd argument of inflate.
private void createUpcomingView(..) {
View mLinearView = inflater.inflate(R.layout.list_item, mLinearLayout, false);
}

Android: setting text in each new view within a for loop

So I have an XML layout1 which is just a LinearLayout with three text views. I also have another XML layout2 with ScrollView and a LinearLayout inside it. I'm using this for loop to create several of the layout2 inside the LinearLayout of the ScrollView. It's working fine but I want to be able to set the text of each of the TextViews within the for loop. I'm not sure how to access these TextViews as I can only set one id within the XML file, will that not cause problems if I tried to access their id inside the for loop?
private void setUpResults() {
for (int i = 1; i < totalQuestions; i++) {
parent.addView(LayoutInflater.from(getBaseContext()).inflate(
R.layout.result_block, null));
}
}
Here is the result_block xml file (layout1) :
<?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" >
<LinearLayout
android:id="#+id/layoutSelectedAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:orientation="horizontal"
android:paddingBottom="#dimen/option_padding_bottom"
android:paddingTop="#dimen/option_padding_top" >
<TextView
android:id="#+id/tvOptionALabel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="#string/option_a"
android:textColor="#color/white"
android:textSize="#dimen/option_text_size" />
<TextView
android:id="#+id/tvSelectedAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="#string/option"
android:textColor="#color/white"
android:textSize="#dimen/option_text_size" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutCorrectAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:orientation="horizontal"
android:paddingBottom="#dimen/option_padding_bottom"
android:paddingTop="#dimen/option_padding_top" >
<TextView
android:id="#+id/tvOptionBLabel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="#string/option_b"
android:textColor="#color/white"
android:textSize="#dimen/option_text_size" />
<TextView
android:id="#+id/tvCorrectAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="#string/option"
android:textColor="#color/white"
android:textSize="#dimen/option_text_size" />
</LinearLayout>
</LinearLayout>
Let's say I wanted to set the TextView with the id as tvCorrectAnswer to a different String value in each loop, how should I access it?
Sure, you can do it like this:
private void setUpResults () {
LayoutInflater i = LayoutInflater.from(getBaseContext());
for (int i = 1 /* should be zero? */; i < totalQuestions; i++) {
View view = i.inflate(R.layout.result_block, parent, false);
TextView correctAnswer = (TextView) view.findViewById(R.id.tvSelectedAnswer);
correctAnswer.setText("My Answer Text");
parent.addView(view);
}
}
The key is, inflate using the parent as the container, but don't attach it (the false parameter). Then you'll get a reference to the inflated view, which you can then directly reference to do your findViewById() calls (which will limit the search to that particular ViewGroup). Then add it to the parent and continue to the next item.
Once you have added all your views in your ViewGroup you can use ViewGroup.getChildAt(int) to get a one of the views you have inserted. Once you get one of the views you can access any of its inner views. Something like this.
for(int i = 0; i < parentView.getChildCount();++i) {
View v = parentView.getChildAt(i);
Textview tv = (TextView) v.findViewById(R.id.tvOptionALabel2);
//And so on...
}

Categories

Resources