Question

I have been searching for the past few hours to the answer to a very dumb question. I know how to draw on the canvas in android if you extend the view class, modify onDraw and set setContentView() to a new instance of that class. However, I need to also have 2 TextViews and 2 EditTexts at the bottom of the activity and if setContentView() is set to only have that view, these views will, obviously, not display. How can put all of these on the screen?

EDIT: Here is my code: (the package name is android.physicsengine)

package android.physicsengine;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class ResultantForceEngine extends Activity {
private EditText mag;
private EditText dir;
private View image;
private RelativeLayout layout;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resultant_force);

    mag = (EditText)findViewById(R.id.magnitude);
    dir = (EditText)findViewById(R.id.direction);
}

public class MyView extends View{
    public MyView(Context context){

        super(context);
    }
    public MyView(Context context, AttributeSet attrs){

        super(context, attrs);

    }
    public MyView(Context context, AttributeSet attrs, int defStyle){

        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas){
        canvas.drawColor(Color.BLACK);
        Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(Color.RED);
        canvas.drawLine(canvas.getWidth()/2, canvas.getHeight()/2-200, canvas.getWidth()/2 ,canvas.getHeight()/2+200, circlePaint);
        canvas.drawLine(canvas.getWidth()/2-200, canvas.getHeight()/2, canvas.getWidth()/2+200 ,canvas.getHeight()/2, circlePaint);
    }
}
}

and the xml

<view class="android.physicsengine.ResultantForceEngine$MyView"
              android:id="@+id/image"
              android:layout_alignParentLeft="true"
              android:layout_alignParentRight="true"
              android:layout_alignParentTop="true"
              android:layout_alignParentBottom="true"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" />
<TextView     android:id="@+id/magText"
              android:text="Magnitude (N) ="
              android:textSize="15dip"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="3dip"
              android:gravity="center_vertical" 
              android:layout_alignParentLeft="true"
              android:layout_alignParentBottom="true" />
    <EditText android:id="@+id/magnitude"
              android:inputType="numberDecimal"
              android:layout_alignParentBottom="true" 
              android:layout_toRightOf ="@id/magText"
              android:layout_width="wrap_content"
              android:padding="3dip"
              android:layout_height="wrap_content" />

    <TextView android:id="@+id/dirText"
              android:text="Angle (deg) ="
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="15dip"
              android:layout_alignParentBottom="true"
              android:layout_toRightOf ="@id/magnitude"
              android:padding="3dip"
              android:gravity="center_vertical" />
    <EditText android:id="@+id/direction"
              android:inputType="numberDecimal"
              android:layout_alignParentBottom="true" 
              android:layout_toRightOf ="@id/dirText"
              android:layout_alignParentRight="true"
              android:layout_height="wrap_content" 
              android:padding="3dip"
              android:layout_width="wrap_content"/>

Was it helpful?

Solution

Basically you need to define your XML file with your custom view class and the other widgets you have. In your case, there would be the custom view, 2 textviews and 2 edittexts in the XML file.

You define the custom view in XML just like any other widget, except you use the namespace of the view and the class name.

<com.example.android.myCustomView
    android:id="@+id/my_custom_view"
    ...

Then inside your activity a simple call

setContentView(R.layout.main); 

Edit: The problem is your class is private, so it "isn't seen" when your activity makes a call the layout and tries to inflate it.

Edit2: Of course this won't work, you're using an inner class! You have to communicate stuff like this if you expect to get answers.

The XML syntax for an inner class is different.

<view class="com.example.android.MyClass$MyInnerClass"

OTHER TIPS

You should be able to use the class that you extended in your res/layout/main.xml just like you would any other View class, and add your TextView and EditTexts in the layout .xml file like normal. I have not personally done this, but I have used custom View classes in this way.

Then for your setContentView you would just use the layout .xml file like:

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top