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);
}
Related
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);
}
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);
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);
}
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...
}
I am trying to add table row dynamically in my activity. The table row is in the relative layout. It looks fine but don't know where I am going wrong. Below is my code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
TableRow tableRow = (TableRow)findViewById(R.id.TableRow);
for(int i = 1; i <3; i++)
RLayout.addView(tableRow); //My code is crashing here
}
And the main.xml as follows
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TableRow
android:id="#+id/TableRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
>
</TextView>
</TableRow>
</RelativeLayout>
Please Help.
It's crashing because that TableRow is already in the layout. If you want to add some dynamically you have to create it programmatically, that is:
// PSEUDOCODE
TableRow newRow = new TableRow(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(/*....*/);
newRow.setLayoutParams(lp);
relLayout.add(newRow);
Actually TableRow should be used inside TableLayout.
If you want to use something more than once, you can use the inflate technique. You need to create an xml layout that includes the only part that you want to repeat (so your TableRow and its children), and then:
LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflated = inflater.inflate(R.layout.your_layout, null);
Now inflated contains the layout you specified. Instead of null, you might want to put there the layout to which attach the inflated one. Every time you need a new element like that, you would have to inflate it the same way.
(You should always report the error you get when it crashes)
------ EDIT -----
ok now i see, this is your code:
RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
TableRow tableRow = (TableRow)findViewById(R.id.TableRow);
for(int i = 1; i <4; i++) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflated = inflater.inflate(R.layout.main, tableRow);
}
this way you're inflating your whole layout inside the original TableRow.
You should have a row.xml layout like this, together with the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TableRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
/>
</TableRow>
and then inflate it like this:
RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
for(int i = 1; i <4; i++) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.row, RLayout);
}
see if it works.