Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android receive value from onActivityResult and set it to a Button

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??

        }
    }
}
like image 217
user2847219 Avatar asked Dec 12 '16 17:12

user2847219


2 Answers

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);
        }
    }
}
like image 178
Jinesh Francis Avatar answered Sep 27 '22 22:09

Jinesh Francis


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);
like image 34
Opiatefuchs Avatar answered Sep 27 '22 20:09

Opiatefuchs