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.
Asked
Active
Viewed 2,130 times
0
-
2I think what you need is to define your own event listener. Look here: http://stackoverflow.com/questions/8292712/android-custom-event-listener – Boris Mocialov Jun 06 '13 at 12:07
-
Are you talking about 1 Activity (which has the View in it) Or are you talking about multiple Activities? – RvdK Jun 06 '13 at 12:26
-
I am talking about 1 activity which has view in it – user2459539 Jun 06 '13 at 13:38
2 Answers
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();

Shubham Baldava
- 121
- 5
-
it is not allowed as i want to start the activity on which i already added that view – user2459539 Jun 06 '13 at 12:18
-
1Sorry, but restarting your own Activity instead of updating it seems to me a horrible solution... – RvdK Jun 06 '13 at 12:40
-
yes Rvdk i also want to update the activity only Is there any solution for updating from custom view? – user2459539 Jun 06 '13 at 12:44
-
@user2459539 what exactly do you wish to update in your activity (the one that contains your custom view)? – Boris Mocialov Jun 06 '13 at 12:46
-
actually there are 2 custom views which i want to use out of which only one will be on activity at a time & on some event detection from that custom view i want to remove that custom view & add another custom view on activity. – user2459539 Jun 06 '13 at 12:50
-
@user2459539 yup, my original answer is one way to go. Have fun – Boris Mocialov Jun 06 '13 at 12:52
-
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