I’m new to Android app development and I would like to ask something.
I have a bitmap inside my SurfaceView that I want to animate and move programmatically. I also want to put a button in the screen to control the moving of the bitmap. But since I cannot put a button in a SurfaceView, I declared it inside a RelativeLayout in my XML layout called main.xml.
Here’s what’s inside my main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_layout" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Test Button" />
</RelativeLayout>
So what I’m trying to do is to “combine” (can’t find a better word) the SurfaceView with the Layout that is in my main.xml.
Here is my Main.java where I try to combine the Layout w/ the SurfaceView
package com.src.test.view;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
public class Main extends Activity {
private RelativeLayout _myXmlLayout;
private MyView _myView;
private Button _myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_myXmlLayout = (RelativeLayout) findViewById(R.id.main_layout);
_myView = new MyView(this);
_myButton = (Button) findViewById(R.id.button1);
_myXmlLayout.addView(_myButton);
_myXmlLayout.addView(_myView);
setContentView(_myXmlLayout);
}
}
but it gives me an “application has stopped unexpectedly” error. Am I doing it wrong? If so, can you suggest a better way of implementing this?
Your response is highly appreciated. Thanks!
p.s. no bitmap animation codes and button controls yet. It’s just the views that I’m currently solving. 🙂
EDIT:
I edited my codes and here it is:
so here’s my edited Main.java
package com.src.test.view;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
}
and here’s my main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.src.test.view.MyView
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:text="Test Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left" />
<Button
android:text="Test Button2"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" />
</FrameLayout>
I tried to run it but it gives me an error.
Here’s the logcat
(ZygoteInit.java:626)
10-29 01:18:51.046: E/AndroidRuntime(318): at dalvik.system.NativeStart.main(Native Method)
10-29 01:18:51.046: E/AndroidRuntime(318): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.src.test.view.MyView
10-29 01:18:51.046: E/AndroidRuntime(318): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
10-29 01:18:51.046: E/AndroidRuntime(318): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
10-29 01:18:51.046: E/AndroidRuntime(318): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
10-29 01:18:51.046: E/AndroidRuntime(318): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-29 01:18:51.046: E/AndroidRuntime(318): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-29 01:18:51.046: E/AndroidRuntime(318): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-29 01:18:51.046: E/AndroidRuntime(318): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
10-29 01:18:51.046: E/AndroidRuntime(318): at android.app.Activity.setContentView(Activity.java:1647)
10-29 01:18:51.046: E/AndroidRuntime(318): at com.src.test.view.Main.onCreate(Main.java:12)
10-29 01:18:51.046: E/AndroidRuntime(318): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-29 01:18:51.046: E/AndroidRuntime(318): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-29 01:18:51.046: E/AndroidRuntime(318): ... 11 more
10-29 01:18:51.046: E/AndroidRuntime(318): Caused by: java.lang.NoSuchMethodException: MyView(Context,AttributeSet)
10-29 01:18:51.046: E/AndroidRuntime(318): at java.lang.Class.getMatchingConstructor(Class.java:660)
10-29 01:18:51.046: E/AndroidRuntime(318): at java.lang.Class.getConstructor(Class.java:477)
10-29 01:18:51.046: E/AndroidRuntime(318): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
10-29 01:18:51.046: E/AndroidRuntime(318): ... 21 more
Thank you for your response
I think you are overcomplicating this for yourself. At least, to simply put a button over a SurfaceView, visually, you can do something like what is shown here, by putting the button in the same FrameLayout as the surface view.
You can declare your custom view in xml as well, so it could look like:
then in
onCreateyou only have to sayYou can position the button wherever you wish, for example if you want it to be in the bottom center, use
android:layout_gravity="bottom|center_horizontal".EDIT:
To make sure that your custom view implements all the necessary constructors (some of which are called when views are created from xml), open your custom view class in the eclipse editor, and go to the menu Source -> Generate Constructors from Superclass…
It will help you create constructors