vicciu
vicciu

Reputation: 164

Can I use setContentView outside the oncreate method?

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);
}
    }

Upvotes: 9

Views: 12678

Answers (3)

Ted Hopp
Ted Hopp

Reputation: 234847

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.

Upvotes: 10

asenovm
asenovm

Reputation: 6517

The setContentView() method can be called only once per activity. If you want to completely change the layout at some point you should either go for ViewFlipper or have 2 layouts in the activity and show only one of them at given time by calling view.setVisibility(View.GONE); and view.setVisibility(View.VISIBLE); respectively.

Upvotes: -4

Guillaume
Guillaume

Reputation: 22822

The best way to do that is to have multiple activities: instead of nou1.nou_alarma();, create an intent and start a new activity with the new layout.

startActivity(new Intent(this, NewAlarmActivity.class));

And in the onCreate method of NewAlarmActivity, set the content view to R.layout.timepicker

Upvotes: 4

Related Questions