Skip to content Skip to sidebar Skip to footer

Load A Layout Xml File In A View Class Android

I have this class: public class MainActivity extends Activity{ public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentV

Solution 1:

I think I solve my problem.

I extended a LinearLayout instead of View class.

publicclassMyViewextendsLinearLayout{
    publicMyView(Context c) {
        super(c);            
        LayoutInflaterinflater= (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
        addView(inflater.inflate(R.layout.union, null));
    }
}

With this I can load my layout. Now, I'll try to draw lines. I hope this will be usefull for other people with the same problem.

(Edit): But now I can't draw a line. I try with the next code in the extended activity class:

Canvascanvas=newCanvas();
MyView.onDraw(canvas);

And I have the next code in the extended LinearLayout class:

publicvoidonDraw(Canvas canvas) {
            canvas.drawLine(0, 0, 9900, 9900, paint);
}

I may draw the line under the whole UI?

((Edit Again)) I found my solution:

public void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.drawLine(0, 0, 9900, 9900, paint);
}

If I call a onDraw method I draw a line "under" de UI, but if I call dispatchDraw() I draw a line above user interface and I can see the line. I hope this will very useful for other people.

Solution 2:

Try to put your first approach in the Activity.onResume() method. The Activity should be on the foreground already. See the Activity lifecycle at http://developer.android.com/reference/android/app/Activity.html

Solution 3:

Do you create the layout file name "main.xml" in your project? If not, create it first.

Post a Comment for "Load A Layout Xml File In A View Class Android"