Ok I'm a newbie at Android programming, have a hard time with the graphics part. Understand the beauty of creating layout in XML file, but lost how to access various elements, especially a View element to draw on it.
See example of my layout main.xml here;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Title" android:text="App title"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#000000"
android:background="#A0A0FF"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/PaperLayout" android:layout_width="fill_parent"
android:layout_height="0dp" android:layout_weight="1"
android:orientation="horizontal" android:focusable="true">
<View android:id="@+id/Paper" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/File" android:layout_width="fill_parent"
android:layout_weight="1" android:layout_height="34dp"
android:layout_alignParentTop="true" android:layout_centerInParent="true"
android:clickable="true" android:textSize="10sp" android:text="File" />
<Button android:id="@+id/Edit" android:layout_width="fill_parent"
android:layout_weight="1" android:layout_height="34dp"
android:clickable="true" android:textSize="10sp" android:text="Edit" />
</LinearLayout>
</LinearLayout>
As you can see I have a custom app title bar, then a View filling middle, and finally two buttons in bottom. Catching button events and responding to, for example changing title bar text and changing View background color works fine, but how the heck do I access and more importantly draw on the view defined in main.xml
UPDATE:
That for your suggestion, however besides that I need a View, not ImageView and you are missing a parameter on canvas.drawText and an ending bracket, it does not work.
Now this is most likely because you missed the fact that I am a newbie and assuming I can fill in any blanks.
Now first of all I do NOT understand why in my main.xml layout file I can create a View or even a SurfaceView element, which is what I need, but according to your solution I don't even specify the View like
Anyways I edited my main.xml according to your solution, and slimmed it down a bit for simplicity;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Title" android:text="App title"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#000000"
android:background="#A0A0FF"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/PaperLayout" android:layout_width="fill_parent"
android:layout_height="0dp" android:layout_weight="1"
android:orientation="horizontal" android:focusable="true">
<com.example.MyApp.CustomView
android:id="@+id/Paper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<com.example.colorbook.CustomView/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/File" android:layout_width="fill_parent"
android:layout_weight="1" android:layout_height="34dp"
android:layout_alignParentTop="true" android:layout_centerInParent="true"
android:clickable="true" android:textSize="10sp" android:text="File" />
</LinearLayout>
</LinearLayout>
In my main java file, MyApp.java, I added this after OnCreate;
public class CustomView extends ImageView {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("Your Text", 1, 1, null);
}
}
But I get error on the "CustomView" part;
"Implicit super constructor ImageView() is undefined for default constructor.Must define an explicit constructor"
Eclipse suggests 3 quick fixes about adding constructor, but none helps, well it removes error but gives error on app when running.
I hope somebody can break this down for me and provide a solution, and perhaps explain why I can't just create a View element in my main.xml layotu file and draw on it in code.