I am inflating a layout in the viewpager of the current activity but I need to set the textview text of this layout so that I can inflate it and it displays the new text I am doing :
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
TextView txt=(TextView)findViewById(R.id.content);
txt.setText(cursor.getString(cursor.getColumnIndex("Content")));
but that's getting me a nullpointerexception, how can I solve this how can I set the text of the textview of a layout that I am not in which mean from outside this layout..
Change - TextView txt=(TextView)findViewById(R.id.content);
to
TextView txt=(TextView)view.findViewById(R.id.content);
To find the textView you want you must specify the parentView
and so
TextView txt=(TextView)view.findViewById(R.id.content);
Related
Using a layout template ex: temp.xml I'm creating a new View with LayoutInflater and I add that View to my MainActivity layout:
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View newView = inflater.inflate(R.layout.temp, null, false);
mainActivLayout.addView(newView);
teml.xml has the following structure:
LinearLayout -> LinearLayout -> (two children) ImageView & TextView
How do I extract from the newView the ImageView and TextView elements so I can add Text and a Image to them ?
Do I have to TypeCast newView to Node or NodeList?
You can use findViewById() on the newView
TextView textView = newView.findViewById(R.id.my_textview_id);
ImageView imageView = newView.findViewById(R.id.my_imageview_id);
You need to set an ID for both the TextView and ImageView in the temp.xml and after that you can use newView.findViewById(VIEW_ID) to get the instance of the View.
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);
}
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);
Programatically I have a RelativeLayout and TextView (called title) within it defined as follows:
RelativeLayout layout = new RelativeLayout(this);
final RelativeLayout.LayoutParams parameters_title =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
title.setLayoutParams(parameters_title);
layout.addView(title,parameters_title);
I intend to add more textviews and buttons later. But for now, I want to "convert" (I am not sure how to put it into words) the RelativeLayout into a View in order to put it into WindowManager. I was trying to use LayoutInflater like this but it wouldn't work
LayoutInflater layoutInflater =
(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = layoutInflater.inflate(layout, null );
then the goal is to put myView into WindowManger through addView. How can I get a View from my RelativeLayout?
you can do this by getChildAt but you will have to keep the position of the view you want to get from layout. To avoid this you can set an id for your view and get the view with findViewById(). If you inflate a view from xml you will get the same views as xml. if you add some new views to the relativeLayout it will not effect the view you inflate from the xml.
when you are adding the view:
myView.setId(id);
layout.addView(myView);
when you are retrieving
View myView = layout.findViewById(id);
I have an activity in which I need to add LinearLayout dynamically, by code (because it depends on the user input).
So, here is what I have done:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout lyt = (LinearLayout) inflater.inflate(R.layout.row_edit, mLytRows);
TextView tvName = (TextView) lyt.findViewById(R.id.name_textview);
tvName.setText(user.getName());
where R.layout.row_edit is a LinearLayout in which there are some Views, including a TextView, whereas mLytRows is a referenced LinearLayout (defined in the XML of the UI), in which I want to add the row_edit layout.
Based on the user input, I repeat this code several times and here is the problem:
when I try to reference the TextView, I aways get the TextView of the first LinearLayout I have added.
Why? How can I solve it, please?
Take a closer look at the documentation.
If you specify the second argument in inflate(), the method returns the parent, not the inflated View.
Hence, lyt is always your parent and findViewById always returns the first element it finds with the R.id.name_textview ID.
You might want to do
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout lyt = (LinearLayout) inflater.inflate(R.layout.row_edit, null);
TextView tvName = (TextView) lyt.findViewById(R.id.name_textview);
tvName.setText(user.getName());
mLytRows.addView(lyt);
And in ViewGroup you can see there are several addView methods to place the view where you want.
In your res/layout/my_image_layout.xml
<LinearLayout
android:id="#+id/buttons"
...>
<ImageView ...
</ImageView>
</LinearLayout>
To grab that Layout by its #+id value inside your app/src/java/MyClass.java code, do this:
String myLinearLayoutName = "buttons";
LinearLayout myLinearLayoutID2 = (LinearLayout) activity.findViewById(activity.getResources()
.getIdentifier(myLinearLayoutName, "id", activity.getPackageName()));
/*Bottom code changes that LinearLayout's background to a different image. "bomb" (R.mipmap.bomb) is the name of an image I have in my drawable folder. */
myLinearLayoutID2.setBackgroundResource(R.mipmap.bomb);