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);
Related
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);
I want to show a screen with text area that is vertically scrolling and image area that is horizontally scrolling.
I have created two layouts:
activity_display.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:orientation="vertical"
tools:context=".MainActivity" >
<ScrollView
android:id="#+id/displaySV1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginBottom="20dp">
<LinearLayout
android:id="#+id/displayLL1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<HorizontalScrollView
android:id="#+id/displaySV2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginBottom="20dp">
<LinearLayout
android:id="#+id/displayLL2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
and custom_layout.xml : I want to add the custom layout in the linear layout displayLL1.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customFrame">
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/customTV1"
android:layout_gravity="start|top"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:text="Test text"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/customTV2"
android:layout_gravity="top|center_horizontal"
android:text="Test Value"
android:layout_marginTop="5dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EDIT"
android:id="#+id/editButton"
android:layout_gravity="right|top" />
</FrameLayout>
Display activity calls:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.displayLL2);
for(int i = 0; i < count1; i++)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.displayLL1);
FrameLayout custLayout = (FrameLayout) findViewById(R.id.customFrame);
TextView label = (TextView) findViewById(R.id.customTV1);
TextView value = (TextView) findViewById(R.id.customTV2);
label.setText("Test");
value.setText("Test");
linearLayout.addView(custLayout);
}
for (int x = 0; x < count2; x++)
{
final ImageView image = new ImageView(this);
image.setBackgroundResource(R.drawable.ic_launcher);
linearLayout2.addView(image);
}
I am able to populate the image part if text part is commented out. But with the above code nothing is displayed on the screen.
Any idea where I might be going wrong?
Thank you!
Probably you have a NullPointerException.
Because there is no customTV1 and customTV2 ids in your activity xml, so calling findViewById(R.id.customTV1); will return a null value, and label.setText("Test"); will throw a NullPointerException.
Actually calling FrameLayout custLayout = (FrameLayout) findViewById(R.id.customFrame); will not add your custom xml to the activity one, you need to inflate it.
Try this code:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.displayLL1);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.displayLL2);
LayoutInflater inflater = LayoutInflater.from(yourActivity.this);
for(int i = 0; i < count1; i++){
View custLayout= inflater.inflate(R.layout.custom_layout, null, false);
TextView label = (TextView) custLayout.findViewById(R.id.customTV1);
TextView value = (TextView) custLayout.findViewById(R.id.customTV2);
label.setText("Test");
value.setText("Test");
linearLayout.addView(custLayout);
}
for (int x = 0; x < count2; x++){
ImageView image = new ImageView(this);
image.setBackgroundResource(R.drawable.ic_launcher);
linearLayout2.addView(image);
}
I have layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/exchangeView">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff000"
android:gravity="center"
android:text="Sticker 1"
android:textColor="#372c24"
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:id="#+id/next">
<Button
android:id="#+id/btn_next"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:text="Next" />
</LinearLayout>
</LinearLayout>
</ScrollView>
and related code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exchange);
layout = (View) findViewById(R.id.exchangeView);
}
next thing I want to do is to get all children elements in this layout with Id=exchangeView, but when I type layout.getChildCount() android studio IDE compiler says:
Cannot resolve method 'getChildCount()'
However, if I put breakpoint on line where I assign layout a value, then if I evaluate expression I can execute method layout.getChildCount()
All I want to do is to execute layout.getChildCount() during runtime.
Thanks in advance!
Change this line
layout = (View) findViewById(R.id.exchangeView);
to
layout = (LinearLayout) findViewById(R.id.exchangeView);
I am assuming you have declared the variable layout as
View layout;
If so, change that to
LinearLayout layout;
The View class doesn't have the method getChildCount(), it is a method in the ViewGroup class. LinearLayout extends ViewGroup, and hence has access to it. View does not. This is related to polymorphism, which I suggest you look up.
Cheers.
You can try to use getChildCount() and also getChildAt() like this may be
int childcount = ll.getChildCount();
for (int i=0; i < childcount; i++){
View v = ll.getChildAt(i);
}
Try this:
LinearLayout layout = setupLayout();
int count = layout.getChildCount();
View v = null;
for(int i=0; i<count; i++) {
v = layout.getChildAt(i);
//do something with your child element
}
i have an ArrayList of usernames for each username i want to add a xml layout (layout_one_user.xml) and put the username String into each TextView (name_user) present in the layout inflated.
my code in the main Activity is this:
ll_counts = (LinearLayout) findViewById(R.id.ll_counts);
lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutInflater inflater_one_user = MainScreen.this.getLayoutInflater();
View layout_one_user = inflater_one_user.inflate(R.layout.layout_one_user, null);
TextView txt_counts = (TextView) layout_one_user.findViewById(R.id.name_user);
ImageView delete_icon = (ImageView) layout_one_user.findViewById(R.id.icon_delete);
ImageView edit_icon = (ImageView) layout_one_user.findViewById(R.id.icon_edit);
for (int i = 0; i < usernames.size(); i++) {
layout_one_user = new View(this);
txt_counts.setText( usernames.get(i) );
ll_counts.addView(layout_one_user);
}
the layout_one_user is:
<?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="wrap_content"
android:orientation="vertical"
android:padding="5dp"
>
<ImageView
android:id="#+id/icon_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_edit_black_24dp" />
<ImageView
android:id="#+id/icon_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/icon_edit"
android:src="#drawable/ic_delete_black_24dp" />
<TextView
android:id="#+id/name_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/icon_delete"
android:layout_centerHorizontal="true"
android:text="text"
/>
and the layout in my main is:
<LinearLayout
android:id="#+id/ll_counts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/action_bar"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
</LinearLayout>
But it doesn't appear anything on the screen.
You inflate your custom layout to layout_one_user:
View layout_one_user = inflater_one_user.inflate(R.layout.layout_one_user, null);
but then, on the very first iteration of your loop, you overwrite this variable with an empty View:
layout_one_user = new View(this);
Thus when you add layout_one_user to your layout, you're adding an "empty view" and you see no result.
It seems to me that you need to inflate a new layout for each user:
for (int i = 0; i < usernames.size(); i++) {
View layout = getLayoutInflater().inflate(R.layout.layout_one_user, ll_counts, false);
TextView textView = (TextView) layout.findViewById(R.id.name_user);
textView.setText(usernames.get(i));
ll_counts.addView(layout);
}
I have an xml file (option_element.xml) that contains a ImageView and a TextView
<?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"
android:layout_marginTop="-18dp" >
<ImageView
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/option_button" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button"
android:layout_alignLeft="#+id/button"
android:layout_alignRight="#+id/button"
android:layout_alignTop="#+id/button"
android:layout_centerHorizontal="true"
android:gravity="center" />
</RelativeLayout>
I should fill a LinearLayout with this View, based on a content of an array
<?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/options_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />
<!-- other layout elements -->
</LinearLayout>
I'm adding it like
LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
View to_add = inflater.inflate(R.layout.options_element,
options_layout);
TextView text = (TextView) to_add.findViewById(R.id.text);
text.setText(options[i]);
text.setTypeface(FontSelector.getBold(getActivity()));
}
But something gone wrong. I'm expecting options.length ImageView with relative TextView filled with options[i] text, but i obtain options.length ImageView, but only first one has text (and text it's not options[0] element, but last one).
For example, if option contains {"one", "two", "three"} inside first imageView i get "three", and other ones are empty.
How can put each string inside each TextView?
The inflate(int resource, ViewGroup root) method return root if root is not null, thus to_add.findViewById() equals options_layout.findViewById() and it always return the Views in the first position.
Change as following should help:
LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
View to_add = inflater.inflate(R.layout.options_element,
options_layout);
TextView text = (TextView) to_add.findViewById(R.id.text);
text.setText(options[i]);
text.setTypeface(FontSelector.getBold(getActivity()));
}
to:
LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
View to_add = inflater.inflate(R.layout.options_element,
options_layout,false);
TextView text = (TextView) to_add.findViewById(R.id.text);
text.setText(options[i]);
text.setTypeface(FontSelector.getBold(getActivity()));
options_layout.addView(to_add);
}