I have a custom layout xml which i use as a template for creating items in a list dynamically. However I can't seem to change the text and color of elements within this custom layout properly before I add it to the main layout. I need to do this as each item in the list can be different.
If I have added more than one of these custom layouts to the main layout any changes I make to the TextView object always happen on the first item in the list.
My custom layout has a textview and a check box within a relative layout in a file called 'cat_panel.xml'.
My coding to produce the layout is:
LinearLayout rootEl = (LinearLayout) findViewById(R.id.pageWrapper);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View vw;
vw=inflater.inflate(R.layout.cat_panel, rootEl, false);
catTitleTv = (TextView) findViewById(R.id.catPanelTitle);
catTitleTv.setText("testing 1");
rootEl.addView(vw);
//the above is then repeated
vw=inflater.inflate(R.layout.cat_panel, rootEl, false);
catTitleTv = (TextView) findViewById(R.id.catPanelTitle);
catTitleTv.setText("testing 2");
rootEl.addView(vw);
Thanks in advance
Change
catTitleTv = (TextView) findViewById(R.id.catPanelTitle);
to
catTitleTv = (TextView) vw.findViewById(R.id.catPanelTitle);
Related
I have inflated one layout in my Activity now in that Activity I have some common fields for multiple times, so I have created one more layout with those common fields and included in my layout.And I got inflated all those fields.But now My problem is that While I am inserting data in those fields only first included fields gets inserted with data not the second, third, and so on... So how to get data inserted in such fields with common ids..
LayoutInflater inflater = LayoutInflater.from(getContext());
View w4FragmentRowView = inflater.inflate(R.layout.weekly_report4_row_item, null);
etWhere = (EditText)w4FragmentRowView.findViewById(R.id.et_weekly_reports_where_w4_tab);
etHowSubStd = (EditText)w4FragmentRowView.findViewById(R.id.et_weekly_reports_how_sub_std_w4_tab);
Now I have to insert data multiple times in same activity with these edit Text fields.
Say you have two views and they have common ids and you inflate them
Like shown below
now for common ids when you are doing find view by id make sure
the point to right parent
//look at item and item2 as while finding view
LinearLayout item = (LinearLayout) mLayoutInflater.inflate(
R.layout.layout1, null);
TextView skunitNameTextView = (TextView) item.findViewById(R.id.textView);
LinearLayout item2 = (LinearLayout) mLayoutInflater.inflate(
R.layout.layout1, null);
TextView skunitNameTextView2 = (TextView) item2.findViewById(R.id.textView);
i think this should solve your problem
Take example you have a layout file with name "reuse.xml" where you have only one view TextView with id "txv".
Now in your Activity you are inserting this layout file multiple times like:-
TextView tv1 = (TextView) mLayoutInflater.inflate(
R.layout.reuse, null);
tv1.setId(R.id.txv_activity_1);
TextView tv2 = (TextView) mLayoutInflater.inflate(
R.layout.reuse, null);
tv2.setId(R.id.txv_activity_2);
TextView tv3 = (TextView) mLayoutInflater.inflate(
R.layout.reuse, null);
tv3.setId(R.id.txv_activity_3);
Now you are using the same layout file but they gave different id so you can easily update any of file which one you want.
But if you are using the same layout more than 3-4 times it is recommended to use it as ListView or RecyclerView item, which also provide you better performance in case of large layout and easy to maintain.
Hello i am developing one app where i want to inflate layout in my Card view fro existing layout.And i want to show list of text view dynamically according to my string array.
Here is my existing layout
and i want to inflate following layout in last cardview of above layout
so tried with coding to do it but it display only one textview
my coding is as follows
But it is not working it display only one textview
i want to add textview dynamically according to my string size
I want to inflaate layout just like listview but i dont want to use listview.i want to implement this without listview dynamically
it display only one textview i want to add textview dynamically
according to my string size
Probably due to inflating inflate_specification every time for each TextView and initializing container inside for-loop.
Do it as :
LinearLayout container = (LinearLayout)findViewById(R.id.temp);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i=0;i<5;i++)
{
View view = inflater.inflate(R.layout.inflate_specification, container,false);
// Create TextView
TextView product = (TextView)view.findViewById(R.id.speci);
product.setText("i= " + i);
container.addView(view);
}
Try this code:
LinearLayout container = (LinearLayout)findViewById(R.id.temp);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
container.removeAllViews();
for(int i=0;i<5;i++) {
View view = inflater.inflate(R.layout.inflate_specification, container,false);
// Create TextView
TextView product = (TextView)findViewById(R.id.speci);
product.setText("i= " + i);
container.addView(view);
}
My intent is to inflate numerous instances of the same view on a LinearLayout, and then modify their views accordingly. However, whenever I inflate a view multiple times, I always end up with the same reference to the first inflated view.
Simplified example:
View v = getActivity().getLayoutInflater().inflate(R.layout.inflatable_layout,rootLinearLayout,true);
TextView t = (TextView) v.findViewById(R.id.text1);
t.setText("Foo");
View v2 = getActivity().getLayoutInflater().inflate(R.layout.inflatable_layout,rootLinearLayout,true);
TextView t2 = (TextView) v2.findViewById(R.id.text1);
t2.setText("Bar");
By doing this, I end up with two inflated views. However, the first view's TextView contains "Bar", and the second one unaltered.
Is there a way to be able to maintain reference to the views I inflate individually?
To solve your problem, just change:
View v = inflater.inflate(R.layout.inflatable_layout,rootLinearLayout,true);
to
View v = inflater.inflate(R.layout.inflatable_layout,rootLinearLayout,false);
rootLinearLayout.addView(v1);
Note that you now have to add the view manualy to your layout.
(Do the same for v2)
This way you should receive two different references.
Please try this:
View v = getActivity().getLayoutInflater().inflate(R.layout.inflatable_layout,null); // Note the null replacing the "ViewGroup"
TextView t = (TextView) v.findViewById(R.id.text1);
t.setText("Foo");
View v2 = getActivity().getLayoutInflater().inflate(R.layout.inflatable_layout,null); // Note the null replacing the "ViewGroup"
TextView t2 = (TextView) v2.findViewById(R.id.text1);
t2.setText("Bar");
// Manually append each layout to your linear layout.
I think this should work, as no root is defined
How can I inflate several same layouts with diffrent texts and images? I tried use this code
for (final Tour t : info.tours) {
final LinearLayout list = (LinearLayout) findViewById(R.id.tours_list);
Log.d(Const.LOG_TAG, "Add child view to tours_list");
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View child = inflater.inflate(R.layout.tour_item_for_guide_info,
list, true);
final ImageView tourImage = (ImageView) findViewById(R.id.tour_image);
final String tourImgUrl = Const.HOST + t.image;
imageLoader.DisplayImage(tourImgUrl, tourImage);
TextView tourText = (TextView) findViewById(R.id.text);
tourText.setText(t.name);
}
but text and image setted in first inflated layout, replacing the previous text and image. I understand what it's happens because layouts have same ids. Can I resolve it? I know about ListView and adapters, but I would like to learn if it might be done without them.
I assume that the ImageView and TextView - tourImage, tourText - are elements of R.layout.tour_item_for_guide_info.
If this is so, then when you reference it, you should use the child view to get them.
With other words, instead of:
ImageView tourImage = (ImageView) findViewById(R.id.tour_image);
TextView tourText = (TextView) findViewById(R.id.text);
You should have:
ImageView tourImage = (ImageView)child.findViewById(R.id.tour_image);
TextView tourText = (TextView)child.findViewById(R.id.text);
Not sure if this will definetly fix your problem, but it looks like a bug.
I have an app that queries a database and get a recordset, on the display, I need to present these data with a row that is a complex layout. Each row contains some ImageView, many TextView etc...
It's really difficult to create the row layout programmatically, is there any way to get the entire row layout (container and children of the row layout) from an xml, edit some of the properties (like the TextViews of the row layout) and add the result to a LinearLayout?
is there any way to get the entire row layout (container and childs of the row layout) from an xml
What you are looking for is how to inflate a view (LayoutInflator)
Now that you have the right term, it should be easy to find examples, inflate is popular in ListView tutorials. For an example, take a look at at getView() in this tutorial for an example:
HowTo: ListView, Adapter, getView and different list items’ layouts in one ListView
http://android.amberfog.com/?p=296
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
...
convertView = mInflater.inflate(R.layout.item1, null);
... edit some of the properties (like the TextViews of the row layout) ...
Once you inflate the view, you can search for the widgets within it, so you can manipulate it.
holder.textView = (TextView) convertView.findViewById(R.id.text);
If your view is fairly complex and/or you will often be looking for widgets within it, I want to point out the ViewHolder technique, shown in the example referenced, relevant bits below:
// Data structure to save lookups
public static class ViewHolder {
public TextView textView;
}
...
// Save lookups to widgets for this view in ViewHolder in tag
ViewHolder holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.text);
view.setTag(holder);
...
// Grab saved widgets - no need to search tree for them via lookup again
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.textView.setText(mData.get(position));
... and add the result to a LinearLayout?
Presumably, you're already programatically adding to the LinearLayout, but if you want to see some code, here's an example that shows setting some layout parameters:
Android LinearLayout
http://developerlife.com/tutorials/?p=312
// main "enclosing" linearlayout container - mainPanel
final LinearLayout mainPanel = new LinearLayout(ctx);
{
mainPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mainPanel.setOrientation(LinearLayout.VERTICAL);
...
}
...
// top panel
LinearLayout topPanel = new LinearLayout(ctx);
{
// WEIGHT = 1f, GRAVITY = center
topPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT,
1));
...
}
...
// bottom panel
LinearLayout bottomPanel = new LinearLayout(ctx);
{
LayoutUtils.Layout.WidthFill_HeightWrap.applyLinearLayoutParams(bottomPanel);
...
}
...
// add the panels
mainPanel.addView(topPanel);
mainPanel.addView(bottomPanel);
...
Lastly, you can do alot (including custom rows) with the AdapterView / Adapter paradigm, e.g. using ListView with a SimpleCursorAdapter. It may save you some code by looking into it. Some babbling about it here:
Android ListView with different layouts for each row
As a rule of thumb, whenever you create an instance of View using code, think twice.
Review LayoutInflater on how to create layout from resources. In your special case, you might also want to check ResourceCursorAdapter if you want to show multiple rows in a ListView.
What kind of table are you using? If your layout would be something like this:
<TableLayout>
<LinearLayout android:id="#+id/linearLayoutTest">
<TextView/>
<TextView ....
<ImageView ....
</LinearLayout>
<LinearLayout ....
</TableLayout>
It would be easy to access the row container through the id and then find by position the desirable child:
LinearLayout linearLayout = (LinearLayout)this.findViewById(R.id.linearLayoutTest);
TextView textView = (TextView)linearLayout.getChildAt(0);
textView.setText("New Text");