0

Hello this is my first question so please forgive me if its in bad format, I have my main activity with three buttons,Button 1 and 2 lead to numberpickers that allow user to pick from numbers to set a background image. My problem is once they pick the color (rgb) i need it sent back to the main activity but then i want the button to immediately change to the background color they chose then they can go to button two and do the same thing, at the end of the process both button1 and 2 should be the colors they have chosen. Which i will then mix in button 3(which i havent at all started on) My main problem is i pick one then it set but then i pick the other one and it onCreates() and sets the other back to a default , So i somehow need to change the colors immedietly after the activity returns to the main activity my code is a bit ugly but ive just been trying to get it working then i was gonna go back and make it more efficient so my apologize

//MAIN CONTENT

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="50dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bignerdranch.android.colormixer.MainActivity"
tools:showIn="@layout/activity_main">



<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mixButton"
    android:id="@+id/screen1MixerButton"
    android:layout_below="@+id/screen1Button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="103dp" />

<Button
    android:text="@string/Color1Text"
    android:layout_width="162dp"
    android:layout_height="188dp"
    android:id="@+id/screen1Button1"
    android:src="#ffffff"
    android:background="#ffffff"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
     />

<Button
    android:text="@string/Color2Text"
    android:layout_width="162dp"
    android:layout_height="188dp"
    android:id="@+id/screen1Button2"
    android:background="#ffffff"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignBottom="@+id/screen1Button1" />

</RelativeLayout>

//COLORRPICKER1 ACTIVITY

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bignerdranch.android.colormixer.ColorPicker1"
tools:showIn="@layout/activity_color_picker1">

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/numberPicker1"></NumberPicker>

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/numberPicker2"
    android:orientation="horizontal"></NumberPicker>

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/numberPicker2"
    android:layout_alignParentEnd="true"
    android:orientation="horizontal"
    android:id="@+id/numberPicker3"></NumberPicker>



    <SurfaceView
        android:layout_width="fill_parent"
        android:layout_height="71dp"
        android:id="@+id/surfaceView1"
        android:layout_gravity="center_vertical"
        android:background="#000000"
        android:layout_below="@+id/numberPicker1"
        android:layout_alignParentStart="true"
        android:layout_marginTop="99dp"></SurfaceView>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pick Color"
    android:id="@+id/PickColor1"
    android:layout_below="@+id/surfaceView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="52dp" />

//COLORPICKER2 activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bignerdranch.android.colormixer.ColorPicker2"
tools:showIn="@layout/activity_color_picker2">

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/numberPicker1"></NumberPicker>

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/numberPicker2"
    android:orientation="horizontal"></NumberPicker>

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/numberPicker2"
    android:layout_alignParentEnd="true"
    android:orientation="horizontal"
    android:id="@+id/numberPicker3"></NumberPicker>



    <SurfaceView
        android:layout_width="fill_parent"
        android:layout_height="71dp"
        android:id="@+id/surfaceView2"
        android:layout_gravity="center_vertical"
        android:background="#000000"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"></SurfaceView>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pick Color"
    android:id="@+id/PickColor2"
    android:layout_below="@+id/surfaceView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="84dp" />


</RelativeLayout>

//MAIN ACTIVITY

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;'

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MYTAG";
public static Button colorButton1;
private Button colorButton2;
private Button mixerButton;
int red1, green1, blue1,red2, green2, blue2 =255;
public static final int REQUEST_CODE= 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "in onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //START SETTING UP BUTTONS
    colorButton1 = (Button) findViewById(R.id.screen1Button1);
    colorButton2 = (Button) findViewById(R.id.screen1Button2);
    mixerButton = (Button) findViewById(R.id.screen1MixerButton);








    //ONCLICKLISTENER FOR MULTIPLE BUTTONS
     View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.screen1Button1:
                    Log.i(TAG, "screen1Button1 TAPPED");
                    Toast.makeText(MainActivity.this, "screen1Button1 TAPPED", Toast.LENGTH_LONG).show();
                    Intent i1 = new Intent(MainActivity.this,ColorPicker1.class);

                    startActivityForResult(i1,REQUEST_CODE);

                    break;

                case R.id.screen1Button2:
                    Log.i(TAG, "screen1Button2 TAPPED");
                    Toast.makeText(MainActivity.this, "screen1Button2 TAPPED", Toast.LENGTH_LONG).show();
                    Intent i2 = new Intent(MainActivity.this,ColorPicker2.class);
                    startActivity(i2);
                    Log.i(TAG, "BACKFROM1");

                    break;

                case R.id.screen1MixerButton:
                    Log.i(TAG, "screen1MixerButton TAPPED");
                    Toast.makeText(MainActivity.this, "screen1MixerButton TAPPED", Toast.LENGTH_LONG).show();
                    Intent i3 = new Intent(MainActivity.this,MixedColorScreen.class);
                    startActivity(i3);

                    Log.i(TAG, "test value recieved" + red1);
                    Log.i(TAG, "test value recieved" + green1);
                    Log.i(TAG, "test value recieved" + blue1);
                    Log.i(TAG, "test value recieved" + red2);
                    Log.i(TAG, "test value recieved" + green2);
                    Log.i(TAG, "test value recieved" + blue2);
                    red1 = getIntent().getIntExtra("ColorPicker1_red",0);
                    green1 = getIntent().getIntExtra("ColorPicker1_green",0);
                    blue1 = getIntent().getIntExtra("ColorPicker1_blue",0);
                    red2 = getIntent().getIntExtra("ColorPicker2_red",0);
                    green2 = getIntent().getIntExtra("ColorPicker2_green",0);
                    blue2 = getIntent().getIntExtra("ColorPicker2_blue",0);
                    colorButton1.setBackgroundColor(Color.rgb(red1, green1, blue1));
                    colorButton2.setBackgroundColor(Color.rgb(red2, green2, blue2));

                    break;

            }

        }
    };

    colorButton1.setOnClickListener(onClickListener);
    colorButton2.setOnClickListener(onClickListener);
    mixerButton.setOnClickListener(onClickListener);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.i(TAG, "in onCreateOptionsMenu");
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.i(TAG, "in onOptionsItemsSelected");
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

//colorpicker1 activity

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

public class ColorPicker1 extends AppCompatActivity {
private static final String TAG = "MYTAG";
NumberPicker np, np2, np3;
SurfaceView img;
int red, green, blue ;
private Button pickColorButton1;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_picker1);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    pickColorButton1 = (Button) findViewById(R.id.PickColor1);
    pickColorButton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {;
            Intent colorPick1 = new Intent(ColorPicker1.this,MainActivity.class);
            colorPick1.putExtra("ColorPicker1_red",red);
            colorPick1.putExtra("ColorPicker1_green", green);
            colorPick1.putExtra("ColorPicker1_blue", blue);
            //TRY

            //MainActivity.colorButton1 = (Button) findViewById(R.id.screen1Button1);
            //colorPick1.setBackgroundColor(Color.rgb(red, green, blue));

            //END TRY
            Log.i(TAG, "PUTING IN PUTEXTRAS VALUES RED BLUE GREEN =" + red +green + blue);
            startActivity(colorPick1);
        }
    });

    //STARTING LOGS ----------------------------------------------------------------------------
    Log.i(TAG,"IN COLORPICKER1");
    Toast.makeText(ColorPicker1.this, "IN COLORPICKER1", Toast.LENGTH_LONG).show();


    //SETUP NUMBERPICKERS-----------------------------------------------------------------------
    np = (NumberPicker) findViewById(R.id.numberPicker1);
    np.setMinValue(0);
    np.setMaxValue(255);
    np.setWrapSelectorWheel(true);

    np2 = (NumberPicker) findViewById(R.id.numberPicker2);
    np2.setMinValue(0);
    np2.setMaxValue(255);
    np2.setWrapSelectorWheel(true);

    np3 = (NumberPicker) findViewById(R.id.numberPicker3);
    np3.setMinValue(0);
    np3.setMaxValue(255);
    np3.setWrapSelectorWheel(true);

    //VALUE CHANGED LISTENERS-------------------------------------------------------------------
    np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            red = np.getValue();
            img = (SurfaceView) findViewById(R.id.surfaceView1);
            img.setBackgroundColor(Color.rgb(red, green, blue));

        }
    });

    np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            green = np2.getValue();
            img = (SurfaceView) findViewById(R.id.surfaceView1);
            img.setBackgroundColor(Color.rgb(red, green, blue));
        }
    });
    np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            blue = newVal;
            img = (SurfaceView) findViewById(R.id.surfaceView1);
            img.setBackgroundColor(Color.rgb(red, green, blue));
        }
    });
    //CLOSE-------------------------------------------------------------------------------------

}

}

//colorpicker2 activity

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

public class ColorPicker2 extends AppCompatActivity {
private static final String TAG = "MYTAG";
NumberPicker np, np2, np3;
SurfaceView img;
int red, green, blue ;//might not need
private Button pickColorButton2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_picker2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    pickColorButton2 = (Button) findViewById(R.id.PickColor2);
    pickColorButton2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ColorPicker2.this, "COlorPick2", Toast.LENGTH_LONG).show();
            Intent colorPick2 = new Intent(ColorPicker2.this,MainActivity.class);
            colorPick2.putExtra("ColorPicker2_red",red);
            colorPick2.putExtra("ColorPicker2_green", green);
            colorPick2.putExtra("ColorPicker2_blue", blue);

            Log.i(TAG, "PUTING IN PUTEXTRAS VALUES RED BLUE GREEN =" + red + green + blue);
            startActivity(colorPick2);
        }
    });

    //STARTING log -----------------------------------------------------------------------------
    Log.i(TAG, "IN COLORPICKER2");
    Toast.makeText(ColorPicker2.this, "IN COLOPICKER2", Toast.LENGTH_LONG).show();



    //SETTING UP NUMBER PICKERS ----------------------------------------------------------------
    np = (NumberPicker) findViewById(R.id.numberPicker1);
    np.setMinValue(0);
    np.setMaxValue(255);
    np.setWrapSelectorWheel(true);

    np2 = (NumberPicker) findViewById(R.id.numberPicker2);
    np2.setMinValue(0);
    np2.setMaxValue(255);
    np2.setWrapSelectorWheel(true);

    np3 = (NumberPicker) findViewById(R.id.numberPicker3);
    np3.setMinValue(0);
    np3.setMaxValue(255);
    np3.setWrapSelectorWheel(true);

    //SETTING UP LISTENERS ---------------------------------------------------------------------
    np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            red = np.getValue();
            img = (SurfaceView) findViewById(R.id.surfaceView2);
            img.setBackgroundColor(Color.rgb(red, green, blue));
        }
    });

    np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            green = np2.getValue();
            img = (SurfaceView) findViewById(R.id.surfaceView2);
            img.setBackgroundColor(Color.rgb(red, green, blue));
        }
    });
    np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            blue = newVal;
            img = (SurfaceView) findViewById(R.id.surfaceView2);
            img.setBackgroundColor(Color.rgb(red, green, blue));
        }
    });
    //end --------------------------------------------------------------------------------------
}

}
K..
  • 83
  • 1
  • 7

1 Answers1

0

For this you have to call startActivityForResult every time when you open ColorPicker1/2/3

Like this

    Intent i1 = new Intent(MainActivity.this,ColorPicker1.class);
                        startActivityForResult(i1,REQUEST_CODE_COlLOR1);
//REQUEST_CODE must be greater than`0` so keep it `101` for example

and for ColorPicker2 keep REQUEST_CODE_COlLOR2 as 102 and for ColorPicker3 REQUEST_CODE_COlLOR2 as 103

and in your ColorActivity1/2/3 Call intent like this with respective request codes

Intent i = new Intent();
i.putExtra("color1", yourColor);
setResult(REQUEST_CODE_COlLOR1, i);
finish();

for in Second color activity

Intent i = new Intent();
i.putExtra("color2", yourColor);
setResult(REQUEST_CODE_COlLOR2, i);
finish();

In your mixed

Intent i = new Intent();
i.putExtra("color3", yourColor);
setResult(REQUEST_CODE_COlLOR3, i);
finish();

and then write OnActivityResult Override method in MainActivity to get the result from ColorPicker1/2/3

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == REQUEST_CODE_COlLOR1) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            //Set your color on your first button
           yourColor =  data.getExtras().get("color1");
        }

    }
    if (requestCode == REQUEST_CODE_COlLOR2) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            //Set your color on your second button
           yourColor2 =  data.getExtras().get("color2");
        }

    }
    if (requestCode == REQUEST_CODE_COlLOR3) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            //Set your color on your mixed color button
            yourColor3 =  data.getExtras().get("color3");
        }

    }
}
Shishram
  • 1,514
  • 11
  • 20