Reputation: 11
I am new in java/Android, I want to pass method where i have defined my button property. and the the method is is written in another class which can not implement the Activity.
ex
class ViewProvider extends xyz
{
public Button getButton(){
Button one=new Button();
one.setText("abc");
one.setTypeface(Typeface.DEFAULT_BOLD);
//and other properties like color,Gravity ect
return one
}
}
Class calcu extends Activity{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.app_list);
ViewProvider v=new ViewProvider();
RelativeLayout relativelayout = new RelativeLayout(this);
relativelayout.addView(v.getButton);
setContentView(relativelayout);
}
But i am getting an error
Upvotes: 0
Views: 263
Reputation: 564
Just create an Activity data type and initialize it through constructor as below
class ViewProvider extends xyz
Activity activity;
{
public ViewProvider(Activity activity, JSONObject json) {
this.activity = activity;
}
public Button getButton(activity){
Button one=new Button();
one.setText("abc");
return one
}
then call the method as below on onCreate(Bundle savedInstanceState)
ViewProvider v=new ViewProvider(this);
RelativeLayout relativelayout = new RelativeLayout(this);
relativelayout.addView(v.getButton);
setContentView(relativelayout);
Upvotes: 2
Reputation: 133560
If you look at the constructors of button @ you have wrong params.
http://developer.android.com/reference/android/widget/Button.html
Button(Context context)
Button(Context context, AttributeSet attrs)
Button(Context context, AttributeSet attrs, int defStyle)
All the three take context as a param.
I would suggest you to read the docs
http://developer.android.com/guide/topics/ui/declaring-layout.html
Also you have setContentView
twice which is not necessary.
Have the button initialized in activity itself
Class calcu extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout relativelayout = new RelativeLayout(this);
Button one=new Button(this);
one.setText("abc");
relativelayout.addView(one);
setContentView(relativelayout);
}
}
Upvotes: 1