Showing dialog boxes with four buttons - android

hy ! i am using dialog im my option . i want four button in that dialog box . i have added three buttons one is
myDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener()
2nd is
myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
and the 3rd one is myDialog.setNeutralButton("Add", new DialogInterface.OnClickListener() but im unable to add 4th one as i tried one more setPositiveButton but its showing only one .how i can add 4th one plz give me hint.
thank you.

If you just want to use a notmal dialog something like this should work
Dialog settingsDialog = new Dialog(this);
settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.button_layout
, null));
settingsDialog.show();
button_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="OK" android:onClick="dismissListener"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="OK" android:onClick="dismissListener"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="OK" android:onClick="dismissListener"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="OK" android:onClick="dismissListener"/>
</LinearLayout>
You can create the layout according to your requirement.

You can't. In that case you must create a custom view for your Dialog and put four buttons at the bottom of that view (at the top, you put a TextView with the message).

Related

Set Done Button on Kotlin DialogFragment with multi choice Items

I have a DialogFragment with multi chose style, which means it does not dismiss on select/deselect. The only way to dismiss it seems to be to click on the device Back button.
Im finding it strange to have click the device back button to dismiss the dialog. Is there a way to add a Done button to the dialog, next to the title?
Its quite simple actually, I'm extending the theoretical answer here:
Create your custom layout eg as myLayoutTitle.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/select_type_title_container"
android:background="#f6f6f6">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
app:layout_constraintLeft_toLeftOf="#+id/select_type_title_container"
app:layout_constraintTop_toTopOf="#+id/select_type_title_container"
android:text="#string/menu_types"
android:layout_gravity ="center_vertical"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="20dp"
android:id="#+id/select_types_textview"/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
app:layout_constraintRight_toRightOf="#+id/select_type_title_container"
app:layout_constraintTop_toTopOf="#+id/select_type_title_container"
android:layout_margin="5dp"
android:text="Done"
android:textColor="#color/colorPrimary"
android:id="#+id/done_select_types"
android:layout_gravity ="center_vertical"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The on your dialog builder
builder.setCustomTitle(activity?.layoutInflater?.inflate(R.layout.myLayoutTitle, null))

How to centralize buttons on android?

I'm having troubles to create a layout like that, the Dialog on left side of image. But I want to create it using horizontal orientation. I want that my layout have this appearance, but only with two buttons horizontally.
My problem is the layout xml file, I don't know how to start this layout... =(
The real problem is how to edit the xml file to the buttons get centralized, I tried a lot of thinks, padding, orientation, align etc... But I cant align that. My current layout is that. How can I centralize the buttons?
Can someone help me?
And here is my xml file code...
<?xml version="1.0" encoding="utf-8"?>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:id="#+id/textView6"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:id="#+id/textView7"
/>
Can someone help me?
you can do somethnig like this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="#+id/button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="#+id/button2" />
</LinearLayout>
</RelativeLayout>
First, you need to create your desired layout with the buttons that you mentioned (I figured you can do this yourself). Then, you would use that layout to inflate an AlertDialog as I have done below:
View alertView = getLayoutInflater().inflate(
R.layout.your_custom_layout,
(ViewGroup) findViewById(R.id.alertViewLayout));
new AlertDialog.Builder(this)
.setTitle(title)
.setView(alertView)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// handle click here
// dismiss AlertDialog
dialog.dismiss();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// handle click here
// dismiss AlertDialog
dialog.dismiss();
}
}).show();
You could access the views in the AlertDialog's layout by referencing the parent view (alertView);
ImageButton button1 = (ImageButton) alertView.findViewById(R.id.button1);
ImageButton button2 = (ImageButton) alertView.findViewById(R.id.button2);
Edit
This is a sample layout file with 2 ImageButtons placed side-by-side.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/alertViewLayout"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:layout_weight=".5"
android:background="#+drawable/imageButton1"
android:text="Button 1" />
<ImageButton
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_weight=".5"
android:background="#+drawable/imageButton2"
android:text="Button 2" />
</LinearLayout>
Edit 2
Since you want your buttons to have custom backgrounds, I suggest you use an ImageButton and set the background property to your choice background.
If you want more details on how to create a layout file, you can check Android Developer documentation.
Let me know if this helps.
Could you be more clear in your question please? I don't know if I got it right but I will try to explain it.
First, if you want to create a custom layout for you dialog, you should create a .xml file and then inflate it with this specific layout.
But I think that is not your case, what you want to do it's to simply create a custom dialog, right? The screen orientation doesn't matter (I don't know if you meant it).
I don't think I should explain you the whole process or just paste the code, it's better to you to get used with android official documentation, so take a look at this link: Dialogs | Android Development
Hope it can help you.
Simplest answer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation = "horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:id="#+id/textView6"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:id="#+id/textView7"/>
</LinearLayout>

Android auto layout method

I have a relative layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/ivLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/b2"
android:text="Button" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="21dp"
android:layout_toRightOf="#+id/ivLogo"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
which contains an imageview, textview, and two buttons, and what I want is to make a list of a info to be filled in this layout auto .. umm, like making in the java file "Heart.png", heart, www.heart.com, 69552874 .. then when I start the app the info I typed in the java class gets in the activity .. the "heart.png" gets in the ivLogo imageview, and the www.heart.com gets into b1 button, like this ..
I can achieve what i want using the xml but it will take a long of time and it'll be very slow because ill be many imageviews .. I want to make a list of things with their logo and name and num. and website , like can the java make a new relative layout for every info ..
like if i write
"Heart.png", heart1, www.heart.com, 69552874
"Heart.png", heart2, www.heart.com, 69552874
"Heart.png", heart3, www.heart.com, 69552874
I see in the app 3 relative layouts for each heart 1,2, and 3 ..
I want a method to do that not using the xml for each one make a new relative layout
isn't there something like makenewLayout etc ... ?
sorry for taking long, a two words hint will be nice to me <3
Yes, you can do this in code.
RelativeLayout layout = new RelativeLayout(this);
ImageView iv = new ImageView(this);
iv.setId(ivid);
....
....
TextView tv = new TextView(this);
tv.setId(tvid);
tv.setText("My text");
...
layout.addView(iv);
layout.addView(tv);
setContentView(layout);
Put this in a function, and call the function for various images, textviews, etc.

I want to create a scrollable dialog box? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Adding a vertical scrollbar to an AlertDialog in Android?
In my project, there is some button.Whenever I click on one button ,it shows a alert dialog box with the description of that button.i have wrote description in string.xml with . whenever i click the button, it fetch the description from string.xml and shows us.But my problem is that my message( which will show in dialog box)is too long.so,i want scrollable alertdialog box.with out creating another xml file with, how is it possible in java code.
You are able to customize the dialog box by using the setView method and apply a custom view to the dialog box.
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setIcon(R.drawable.icon);
ad.setTitle("Instructions ...");
**ad.setView(LayoutInflater.from(this).inflate(R.layout.instructions_dialog,null));**
ad.setPositiveButton("OK",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
// OK, go back to Main menu
}
}
);
ad.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
// OK, go back to Main menu
}}
);
I hope this is helpful.
this is an example.you can change according to your requirement
you can add this on button event
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.alert);
Spinner spin = (Spinner)dialog.findViewById(R.id.spinner1);
Spinner spin2 = (Spinner)dialog.findViewById(R.id.spinner2);
EditText t1 =(EditText)dialog.findViewById(R.id.editText1);
EditText t2 =(EditText)dialog.findViewById(R.id.editText2);
EditText t3 =(EditText)dialog.findViewById(R.id.editText3);
EditText t4 =(EditText)dialog.findViewById(R.id.editText4);
dialog.show();
alert.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/widget54"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="211dp"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="11dp"
>
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="208dp"
android:layout_height="wrap_content"
android:layout_x="19dp"
android:layout_y="76dp"
/>
<EditText
android:id="#+id/editText3"
android:layout_width="208dp"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="138dp"
/>
<EditText
android:id="#+id/editText4"
android:layout_width="212dp"
android:layout_height="wrap_content"
android:layout_x="18dp"
android:layout_y="196dp"
/>
<Spinner
android:id="#+id/spinner1"
android:layout_width="222dp"
android:layout_height="wrap_content"
android:layout_x="5dp"
android:layout_y="254dp" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="310dp" />
</LinearLayout>
</ScrollView>

Are there buttons like these built into the Android that Devs can use?

I want to try to get nice buttons that will work similar to tabs down the bottom of my app, but I am wondering if there are buttons like this built in:
It would make navigation better, and developing a lot easier & faster if they are.
Your example screenshot is showing a mostly-untouched stock Android 2.2 launcher. Since it's open source you can take a look at how it's done. Here's the layout file it uses.
Note the RelativeLayout at the bottom containing the three "hotseat" buttons. Each of them uses a custom style to get the appearance, those styles are defined here.
What you are showing in the picture is HTC Sense Launcher. It's an add-on and not part of core Android OS.
BTW it's easy to do custom transparent button like this. Let me know if you need any help with this.
Update:
Use this example to create a custom Dialog, that is not modal and is floating and transparent.
dialog = new Dialog(activityRequestingProgressDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progress_upload);
progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar);
progressText = (TextView) dialog.findViewById(R.id.progressText);
progressText.setText("0 %");
progressText.setTextSize(18);
Button buttonCancel = (Button) dialog.findViewById(R.id.btnCancel);
buttonCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
cancelProgressDialog();
}
});
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.BOTTOM);
dialog.show();
And the dialog layout is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/progressDialog"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"
android:padding="10dp"
android:text="#string/progress_title"/>
<LinearLayout android:id="#+id/progressDialog"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_centerVertical="true">
<ProgressBar android:id="#+id/progressBar"
android:layout_width="150dp"
android:layout_height="34dp"
android:paddingRight="10dp"
android:max="100"
android:progress="0"
android:fadingEdge="vertical"
style="?android:attr/progressBarStyleHorizontal"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/progressText"
android:paddingRight="10dp"/>
<Button android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dip"
android:text="#string/button_options_text"
android:textColor="#color/button_text_grey"
android:drawableTop="#drawable/button_options"
android:drawablePadding="-5dip"
android:background="#null"/>
</LinearLayout>
</LinearLayout>
This is an example of a progress dialog with progress bar, text and cancel button. You can easily change this to three buttons. Note that button is transparent with icon and text.

Categories

Resources