[Solved] How to pass data from activity to class in android? [closed]


Create a class for your requirement as CustomView and define public method to pass integer value and use it there. As I have created setIntValue().

    public class CustomView extends View {
    private int mIntValue;

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context) {
        super(context);
    }

    public void setIntValue(int value) {
        //Like this you can use this integer value
        //this.mIntValue = value;
        //this.setText(value);
    }
}

Usage:
1. With CustomView class instance directly:

CustomView mView = new CustomView(this);
mView.setIntValue(100);

2. With CustomView in layout xml:

In your layout xml-

<your_app_package_for_custom_view_class.CustomView
        android:id="@+id/customview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

In your Activity-

CustomView mView = (CustomView)findViewById(R.id.customview);
mView.setIntValue(100);

19

solved How to pass data from activity to class in android? [closed]