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.
Related
In my onCreate I have a layout which i want to set Dynamically
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View coachLayout = inflater.inflate(R.layout.coach_content, null);
LinearLayout coachLinear = (LinearLayout) coachLayout.findViewById(R.id.coachLinear);
TextView titleView = (TextView) coachLayout.findViewById(R.id.coachTitle);
updateTitleView(activity, widget, titleView);
I wish to pass my View coachlayout, into setContentView(R.layout.activity_main)
However setContentView only accepts the Resource id or int. So is there anyway to pass my coachlayout into setContentView?
Thanks
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 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);
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 created the Layout design using java code only not from the XML Layout Designs. The code I used is following
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(mContext);
tv.setText(hotelList.get(position).name);
return tv;
}
How to use layoutInflator for creating layout fro this. I need 2 more textviews in a single list item. the whole list contains 10 different list items
Please provide some codes for this. Help appreciated
I have gone through this before by having my static class too. Check this out, it will help:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if ( rowView == null) {
LayoutInflater inflator = this._activity.getLayoutInflater();
rowView = inflator.inflate(R.layout.todolistlisting, null);
TodoListViewHolder viewHolder = new TodoListViewHolder();
viewHolder._name = (TextView) rowView.findViewById(R.id.tVTLName);
viewHolder._completed = (TextView) rowView.findViewById(R.id.tVTLCCount);
viewHolder._remaining = (TextView) rowView.findViewById(R.id.tVTLRCount);
rowView.setTag(viewHolder);
}
TodoListViewHolder holder = (TodoListViewHolder) rowView.getTag();
VO_TodoList votodolist = this._items.get(position);
holder._name.setText(votodolist._title);
holder._completed.setText(votodolist._completed);
holder._remaining.setText(votodolist._remaining);
return rowView;
}
TodoListViewHolder is my view component holder here. like your TextView.
I guess you know how to make XML layout for this layout. So just make the XML layout and get the object of the main layout using the following code:
LinearLayout mainLayout=(LinearLayout) View.inflate(R.layout.yourlayout); //if yourlayout.xml is the name of the xml file you made and put in the layout folder.
To get the child of the layout, let's say if it's a TextView with the id text, then the code would be:
TextView textView=(TextView)mainLayout.findViewById(R.id.text);
You can add view at runtime by using inflater like this
LinerLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.news_categories_item, null);
TextView categoryValueTextView = (TextView)linearLayout.findViewById(R.id.news_category_item_value);
mMainLinearLayout.addView(categoryValueTextView);
Here i am inflating one text view which is there in another linear layout(this is simple linear layout which holds only textview) at runtime and adding it to my main linear layout.
you can get the inflater object in your acitivity by using getLayoutInflater(). And if you want to get inflater in adapter you have to pass inflater object to constructor of adapter from your activity.