0

I have level1.xml which has relative layout and image view as child I want to retrieve those views to set their margins progrmatically here is my xml

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/Level1Layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="objectClick" 
            android:background="@drawable/hh_gmw_03">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="593dp"
        android:layout_marginLeft="998dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image1"
        android:src="@drawable/hht3l01"/>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="39dp"
        android:layout_marginLeft="20dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image1"
        android:src="@drawable/hht3l02" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="153dp"
        android:layout_marginLeft="497dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image3"
        android:src="@drawable/hht3l03" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="524dp"
        android:layout_marginLeft="998dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image4"
        android:src="@drawable/hht3l04" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="183dp"
        android:layout_marginLeft="715dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image5"
        android:src="@drawable/hht3l05" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="413dp"
        android:layout_marginLeft="557dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image6"
        android:src="@drawable/hht3l06" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="53dp"
        android:layout_marginLeft="532dp" 
        android:onClick="objectClick" 
        android:contentDescription="Image1"
        android:tag="Image7"
        android:src="@drawable/hht3l07" />
</RelativeLayout>

and here is my ExampleActivity

public class ExampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView[] Objects = (ImageView[]) getViewArrays(R.layout.level1);
    Log.d("Check","Objects" + Objects.toString());
}



public View[] getViewArrays(int resid)
{
    ViewGroup layout = (ViewGroup) this.findViewById(resid);

    View[] views = new View[layout.getChildCount()];

    for (int i = 0; i < layout.getChildCount(); i++) {
        views[i] = layout.getChildAt(i);
    }

    return views;
}

public int[] getViewIds(int resid)
{
    ViewGroup layout = (ViewGroup) this.findViewById(resid);

    int[] ids = new int[layout.getChildCount()];

    for (int i = 0; i < layout.getChildCount(); i++) {
        ids[i] = layout.getChildAt(i).getId();
    }

    return ids;
}

}

It is giving me null exception error at this line View[] views = new View[layout.getChildCount()]; ...... Why my layout is not found ?

2 Answers 2

1

Is your xml file main.xml? Then you should replace this line:

ImageView[] Objects = (ImageView[]) getViewArrays(R.layout.level1);

By this:

ImageView[] Objects = (ImageView[]) getViewArrays(R.id.Level1Layout);

The problem is that you give a layout xml file in parameter, that's why you have an exception: you have to give a view id.

Sign up to request clarification or add additional context in comments.

Comments

0

Found answer by myself .... I have remove that this.findviewbyid () ...instead only findviewbyID

But now it return Views and not imageview[] . It saying View[] can not be typecast to Imageview[]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.