I want to make expandable recycler view with dynamic no of item. I need same recycler view as imagur comment section for each image. How to add that dynamic no of childern view in row of recyclerview.
Thank you
I asked similar question but no one answer so i searched and find a way.
you can define a layout for your view and inflate it programmatically.
it's not against preferences in Android. So inflate your layout using LayoutInflater :
LayoutInflater inflater = LayoutInflater.from(context);
View inflatedLayout= inflater.inflate(R.layout.yourLayout, null, false);
yourParentView.addView(inflatedLayout);
"yourParentView" is the Relative or Linear Layout. don't forget to add this LayoutInflater in your "onBindViewHolder". hope this work.
Related
I need to generate a dynamic custom view in my application.
I need a custom view that is formed by an image button and 2 text views, above and bellow the button. There should also be an onClick listener on the image button, that will call a function when pressed.
By dynamic I mean those views will be created on demand, there should be a "dummy" structure from which I should be able to create as many custom views as I need.
How can I do that?
Just create the layout for your view with an xml and then use the layout inflater:
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View yourView = inflater.inflate(R.layout.view_layout, parent, false);
To access it's components then you use:
ImageButton imageButton = (ImageButton) yourView.findViewById(R.id.button);
You can start by creating a custom view class MyCustomView that extends the View class. Create a separate XML layout (the dummy structure) for the custom view and inflate it into the custom view constructor,
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_custom_view_layout, this, true);
You can add any listeners to the Button views that will make part of this compound view class. Take a look at this link for further understanding: http://www.vogella.com/tutorials/AndroidCustomViews/article.html
At the end, just treat this object as just a view and you place it on the interface wherever you want.
I'm trying to create something like editable UITableView on iPhone.
I added a button to add new items at the end of the list which would be the equivalent of the "+" button on iphone, this takes user to a list of items to add. My problem is that when they select one I don't know how to add it to the list.
<LinearLayout
android:key="application_preferences"
android:title="Applications">
</LinearLayout>
How can I append a view inside the LinearLayout programatically?
You can use addView to add a child view to a ViewGroup, but also consider just using ListView instead of a LinearLayout.
Your question is not very clear but if you try to add a view to the linear layout programmatically just use Layout Inflater
LayoutInflater localLayoutInflater =(LayoutInflater)getBaseContext().getSystemService("layout_inflater");
View myView = localLayoutInflater.inflate(R.layout.custom_layout, null);
final LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayoutID);
ll.addView(myView);
I'm trying to create a scrollable layout with an embedded list, similar to the Spotify application for Android one shown here (One image at the top followed by a list and the whole layout scrolls). I'm aware that the Android developer guidelines state that you shouldn't put a ListView inside of a ScrollView layout so I'm wondering how this is accomplished. Do I need to make each list item manually using layouts and TextViews?
Yes as the List view has default scroll able functionality . If you place a listview inside the scroll view . The list scroll will hamper.
To Avoid such thing , list view has the concepts of header and footer views.
You can add n number of header and footers to the list.
Below is a sample code snippet how to add header/footer by inflating any xml layout to it
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout listFooterView = (LinearLayout)inflater.inflate(
R.layout.footer_layout, null);
list.addFooterView(listFooterView);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.header_layout, null);
list.addHeaderView(listHeaderView);
I have created some xml layouts. Now I am creating a custom layout (in my java file) with some elements and want to add the previously created layout(xml layouts). How can I do that?
You can use LayoutInflater to inlate layouts. And add inflated view to your custom view by addView() method
For ex:
LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewTobeLoaded = mInflater.inflate(R.layout.ilan_list_item, null);
yourView.addView(viewTobeLoaded);
Hope helps
Use <include> tag in your new layout. See docs here.
I have a pretty complex layout defined in xml file , now I want to add this layout as a view using addView or something else.
As layout is pretty much complex , its pretty cumbersome process to write the code for layout and then add it as a view. So is there anyway to load layout resource and add it as a view.
I want to add the layout into WebView
Use
LayoutInflater factory = LayoutInflater.from(this);
View myView = factory.inflate(R.layout.my_layout_id, null);
then use addView(myView)
You can also reduce this to one line of code;
View view = View.inflate(getActivity, R.layout.my_layout,null);
then add to your view.
If your view is a ViewGroup (layout)
You can use InflaterService.inflate(id, ViewGroup) and set the ViewGroup, it will set the current child(s) with the content of your xml.