Stefan Stefan - 1 year ago 160
Android Question

Reference findViewbyID from Fragment in Non-Fragment class

I have a Fragment class

in which I can directly reference


In my second non-fragment class
I want to address this View and change its background.

How can I address it in onInitSuccess like so:

public void onInitSuccess() {
// EchossStampActivity.setText("onInitSuccess");

in the

EchossStampFragment Class

public class EchossStampFragment extends Fragment implements EchossStampInterface {
private static final String TAG = MainActivity.class.getSimpleName();
View myView;
private SupportMapFragment mapFragment;
EchossMainView mainView = null;
static TextView textView = null;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.activity_echoss, container, false);
return myView;

public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mainView = (EchossMainView) getView().findViewById(;
textView = (TextView) getView().findViewById(;
FragmentManager fm = getChildFragmentManager();

// Display Initialization

DisplayMetrics metric = new DisplayMetrics();

mainView.initEchossMainView(this); // this is where we are passing the current activity as interface

// Library Initialization for Stamp
mainView.echossPlatformInit(APIKEY, REGION, LANGUAGE, INDEX);


EchossMainView Class

public class EchossMainView extends StampBaseView {

private EchossStampInterface mEchossStampInterface;

public EchossMainView(Context context) {

public EchossMainView(Context context, AttributeSet attrs) {
super(context, attrs);

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

public void onInitSuccess() {

public void onInitError(String code, String message) {
EchossStampFragment.setText("onInitError - " + code + " " + message);


public void onStampSuccess(String stampResult) {
EchossStampFragment.setText("onStampSuccess - " + stampResult);


public void onStampError(String code, String message) {
EchossStampFragment.setText("onStampError - " + code + " " + message);

Answer Source

Since EchossMainView must eventually extend from View, you can call setBackground from within that class itself:

public void onInitSuccess() {

Docs for method here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download