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: Thank you very much 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 layout file and draw on it in code.
UPDATE: Ok I am still trying to use your solution, which is still giving me problems and various errors. But first and foremost I still do NOT understand why I have to create a custom class to use in my layout, when I have to option to even use the wysiwyg layout editor to insert a View element and "just" draw on that in my main, well for now only code file. So pleeeeeease someone answer me that one.
Anyways now I have;
package com.example.myapp;
import com.example.myapp.CustomView;
and here comes other import
public class MyApp extends Activity {
rest of code including OnCreate
And i created a new class, new file CustomView.Java, where I directly pasted your code so it looks like;
package com.example.myapp;
public class CustomView extends ImageView {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setARGB(255, 150, 205, 129);
paint.setTextSize(20);
canvas.drawText("Your Text", 1, 1, paint);
}
}
Ok so this class code wants me to add imports for ImageView, Canvas, and Paint, which I did, and then again I am back to the error the public class "CustomView" part; "Implicit super constructor ImageView() is undefined for default constructor.Must define an explicit constructor".
I appreciate you trying to help me, THANKS!