0

enter image description here

Actually I am Making an App Of Pin Lock Screen and I made only two activities. On Activity1, I want that If I click On ImageView Then On Activity2 Another image Display From drawable

enter image description here

On TextView and it saved On SharedPreference I am a beginner to this any Help please I'm going to stuck.

First Activity

public class MainActivity extends Activity implements View.OnClickListener{
    ImageView mpink,mpurple;
    int view;
    String display;
    private ImageView results;
    private Object get_input;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mpink=(ImageView) findViewById(R.id.imageView);
        mpurple=(ImageView) findViewById(R.id.imageView2);

        mpink.setOnClickListener(this);
        mpurple.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (results == mpurple) {
            display = "get_input";
            view = R.drawable.comb1;
        }
        else if (results == mpink) {
            display = "get_input";
            view = R.drawable.comb2;
        }
        else {
            display = "get_input";
            view = R.drawable.comb3;
        }
        Intent home_intent=new Intent(getApplicationContext(),Main2Activity.class);
        home_intent.putExtra("home_store_image_src", (Parcelable) mpink);
        home_intent.putExtra("home_store_image_src", (Parcelable) mpurple);
        startActivity(home_intent);
    }}

Second Activity

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        get_result=(TextView)findViewById(R.id.displayresult);
        Intent result_intent=getIntent();
        String text=result_intent.getStringExtra("get_input");
        get_result.setText(text);// display what the user has enter

        // get_image.setImageResource
        //OR other coding to display imageview by using the image_id from activty 1?
        int defaultIdWhenCouldNotFindImageSourceId = -1;//should use a negative value
        int imageId = result_intent.getIntExtra("home_store_image_src", defaultIdWhenCouldNotFindImageSourceId);
        if( defaultIdWhenCouldNotFindImageSourceId != imageId) {
            //when you can find an valid image id in intent extra, display it
            get_image.setImageResource(imageId);
        }
    }}
olajide
  • 972
  • 1
  • 13
  • 26

1 Answers1

0

please check this question to help you about sending drawables from one activity to another

How to pass drawable between activities

So when you use onClick

 @Override
public void onClick(View v) {
Bitmap bitmap;
    if (results == mpurple) {
        display = "get_input";
       bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.comb1);    
    }
    else if (results == mpink) {
        display = "get_input";
       bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.comb2);    
    }
    else {
        display = "get_input";
       bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.comb3);    
    }
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
   byte[] b = baos.toByteArray();

    Intent home_intent=new Intent(getApplicationContext(),Main2Activity.class);
    home_intent.putExtra("picture", b);
    home_intent.putStringExtra("getInput", display);
    startActivity(home_intent);
}}

Also, you need to edit your second activity and put an ImageView in its XML, you can't pass ImageView from Activity to another, but you can pass a drawable and set it to the ImageView in the second Activity

olajide
  • 972
  • 1
  • 13
  • 26
Ezaldeen sahb
  • 719
  • 7
  • 12