-1

Trying to change a layout from an ImageButton but when i run it on my phone it does not switch instead it says program has stopped.

Java;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageButton ImageButton = (ImageButton) findViewById(R.id.image);
        ImageButton.setBackgroundColor(1);

        ImageButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view)
            {
                setContentView(R.id.aaa);
            }
        });


    }
}

XML for first layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.bassammetwally.anew.MainActivity">
    <RelativeLayout
        android:layout_width="420dp"
        android:layout_height="50dp"
        android:background="#3955a1">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Writer's Block"
            android:textColor="#ffffff"
            android:textSize="30dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="4dp"
            android:id="@+id/Asd"/>
        <ImageButton
            android:layout_width="20dp"
            android:layout_height="40dp"
            android:src="@drawable/pen"
            android:layout_marginLeft="380dp"
            android:layout_marginTop="5dp"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:id="@+id/image"/>
        </RelativeLayout>
</LinearLayout>

Second layout to change to;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/aaa">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ans"/>

</LinearLayout>

I think it might have something to do with my syntax.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
asdwe
  • 23
  • 5
  • `R.id.aaa` is not a layout. It's the ID for the `LinearLayout` in your second layout. Layout identifiers start with `R.layout`, like the one you have in the first `setContentView()` call. Also, switching layouts like that usually isn't a good idea. – Mike M. Apr 23 '16 at 23:50
  • Check this http://stackoverflow.com/questions/2335813/how-to-inflate-one-view-with-a-layout?rq=1 – Yousef Zakher Apr 23 '16 at 23:54

1 Answers1

0

You need to use a layout reference instead of an id

setContentView(R.layout.view);

Where view.xml is the layout you want.

You may want to rethink your approach, though. Using Fragments is preferred over completely replacing the layout because your ImageButton is no longer usable and you'll need to recall findViewById for any new views that are displayed.

If you just want to load a layout file and add it to the current layout, see Inflating one layout

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245