I am relatively new to android, i need to open a new screen when a button is clicked. As i checked the posts here they suggest using setContentView() or using an Intent. In case 2 a new the screen is opened as a new Activity, as i dont want this to happen could u tell me if i can use LayoutInflater to open a new screen, if so how to do this.
Asked
Active
Viewed 387 times
1
-
Why wouldn't you want to open a new activity? What do you want to achieve? If you tell us more details we can help you providing an ideal solution.. that could be: opening a new activity, opening a dialog, replacing the activity content view, fill a layout container with an inflated view, etc.. – BFil May 21 '11 at 09:13
-
I thought using LayoutInflater will also make the backbutton to work automatically.Also since the new screen has only TextView items i did not want it to open in a new Activity. – Deepak May 21 '11 at 11:39
1 Answers
1
your question suggests that, there is no need to use LayoutInflater. Check this code:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_layout_1);
Button button = (Button) findViewById(R.id.basic_layout_1_button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.basic_layout_2);
}
});
}
Here is all you need I think. But just in case I'm also posting XML files:
basic_layout_1.xml code
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world! - Basic layout 1"
android:textSize="32dip">
</TextView>
<Button
android:id="@+id/basic_layout_1_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basic layout 1 Button">
</Button>
</LinearLayout>
and basic_layout_2.xml code
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world! - Basic layout 2"
android:textSize="32dip">
</TextView>
</LinearLayout>
//EDIT What is important, this method of creating a new screen will cause that back button won't display basic_layout_1 but it'll exit your app (in example above). If you'd like to display previous screen you should override back button action (but in that case, why not use Intent?)

kamil zych
- 1,682
- 13
- 21