I have two layouts and I am inflating it one after another ,I want to dismiss previous view and inflate another but the view is shifted to the background ,hence both the dialogs are present.I would like to dismiss the dialog which is in the background.
I have tried following fixes:
1.dialog.dismiss(); — not working
2.view.setVisibility(View.GONE); — Title of the dialog still remains along with dialog.
I am attaching the entire code.Please let me know how to resolve this.Thanks.
menuproject.java
package com.menuproject;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class menuproject extends Activity {
/** Called when the activity is first created. */
AlertDialog.Builder dialog;
AlertDialog.Builder dialog2;
private AlertDialog alert = null;
private AlertDialog alert2 = null;
View updatedialog;
View updatedialog2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
protected void layoutsecond() {
// TODO Auto-generated method stub
this.dialog2 = new AlertDialog.Builder(this);
this.dialog2.setTitle("Update");
LayoutInflater layoutInflater = LayoutInflater.from(this);
updatedialog2 = layoutInflater.inflate(R.layout.updatedialog2, null);
this.dialog2.setView(updatedialog2);
alert2 = this.dialog2.create();
this.dialog2.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuItem mi = menu.add(1, 1, 1, "My Menu1");
mi.setIcon(getResources().getDrawable(R.drawable.icon));
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
layoutfirst();
Button b = (Button) updatedialog.findViewById(R.id.Button11);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// updatedialog.setVisibility(View.GONE);
// alert.dismiss();
layoutsecond();
}
});
break;
}
return super.onOptionsItemSelected(item);
}
private void layoutfirst() {
// TODO Auto-generated method stub
this.dialog = new AlertDialog.Builder(this);
this.dialog.setTitle("Update");
// this.dialog.setMessage("");
LayoutInflater layoutInflater = LayoutInflater.from(this);
updatedialog = layoutInflater.inflate(R.layout.updatedialog, null);
this.dialog.setView(updatedialog);
alert = this.dialog.create();
this.dialog.show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
updatedialog.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView02" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fillViewport="true">
<LinearLayout
android:id="@+id/liner1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<requestFocus></requestFocus>
<Button
android:id="@+id/Button11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@android:drawable/ic_menu_call"
android:text="Update Tel" />
<Button
android:id="@+id/Button10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@android:drawable/ic_menu_call"
android:text="Update Address" />
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</View>
</LinearLayout>
</ScrollView>
updatedialog2.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView021" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<requestFocus></requestFocus>
<Button
android:id="@+id/Button01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@android:drawable/ic_menu_call"
android:text="Update Mobile" />
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</View>
</LinearLayout>
</ScrollView>
Have you tried alert.setContentView(inflated_layout) instead dialog.setView(view_name)