Reputation: 1
Good Morning,
I just started my first android project (but have knowledge in Java, VB, etc).
I'm searching all day long bt untl know I was unable to find an answer to my problem, I find a lot of ways to draw on an empty screen ... But I searching a methode to draw multiple diagramms and so on directly on my MainActivity next to my buttons and other controls. In Java I would have used different drawPanels to get the job down but due to missing swing library I can't proceed that way. Are their good tutorials out there on that topic or do you have an advice? I'm glad for every tipp.
Dwon there / you will find an example of my current try, but as said it will just draw the "BAttery" and not the activity components:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawBattery draw = new DrawBattery(this, 96);
setContentView(draw);
}
-
public class DrawBattery extends View {
private int batteryCharge = 0; //battery charge in %
public DrawBattery(Context pContext, int pBatteryCharge)
{
super(pContext);
batteryCharge = pBatteryCharge;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
...
Upvotes: 0
Views: 111
Reputation: 1254
You should use custom View
s. You can create a new class that extends View
, then override the onDraw
method (and onMeasure / onSizeChanged if you need).
When you want to draw something to the View, you can, for instance, create a setter / init method on the view that will initialize the data that you want to draw.
Then, put your view(s) inside a custom layout, and inflate an activity / fragment with that layout. If you need more or you need them to be dynamically added you can programatically add them in the code.
Upvotes: 0
Reputation: 1006614
Rather than calling setContentView()
twice, have your res/layout/activity_main.xml
put an instance of your DrawBattery
View
where you want it.
You may wish to read through the "Creating Custom Views" portion of the Android documentation.
Upvotes: 1