Create a custom View by inflating a layout?
Yes you can do this. RelativeLayout, LinearLayout, etc are Views so a custom layout is a custom view. Just something to consider because if you wanted to create a custom layout you could.
What you want to do is create a Compound Control. You'll create a subclass of RelativeLayout, add all our your components in code (TextView, etc), and in your constructor you can read the attributes passed in from the XML. You can then pass that attribute to your title TextView.
http://developer.android.com/guide/topics/ui/custom-components.html
Android correct place to inflate custom view
The right place to inflate your custom view layout is in the constructor:
class BaseView extends FrameLayout {
public BaseView(Context context) { this(context, null); }
public BaseView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view, this, true);
...
}
}
Manually inflating custom view yields different layouts for ActionBar custom view
Actually, the issue here is that in second case ActionBar needs additonal layout parameters:
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_action_bar, null);
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
So, it covers all ActionBar area. Looks like by default WRAP_CONTENT
llayout parameters get applied to custom view.
How to inflate one view with a layout
I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
How to use View Binding on custom views
Just inform the root, and whether you want to attach to it
init { // inflate binding and add as view
binding = ResultProfileBinding.inflate(LayoutInflater.from(context), this)
}
or
init { // inflate binding and add as view
binding = ResultProfileBinding.inflate(LayoutInflater.from(context), this, true)
}
which inflate method to use will depend on the root layout type in xml.
Custom view not inflating layout when added programmatically
I eventually worked around this by extending FrameLayout instead of View, and passing this to the inflate call. I think the view was being added and inflating, but it didn't know how to lay out the children correctly, which is resolved if you pass the parent to the inflate call initially:
public class AddressView extends FrameLayout {
public AddressView(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.place_address, this);
}
public AddressView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.place_address, this);
}
public AddressView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.place_address, this);
}
}
How to write a custom View class based on a xml layout
One simple way to achieve it is to extend FrameLayout
and attach the inflated layout to oneself (this
) in the constructor:
public class MyView extends FrameLayout {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.my_view, this);
}
// Your view logic here
}
Then you can use your brand new view programatically:
MyView myView = new MyView(context);
Or in an XML layout:
<packageName.MyView
android:layout_width="match_parent"
android:layout_height="match_parent" />
Related Topics
Getting Error "Gradle Dsl Method Not Found: 'Compile()'" When Syncing Build.Gradle
Android Webview HTML5 Video Autoplay Not Working on Android 4.0.3
How to Handle Empty Response Body with Retrofit 2
How to Replace a Fragment on Button Click of That Fragment
Stackoverflow: Caused by Nested Views
How to Store Hashmap So That It Can Be Retained It Value After a Device Reboot
Android Studio: Exclude Resource File Under Resources Sourcesets
How to Create Ripple Effect in Simple Layout
Android: HTML Anchor Link Works Only Once in Webview
How to Load Image into Imageview from Url Using Glide V4.0.0Rc1
Android-Textview Settext in HTML.Fromhtml to Display Image and Text
Which HTML Tags Are Supported by Android Textview
How to Create a Date and Time Picker in Android
Partial Invalidation in Custom Android View with Hardware Acceleration