With this code, I can easily insert dynamically some layouts. The layout contains a Button
, which I want to launch startActivityForResult
. Now when I get the result (text), I want to set it on the Button
.
btnAggiungiCampo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(this, btnAggiungiCampo);
popup.getMenuInflater().inflate(R.menu.menu_campi, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
View child = null;
if (item.getTitle().equals(getString(R.string.Text))) {
child = getLayoutInflater().inflate(R.layout.inflate_campo, null);
rlCampi.addView(child);
Button btnGeneraPSW = (Button) child.findViewById(R.id.imageButton3);
btnGeneraPSW.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent inte = new Intent(this, Genera_password.class);
startActivityForResult(inte, REQ_CODE_ACT1);
}
});
}
}
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQ_CODE_ACT1) {
// how can I set??
}
}
}
Do this in Genera_password Activity after completing all the operations.
Intent data=new Intent();
data.putExtra("text",requiredText);
setResult(Activity.RESULT_OK,data);
finish(); //to destroy Genera_password Activity
In OnActivityResult of the current activity
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQ_CODE_ACT1) {
String requredText=data.getExtras().getString("text");
button.setText(requredText);
}
}
}
You cannot set a text on an ImageButton. ImageButton has no method for this. Instead you have to use a Button, or, if the image is important, use an ImageButton with a TextView beneath.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerInside"
android:id="@+id/yourImageButton"
android:src="@drawable/yourSource"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/yourTextView"
/>
</LinearLayout>
And then set the text you retrieve to your TextView:
mYourTextView.setText(retrievedText);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With