Skip to content Skip to sidebar Skip to footer

Sending Text From Fragment To Custom View Where I Want To Use Canvas.drawtext

I am new to Android and want to pass some text from a fragment to a custom view. Inside the custom view I want to use canvas.drawText to position the string. The reason I want to u

Solution 1:

You could create the com.example.stuff.View1 custom view dynamically, i.e. dont add it in the xml but add it using the code. And then you could pass the text in the constructor of the com.example.stuff.View1. Other way could be to create a method in the com.example.stuff.View1 class and set the text there. For example

publicclassView1extendsTextView {

    //constructors:publicView1(Context context, AttributeSet ats, int ds) {
        super(context, ats, ds);
        init();
    }

    publicView1(Context context) {
        super(context);
        init();
    }

    publicView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    publicvoidsetMyText(String string) {
        myString=string;
    }
...
canvas.drawText(myString, margin1, margin2, paint);
....
}

then in your code do something like this

Viewview= inflater.inflate(R.layout.fragment_layout, container,false);
    txtBox = (TextView) view.findViewById(R.id.TxtBox);
    StringmyString="testing";
    txtBox.setMyText(myString);

Solution 2:

Post a Comment for "Sending Text From Fragment To Custom View Where I Want To Use Canvas.drawtext"