Reputation: 1131
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 button
s in the bottom. Catching button
s' 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 <View ....../>
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;
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 import
s 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!
Upvotes: 1
Views: 4829
Reputation: 1103
To draw on a view, creat your own custom view.
To do so, you need to add a new class. With eclipse it's just File->New->Class and there you go. Override the onDraw()-Method to fit your needs and add your CustomView to your Layout:
public class CustomView extends ImageView {
// The needed constructors:
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@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", xposition, yposition, paint);
}
}
In your xml-File you add your CustomView like this:
<com.example.CustomView
android:id="@+id/yourID"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.example.CustomView>
If you wan't to draw onto a drawable from your res-Folder, specifiy the "src"-parameter of your customView.
Upvotes: 3
Reputation: 16633
to use the view defined in main.xml in an Activity, override Activity's onCreate
method such as:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
edit: sorry, I misread "draw on view" as "draw view" in your question :) See L0rdAli3n's answer for that.
Upvotes: 0
Reputation: 13582
override the onCreate method and then use the method findViewById
. For example to use and modify the view background color use the following :
View v = (View)findViewById(R.id.Paper);
v.setBackgroundColor(myColor);
Similarly modify any other view/Widget defined in your layout
Upvotes: 0