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.
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
I have this problem with my InfoWindow in my Android app. When I run this code:
#Override
public View getInfoWindow(Marker marker) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.lkonty, null);
TextView txtName= (TextView) findViewById(R.id.txtName);
txtName.setText(marker.getTitle());
return view;
The app crashes while running the setText method. I found a solution by adding this line of code:
setContentView(R.layout.lkonty);
The app is no longer crashing but now the InfoWindow fills the whole screen and I don't know how to change that.
Try this
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.lkonty,parent,false);
TextView txtName= (TextView) view.findViewById(R.id.txtName);
txtName.setText(marker.getTitle());
App crashes because your textview txtName is null.
You must find your txtName by view you inflated.
It will like this:
#Override
public View getInfoWindow(Marker marker) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.lkonty, null);
TextView txtName= (TextView) view.findViewById(R.id.txtName);
txtName.setText(marker.getTitle());
return view;
}
i think you forgot to use you view while binding your textview.
TextView txtName= (TextView) view.findViewById(R.id.txtName);
you have to use view while use of inflater.
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 created two activites, the first one is containing the dynamic added textview and the second one will do some operation then settext to the dynamic added textview in activity one. please help to suggest a sample code for my reference. Thank you!
To add textViews can dynamically using the following method.
private void addChild(boolean right) {
LayoutInflater inflater = LayoutInflater.from(this);
int id = R.layout.unLayout_;
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(id, null, false);
TextView textView = (TextView) relativeLayout.findViewById(R.id.textViewDate);
textView.setText(String.valueOf(System.currentTimeMillis()));
layout.addView(relativeLayout);
}
So far I have been able to add a few LinearLayouts to another LinearLayout using this bit of code:
setContentView(R.layout.view_editscore);
ViewGroup container = (ViewGroup) findViewById(R.id.container1);
for (String s : PlayersActivity.playersList) {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.edit_duplicate, null);
TextView tv = (TextView) view.findViewById(R.id.userName);
tv.setText(s);
container.addView(view);
}
And then I am also able to fill a custom AlertDialog with the R.layout.view_editscore View using this code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.editScore);
LayoutInflater in = getLayoutInflater();
builder.setView(in.inflate(R.layout.view_editscore, null));
But for some reason, when I use the setView() to put the R.layout.view_editscore in the AlertDialog it only adds the version that was the "outer shell" before I added each of the LinearLayouts.
I have tried building the custom AlertDialog both before and after I fill the View with the other LinearLayouts but both ways are still yielding the same result.
What could I be doing wrong?