Is it possible to pass my customView into setContentView in Android? - android

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

Related

Get nodes from a xml layout in Android

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.

Unable to add dynamic textView in android

I want to add textview dynamically for which I have added the below code.But I am do not see any textview on my activity. I cant add a listview as I am already using recycleview . Any one any error ?
{
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout review_layout = (LinearLayout) rootMovieView.findViewById(R.id.review_comment);
for (ReviewsResult comment : review.getReviewsResults()) {
View view = layoutInflater.inflate(R.layout.layout_movie_comments, review_layout, false);
TextView tvCommentBy = (TextView) view.findViewById(R.id.tv_comment_by);
TextView tvCommentContent = (TextView) view.findViewById(R.id.tv_comment_content);
tvCommentContent.setText(comment.getContent());
tvCommentBy.setText(comment.getAuthor());
review_layout.addView(view);
}
}
You can also try something like this
View myLay; // view that contains you textView
LinearLayout rootlay; //root view in which you want to add
myLay = LayoutInflater.from(this).inflate(R.layout.layout_movie_comments,
null);
TextView tvCommentBy = (TextView) view.findViewById(R.id.tv_comment_by);
TextView tvCommentContent = (TextView) myLay.findViewById(R.id.tv_comment_content);
tvCommentContent.setText(comment.getContent());
tvCommentBy.setText(comment.getAuthor());
rootlay.addView(myLay);
Hope this helps :)
Do below two changes and check weather it is working or not, as nothing else seems to wrong in code.
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.layout_movie_comments, null);
rootMoviewView is not the rootView it was a recylceView in a rootView .This way the textview is generated in rootView not in recylceView.

how to set a text of a textview from another activity

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);

Add custom layout (from file) to another layout

Content view is a LinearLayout. We'll call it llOne and we'll say it's in the file llOne.xml.
The view I'm trying to add is also a LinearLayout but they're in separate files. We'll call it llTwo and we'll say it's in the file llTwo.xml.
setContentView(R.layout.llOne);
LinearLayout llOne = (LinearLayout) findViewById(R.id.llOne);
LinearLayout llTwo = (LinearLayout) findViewById(R.id.llTwo);
llOne.addView(llTwo); //NullPointerException
you need to inflate the second layout, as setContentView only inflate your llOne
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View otherView = inflater.inflate(R.layout.yourSecondLayoutFileName, null);
and then
LinearLayout llTwo = (LinearLayout) otherView .findViewById(R.id.llTwo);
llOne.addView(llTwo);

dynamically cloning a LinearLayout in android?

Say I have a LinearLayout with some elements in it as an .xml file.
In Java, I need to somehow "clone" it a few times into an array, edit some of its children, and then loop through the array, adding each LinearLayout to my main view.
What do you think would be the correct way to "clone" this layout from an xml file into an array element in java?
Thanks!
LayoutInflater vi = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.yourLayoutId, null);
you can do some thing like this to inflate the view, and then modify the element iside the view using the findViewById method. Hope this will help
Something like this:
....
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = null;
for(....) {
layout = (LinearLayout) inflater.inflate(R.layout.YOUR_LAYOUT_ID, null);
someList.add(layout);
}
.....
Try getting the layout in a variable:
for (int c=0; c < count; c++)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.yourmainlayout);
// do something with layout
// assign layout to a variable or add it on another layout
}

Categories

Resources