Hi Justin,

my main.xml looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="@string/text_a"
    />
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="@string/text_b"
    />
<Button
    android:id="@+id/flag_button"
    android:text="@string/button_string1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout>

I'd really like to get around having to declare a separate imageView
in the XML layout. Isn't there a way of instantiating such an object
by hand and adding it to the application's layout at runtime?

Best,

Sven

On Oct 13, 5:28 am, Justin Anderson <[email protected]> wrote:
> What does the XML for your res/layout/main.xml look like?
>
> Thanks,
> Justin
>
> ----------------------------------------------------------------------
> There are only 10 types of people in the world...
> Those who know binary and those who don't.
> ----------------------------------------------------------------------
>
> On Mon, Oct 12, 2009 at 9:28 AM, Sven <[email protected]> wrote:
>
> > Hi,
>
> > I want to add an ImageView to my layout when a button has been
> > pressed. Sadly, I cannot get a handle to the main application's
> > linearlayout, I always get a null pointer error when trying to
> > retrieve the layout using findViewByID.
>
> > Here's the code that ought to display the image when button b has been
> > pressed, but I think I have missed something obious:
>
> > package com.foo.bar;
>
> > import android.app.*;
> > import android.os.Bundle;
> > import android.view.*;
> > import android.view.ViewGroup.LayoutParams;
> > import android.widget.*;
> > import android.content.*;
> > import android.util.Log;
>
> > public class AndroidTest extends Activity {
> >    /** Called when the activity is first created. */
> >   �...@override
> >    public void onCreate(Bundle savedInstanceState) {
> >        super.onCreate(savedInstanceState);
> >        setContentView(R.layout.main);
>
> >     // assign flag.png to the button, loading correct flag image for
> > current locale
> >        Button b;
> >        b = (Button)findViewById
> > (R.id.flag_button) ;//.setBackgroundDrawable(this.getResources
> > ().getDrawable(R.drawable.developers));
>
> >        // build dialog box to display when user clicks the flag
> >        AlertDialog.Builder builder = new AlertDialog.Builder(this);
> >        builder.setMessage(R.string.dialog_text)
> >            .setCancelable(false)
> >            .setTitle(R.string.dialog_title)
> >            .setPositiveButton("Done", new
> > DialogInterface.OnClickListener() {
> >                public void onClick(DialogInterface dialog, int id) {
> >                dialog.dismiss();
> >                }
> >            });
> >        final AlertDialog alert = builder.create();
>
> >        // set click listener on the flag to show the dialog box
> >        b.setOnClickListener(new View.OnClickListener() {
> >            public void onClick(View v) {
> >                // alert.show();
> >                ImageView image;
> >                image = new ImageView(v.getContext());
> >                image.setImageDrawable(v.getResources().getDrawable
> > (R.drawable.developers));
>
> >                LinearLayout main = (LinearLayout)
> > AndroidTest.this.findViewById(R.layout.main);
> >                // some debug code
> >                if (main == null)
> >                {
> >                        Log.v("abc", "Main is null");
> >                        }
> >                else
> >                {
> >                main.addView(image, 200, 200);
> >                }
> >            }
> >            });
>
> >    }
> > }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to