Reputation: 37
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{
MyView view;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Context ctx = getApplicationContext();
view=new MyView(ctx);
setContentView(view);
}
}
public class MyView extends View{
TextView tvThread;
public MyView(Context context) {
super(context);
tvThread=new TextView(context);
tvThread.setText("Hello This Is Myview");
// TODO Auto-generated constructor stub
}
}
Upvotes: 0
Views: 4875
Reputation: 133560
Change this
view=new MyView(ctx);
to
view=new MyView(ThreadCheck.this);
And change to
public class MyView extends TextView{
public MyView(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.
public class MyView extends RelativeLayout{
public MyView(Context context) {
super(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(context);
tv.setText("hello");
tv.setId(1);
Button b = new Button(context);
params1.addRule(RelativeLayout.BELOW, tv.getId());
b.setText("Hi");
b.setId(2);
this.addView(tv);
this.addView(b, params1);
}
}
Upvotes: 2