Can I use setContentView outside the oncreate method?

vicciu picture vicciu · Jan 8, 2012 · Viewed 11.9k times · Source

I have seen many people telling that you can set setContentView outside the oncreate method, but I didn't find anywhere an example. Now when I try to use setContentView, my app just crashes. Here is my source code:

AlarmActivity.java:

package com.alarm.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;

public class AlarmActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button buton = new Button(this);
        buton.setId(101);
        buton.setText("New alarm");
        buton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        RelativeLayout layout1 = new RelativeLayout(this);  
        layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
    layout1.addView(buton);  
    setContentView(layout1); 

    Button nou =(Button)findViewById(101);
    nou.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            New_ala nou1=new New_ala();
            nou1.nou_alarma();
            }
        });
    }
}

New_ala.java:

package com.alarm.example;

public class New_ala extends AlarmActivity{
public void nou_alarma() {
setContentView(R.layout.timepicker);        
}
}

TimePicker.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <LinearLayout 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_alignParentBottom="true">

        <Button 
        android:id="@+id/picker_save" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Save">
        </Button>

        <Button 
        android:id="@+id/picker_cancel" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="Cancel">
        </Button>
</LinearLayout>

</LinearLayout>

On more detail, I can use setContentView(R.layout.timepicker) inside the oncreate method without any problems so the problem is that setContentView isn't working properly inside the New_ala.java class. Can anyone help me?

The code after setting the intent: AlarmActivity:

package com.alarm.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startActivity(new Intent(this, NewAlarmActivity.class));
        }
    }

NewAlarmActivity:

package com.alarm.example;
import android.os.Bundle;
public class NewAlarmActivity extends AlarmActivity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timepicker);
}
    }

Answer

Ted Hopp picture Ted Hopp · Jan 8, 2012

You can call setContentView any time you are running on the event (UI) thread. Be aware that when you do, any fields you initialized by calling findViewById will need to be reset.