Android Open Source - Text-Edit-for-Android Color Preference






From Project

Back to project page Text-Edit-for-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project Text-Edit-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.paulmach.textedit;
// w ww .j  av  a 2  s  . c  o  m
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

/* ColorPreference
 *     Lets you pick a color
 */
public class ColorPreference extends DialogPreference
{
  protected int color;
  protected int defcolor;
  protected String attribute;
  
  // This is the constructor called by the inflater
  public ColorPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    
    attribute = attrs.getAttributeValue(1);
    
    // set the layout so we can see the preview color
    setWidgetLayoutResource(R.layout.prefcolor);

    // figure out what the current color is
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
    
    if (attribute.equals("fontcolor"))
      defcolor = 0xFFCCCCCC;
    else
      defcolor = 0xFF000000;
    
    color = sharedPref.getInt(attribute, defcolor);
  }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        
        // Set our custom views inside the layout
        final View myView = (View) view.findViewById(R.id.currentcolor);
        if (myView != null) {
            myView.setBackgroundColor(color);
        }
    }
  
  @Override
  protected void onPrepareDialogBuilder(AlertDialog.Builder builder){
      // Data has changed, notify so UI can be refreshed!
    builder.setTitle("Choose a color");
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
        // save the color
        Editor editor = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();            
        editor.putInt(attribute, color);
        editor.commit();
        
        notifyChanged();
      }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
        // set it back to original
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
        color = sharedPref.getInt(attribute, defcolor);
      }
    });
    
    // setup the view
    LayoutInflater factory = LayoutInflater.from(getContext());
    final View colorView = factory.inflate(R.layout.colorchooser, null);
    final ImageView colormap = (ImageView) colorView.findViewById(R.id.colormap);

    // set the background to the current color
    colorView.setBackgroundColor(color);
    
    // setup the click listener
    colormap.setOnTouchListener(new OnTouchListener() {
      public boolean onTouch(View v, MotionEvent event) {
        BitmapDrawable bd = (BitmapDrawable) colormap.getDrawable();
        Bitmap bitmap = bd.getBitmap();

        // get the color value. 
        // scale the touch location
        int x = (int) ((event.getX()-15) * bitmap.getWidth() / (colormap.getWidth()-30));
        int y = (int) ((event.getY()-15) * bitmap.getHeight() / (colormap.getHeight()-30));

        if (x >= bitmap.getWidth())
          x = (int) bitmap.getWidth() - 1;
        if (x < 0)
          x = 0;

        if (y >= bitmap.getHeight())
          y = (int) bitmap.getHeight() - 1;
                if (y < 0)
                  y = 0;
        
                // set the color
        color = bitmap.getPixel(x, y);
        colorView.setBackgroundColor(color);
        
        return true;
      }
    });
    builder.setView(colorView);
    }
  
} // end class ColorPreference





Java Source Code List

org.paulmach.textedit.ClearRecentFileListPreference.java
org.paulmach.textedit.ClearSearchSuggestions.java
org.paulmach.textedit.ColorPreference.java
org.paulmach.textedit.DefaultDirPreference.java
org.paulmach.textedit.EditPreferences.java
org.paulmach.textedit.FileAutoCompleteArrayAdapter.java
org.paulmach.textedit.FileBrowser.java
org.paulmach.textedit.FileViewArrayAdapter.java
org.paulmach.textedit.FontSizePreference.java
org.paulmach.textedit.FontTypePreference.java
org.paulmach.textedit.SearchSuggestions.java
org.paulmach.textedit.WarrantyPreference.java
org.paulmach.textedit.pmTextEdit.java