0

I have the following code where MyClass basically extends View. I was wondering if I need to use both setContentView(R.layout.activity_mainlcass_app) and setContentView(myDrawing) to show the 2D graphics that I draw in MyClass.

public class MainClass extends Activity {
    MyClass myDrawing;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainlcass_app);

        myDrawing = new myDrawing(this);
            setContentView(myDrawing);
            myDrawing.requestFocus();
    }
}
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
MKS
  • 736
  • 2
  • 14
  • 33
  • 1
    Why do you want to implement like this ? You can set multiple views in the same activity but you need to inflate it based on some condition . You can not inflate two views at the same time as you are doing. – GrIsHu Jan 25 '13 at 07:07
  • 1
    you can use addContentView for your custom view to be drawn on top ov the view inflated from xml..Have you tried that..? – Purush Pawar Jan 25 '13 at 07:13
  • Thanks @PuruPawar You saved the day... Totally new to canvas and draw here – Shachi Nov 27 '18 at 13:09

4 Answers4

2

No, You can't do that. The second layout will override on the parent view.

R9J
  • 6,605
  • 4
  • 19
  • 25
2

In you main layout (activity_mainlcass_app) just add MyClass

<?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" >

    <com.example.MyClass
        android:id="@+id/myclass"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

Try like this

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

<com.example.firstactivity
android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>


<com.example.secondactivity
android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>

</LinearLayout>

For more information check this link and this example

Community
  • 1
  • 1
Janmejoy
  • 2,721
  • 1
  • 20
  • 38
0

If you want new drawing on top of other (with different layouts sizes maybe) use FrameLayout for original layout and use gravity to position new drawing and use addView methods of activity.

android2013
  • 415
  • 2
  • 5