Reputation: 481
I'm new to android.
I want to give value from a edittext and set it to a button text. I Can do that when edittext and button are in separate activities (with intents and putting extras), but what about when they are in same activity?
here is the code that I so far got (I'm using dialog for getting edittext value and the edittext is in "addalbumsdialog" layout):
public class Albums extends Activity {
Button b1;
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
Button b2 = (Button) findViewById(R.id.addalbum);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startDialog();
}
});
}
protected void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
et1 = (EditText) findViewById(R.id.editText1);
myAlertDialog.setTitle("Name?");
myAlertDialog.setView(inflater.inflate(R.layout.addalbumsdialog, null));
myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
b1 = (Button) findViewById(R.id.album1);
b1.setText(et1.getText().toString());
b1.setVisibility(View.VISIBLE);
}
});
myAlertDialog.show();
}
}
Thanks.
Upvotes: 1
Views: 113
Reputation: 47817
You should replace this
LayoutInflater inflater = this.getLayoutInflater();
et1 = (EditText) findViewById(R.id.editText1);
myAlertDialog.setTitle("Name?");
myAlertDialog.setView(inflater.inflate(R.layout.addalbumsdialog, null));
With
View view = (View) inflater.inflate(R.layout.addalbumsdialog, null);
myAlertDialog.setView(view);
et1 = (EditText) view.findViewById(R.id.editText1);
b1 = (Button) view.findViewById(R.id.album1);
myAlertDialog.setTitle("Name?");
And add create Button b2
as instance variable like:
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
b2 = (Button) findViewById(R.id.addalbum);
And then on Button click
@Override
public void onClick(DialogInterface arg0, int arg1) {
b2.setText(et1.getText().toString();
}
});
Upvotes: 1
Reputation: 133560
You need to change
View view = (View) inflater.inflate(R.layout.addalbumsdialog, null);
myAlertDialog.setView(view);
et1 = (EditText) view.findViewById(R.id.editText1);
myAlertDialog.setTitle("Name?");
Declare Button as a instance variable
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
b2 = (Button) findViewById(R.id.addalbum);
Then on Button click
@Override
public void onClick(DialogInterface arg0, int arg1) {
b2.setText(et1.getText().toString();
}
});
If Button b1 belongs to the view inflated and you need to setText to it
Then
protected void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
View view = (View) inflater.inflate(R.layout.addalbumsdialog, null);
myAlertDialog.setView(view);
et1 = (EditText) view.findViewById(R.id.editText1);
b1 = (Button) view.findViewById(R.id.album1);
myAlertDialog.setTitle("Name?");
myAlertDialog.setView(inflater.inflate(R.layout.addalbumsdialog, null));
myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
b1.setVisibility(View.VISIBLE);
b1.setText(et1.getText().toString());
}
});
myAlertDialog.show();
}
You haven't mentioned which button text you want to change. So posted both
Upvotes: 1
Reputation: 1265
replace the code like this
public class Albums extends Activity {
Button b1;
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
Button b2 = (Button) findViewById(R.id.addalbum);
b1 = (Button) findViewById(R.id.album1);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startDialog();
}
});
}
protected void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
et1 = (EditText) findViewById(R.id.editText1);
myAlertDialog.setTitle("Name?");
myAlertDialog.setView(inflater.inflate(R.layout.addalbumsdialog, null));
myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
b1.setText(et1.getText().toString());
}
});
myAlertDialog.show();
}
}
Upvotes: 0