I have created a custom dialog that is used from a preference screen. Everything works fine except one thing: I want to switch the visibility of the Cancel button based on the status of an internal check.
Normally you have onPrepareDialog and onCreateDialog and you can do this in onCreateDialog. But here we have onPrepareDialogBuilder... so where is onCreateDialogBuilder? Where can I do something like
builder.setNegativeButton(null, null);
after onPrepareDialogBuilder? I cannot do it IN onPrepareDialogBuilder since I need the Cancel button in case the internal check fails.
Can you please help me to get into the right direction?
public UnlockPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.dialog_enter_registration);
}
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder);
builder.setTitle(R.string.label_enter_registration);
}
// would need something like
#Override
protected void onCreateDialogBuilder(AlertDialog.Builder builder) {
super.onCreateDialogBuilder(builder);
if (internalCheckOk())
builder.setNegativeButton(null, null);
else
builder.setNegativeButton(..., ...);
}
Related
I want to implement an about box in my wallpaper. For that, I want a window to show up which contains the information when a user clicks on it.
None of the available controls like checkbox, listview etc. can be used for this......
I also tried to make a Dialog Pref by following advice from some other questions I looked at but ended up with a dialog that just does not seem appropriate for an about box.
My DialogPref:
public class DialogPref extends DialogPreference {
public DialogPref(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
persistBoolean(positiveResult);
}
}
So can you help me implement a popup window for an about box?
I have an activity (MyActivity1) which starts a view class (MyView1) using setContentView();
In MyView1, after some drawings with canvas, I want to be able to return to MyActivity1. I tried MyView1.this.setVisibility(View.INVISIBLE) or View.GONE but these just make the screen blank.
I also tried ((Activity)getContext()).finish(); but it creates a runtime error.
I should say that the back button works fine and pressing it closes MyView1 and brings the activity back. However, I want to be able to do that programmatically inside the view. for example I want when user touches a specific part of the screen in the view class, it closes itself and brings back the parent activity. How should I implement this?
It seems a very simple task! but I could not find the answer after searching through the similar questions.
Here is the way MyActivity1 starts the view:
public class MyActivity1 extends Activity {
MyView1 View1;
public void Start_Button(View view)
{
Context ctx = getApplicationContext();
View1= new MyView1(ctx, null);
setContentView(View1);
}
}
and the part in MyView1 where I want to write something to close itself and bring MyActivity1 back:
public class MyView1 extends View {
static Context mycontext;
public MyView1(Context context, AttributeSet attrs) {
super(context, attrs);
mycontext=context;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.setVisibility(View.INVISIBLE);
((Activity) mycontext).setContentView(R.layout.activity_myactivity1);
// This gives runtime error
}
}
public public MyView1(Activity activity, AttributeSet attrs) {
super(context, attrs);
myActivity=activity;
}
public void Start_Button(View view)
{
View1= new MyView1(this, null);
setContentView(View1);
}
Just call myActivity.setContentView(R.layout.original_layout_for_MyActivity1);
I've got with my own layout.
I would like to receive the text from the editText after positive button on the layout is clicked. But in onPreferenceChange, I always get only the default value.
It seems that I need to bind my own EditText to the preferences somehow, but I don't know how and where to do this.
Can anybody help me?
To answer my own question:
First of all, in PreferenceScreen, you need to state:
<full.path.to.your.OwnLayoutClass
android:name="whatevever"
android:dialogLayout="#layout/your_own_layout" />
your_own_layout can be anything you'd like, linearlayout with of buttons, editTexts, according to your wishes.
Essential is the class representing your own preference Dialog. Here is a simple example of how to do it:
public class YourOwnLayoutClass extends DialogPreference {
private LinearLayout mView;
public YourOwnLayoutClass(Context context, AttributeSet attrs) {
super(context, attrs);
setPersistent(false);
setDialogLayoutResource(R.layout.your_own_layout);
}
#Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
mView = (LinearLayout) view;
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
// get value from editFields, do whatever you want here :)
// you can acces them through mView variable very easily
}
}
}
Important references:
I need to have a custom dialog in Preferences
Concise way of writing new DialogPreference classes?
Android: launch a custom Preference from a PreferenceActivity
I have an EditTextPreference in the PreferenceActivity. When user click the EditTextPreference will show a dialog. In the dialog, user can input a value, and the dialog has "OK" and "Cancel" buttons. I want to call the click event of ok button to check the value, but I do not know how to call the click even.
I know I can use EditTextPreference.setOnPreferenceChangeListener(), but I want to know if I can use OK button click event.
You can extend EditTextPreference to get control over the click handler.
package myPackage;
public class CustomEditTextPreference extends EditTextPreference {
public CustomEditTextPreference(Context context) {
super(context);
}
public CustomEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
// add Handler here
}
super.onClick(dialog, which);
}
}
In the Xml instead of <EditTextPreference/> reference it like this:
<myPackage.CustomEditTextPreference android:dialogTitle="Registration Key" android:key="challengeKey" android:title="Registration Key" android:summary="Click here to enter the registration key you received by email."/>
Actually you can't since the preference is using an internal AlertDialog.Builder and creates a new dialog every time you click the preference. The next problem is that the dialog builder sets the click listener for you and if you override them you might destroy the close behavior of the button click.
This bothered me since I wanted a preference which only closes on valid input (otherwise a toast is shown and user should press cancel if he can't get it right).
(If you really need a solution for exactly this problem) You can find general solution of a validating DialogPreference here and a validating EditTextPreference here which I wrote myself.
Your preference activity doesn't appear to be implementing a
OnSharedPreferenceChangeListener
You may want to read over the excellent answer to the question: Updating EditPreference
Take a look at this example:
public class TestEditSoftKbdActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.editText1).setFocusable(false);
findViewById(R.id.editText1).setClickable(false);
findViewById(R.id.editText1).setEnabled(false);
findViewById(R.id.editText1).setFocusable(true);
findViewById(R.id.editText1).setClickable(true);
findViewById(R.id.editText1).setEnabled(true);
findViewById(R.id.editText1).invalidate();
findViewById(R.id.editText1).requestLayout();
}
}
After this sequence of calls the edit text view would no longer pop up its soft input method upon being touched :(
Could someone explain what is going wrong here?
If you want to close soft keyboard for your text view follow this link. Here is a solution for you. But you need to define your own TextView to do that. He suggests using;
public class NoImeEditText extends EditText {
public EditTextEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onCheckIsTextEditor() {
return false;
}
}
Hope it works.