Reputation: 14791
I am developing an Android app. In my app, I want to show data in dialog. Basically, I want to show an Activity
in dialog when a button is clicked. I know how to show Activity
as dialog. Here is how I am currently showing activity as dialog.
I set Activity
in AndroidManifest
like this
<activity
android:configChanges="orientation|screenSize"
android:name=".MakeCommentActivity"
android:label="Comment"
android:theme="@android:style/Theme.Holo.Light.Dialog" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
So when I open activity. It show as dialog as below.
As you can see in screenshot, the problem with above code is I cannot fully control over design. I mean I cannot set the background color of dialog as I want. I will change depending on the device. I cannot change the theme of dialog. Besides, I cannot remove the title of dialog and so on.
Like in above screenshot, I will have fully control over design. I can add icon in title. I can set whatever theme I want and so on.
So what I can think is to use an alert dialog with custom view. But if I do it, code will not be clean and neat because all the logic and code will be places in the parent activity. Not in different Activity.
So my question is how can I open an Activity
as dialog that can be fully customized in design? If cannot fully customized, using alert dialog with custom view is a good idea? Because I will show comments like in Facebook. User can make comment as well.
Upvotes: 1
Views: 12304
Reputation: 4480
What you are actually doing is that you are only changing the theme of your activity.
The best way to achieve what you want is by creating a custom view Dialog
:
//Here you inflate your design (xml layout)
View view = getLayoutInflater().inflate(R.layout.your_view, null);
//Here you can get any component of that layout (ex: textView)
TextView yourTextView = view.findViewById(R.id.your_textview_id);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
AlertDialog alert = builder.create();
alert.show();
This way you can control it as you wish
Upvotes: 4
Reputation: 24211
I had a working Activity
with theme type Dialog
. Its easily customizable and yes the code becomes cleaner when you put the login in another class.
So I would like to suggest you something. First thing is to remove the intent-filter
. I don't actually know why the intent-filter
is used, but anyway, doesn't look very necessary.
<activity
android:configChanges="orientation|screenSize"
android:name=".MakeCommentActivity"
android:label="Comment"
android:theme="@android:style/Theme.Holo.Light.Dialog" >
</activity>
The layout of your Activity
which is a dialog, is important. You need to set the layout_height
and layout_width
to match_parent
to check if you can get the customized behaviour.
The other thing you mentioned about the label of the Activity
which can be changed easily by setTitle
from the onCreate
function of your Activity
.
Here's my DialogActivity
for example
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
setTitle("My custom title");
}
}
And the layout used here is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_margin="@dimen/activity_horizontal_margin"
android:background="@android:color/black"
android:layout_height="200dp" />
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/black" />
</LinearLayout>
Upvotes: 1
Reputation: 177
Its better to use AlertDialog. Because if you done it by theme, then for different version devices its appearance will changed.
Upvotes: 0
Reputation: 440
You can use AlertDialog basically android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.
Android AlertDialog is composed of three regions: title, content area and action buttons. you can leave action buttons if you not need like in your case.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//Setting message manually and performing action on button click
builder.setMessage("Do you want to close this application ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("AlertDialogExample");
alert.show();
This will show output in this way.
Upvotes: 0
Reputation: 2757
More often than not these seemingly beautiful alert dialogs are not alertdialogs at all but an activity having a transparent background. Onclicklisteners are duly placed(for example clicking the transparent background will close the activity). Therefore designing these activities is easier compared to customizing an alertdialog
Upvotes: 2