MichalCh
MichalCh

Reputation: 241

Changing button text from AlertDialog

i am new to developing apps for android and i want to create a simple Conterter app, just for the start. In my view i have a edittext and a Button. If i click the button, it will open a AlertDialog with list of strings. I cant figure out how to manage this: When i click on one item in the AlertView i want to set the text of the button to the selected string and dismiss the AlertDialog. Can somebody please help me ?

public class VypocetDlzkyActivity extends Activity {

EditText HodnotaDlzka;
Button prevodDlzkaZtlacidlo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vypocet_dlzky);




}

public void zmenPrevodZ(View view){

    final String[] jednotkyDlzky = {"milimeter", "centimeter", "decimeter", "meter", "kilometer", "svetelny rok"};
    AlertDialog.Builder builder = new AlertDialog.Builder(VypocetDlzkyActivity.this);
    builder.setTitle("Vyberte jednotku");
    builder.setItems(jednotkyDlzky,null);
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
        String value = jednotkyDlzky[item].toString();
        prevodDlzkaZtlacidlo.setText(value);
                 dialog.cancel();
    }
    };

    final AlertDialog alert = builder.create();
    alert.show();

}

Upvotes: 0

Views: 800

Answers (2)

Sjk
Sjk

Reputation: 387

Problem is you didnt add any onClick listnerz.On clicking on button you need to call the required method.

public class MainActivity extends Activity implements OnClickListener {

EditText HodnotaDlzka;
Button prevodDlzkaZtlacidlo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    HodnotaDlzka = (EditText) findViewById(R.id.e1);
    prevodDlzkaZtlacidlo = (Button) findViewById(R.id.b1);
    prevodDlzkaZtlacidlo.setOnClickListener(this);

}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
     final String[] jednotkyDlzky = {"milimeter", "centimeter", "decimeter", "meter", "kilometer", "svetelny rok"};
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Vyberte jednotku");
        builder.setItems(jednotkyDlzky,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
            String value = jednotkyDlzky[item].toString();
            prevodDlzkaZtlacidlo.setText(value);
        }
        });
    enter code here
        final AlertDialog alert = builder.create();
        alert.show();
}
}

Upvotes: 0

David Wasser
David Wasser

Reputation: 95578

You need to set the values of these 2 member variables in your onCreate() method, like this:

HodnotaDlzka = (EditText)findViewById(R.id.xxxx);
prevodDlzkaZtlacidlo = (Button)findViewById(R.id.yyyy);

xxxx is the ID you gave the EditText in activity_vypocet_dlzky.xml and yyyy is the ID you gave to the Button.

Also, after a button is clicked in the AlertDialog, the dialog is automatically dismissed, so you don't need to call dialog.cancel().

Upvotes: 1

Related Questions