Android dialog mutable single line type

In a nutshell, I am working on a "DurationPickerDialog" which works similarly to how DatePickerDialog works, but based on xsd: duration , so the user specifies the number of years, months, days, etc.

I also implemented a "fuzzy duration" function that gives me the duration as a string like "a month ago". I would really like to update the DurationPickerDialog header in the same way as the DatePickerDialog header, but there seems to be a problem. In the DatePickerDialog, they are set as a single string anyway, so it doesn't get "jittery". Here, as Android source is named DatePickerDialog.

// Note: before the skim-readers look at this bit, realize that this is NOT my
// code but Android internal code for the DatePickerDialog.
@Override
public void show() {
    super.show();

    /* Sometimes the full month is displayed causing the title
     * to be very long, in those cases ensure it doesn't wrap to
     * 2 lines (as that looks jumpy) and ensure we ellipsize the end.
     */
    TextView title = (TextView) findViewById(R.id.alertTitle);
    title.setSingleLine();
    title.setEllipsize(TruncateAt.END);
}

      

Unfortunately, I cannot access them R.id.alertTitle

because it is part of com.android.internal.R

.

I've seen implementations like this StackOverflow question where I needed to change the attribute Window.FEATURE_CUSTOM_TITLE

, but that doesn't seem to allow me to change the title (easily) after that.

There was also a StackOverflow question that mentioned how to change the title at runtime between two different XML layouts, but that also doesn't seem like it'll be all too useful since the title has to be changed every time when the duration changes, and creating an XML layout for each duration is obviously not a good idea.

So, since they "cheat" by referring to a value that we mere mortals do not have access to, is there any other way I could do it?


Edit: And through some black magic it seems like it now makes the text ellipsis the way I wanted it to? This was not the case before, and now I cannot reproduce the problem. So, I suppose while we are doing this, can someone explain to me how I could do this magic?

0


source to share


2 answers


I would implement a custom dialog by extending the class Dialog

and creating a custom xml layout for it.
To control the values, you will need some custom button backgrounds for the plus / minus, top / bottom, and some buttons. Since you are going to use a duration value, you will probably need more space than anyway. This way you can set the title to whatever you like. Let me know if you need some sample code.

Example: Dialog class:

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DurationDialog extends Dialog {

    private Button yearButtonPlus;
    private Button yearButtonMinus;
    private TextView dialogBody;
    private TextView dialogTitle;

    private String dialogBodyString;
    private String dialogTitleString;


    public DurationDialog(final Context context, String dialogBody, String dialogTitle) {
        super(context,R.style.CustomDialogTheme);
        this.dialogBodyString = dialogBody;
        this.dialogTitleString = dialogTitle;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.my_dialog);

        yearButtonPlus = (Button) findViewById(R.id.dialog_year_button_plus);
        yearButtonMinus = (Button) findViewById(R.id.dialog_year_button_minus);
        dialogBody = (TextView) findViewById(R.id.dialog_body);
        dialogTitle = (TextView) findViewById(R.id.dialog_title);

        dialogBody.setText(dialogBodyString);
        dialogTitle.setText(dialogTitleString);


        yearButtonPlus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //do year increment here
            }
        });

        //etc...
    }

}

      

In your activity, you call this.showDialog(DURATION_DIALOG);

// DURATION_DIALOG - this is an integer specified at the top of your activity, to identify the dialog for the following piece of code that handles the actual creation of the dialog:



import android.app.Activity;
import android.app.Dialog;

public class MyActivity extends Activity  {

    private Dialog dialog;


    //Lots of other activity stuff...



    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case DURATION_DIALOG:
                dialog = new DurationDialog(Activity.this, "your title", "your body");
                dialog.setOnDismissListener(onDismissListener);
                break;
            default:
                dialog = null;
        }
        return dialog;
    }

}

//put this listener in as an inner class of MyActivity:
    private DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {

        DurationDialog dialog = (DurationDialog) dialog;
        //grab the duration stuff out of your dialog and do stuff with it....

    }
};

      

Finally, you can set the dialog theme as above in the file styles.xml

. For example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/bgnd_transparent</item>
        <item name="android:windowIsFloating">true</item>
    </style>
</resources>

      

Good luck!

0


source


If you want to change the title of the TextView dialog (change text, style, behavior ...) without using a custom view, just do this:



TextView tv = (TextView) dialog.findViewById(android.R.id.title);
tv.setText("New title");
...

      

0


source







All Articles