tags:

views:

90

answers:

3

I create a subclass of View as an inner class in my Activity. I just want to inflate an xml into the class which extending view. Can anyone provide some code/syntax of both class and xml?

public class drawiact extends Activity {

       DrawView drawView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        drawView = new DrawView(this, null);
         setContentView(drawView);


    }
    public class DrawView extends View implements OnTouchListener {
        Path path;
        Paint paint = new Paint();
        private ShapeDrawable mDrawable;
        private ArrayList<Path> graphics = new 
        public DrawView(Context context,AttributeSet attrs) {
            super(context,attrs);
            //I want to load the contents of an xml in this class
    //
  }

}

xml is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.drawing"
        android:orientation="vertical"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">

 <Button android:id="@+id/Button02" 
    android:layout_width="wrap_content" 
    android:layout_height="200dip"

     android:layout_gravity="right"></Button>
</LinearLayout>
A: 

Just keep in mind that once you have inflated something, it will be just View object. Not sure what you want to do, but can do something like:

public class drawiact extends Activity {
    DrawView drawView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        drawView = new DrawView(this, null);
        setContentView(drawView);
    }
    public class DrawView extends View implements OnTouchListener {
        Path path;
        Paint paint = new Paint();
        private ShapeDrawable mDrawable;
        private ArrayList graphics = new 
        public DrawView(Context context,AttributeSet attrs) {
            super(context,attrs);
            View view = getLayoutInflater().inflate(R.layout.your_layout, null);
            // then do whatever you want with the 'view' object
       }
  }
}
Cristian
@Cristian: I tried this code. But it doesn't showing any widgets in XML file. My need is to give action to the ImageView and Button widgets which is in the loaded XML.
antony
@Cristian: Is it possible to load xml into View and make actions for the objects.
antony
Place your views (objects, TextView, buttons, etc.) in your_layout.xml and them access them by calling `view.findViewById(R.id.my_text_view)`
Asahi
@Asahi:I tried this code. But it also doesn't showing any widgets in XML file.Should I make any modification for xml...??
antony
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.drawing" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="200dip" android:layout_gravity="right"></Button> </LinearLayout>
antony
The button is defined as `wrap_content` and it does not have any content. Could be a reason you do not see it. Another point: if you have overridden any methods make sure to call `super` where needed.
Asahi
By the way... why do you have two `setContentView` invokations?
Cristian
@Cristian: I need only one setContentView...
antony
A: 

package com.drawing;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class drawingact extends Activity {
      DrawView drawView;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            try{
            System.out.println("onCreate");
            drawView = new DrawView(this);
            System.out.println("onCreate");
            setContentView(drawView);
            System.out.println("onCreate");
            }catch(Exception e){
                  System.out.println("Error : " + e.getMessage());
            }
        }
        public class DrawView extends View implements OnTouchListener {

            Path path;
            Paint paint = new Paint();
            @SuppressWarnings("unused")
            private ShapeDrawable mDrawable;
            @SuppressWarnings("unused")
            private ArrayList graphics;

//          public DrawView(Context context) {
//              super(context,attrs);
//              
//             // addContentView(view,null);
//             
//          }
            public DrawView(Context context) {
                super(context);
                System.out.println("DrawView");
                View view = getLayoutInflater().inflate(R.layout.main, null);
                System.out.println("DrawView");

            }
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                System.out.println("onTouch");
                return false;
            }
      }
    }

I used this code but nothing is coming.can you find what is the error.

antony
A: 

Hello Cristian,

I have a question, since your answer is very much the dilemna i am facing, I have almost the same scenario as antony, and yes, the object returned while using inflation is a View.

however on my xml file that i am trying to inflate, I have a view,call it miniView for the sake of this example. is there a way i can get a reference to that view after inflation that will also allow me to draw on -- assuming the class is like DrawView which extends a view and I can use it to overrides the onDraw method?

thanks

Kerric