Android receive value from onActivityResult and set it to a Button
Asked Answered
O

5

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

        }
    }
}
Ollieollis answered 12/12, 2016 at 17:49 Comment(1)
Set flag onActivityResult and then add your view in onResume.Emmery
A
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);
        }
    }
}
Ataghan answered 20/12, 2016 at 8:13 Comment(1)
This will fail because the view is created after onActivityResultBluestocking
B
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);
Bogtrotter answered 12/12, 2016 at 18:40 Comment(0)
T
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 
            }
        }
    }
}
Triangular answered 12/12, 2016 at 19:5 Comment(4)
Yes u should beTriangular
my setting result but not work: Intent intent = new Intent(); intent.putExtra("btnGeneraPSW_position", tvPassword.getText().toString()); setResult(RESULT_OK, intent); finish();Ollieollis
You are not doing well you have to do as i told you to do. You should intent.putExtra("btnGeneraPSW_position", (int) v.getTag()) instead of intent.putExtra("btnGeneraPSW_position", tvPassword.getText().toString())Triangular
with your response I get "null"Ollieollis
Z
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.

Zany answered 23/12, 2016 at 12:30 Comment(0)
B
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;
    }

}
Bluestocking answered 23/12, 2016 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.