I have problems with adding a custom class to my Main Activity.
code in my custom class:
public class DetailView extends View {
public DetailView(Context context) {
super(context);
this.setBackgroundColor(0xFF00FF00 );
}
}
code in main activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setOrientation(LinearLayout.VERTICAL);
txt = new TextView(this);
txt.setText("hello");
txt.setId(6);
txt.setLayoutParams(params);
linearLayout.addView(txt);
DetailView detailView = new DetailView(this.getApplicationContext());
linearLayout.addView(detailView);
}
Why can't is see the detailView? Im new to android development so I need any help I can get, or some good links or anything. Thanks
The view is added, but it has no dimension set. Looking at existing code I guess you want it do fill the width and have a small height - just assuming. So try the following:
DetailView detailView = new DetailView(this);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2); // 2 pixels height
linearLayout.addView(detailView, params);
Also, the context of the DetailView
is the activity, not the application context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With