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.
Related
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.
I always had ambiguity on why we need to use inflater in android, Why are they used in ListView for custom layouts (like below)?
What is an Inflater ?
What is the advantage of using Inflater ?
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_mobile, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
Thanks,
What is an Inflater ?
To summarize what the LayoutInflater Documentation says... A LayoutInflater is one of the Android System Services that is responsible for taking your XML files that define a layout, and converting them into View objects. The OS then uses these view objects to draw the screen.
I always had ambiguity on why we need to use inflater in android, Why
are they used in android ListView for a custom layout ?
Typically, you don't ever need to directly use a LayoutInflater. Android does most of the layout inflation for you when you call setContentView() in the onCreate() method of your activity. So you, as the programmer, are responsible for making sure the views are inflated. Now you want to inflate views in the context of a ListView. The Adapter class can do the inflation for you if you do not want to customize each item. But if you want to customize the views shown in a list, you will have to manually inflate each view with the LayoutInflater, since there is no other existing method you can use.
What is the advantage of using Inflater ?
There is no advantage to using it. You are required to use a LayoutInflater in some shape or form to inflate your static XML layouts.
Alternatively, you could create views dynamically with java code. However, you would need to call methods to set each property for the view by hand. In my opinion, it is easier to use the XML/inflation process. In addition, Android pre-processes your XML files at build time, so this results in a faster execution time.
I always had ambiguity on why wee need to use inflater in android, Why are they used in android ListView for a custom layout ?
They are used to create the Views for each row.
What is an Inflater ?
A system service that creates a View out of an XML layout.
In the below code i am trying to understand why Inflator is used ?
The inflater is used to create the Views for the rows.
What is the advantage of using Inflater ?
Compared to what? How do you want to create the Views out of the XML layout?
Put simply, an inflater allows you to create a View from a resource layout file so that you do not need to create everything programmatically.
In your example, you inflate the layout R.layout.list_mobile. This lets you access all of the views within it. For example, you then call:
TextView textView = (TextView) rowView.findViewById(R.id.label);
By calling rowView.findViewById() you are able to access views that were created within that layout. Often for ListViews you will have a row XML file that you then inflate and put your data into the views.
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.
I have a custom class called NoteView that extends FrameLayout. Previously, I had just been using the stock LinearLayout and have an XML layout built already that I want to reuse by adding that entire hierarchy (with some other overlay views which is why I needed framelayout) to the NoteView.
The problem is I can't figure out how to inflate the XML and add it to the NoteView from within the NoteView Class. I can add it after initialization is complete, but I want to be able to be able to inflate that hierarchy and add it automatically when my NoteView is either instantiated or inflated from XML.
How can I populate my extended FrameLayout with a layout from XML by adding it from within the NoteView class itself?
Try LayoutInflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myView = (LinearLayout) inflater.inflate(R.layout.my_layout, null);
frameLayout.addChild(myView);
Have a look at the documentation for LayoutInflater here: http://d.android.com/reference/android/view/LayoutInflater.html