use custom view
custom_button_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iconImgV"
android:layout_alignParentRight="true"
android:layout_width="100dp"
android:layout_alignParentTop="true"
android:background="#ff0000"
android:layout_height="100dp" />
<TextView
android:id="@+id/titleTv"
android:textSize="18sp"
android:text="Title"
android:layout_toLeftOf="@+id/iconImgV"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/descriptionTv"
android:layout_width="wrap_content"
android:text="description"
android:textSize="14sp"
android:layout_below="@+id/titleTv"
android:layout_toLeftOf="@+id/iconImgV"
android:layout_height="wrap_content" />
</RelativeLayout>
and CustomButtonView.java
public class CustomButtonView extends RelativeLayout {
Context context;
//view
ImageView iconImgV;
TextView titleTv, descriptionTv;
//value
int drawableIcon;
String title,description;
public CustomButtonView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init();
}
public CustomButtonView(Context context) {
super(context);
init();
}
public CustomButtonView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.customButtonViewAttrs);
drawableIcon = typedArray.getInt(R.styleable.customButtonViewAttrs_drawableIcon,0);
title = typedArray.getString(R.styleable.customButtonViewAttrs_title);
description = typedArray.getString(R.styleable.customButtonViewAttrs_description);
typedArray.recycle();
init();
}
public void init() {
if (isInEditMode())
return;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.custom_button_view, null);
iconImgV = (ImageView) myView.findViewById(R.id.iconImgV);
titleTv = (TextView) myView.findViewById(R.id.titleTv);
descriptionTv = (TextView) myView.findViewById(R.id.descriptionTv);
titleTv.setText(title);
addView(myView);
}
}
and value/attrs.xml
<declare-styleable name="customButtonViewAttrs">
<attr name="title" format="string" />
<attr name="description" format="string" />
<attr name="drawableIcon" format="integer" />
</declare-styleable>
you can use this way
<com.example.rasoul.testprojectstacj.CustomButtonView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:title = "Car"/>