0

I have prepared one custom view & it is added on one of activity.now on the events which will happen on that view according to that i want to change the content of the activity.As the custom view class & activity classes are different i am unable to handle it.please help me out for this.thanks in advnce.

Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55
user2459539
  • 29
  • 1
  • 5

2 Answers2

0

if you have to update any content on Main_activity(which contains view) then you should create a Main_activity variable in custom view like this:

private Main_activity activity;

and then u can call any function update() in main activity like this:

this.activity.update();
0

In order to construct application with two custom views and with custom listeners from main activity:

I know it had been many similar posts on the internet / throughout SO, but this one is simple and complete.

Your activity class:

public class MyActivity extends Activity{
  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    //initializing custom views
    MyCustomView1 myCustomView1 = new MyCustomView1(parameterList);
    MyCustomView2 myCustomView2 = new MyCustomView2(parameterList);

    //adding both custom views to the main activity
    mainView.addView(myCustomView1);
    mainView.addView(myCustomView1);

    //adding custom listener to the custom view 1
    myCustomView1.setCustomEventListener(new OnCustomEventListener() {

        @Override
        public void onEvent() {
            //firing an event of custom view 1
            Toast.makeText(MainActivity.this, "Touched custom view 1",
                    Toast.LENGTH_SHORT).show();
        }
    });

    //adding custom listener to the custom view 2
    myCustomView2.setCustomEventListener(new OnCustomEventListener() {

        @Override
        public void onEvent() {
            //firing an event of custom view 2
            Toast.makeText(MainActivity.this, "Touched custom view 2",
                    Toast.LENGTH_SHORT).show();
        }
    });
  }
}

Your MyCustomView1 class:

public class MyCustomView1 extends LinearLayout{
  OnCustomEventListener myCustomEventListener;

  public MyCustomView1(ParameterList){
    super(ContextFromParameterList);
    //Just adding something to the custom view 1 in order to distinguish it on the screen
    TextView tv = new TextView(ContextFromParameterList);
    tv.setText("Hello world from custom view 1");
    addView(tv);

    this.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //delegating one event to another (delegating touch event to custom event)
            if (MyCustomView1.this.myCustomEventListener != null)
                MyCustomView1.this.myCustomEventListener.onEvent();
            return false;
        }
    });
  }

  public void setCustomEventListener(OnCustomEventListener eventListener) {
    //setting custom listener from activity
    myCustomEventListener = eventListener;
  }
}

Your MyCustomView2 class:

public class MyCustomView2 extends LinearLayout {
OnCustomEventListener myCustomEventListener;

public MyCustomView2(ParameterList) {
    super(ContextFromParameterList);
    //Just adding something to the custom view 1 in order to distinguish it on the screen
    TextView tv = new TextView(ContextFromParameterList);
    tv.setText("Hello world from custom view 2");
    addView(tv);

    this.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //delegating one event to another (delegating touch event to custom event)
            if (MyCustomView2.this.myCustomEventListener != null)
                MyCustomView2.this.myCustomEventListener.onEvent();
            return false;
        }
    });
}

  public void setCustomEventListener(OnCustomEventListener eventListener) {
    //setting custom listener from activity
    myCustomEventListener = eventListener;
  }
}

Your listener interface:

public interface OnCustomEventListener{
  //interface defines one method. Can be more and methods may have parameters
  public void onEvent();
}

So, instead of showing toasts as I do it here in activity, you may do it your own way - hide one view, show another, etc.

Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55