Reputation: 1126
I've tried to solve this issue reading around on stackoverflow and also I've tried to understand better the use of getButton
method reading directly from android studio website.
Below the description directly from developer.android.com (bold is mine) :
public Button getButton (int whichButton)
Gets one of the buttons used in the dialog. Returns null if the specified button does not exist or the dialog has not yet been fully created (for example, via Dialog.show() or Dialog.create()).
MapsActivity.java
As you can see below I use getButton
method after Dialog.show()
but i get error: cannot find symbol method getButton(int)
. Could you tell me which is the problem? Thanks
...
case R.id.settings:
AlertDialog.Builder builder2 = new AlertDialog.Builder(this, R.style.SettingDialogStyle);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.setting_alert_dialog, null);
SeekBar sb = (SeekBar) v.findViewById(R.id.seekBar2);
final TextView txtView = (TextView) v.findViewById(R.id.intervalValue);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
txtView.setText(String.valueOf(progress)+ " sec");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
builder2.setView(v);
builder2.setTitle("Interval between updates");
builder2.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder2.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder2.show();
final Button positiveButton = builder2.getButton(DialogInterface.BUTTON_POSITIVE);
LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) positiveButton.getLayoutParams();
positiveButtonLL.gravity = Gravity.CENTER;
positiveButton.setLayoutParams(positiveButtonLL);
return true;
...
setting_alert_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/intervalValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:textColor="#FFFFFF"
android:text="TextView" />
<SeekBar
android:id="@+id/seekBar2"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:max="60"
android:progress="3"
android:progressTint="#FFFFFF"
android:thumbTint="#FFFFFF" />
</RelativeLayout>
Upvotes: 1
Views: 1678
Reputation:
getButton()
is a member of the class AlertDialog
not AlertDialog.Builder
.
if you change to:
AlertDialog alertDialog = new AlertDialog(this, R.style.SettingDialogStyle);
then you have access to alertDialog.getButton()
Alternative, after this line:
AlertDialog.Builder builder2 = new AlertDialog.Builder(this, R.style.SettingDialogStyle);
by this:
AlertDialog alertDialog = builder2.create();
you have the alert dialog that can access getButton()
Upvotes: 2
Reputation: 6160
You can verify that getButton()
is a method of AlertDialog
class by taking a look at the AOSP project at
so, the correct code should be the following:
AlertDialog dialog = builder2.show();
final Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
Upvotes: 1