Create a custom View by inflating a layout ?

Create a custom View by inflating a layout ?

Asked on January 12, 2019 in XML.
Add Comment


  • 7 Answer(s)

    To create a custom View by inflating a layout:

         Create a Compound Control first. Then create a subclass of RelativeLayout, add all the components in code and in the constructor we can read the attributes passed in from the XML. Then we can pass that attribute to our title TextView.

    Answered on January 12, 2019.
    Add Comment

         Try the following code to create a custom view by inflating a layout

    public class MyView extends FrameLayout {
       public MyView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
          initView();
       }
     
       public MyView(Context context, AttributeSet attrs) {
          super(context, attrs);
          initView();
       }
     
       public MyView(Context context) {
          super(context);
          initView();
       }
     
       private void initView() {
          View view = inflate(getContext(), R.layout.my_view_layout, null);
          addView(view);
       }
    }
    
    Answered on January 12, 2019.
    Add Comment

    This could be an alternative code:

    public View myView(){
       View v; // Creating an instance for View Object
       LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       v = inflater.inflate(R.layout.myview, null);
     
       TextView text1 = (TextView) v.findViewById(R.id.dolphinTitle);
       Button btn1 = (Button) v.findViewById(R.id.dolphinMinusButton);
       TextView text2 = (TextView) v.findViewById(R.id. dolphinValue);
       Button btn2 = (Button) v.findViewById(R.id. dolphinPlusButton);
     
       return v;
    }
    
    Answered on January 12, 2019.
    Add Comment
    <RelativeLayout
     android:id="@+id/dolphinLine"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
        android:layout_centerInParent="true"
     android:background="@drawable/background_box_light_blue"
     android:padding="10dip"
     android:layout_margin="10dip">
      <TextView
       android:id="@+id/dolphinTitle"
       android:layout_width="200dip"
       android:layout_height="100dip"
       android:layout_alignParentLeft="true"
       android:layout_marginLeft="10dip"
       android:text="@string/my_title"
       android:textSize="30dip"
       android:textStyle="bold"
       android:textColor="#2E4C71"
       android:gravity="center"/>
      <Button
       android:id="@+id/dolphinMinusButton"
       android:layout_width="100dip"
       android:layout_height="100dip"
       android:layout_toRightOf="@+id/dolphinTitle"
       android:layout_marginLeft="30dip"
       android:text="@string/minus_button"
       android:textSize="70dip"
       android:textStyle="bold"
       android:gravity="center"
       android:layout_marginTop="1dip"
       android:background="@drawable/button_blue_square_selector"
       android:textColor="#FFFFFF"
       android:onClick="onClick"/>
      <TextView
       android:id="@+id/dolphinValue"
       android:layout_width="100dip"
       android:layout_height="100dip"
       android:layout_marginLeft="15dip"
       android:background="@android:drawable/editbox_background"
       android:layout_toRightOf="@+id/dolphinMinusButton"
       android:text="0"
       android:textColor="#2E4C71"
       android:textSize="50dip"
       android:gravity="center"
       android:textStyle="bold"
       android:inputType="none"/>
      <Button
       android:id="@+id/dolphinPlusButton"
       android:layout_width="100dip"
       android:layout_height="100dip"
       android:layout_toRightOf="@+id/dolphinValue"
       android:layout_marginLeft="15dip"
       android:text="@string/plus_button"
       android:textSize="70dip"
       android:textStyle="bold"
       android:gravity="center"
       android:layout_marginTop="1dip"
       android:background="@drawable/button_blue_square_selector"
       android:textColor="#FFFFFF"
       android:onClick="onClick"/>
    </RelativeLayout>
    Answered on February 25, 2019.
    Add Comment
    public View myView(){
        View v; // Creating an instance for View Object
        LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.myview, null);
    
        TextView text1 = (TextView) v.findViewById(R.id.dolphinTitle);
        Button btn1 = (Button) v.findViewById(R.id.dolphinMinusButton);
        TextView text2 = (TextView) v.findViewById(R.id. dolphinValue);
        Button btn2 = (Button) v.findViewById(R.id. dolphinPlusButton);
    
    
        return v;
    }
    Answered on February 25, 2019.
    Add Comment
    <resources>
    
        <declare-styleable name="CustomView">
            <attr format="string" name="text"/>
            <attr format="reference" name="image"/>
        </declare-styleable>
    </resources>
    Answered on February 25, 2019.
    Add Comment
    class CustomView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
            ConstraintLayout(context, attrs, defStyleAttr) {
    
        init {
            init(attrs)
        }
    
        private fun init(attrs: AttributeSet?) {
            View.inflate(context, R.layout.custom_layout, this)
    
            val ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView)
            try {
                val text = ta.getString(R.styleable.CustomView_text)
                val drawableId = ta.getResourceId(R.styleable.CustomView_image, 0)
                if (drawableId != 0) {
                    val drawable = AppCompatResources.getDrawable(context, drawableId)
                    image_thumb.setImageDrawable(drawable)
                }
                text_title.text = text
            } finally {
                ta.recycle()
            }
        }
    }
    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.