Need help by creating a dialog with 2 NumberPickers - android

I'm trying to create a dialog with 2 NumberPickers. I would like them to be default Holo theme styled. I cant find any good examples. So far i got:
LayoutInflater inflater = (LayoutInflater)
getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View npView = inflater.inflate(R.layout.number_picker_dialog, null);
NumberPicker minPicker = (NumberPicker) npView.findViewById(R.id.min_picker);
minPicker.setMaxValue(100);
minPicker.setMinValue(0);
NumberPicker maxPicker = (NumberPicker) npView.findViewById(R.id.max_picker);
maxPicker.setMaxValue(100);
maxPicker.setMinValue(0);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Text Size:");
builder.setView(npView);
builder.setPositiveButton("Okay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
dialog = builder.create();
dialog.show();
number_picker_dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:holo="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<NumberPicker
android:id="#+id/min_picker"
android:width="100dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:/>
<NumberPicker
android:id="#+id/max_picker"
android:width="100dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
But the colorpicker text are white (Like the background), and i cant set the textColor of the NumberPicker.
How can i set the textColor or does anyone knows a good NumberPicker example?

I found the problem why my NumberPicker wasn't Holo.Light styled:
instead of calling:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View npView = inflater.inflate(R.layout.number_picker_dialog, null);
i solved it by calling:
View npView = getLayoutInflater().inflate(R.layout.number_picker_dialog, null);

My approach was to create a Custom Dialog class as show below.
public class CustomDialog extends Dialog {
public CustomDialog(Context context) {
super(context, R.style.customDialog); //use your style id from styles.xml
}
public void setNumberDialog() {
setContentView(R.layout.number_picker_dialog);
//add required listeners
show();
}
}
Invoke the dialog from calling acitivty.
new CustomDialog(context).setNumberDialog();
And the style parameters are defined in styles.xml
<style name="customDialog" parent="android:Theme.Dialog">
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:textColor">#color/textColorWhite</item>
</style>

Try :
<NumberPicker
style="#android:style/TextAppearance">
</NumberPicker>

Related

AlertDialog with custom layout doesn't render properly (a border gets added)

I'm currently having a problem when I try to use the AlertDialog.setView(View v) function.
When the dialog is rendered, no matter the theme used, it generates a border around the layout, just as if another layout was encapsulating it.
Using builder.show() alone, builder.create() alone or both together yields the same results.
Am I missing something somewhere, or is it a known bug ? Thanks in advance if you can help.
dialog_status.xml :
<?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:orientation="vertical">
<Spinner
android:id="#+id/spinnerStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/editTextStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Status"
android:inputType="textMultiLine"/>
<CheckBox
android:id="#+id/checkBoxStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="#string/save_status"/>
</LinearLayout>
Code inside Activity :
private void showStatusDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_Holo_Dialog);
//AlertDialog.Builder builder = new AlertDialog.Builder(this); (Yields the same results, just another theme)
View root = View.inflate(this, R.layout.dialog_status, null);
final EditText input = (EditText) root.findViewById(R.id.editTextStatus);
final Spinner spinner = (Spinner) root.findViewById(R.id.spinnerStatus);
final CheckBox checkBox = (CheckBox) root.findViewById(R.id.checkBoxStatus);
final Preferences p = new Preferences(FChatActivity.this);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
input.setText(p.getDefaultStatusMessage(FCharacter.Status.getIdentifiers().get(i)));
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
adapter.addAll(FCharacter.Status.getLabels());
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
builder.setView(root)
.setMessage("Update your status")
.setTitle("Status")
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
setStatus(FCharacter.Status.getIdentifiers().get(spinner.getSelectedItemPosition()),input.getText().toString(), checkBox.isChecked());
dialogInterface.dismiss();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.create();
builder.show();
}
Screenshots :
Okay, I found the solution on another completely unrelated question (How to show a Holo (dark) AlertDialog in a themed activity?) :
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
Create a new style in styles.xml like
<style name="Dialog.NoActionBar" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">#color/colorPrimary</item>
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
Then use the style in your AlertDialog like.
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Dialog_NoActionBar);
hope this can help.

How to change the text gravity on alertdialog

I've been searching for a while and I haven't found the solution to this problem of mine yet, I have a alertdialog with a few items (Here it is)
But as you can see, the text is aligned to the left. Is there a way to align the items text to the center?
Here's a bit of my code
chosenItem.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose a category");
// add a list
String[] items = {"Hey", "stack", "overflow", "please", "help", "Medical", "Ammo"};
// String[] items = {"Weapons", "Tools", "Items", "Clothing", "Components", "Medical", "Ammo"};
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
reciclador(3, rSlotFrame, rSlotText, rSlotProbabilidade, 30, 50, 1, 0, 100, 100, 100, 0, "cloth_icon", "metalfrags_icon", "sewingkit_icon", "");
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getWindow().setLayout(800, 700);
}
});
}
Thanks in advance.
you can use custom dialog for that like this create a custom layout like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="title"
android:textColor="#000" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text " />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="description" />
</LinearLayout>
now create custom dailog llike below code
final Dialog dialog = new Dialog(LoginActivity.this);
dialog.setContentView(R.layout.custom_dialog_layout);
Textview tvTitle;
tvTitle = (Textview) dialog.findViewById(R.id.tvTitle);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
tvTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// perform your action here
}
});
dialog.show();
You can use custom view in AlertDialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(YourActivity.this);
View alertView = getLayoutInflater().inflate(R.layout.fragment_dialog, null, false);
alertDialog.setView(alertView);
Add your custom requirement of your text in your layout file.
From documenation:
If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog.Builder object.
you need to apply theme for the dialog on the initial state
AlertDialog dialog = new AlertDialog(context, R.theme.DialogTheme);
and declare theme in your theme.xml or styles.xml
<style name="DialogTheme" parent="Animation.AppCompat.Dialog">
<item name="android:itemTextAppearance">#style/DialogTextAppear</item>
</style>
<style name="DialogTextAppear" parent="TextAppearance.AppCompat">
<item name="android:gravity">center</item>
</style>
You can find AlertDialog Title view by this code and set gravity manual but first you must create a AlertDialogBuilder and one Alert dialog like in code
MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(this);
dialogBuilder
.
.
.
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
LinearLayout titleTemplate = alertDialog.findViewById(R.id.title_template);
titleTemplate.setGravity(Gravity.RIGHT);
You can find all view in alert_dialog.xml and find in code and use it.

How to make an AppCompat Activity as a Dialog?

I need to use my AppCompat Activity as a Dialog.For this I tried so my solution that answered in StackOverflow. But nothing worked.Please answer me. I am getting activity as dialog. But it shows very narrow both in height & width.
I used the following Theme:
<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="windowNoTitle">true</item>
<item name="android:background">#android:color/transparent</item>
</style>
You can use DialogFragment and customize the layout accordingly.
public class CustomDialogFrag extends DialogFragment{
static FragmentManager fragmentManager;
public static CustomDialogFrag showDialog(FragmentManager fm){
CustomDialogFrag customDialogFrag=new CustomDialogFrag();
fragmentManager=fm;
return customDialogFrag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialogfrag_layout, null);
alertDialogBuilder.setView(view);
setupUI(view);
alertDialogBuilder.setTitle("Notification Message");
alertDialogBuilder.setIcon(R.drawable.notificationicon);
alertDialogBuilder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
void setupUI(View view){
TextView textViewOne=(TextView)view.findViewById(R.id.txtEventAlias);
TextView textViewTwo=(TextView)view.findViewById(R.id.txtTime);
TextView textViewThree=(TextView)view.findViewById(R.id.txtLogMessage);
textViewOne.setText("Text 1");
textViewTwo.setText("Text 2");
textViewThree.setText("Text 3");
}
}
And the dialogfrag_layout.xml will be
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/margin_10"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtEventAlias"
android:text="Sample"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/textSizeMedium"
android:padding="#dimen/margin_10"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTime"
android:text="Sample"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/textSizeMedium"
android:padding="#dimen/margin_10"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtLogMessage"
android:text="Sample"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/textSizeMedium"
android:padding="#dimen/margin_10"
/>
</LinearLayout>
For invoking this Dialog from a Fragment:
DialogFragment dialogFragment=CustomDialogFrag.showDialog(getFragmentManager());
dialogFragment.show(getActivity().getFragmentManager(), "tag");
In your activity's onCreate put the following lines:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your);
// Make the window's width full sized
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
Window window = getWindow();
layoutParams.copyFrom(window.getAttributes());
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
}
Tested and works. You can set to both width and height to WRAP_CONTENT if needed.

Dialog.setTitle not showing a title

I am trying to add a custom title to my Dialog, however whenever I run my application it doesn't show a title.
My code for creating the dialog is
final Dialog passwordDialog = new Dialog(this);
passwordDialog.setContentView(R.layout.admin_password_dialog);
passwordDialog.setTitle("Enter An Administrative Password");
passwordDialog.show();
And my layout file is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_confirmPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/edit_adminPassword"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="#string/confirmPassword"/>
<EditText
android:id="#+id/edit_adminPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:ems="10"
android:inputType="textPassword"/>
And here is what I am getting
Is there something I am missing?
you should define your style like this:
<style name="Dialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">false</item>
<item name="android:windowIsFloating">true</item>
</style>
and then pass this style to the constructor of the Dialog
final Dialog passwordDialog = new Dialog(this,R.style.Dialog);
Like the other answer, but more concise
final AlertDialog diag = new AlertDialog.Builder(this)
.setTitle("Enter An Administrative Password")
.setView(R.layout.admin_password_dialog)
.create();
diag.show();
Button diagButton = (Button) diag.findViewById(R.id.btn_confirmPassword);
diagButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// handle button click
EditText input = (EditText) diag.findViewById(R.id.edit_adminPassword);
String s = input.getText().toString();
}
});
You can try this method as well and get different view styles based upon theme used.
<style name="FilterDialogTheme" parent="#android:style/Theme.Holo.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
In Dialog constructor
public FilterDialog(Context context) {
super(context, R.style.FilterDialogTheme);
}
Use #style/Theme.Appcompat.Light.Dialog for your project.
You should use an AlertDialog.Builder instead of just creating a Dialog:
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setView(R.layout.admin_password_dialog);
builder.setTitle("Enter An Administrative Password");
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();
See here for the Android Developers Guide on Dialogs.

Android Custom Alert Dialog Design

I´m trying to implement a basic custom Alert Dialog.
It should look like this
With the following XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/tv_custom_dialog_event"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/actionbar_background"
android:gravity="top"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="100dp"
android:text="Delete Event ?"
android:textColor="#color/white"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DFDFDF" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/btn_custom_dialog_events_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_action_cancel" />
<ImageButton
android:id="#+id/btn_custom_dialog_events_true"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_action_accept" />
</LinearLayout>
</LinearLayout>
However when I run it on my emulator it looks like this:
Does anybody know why this happens and how I can fix that?
Assuming you have a class that extends Dialog you can do the following:
First define a style in styles.xml with something like this:
<style name="CustomDialogThemeTrasparent" parent="#android:style/Theme.Dialog">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Then in the constructor of your custom dialog class you set this theme:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(final Context context)
{
super(context, R.style.CustomDialogThemeTrasparent);
}
To set your custom layout as view you can make a function called setTheme() and then called in the show() of your dialog, even with parameters and a nice done layout you can make a more generic class that you can use to show your custom dialogs all over your app, something like this:
//Function to set the layout when the dialog is instantiated, here we
//set the layout and if you want you can set parameter to
//show/hide controls/views and can show different types of dialogs with the same class but with a unified style
private void setTheme()
{
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.custom_dialog, null);
this.setContentView(mView);
}
then you can make function(s) to show your dialog(s), like:
public void showDeletionDialog(String pMessage)
{
this.setTheme();
this.show();
}
hope it help you
Try this approach. You can inflate your layout:
public AlertDialog displayLayoutDialog(int layout,final Context context, int theme){
AlertDialog.Builder builder = new AlertDialog.Builder(context, theme);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layout, null);
builder.setView(view);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog= builder.create();
dialog.show();
Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
tb.setOnClickListener(new CustomListener(dialog, context));
return dialog;
}
And your Listener where all the validation and anything else that you need happens:
public class CustomListener implements View.OnClickListener {
private final Dialog dialog;
private Context context;
public CustomListener(Dialog dialog, Context context) {
this.dialog = dialog;
this.context = context;
}
#Override
public void onClick(View v) {
Toast.makeText(context, "Custom Layout", Toast.LENGTH_LONG).show();
}
}
Hope it helps!!!

Categories

Resources