Search code examples
androidcolor-picker

Android how to save picked color from a colorpicker?


I have a colorpicker with libs. Now when i tap on the Button it open a all colors that o can use.and when i tap on a color i want that this color that i taoped on a button..(how to save the picked color on/in a button)? Sorry for my bad english.... main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

</LinearLayout>

Menu_java.java(Mainactivity)

import android.app.*;
import android.os.*;
import com.fourmob.colorpicker.ColorPickerDialog;
import com.fourmob.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
import android.graphics.*;
import android.widget.*;
import android.view.*;
import android.support.v4.app.*;


public class Menu_java extends FragmentActivity {

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

        final ColorPickerDialog colorPickerDialog = new ColorPickerDialog();
        colorPickerDialog.initialize(R.string.dialog_title, new int[] { Color.CYAN, Color.LTGRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED, Color.GRAY, Color.YELLOW }, Color.YELLOW, 3, 2);
        colorPickerDialog.setOnColorSelectedListener(new OnColorSelectedListener() {

                @Override
                public void onColorSelected(int color) {
                    Toast.makeText(Menu_java.this, "selectedColor : " + color, Toast.LENGTH_SHORT).show();
                }
            });

        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    colorPickerDialog.show(getSupportFragmentManager(), "colorpicker");
                }
            });
    }





        }

Solution

  • you can save it in sharedpreferences like this:

     private SharedPreferences sharedprefs = getSharedPreferences(yourapplicationnamehere), MODE_PRIVATE);
    sharedprefs.edit().putInt("colorint" , color).apply();
    

    and read it like this:

    int colorint = sharedprefs.getString("colorint" , 0);