14

I have following main layout:

<LinearLayout android:id="@+id/LinearLayout01" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical">

 <ViewFlipper android:id="@+id/viewstack" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

        <!-- Here I want to add my views which are located in separated xml files. -->

        </ViewFlipper>

</LinearLayout>

Here is example of my view:

view_url.xml

<LinearLayout android:id="@+id/LinearLayout01" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:gravity="center">
 <EditText android:text="@+id/EditText01" 
  android:id="@+id/EditText01" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"/>
 <Button android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:id="@+id/btnGenerate" 
                android:text="Generate"/>
</LinearLayout>

view_text.xml

<LinearLayout android:id="@+id/LinearLayout01" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical">

 <EditText android:text="@+id/EditText01" 
  android:id="@+id/EditText01" 
  android:layout_height="wrap_content" 
  android:contentDescription="Enter your text here" 
  android:layout_width="fill_parent" 
  android:height="200dp"/>
</LinearLayout>

I am trying to add views:

viewstack = (ViewFlipper) findViewById(R.id.viewstack);));

View viewText = (View) findViewById(R.layout.view_text);
viewstack.addView(viewText); < -- Emulator is crashing at this line
View viewUrl = (View) findViewById(R.layout.view_url);
viewstack.addView(viewUrl);

I dont have any idea what is wrong with my code. I decided to put all my views in one file, but I still want to know how to fix my initial code.

5 Answers 5

8

Yout View viewUrl = (View) findViewById(R.layout.view_url); is very wrong. findViewById is kinda like the get(String key) method the the directory where your directory is your current view/activity. It only looks up the element with that Id under it's children.

To create Java objects out of XML files you need use you need to use the LayoutInflater. Which is pretty straight forward, out of that you get the object you can pass to viewstack.addView(..) method.

Another way to achieve this would be to just include the other XML files into the first one by using either the include, merge tags, or the ViewStub. Depending on your requirements these might not be an option though, but what you are describing they should be, and you should use these instead of doing it programatically because it's just cleaner this way.

0
5

maybe this help:

    this.flipper=(ViewFlipper)findViewById(R.id.flipper);

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    this.viewLoader=(View)inflater.inflate(R.layout.search_result_grid, null);
    flipper.addView(viewLoader);        

    this.viewResultGrid=(View)inflater.inflate(R.layout.search_result_grid, null);
    gvSearchResult=(GridView)viewResultGrid.findViewById(R.id.gridViewSearchResult);        
    flipper.addView(viewResultGrid);
0

It's name suggests that It will find by id not by layout.You should understand difference between layout and id.use like this one View v=(View)findViewById(R.id.your_view_id);

0

Easiest way to add by inflating View. Here some small snippet where you can find reference.

 private View viewText;
 private TextView txtPost;
 private void setViewFlipperPost(String postData, String postType) {

     if (postType.toLowerCase().toString().equals("text")) {

            viewText = LayoutInflater.from(mContext).inflate(R.layout.activity_full_screen_textpost, null, false);
            viewText.setTag(TAG_TEXT);

            txtPost = (TextView) viewText.findViewById(R.id.txtTextPostFullScreenText);
            txtPost.setText(postData);

            viewFlipper.addView(viewText);
    }
}
-1
use viewflipper

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/white"
        android:orientation="vertical" >
    <ViewFlipper
        android:id="@+id/view_flipper"
        android:layout_width="match_parent"
        android:layout_height="410dp" >


               <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:orientation="vertical" >
               <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"

                    android:text="first layout"
                    android:textColor="#845965"
                    android:textSize="25dp"
                    android:textStyle="bold" >
                </TextView>


            </LinearLayout>


               <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"

                    android:text="second layout"
                    android:textColor="#654123"
                    android:textSize="25dp"
                    android:textStyle="bold" >
                </TextView>


            </LinearLayout>
    //you can add many layout here
    </viewFlipper>
      <Button
                    android:id="@+id/flipbyclickNext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/bn1"
                    android:onClick="flipByClickNext" />
<Button
                    android:id="@+id/flipbyclickprevious"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:onClick="flipByClickPrevious"
                    android:background="@drawable/bp1" />
    </LinearLayout>

MainActivity.java

    public class MainActivity extends Activity {
    private ViewFlipper viewFlipper;

    //Button next,prev;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
}
public void flipByClickNext(View v)
    {
        if(viewFlipper.isFlipping())//Checking flipper is flipping or not.
        {      
            viewFlipper.stopFlipping();       //stops the flipping .
        }

        viewFlipper.showNext();//shows the next view element of ViewFlipper
    }

    public void flipByClickPrevious(View v)
    {
        if(viewFlipper.isFlipping())//Checking flipper is flipping or not.
        {      
            viewFlipper.stopFlipping();       //stops the flipping .
        }
        viewFlipper.showPrevious();

    }

}
1
  • How does your code answer the question?He is trying to inflate a custom view to the flipper.. Commented Oct 22, 2015 at 11:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.