9

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

        }
    }
}
Blo
  • 11,903
  • 5
  • 45
  • 99
user2847219
  • 555
  • 1
  • 16
  • 27

5 Answers5

8

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);
        }
    }
}
Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
2

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);
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
2

Your rlCampi is a ViewGroup and you are adding child in it using rlCampi.addView(child). You can find how many child's are present in your View using rlCampi.getChildCount().

Now replace your code with below

ImageButton btnGeneraPSW = (ImageButton) child.findViewById(R.id.imageButton3);
btnGeneraPSW.setTag(rlCampi.getChildCount());
btnGeneraPSW.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, Genera_password.class);
        intent.putExtra("btnGeneraPSW_position", (int) v.getTa());
        startActivityForResult(intent, REQ_CODE_ACT1);
    }
});

And when you are setting result add these lines

Intent data = new Intent();
data.putExtra("btnGeneraPSW_position", getIntent().getIntExtra("btnGeneraPSW_position", -1));
setResult(Activity.RESULT_OK, data);

And inside onActivityResult

@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) {
            int btnPosition = data.getIntExtra("btnGeneraPSW_position", -1);
            if(btnPosition != -1){
                View childView = rlCampi.getChildAt(btnPosition);
                // now you have your childView and activity result data 
                // using childView find your view and change its text you have from activity result data 
            }
        }
    }
}
Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28
1

You should set result in Genera_password activity. You can do this like:

Intent intent = new Intent(); 
intent.putExtra("btnGeneraPSW_position", tvPassword.getText().toString());
setResult(RESULT_OK, intent); 
finish();

Then you can get this String value from onActivityResult like this:

@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 tvPassword = data.getExtras().getString("btnGeneraPSW_position");
            ((Button) child.findViewById(R.id.imageButton3)).setText(tvPassword);
        }
    }
}

Good luck.

Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
1

onActivityResult is called before the view is created. It means you cannot set anything to a button because the button doesn't exits yet. As @Qamar said you have to save the result into a variable and the check in onActivityResume that variable and set the correct value to the button.

Object result = null;

@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) {
            result = data.get....
        }
    }
}

@Override
public void onResume() {
     if (data != null) {
          findViewById(id).setValue(result);
          result = null;
    }

}
jorgemf
  • 1,123
  • 8
  • 13