Dans mon application, j'ai une activité avec 5 EditText dont 3 EditText avec 3 Buttons. Si on clique sur un des boutons un Color Picker Dialog s'ouvre. Lorsque l'utilisateur sélectionne une couleur cela va entrainer le changement du background de l'Edittext en question.
Je souhaite sauver la couleur selectionné par l'utilisateur dans ma base de données en format hexadécimal.
Si quelqu'un pourrait m'aider. Voilà mon code :
VitrineActivity4.java :
- Code: Tout sélectionner
public class VitrineActivity4 extends Activity implements ColorPickerSecondaire.OnColorChangedListener, ColorPickerPrimaire.OnColorChangedListener, ColorPickerTertiaire.OnColorChangedListener {
public Button btnSuivant, btnPrecedent, choisir;
public EditText reference, couleur_primaire, couleur_secondaire, couleur_tertiaire, commentaire;
private ArrayList VitrineActivity4_ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vitrine4);
btnSuivant = (Button) findViewById(R.id.btnSuivant);
btnPrecedent = (Button) findViewById(R.id.btnPrecedent);
choisir = (Button) findViewById(R.id.choisir);
reference = (EditText) findViewById(R.id.reference);
commentaire = (EditText) findViewById(R.id.commentaire);
couleur_primaire = (EditText) findViewById(R.id.couleur_primaire);
couleur_secondaire = (EditText) findViewById(R.id.couleur_secondaire);
couleur_tertiaire = (EditText) findViewById(R.id.couleur_tertiaire);
VitrineActivity4_ArrayList = new ArrayList();
// Récupération de l'image selectionné parmi les maquettes si le bouton "Choisir" a été séléctionné
Bundle extras = getIntent().getExtras();
if (this.getIntent().getExtras()!=null){
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(bmp);
}
choisir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Passage à l'activité permettant de choisir la maquette
Intent intent = new Intent(VitrineActivity4.this, ReferencesActivity.class);
startActivity(intent);
}
});
// Bouton "Suivant" et ses conditions pour passer à la deuxième activité
btnSuivant.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int len1 = reference.length();
if(len1==0) {
Toast.makeText(getApplicationContext(), "Veuillez choisir une référence", Toast.LENGTH_LONG).show();
}
else {
String et1 = reference.getText().toString();
String et2 = commentaire.getText().toString();
GRAPHISME ic = new GRAPHISME();
ic.setReference(et1);
ic.setCommentaire(et2);
VitrineActivity4_ArrayList.add(ic);
insertText(ic);
// Si toutes les conditions sont remplies, passage à la seconde activité
Intent intent = new Intent(VitrineActivity4.this, VitrineActivity5.class);
startActivity(intent);
}
}
});
// Bouton "Précédent", retour à l'activité précédente
btnPrecedent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
// Changement de la couleur du fond de l'EditText "Primaire" par la couleur selectionné dans la palette
@Override
public void colorChanged1(String key, int color) {
couleur_primaire = (EditText) findViewById(R.id.couleur_primaire);
couleur_primaire.setBackgroundColor(color);
}
// Montre la palette de couleur au click du premier bouton "Choisir"
public void getColor1(View view) {
ColorPickerPrimaire color = new ColorPickerPrimaire(this,this, "picker",Color.WHITE,Color.WHITE);
color.show();
}
// Changement de la couleur du fond de l'EditText "Secondaire" par la couleur selectionné dans la palette
@Override
public void colorChanged2(String key, int color) {
couleur_secondaire = (EditText) findViewById(R.id.couleur_secondaire);
couleur_secondaire.setBackgroundColor(color);
}
// Montre la palette de couleur au click du deuxième bouton "Choisir"
public void getColor2(View view) {
ColorPickerSecondaire color = new ColorPickerSecondaire(this,this, "picker",Color.WHITE,Color.WHITE);
color.show();
}
// Changement de la couleur du fond de l'EditText "Tertiaire" par la couleur selectionné dans la palette
@Override
public void colorChanged3(String key, int color) {
couleur_tertiaire = (EditText) findViewById(R.id.couleur_tertiaire);
couleur_tertiaire.setBackgroundColor(color);
}
// Montre la palette de couleur au click du troisième bouton "Choisir"
public void getColor3(View view) {
ColorPickerTertiaire color = new ColorPickerTertiaire(this,this, "picker",Color.WHITE,Color.WHITE);
color.show();
}
public void insertText(GRAPHISME insert){
DBAdapter db = new DBAdapter(this);
SQLiteDatabase sqliteDatabase = db.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DBAdapter.REFERENCE, insert.getReference());
contentValues.put(DBAdapter.COULEUR_PRIMAIRE, insert.getCouleur_primaire());
contentValues.put(DBAdapter.COULEUR_SECONDAIRE, insert.getCouleur_secondaire());
contentValues.put(DBAdapter.COULEUR_TERTIAIRE, insert.getCouleur_tertiaire());
contentValues.put(DBAdapter.COMMENTAIRE, insert.getCommentaire());
long affectedColumnId = sqliteDatabase.insert(DBAdapter.TABLE_GRAPHISME, null, contentValues);
sqliteDatabase.close();
}
}
activity_vitrine4.xml :
- Code: Tout sélectionner
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".LoginActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/graphisme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:text="Graphisme"
android:gravity="center"
android:textSize="30dip"
android:textColor="#006FB7"/>
<EditText
android:id="@+id/reference"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="#ffffff"
android:hint="Modèle de référence *"
android:cursorVisible="false"
android:inputType="text"
android:padding="10dp"
android:singleLine="true"
android:textSize="20sp"
android:textColor="#222222"
android:textColorHint="#999999" />
<Button
android:id="@+id/choisir"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#006FB7"
android:text="Choisir"
android:textColor="#ffffff"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20sp"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<TextView
android:id="@+id/couleurs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Couleurs dominantes du site :"
android:textSize="20sp"
android:layout_marginTop="20dip"/>
<EditText
android:id="@+id/couleur_primaire"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Primaire"
android:background="#ffffff"
android:textColor="#222222"
android:inputType="text"
android:padding="10dp"
android:textColorHint="#999999"
android:textSize="20sp"
android:layout_marginTop="20dip"
android:layout_column="1"
android:layout_marginLeft="50sp"/>
<Button
android:id="@+id/button1"
android:layout_width="100sp"
android:layout_height="45dip"
android:onClick="getColor1"
android:layout_marginTop="20dip"
android:textColor="#ffffff"
android:background="#006FB7"
android:text="Choisir"
android:layout_marginLeft="50sp"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/couleur_secondaire"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Secondaire"
android:background="#ffffff"
android:textColor="#222222"
android:inputType="text"
android:padding="10dp"
android:textColorHint="#999999"
android:textSize="20sp"
android:layout_marginTop="20dip"
android:layout_column="1"
android:layout_marginLeft="50sp"/>
<Button
android:id="@+id/button2"
android:layout_width="100sp"
android:layout_height="45dip"
android:onClick="getColor2"
android:layout_marginTop="20dip"
android:textColor="#ffffff"
android:background="#006FB7"
android:text="Choisir"
android:layout_marginLeft="50sp"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/couleur_tertiaire"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Tertiaire"
android:background="#ffffff"
android:textColor="#222222"
android:inputType="text"
android:padding="10dp"
android:textColorHint="#999999"
android:textSize="20sp"
android:layout_marginTop="20dip"
android:layout_column="1"
android:layout_marginLeft="50sp"/>
<Button
android:id="@+id/button3"
android:layout_width="100sp"
android:layout_height="45dip"
android:onClick="getColor3"
android:layout_marginTop="20dip"
android:textColor="#ffffff"
android:background="#006FB7"
android:text="Choisir"
android:layout_marginLeft="50sp"/>
</TableRow>
</TableLayout>
<EditText
android:id="@+id/commentaire"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:hint="Commentaires"
android:inputType="text"
android:padding="10dp"
android:singleLine="true"
android:textSize="20sp"
android:textColor="#222222"
android:textColorHint="#999999" />
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*"
android:id="@+id/tablelayout">
<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal">
<Button
android:id="@+id/btnPrecedent"
android:layout_width="300dip"
android:layout_height="45dip"
android:layout_marginTop="40dip"
android:textColor="#ffffff"
android:background="#006FB7"
android:text="Précédent"/>
<Button
android:id="@+id/btnSuivant"
android:layout_width="300dip"
android:layout_height="45dip"
android:layout_marginTop="40dip"
android:layout_marginLeft="80sp"
android:textColor="#ffffff"
android:background="#006FB7"
android:text="Suivant"/>
</TableRow>
</TableLayout>
<Button
android:id="@+id/btnEnregistrer"
android:layout_width="300dip"
android:layout_height="45dip"
android:layout_marginTop="40dip"
android:textColor="#ffffff"
android:background="#006FB7"
android:text="Enregistrer"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
ColorPickerPrimaire.java : (ColorPickerSecondaire.java et ColorPickerTertiaire.java sont identiques, seul les chiffres changent)
- Code: Tout sélectionner
public class ColorPickerPrimaire extends Dialog {
public interface OnColorChangedListener {
void colorChanged1(String key, int color);
}
private OnColorChangedListener mListener;
private int mInitialColor, mDefaultColor;
private String mKey;
private static class ColorPickerView extends View {
private Paint mPaint;
private float mCurrentHue = 0;
private int mCurrentX = 0, mCurrentY = 0;
private int mCurrentColor, mDefaultColor;
private final int[] mHueBarColors = new int[258];
private int[] mMainColors = new int[65536];
private OnColorChangedListener mListener;
ColorPickerView(Context c, OnColorChangedListener l, int color,
int defaultColor) {
super(c);
mListener = l;
mDefaultColor = defaultColor;
// Get the current hue from the current color and update the main
// color field
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
mCurrentHue = hsv[0];
updateMainColors();
mCurrentColor = color;
// Initialize the colors of the hue slider bar
int index = 0;
for (float i = 0; i < 256; i += 256 / 42) // Red (#f00) to pink
// (#f0f)
{
mHueBarColors[index] = Color.rgb(255, 0, (int) i);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) // Pink (#f0f) to blue
// (#00f)
{
mHueBarColors[index] = Color.rgb(255 - (int) i, 0, 255);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) // Blue (#00f) to light
// blue (#0ff)
{
mHueBarColors[index] = Color.rgb(0, (int) i, 255);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) // Light blue (#0ff) to
// green (#0f0)
{
mHueBarColors[index] = Color.rgb(0, 255, 255 - (int) i);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) // Green (#0f0) to yellow
// (#ff0)
{
mHueBarColors[index] = Color.rgb((int) i, 255, 0);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) // Yellow (#ff0) to red
// (#f00)
{
mHueBarColors[index] = Color.rgb(255, 255 - (int) i, 0);
index++;
}
// Taille et alignement du texte des boutons
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setTextSize(16);
}
// Obtention de la couleur sélectionnée dans la barre de teinte
private int getCurrentMainColor() {
int translatedHue = 255 - (int) (mCurrentHue * 255 / 360);
int index = 0;
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb(255, 0, (int) i);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb(255 - (int) i, 0, 255);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb(0, (int) i, 255);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb(0, 255, 255 - (int) i);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb((int) i, 255, 0);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb(255, 255 - (int) i, 0);
index++;
}
return Color.RED;
}
// Mettre à jour les couleurs principales en fonction de la teinte sélectionnée actuelle
private void updateMainColors() {
int mainColor = getCurrentMainColor();
int index = 0;
int[] topColors = new int[256];
for (int y = 0; y < 256; y++) {
for (int x = 0; x < 256; x++) {
if (y == 0) {
mMainColors[index] = Color.rgb(
255 - (255 - Color.red(mainColor)) * x / 255,
255 - (255 - Color.green(mainColor)) * x / 255,
255 - (255 - Color.blue(mainColor)) * x / 255);
topColors[x] = mMainColors[index];
} else
mMainColors[index] = Color.rgb(
(255 - y) * Color.red(topColors[x]) / 255,
(255 - y) * Color.green(topColors[x]) / 255,
(255 - y) * Color.blue(topColors[x]) / 255);
index++;
}
}
}
@Override
protected void onDraw(Canvas canvas) {
int translatedHue = 255 - (int) (mCurrentHue * 255 / 360);
// Affichage de toutes les couleurs de la barre de teintes avec des lignes
for (int x = 0; x < 256; x++) {
// Si ce ne est pas la teinte sélectionnée, afficher la couleur d'origine
if (translatedHue != x) {
mPaint.setColor(mHueBarColors[x]);
mPaint.setStrokeWidth(1);
} else
// Sinon afficher une ligne blanche
{
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(5);
}
canvas.drawLine(x + 10, 0, x + 10, 40, mPaint);
}
// Affichage des couleurs principales à l'aide de LinearGradient
for (int x = 0; x < 256; x++) {
int[] colors = new int[2];
colors[0] = mMainColors[x];
colors[1] = Color.BLACK;
Shader shader = new LinearGradient(0, 50, 0, 306, colors, null,
Shader.TileMode.REPEAT);
mPaint.setShader(shader);
canvas.drawLine(x + 10, 50, x + 10, 306, mPaint);
}
mPaint.setShader(null);
// Affichage du cercle autour de la couleur actuellement sélectionnée
if (mCurrentX != 0 && mCurrentY != 0) {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.WHITE);
canvas.drawCircle(mCurrentX, mCurrentY, 10, mPaint);
}
// Coloration du bouton "Ok" avec la couleur actuellement sélectionnée
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mCurrentColor);
canvas.drawRect(10, 316, 138, 356, mPaint);
// Définit la couleur du texte en fonction de la luminosité de la couleur
if (Color.red(mCurrentColor) + Color.green(mCurrentColor)
+ Color.blue(mCurrentColor) < 384)
mPaint.setColor(Color.WHITE);
else
mPaint.setColor(Color.BLACK);
canvas.drawText(
getResources()
.getString(R.string.settings_bg_color_confirm), 74, 340, mPaint);
// Coloration du bouton "Annuler" avec la couleur par défaut
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mDefaultColor);
canvas.drawRect(138, 316, 266, 356, mPaint);
// Définit la couleur du texte en fonction de la luminosité de la couleur
if (Color.red(mDefaultColor) + Color.green(mDefaultColor)
+ Color.blue(mDefaultColor) < 384)
mPaint.setColor(Color.WHITE);
else
mPaint.setColor(Color.BLACK);
canvas.drawText(
getResources().getString(
R.string.settings_default_color_confirm), 202, 340,
mPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(276, 366);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN)
return true;
float x = event.getX();
float y = event.getY();
if (x > 10 && x < 266 && y > 0 && y < 40) {
mCurrentHue = (255 - x) * 360 / 255;
updateMainColors();
// Mettre à jour la couleur actuellement sélectionnée
int transX = mCurrentX - 10;
int transY = mCurrentY - 60;
int index = 256 * (transY - 1) + transX;
if (index > 0 && index < mMainColors.length)
mCurrentColor = mMainColors[256 * (transY - 1) + transX];
// Forcer le rafraichissement de la boîte de dialogue
invalidate();
}
if (x > 10 && x < 266 && y > 50 && y < 306) {
mCurrentX = (int) x;
mCurrentY = (int) y;
int transX = mCurrentX - 10;
int transY = mCurrentY - 60;
int index = 256 * (transY - 1) + transX;
if (index > 0 && index < mMainColors.length) {
mCurrentColor = mMainColors[index];
invalidate();
}
}
// If the touch event is located in the left button, notify the
// listener with the current color
if (x > 10 && x < 138 && y > 316 && y < 356)
mListener.colorChanged1("", mCurrentColor);
// If the touch event is located in the right button, notify the
// listener with the default color
if (x > 138 && x < 266 && y > 316 && y < 356)
mListener.colorChanged1("", mDefaultColor);
return true;
}
}
public ColorPickerPrimaire(Context context, OnColorChangedListener listener,
String key, int initialColor, int defaultColor) {
super(context);
mListener = listener;
mKey = key;
mInitialColor = initialColor;
mDefaultColor = defaultColor;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OnColorChangedListener l = new OnColorChangedListener() {
public void colorChanged1(String key, int color) {
mListener.colorChanged1(mKey, color);
dismiss();
}
};
setContentView(new ColorPickerView(getContext(), l, mInitialColor,
mDefaultColor));
setTitle("Choisissez une couleur");
}
}
DBAdapter.java :
- Code: Tout sélectionner
public class DBAdapter extends SQLiteOpenHelper {
// Initialisation de la base de données "cahierdescharges"
public static final String DB_NAME = "cahierdescharges.db";
public static final int DB_VERSION = 20;
// Initialisation de la table "graphisme" et de ses attributs
public static final String TABLE_GRAPHISME = "graphisme";
public static final String REFERENCE = "reference";
public static final String COULEUR_PRIMAIRE = "couleur_primaire";
public static final String COULEUR_SECONDAIRE = "couleur_secondaire";
public static final String COULEUR_TERTIAIRE = "couleur_tertiaire";
public static final String COMMENTAIRE = "commentaire";
public DBAdapter(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Création de la table "graphisme" et de ses attributs
String CREATE_TABLE_GRAPHISME = "create table if not exists " + TABLE_GRAPHISME + " ( " + PRODUITS.ID + " integer primary key autoincrement, "
+ REFERENCE + " text not null, "
+ COULEUR_PRIMAIRE + " numeric, "
+ COULEUR_SECONDAIRE + " numeric, "
+ COULEUR_TERTIAIRE + " numeric, "
+ COMMENTAIRE + " text not null);";
db.execSQL(CREATE_TABLE_GRAPHISME);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Suppression de la table si elle existe déjà dans la base de données
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GRAPHISME);
onCreate(db);
}
}
Graphisme.java :
- Code: Tout sélectionner
public class GRAPHISME {
private String reference;
private String commentaire;
private int couleur_primaire;
private int couleur_secondaire;
private int couleur_tertiaire;
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public int getCouleur_primaire() { return couleur_primaire; }
public void setCouleur_primaire(int couleur_primaire) { this.couleur_primaire = couleur_primaire; }
public int getCouleur_secondaire() { return couleur_secondaire; }
public void setCouleur_secondaire(int couleur_secondaire) { this.couleur_secondaire = couleur_secondaire; }
public int getCouleur_tertiaire() { return couleur_tertiaire; }
public void setCouleur_tertiaire(int couleur_tertiaire) { this.couleur_tertiaire = couleur_tertiaire; }
public String getCommentaire() {
return commentaire;
}
public void setCommentaire(String commentaire) {
this.commentaire = commentaire;
}
}
Si quelque a des idées, merci.