Setcontentview By Using Extending View Class
I am using extends View for setting content of an activity..what is happening it is showing blank activity.instead of setting text. public class ThreadCheck extends Activity{
Solution 1:
Change this
view=new MyView(ctx);
to
view=new MyView(ThreadCheck.this);
And change to
publicclassMyViewextendsTextView{
publicMyView(Context context) {
super(context);
this.setText("Hello This Is Myview");
}
}
Edit: to the question in comment. You need a ViewGroup to which you add your views.
publicclassMyViewextendsRelativeLayout{
publicMyView(Context context) {
super(context);
RelativeLayout.LayoutParamslayoutParams=newRelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.setLayoutParams(layoutParams);
RelativeLayout.LayoutParamsparams1=newRelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextViewtv=newTextView(context);
tv.setText("hello");
tv.setId(1);
Buttonb=newButton(context);
params1.addRule(RelativeLayout.BELOW, tv.getId());
b.setText("Hi");
b.setId(2);
this.addView(tv);
this.addView(b, params1);
}
}
Post a Comment for "Setcontentview By Using Extending View Class"