First of all add all the images in drawable no metter how big it is.
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView image1;
Gallery gallery;
private Runnable r;
private static int i = 0;
int[] image_array = { R.drawable.i_pad0000, R.drawable.i_pad0001,
R.drawable.i_pad0002, R.drawable.i_pad0003, R.drawable.i_pad0004,
R.drawable.i_pad0005, R.drawable.i_pad0006, R.drawable.i_pad0007,
R.drawable.i_pad0008, R.drawable.i_pad0009, R.drawable.i_pad0010,
R.drawable.i_pad0011, R.drawable.i_pad0012, R.drawable.i_pad0013,
R.drawable.i_pad0014, R.drawable.i_pad0015, R.drawable.i_pad0016,
R.drawable.i_pad0017, R.drawable.i_pad0018, R.drawable.i_pad0019,
R.drawable.i_pad0020, R.drawable.i_pad0021, R.drawable.i_pad0022,
R.drawable.i_pad0023, R.drawable.i_pad0024, R.drawable.i_pad0025,
R.drawable.i_pad0027, R.drawable.i_pad0028, R.drawable.i_pad0029,
R.drawable.i_pad0030, R.drawable.i_pad0031, R.drawable.i_pad0032,
R.drawable.i_pad0033, R.drawable.i_pad0034, R.drawable.i_pad0035,
R.drawable.i_pad0036, R.drawable.i_pad0037, R.drawable.i_pad0038,
R.drawable.i_pad0039, R.drawable.i_pad0040, R.drawable.i_pad0041,
R.drawable.i_pad0042, R.drawable.i_pad0043, R.drawable.i_pad0044,
R.drawable.i_pad0045, R.drawable.i_pad0046, R.drawable.i_pad0047,
R.drawable.i_pad0048, R.drawable.i_pad0049, R.drawable.i_pad0050,
R.drawable.i_pad0051, R.drawable.i_pad0052, R.drawable.i_pad0053,
R.drawable.i_pad0054, R.drawable.i_pad0055, R.drawable.i_pad0056,
R.drawable.i_pad0057, R.drawable.ipad0058, R.drawable.i_pad0059,
R.drawable.i_pad0060, R.drawable.i_pad0061, R.drawable.i_pad0062,
R.drawable.i_pad0063, R.drawable.i_pad0064, R.drawable.i_pad0065,
R.drawable.i_pad0066, R.drawable.i_pad0067, R.drawable.i_pad0068,
R.drawable.i_pad0069, R.drawable.i_pad0070, R.drawable.i_pad0071,
R.drawable.i_pad0072, R.drawable.i pad0073, R.drawable.i_pad0074};
int[] verse_titles_hindi = { R.drawable.img_1,
R.drawable.img_2, R.drawable.img_3,
R.drawable.img_4, R.drawable.img_5,
R.drawable.img_6, R.drawable.v_7,
R.drawable.img_8, R.drawable.img_9,
R.drawable.img_10, R.drawable.img_11,
R.drawable.img_12, R.drawable.img_13,
R.drawable.img_14, R.drawable.img_15,
R.drawable.img_16, R.drawable.img_17,
R.drawable.img_18, R.drawable.img_19,
R.drawable.img_20, R.drawable.img_21,
R.drawable.img_22, R.drawable.img_23,
R.drawable.img_24, R.drawable.img_25,
R.drawable.img_26, R.drawable.img_27,
R.drawable.img_28, R.drawable.img_29,
R.drawable.img_30, R.drawable.img_31,
R.drawable.img_32, R.drawable.img_33,
R.drawable.img_34, R.drawable.img_35,
R.drawable.img_36, R.drawable.img_37,
R.drawable.img_38, R.drawable.img_39,
R.drawable.img_40, R.drawable.img_41,
R.drawable.img_42, R.drawable.img_43 };
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 = (ImageView) findViewById(R.id.image);
gallery = (Gallery) findViewById(R.id.versesIndex);
gallery.setAdapter(new ImageAdapter(this, verse_titles_hindi));
r = new Runnable() {
public void run() {
image1.setImageResource(image_array[i]);
System.gc();
i++;
image1.postDelayed(r, 25);
if (i >= image_array.length) {
Log.i("log_tag", "" + i);
i = 0;
// image1.removeCallbacks(r);
}
}
};
image1.postDelayed(r, 25);
}
private class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private int[] verse_title;
public ImageAdapter(Context c, int[] verse_title) {
// TODO Auto-generated constructor stub
this.verse_title = verse_title;
this.mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Theme);
mGalleryItemBackground = a.getResourceId(
R.styleable.Theme_android_galleryItemBackground, 0);
a.recycle();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return verse_title.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
i.setImageResource(verse_title[position]);
i.setLayoutParams(new Gallery.LayoutParams(353, 159));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
}
This would be run with the gallery as well as multiple image in one image view.
Search This Blog
Showing posts with label ImageView. Show all posts
Showing posts with label ImageView. Show all posts
Monday, February 4, 2013
Wednesday, January 2, 2013
Get all the application installed on your system
First of all add the xml for the list of the application installed on your devices.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android1:id="@+id/listView1"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:layout_alignParentLeft="true"
android1:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Here is the code for the row of the list.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="22dp"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_marginLeft="36dp"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
</RelativeLayout>
Now after that add the following code in your main activity.
public class MainActivity extends Activity {
ArrayList<PInfo> arrylist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PInfo p = new PInfo();
arrylist = getPackages();
for (int i = 0; i < arrylist.size(); i++) {
Log.i("log_tag", "=======" + arrylist.get(i).icon);
}
ListView l = (ListView) findViewById(R.id.listView1);
l.setAdapter(new ListAdatp(this, arrylist));
}
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private String move2sd;
private Drawable icon;
private void prettyPrint() {
Log.v(appname + "\t" + pname + "\t" + versionName + "\t"
+ versionCode, "");
}
}
private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); /*
* false = no system
* packages
*/
final int max = apps.size();
for (int i = 0; i < max; i++) {
apps.get(i).prettyPrint();
}
return apps;
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager())
.toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
public class ListAdatp extends BaseAdapter {
Activity ativity;
ArrayList<PInfo> adptarrlist;
public ListAdatp(MainActivity mainActivity, ArrayList<PInfo> arrylist) {
// TODO Auto-generated constructor stub
ativity = mainActivity;
adptarrlist = arrylist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return adptarrlist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflt = (LayoutInflater) ativity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflt.inflate(R.layout.listrow, null);
ImageView img = (ImageView) convertView
.findViewById(R.id.imageView1);
TextView txt = (TextView) convertView.findViewById(R.id.textView1);
img.setBackgroundDrawable(adptarrlist.get(position).icon);
txt.setText("(" + adptarrlist.get(position).appname + ") "
+ adptarrlist.get(position).pname);
txt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage(
adptarrlist.get(position).pname);
startActivity(LaunchIntent);
}
});
return convertView;
}
}
}
Labels:
Activity,
ArrayList,
BaseAdapter,
ImageView,
Intent,
LayoutInflater,
List view,
PackageInfo
Tuesday, February 14, 2012
Color Picker Dialog With Seekbar
This color picker dialog has sliders (SeekBars) and EditTexts for red, green, blue and alpha values. It has a live preview of the color. It returns int values for R, G, B, A for easy use with Color.argb().
Make an .xml file main.xml and put the code below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/color"
android:layout_width="fill_parent"
android:layout_height="60dip" />
<Button
android:id="@+id/select"
android:text="Select Color"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Now make an another .xml file named color_layout.xml and put the code below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/main"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:minWidth="300dp">
<TextView
android:id="@+id/red_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="red" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|left">
<SeekBar
android:id="@+id/red_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:layout_weight="1"
android:paddingLeft="7dp"
android:paddingRight="7dp" />
<EditText
android:id="@+id/red_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0" />
</LinearLayout>
<TextView
android:id="@+id/green_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="green" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|left">
<SeekBar
android:id="@+id/green_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:layout_weight="1"
android:paddingLeft="7dp"
android:paddingRight="7dp" />
<EditText
android:id="@+id/green_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0" />
</LinearLayout>
<TextView
android:id="@+id/blue_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="blue" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|left">
<SeekBar
android:id="@+id/blue_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:layout_weight="1"
android:paddingLeft="7dp"
android:paddingRight="7dp" />
<EditText
android:id="@+id/blue_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0" />
</LinearLayout>
<TextView
android:id="@+id/alpha_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="alpha" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|left">
<SeekBar
android:id="@+id/alpha_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255"
android:layout_weight="1"
android:paddingLeft="7dp"
android:paddingRight="7dp" />
<EditText
android:id="@+id/alpha_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="255"
/>
</LinearLayout>
<ImageView
android:id="@+id/color_preview"
android:layout_width="fill_parent"
android:layout_height="40dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_marginTop="20dp">
<Button
android:id="@+id/ok"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="@+id/cancel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
First of all make a java file named ColorPickerDialog and insert the following code into it.
public class ColorPickerDialog extends Dialog implements SeekBar.OnSeekBarChangeListener, TextWatcher, OnClickListener {
SeekBar redBar;
EditText redText;
SeekBar greenBar;
EditText greenText;
SeekBar blueBar;
EditText blueText;
SeekBar alphaBar;
EditText alphaText;
ImageView colorPreview;
Button ok;
Button cancel;
String color;
public interface OnColorChangedListener {
void colorChanged(int a, int r, int g, int b);
}
private OnColorChangedListener mListener;
public ColorPickerDialog(Context context, OnColorChangedListener listener, String color) {
super(context);
mListener = listener;
this.color = color;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_layout);
setTitle("Color Picker");
redBar = (SeekBar) findViewById(R.id.red_bar);
redText = (EditText) findViewById(R.id.red_text);
greenBar = (SeekBar) findViewById(R.id.green_bar);
greenText = (EditText) findViewById(R.id.green_text);
blueBar = (SeekBar) findViewById(R.id.blue_bar);
blueText = (EditText) findViewById(R.id.blue_text);
alphaBar = (SeekBar) findViewById(R.id.alpha_bar);
alphaText = (EditText) findViewById(R.id.alpha_text);
ok = (Button) findViewById(R.id.ok);
cancel = (Button) findViewById(R.id.cancel);
colorPreview = (ImageView) findViewById(R.id.color_preview);
//set initial colors
String[] colorVal = color.split(",");
int a = Integer.parseInt(colorVal[0]);
int r = Integer.parseInt(colorVal[1]);
int g = Integer.parseInt(colorVal[2]);
int b = Integer.parseInt(colorVal[3]);
colorPreview.setBackgroundColor(Color.argb(a, r, g, b));
redBar.setProgress(r);
greenBar.setProgress(g);
blueBar.setProgress(b);
alphaBar.setProgress(a);
redText.setText(colorVal[1]);
greenText.setText(colorVal[2]);
blueText.setText(colorVal[3]);
alphaText.setText(colorVal[0]);
redBar.setOnSeekBarChangeListener(this);
greenBar.setOnSeekBarChangeListener(this);
blueBar.setOnSeekBarChangeListener(this);
alphaBar.setOnSeekBarChangeListener(this);
redText.addTextChangedListener(this);
greenText.addTextChangedListener(this);
blueText.addTextChangedListener(this);
alphaText.addTextChangedListener(this);
ok.setOnClickListener(this);
cancel.setOnClickListener(this);
setCancelable(false);
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
switch (seekBar.getId()) {
case R.id.red_bar:
redText.setText(Integer.toString(progress));
break;
case R.id.green_bar:
greenText.setText(Integer.toString(progress));
break;
case R.id.blue_bar:
blueText.setText(Integer.toString(progress));
break;
case R.id.alpha_bar:
alphaText.setText(Integer.toString(progress));
break;
}
}
colorPreview.setBackgroundColor(Color.argb(alphaBar.getProgress(), redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress()));
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void afterTextChanged(Editable arg0) {
if(Integer.parseInt(redText.getText().toString()) > 255)
redText.setText("255");
if(!redText.getText().toString().equals("")){
if(Integer.parseInt(redText.getText().toString()) > 255)
redText.setText("255");
redBar.setProgress(Integer.parseInt(redText.getText().toString()));
} else
redBar.setProgress(0);
if(!greenText.getText().toString().equals("")){
if(Integer.parseInt(greenText.getText().toString()) > 255)
greenText.setText("255");
greenBar.setProgress(Integer.parseInt(greenText.getText().toString()));
} else
greenBar.setProgress(0);
if(!blueText.getText().toString().equals("")){
if(Integer.parseInt(blueText.getText().toString()) > 255)
blueText.setText("255");
blueBar.setProgress(Integer.parseInt(blueText.getText().toString()));
}else
blueBar.setProgress(0);
if(!alphaText.getText().toString().equals("")){
if(Integer.parseInt(alphaText.getText().toString()) > 255)
alphaText.setText("255");
alphaBar.setProgress(Integer.parseInt(alphaText.getText().toString()));
}else
alphaBar.setProgress(0);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.ok:
mListener.colorChanged(alphaBar.getProgress(), redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress());
dismiss();
break;
case R.id.cancel:
dismiss();
break;
}
}
}
Now make a main java File named MyActivity with implementing the ColorPickerDialog.OnColorChangedListener. And put the code in it.
public class MyActivity extends Activity implements ColorPickerDialog.OnColorChangedListener {
String color = "255,255,000,000"; //alpha, red, green, blue
ImageView image;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.color);
String[] colorVal = color.split(",");
image.setBackgroundColor(Color.argb(Integer.parseInt(colorVal[0]),Integer.parseInt(colorVal[1]),Integer.parseInt(colorVal[2]),Integer.parseInt(colorVal[3])));
Button btn = (Button) findViewById(R.id.select);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new ColorPickerDialog(MyActivity.this, MyActivity.this, color).show();
}
});
}
public void colorChanged(int a, int r, int g, int b) {
color = a + "," + r + "," + g + "," + b;
image.setBackgroundColor(Color.argb(a,r,g,b));
}
}
Make an .xml file main.xml and put the code below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/color"
android:layout_width="fill_parent"
android:layout_height="60dip" />
<Button
android:id="@+id/select"
android:text="Select Color"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Now make an another .xml file named color_layout.xml and put the code below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/main"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:minWidth="300dp">
<TextView
android:id="@+id/red_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="red" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|left">
<SeekBar
android:id="@+id/red_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:layout_weight="1"
android:paddingLeft="7dp"
android:paddingRight="7dp" />
<EditText
android:id="@+id/red_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0" />
</LinearLayout>
<TextView
android:id="@+id/green_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="green" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|left">
<SeekBar
android:id="@+id/green_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:layout_weight="1"
android:paddingLeft="7dp"
android:paddingRight="7dp" />
<EditText
android:id="@+id/green_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0" />
</LinearLayout>
<TextView
android:id="@+id/blue_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="blue" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|left">
<SeekBar
android:id="@+id/blue_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:layout_weight="1"
android:paddingLeft="7dp"
android:paddingRight="7dp" />
<EditText
android:id="@+id/blue_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0" />
</LinearLayout>
<TextView
android:id="@+id/alpha_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="alpha" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|left">
<SeekBar
android:id="@+id/alpha_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255"
android:layout_weight="1"
android:paddingLeft="7dp"
android:paddingRight="7dp" />
<EditText
android:id="@+id/alpha_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="255"
/>
</LinearLayout>
<ImageView
android:id="@+id/color_preview"
android:layout_width="fill_parent"
android:layout_height="40dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_marginTop="20dp">
<Button
android:id="@+id/ok"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="@+id/cancel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
First of all make a java file named ColorPickerDialog and insert the following code into it.
public class ColorPickerDialog extends Dialog implements SeekBar.OnSeekBarChangeListener, TextWatcher, OnClickListener {
SeekBar redBar;
EditText redText;
SeekBar greenBar;
EditText greenText;
SeekBar blueBar;
EditText blueText;
SeekBar alphaBar;
EditText alphaText;
ImageView colorPreview;
Button ok;
Button cancel;
String color;
public interface OnColorChangedListener {
void colorChanged(int a, int r, int g, int b);
}
private OnColorChangedListener mListener;
public ColorPickerDialog(Context context, OnColorChangedListener listener, String color) {
super(context);
mListener = listener;
this.color = color;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_layout);
setTitle("Color Picker");
redBar = (SeekBar) findViewById(R.id.red_bar);
redText = (EditText) findViewById(R.id.red_text);
greenBar = (SeekBar) findViewById(R.id.green_bar);
greenText = (EditText) findViewById(R.id.green_text);
blueBar = (SeekBar) findViewById(R.id.blue_bar);
blueText = (EditText) findViewById(R.id.blue_text);
alphaBar = (SeekBar) findViewById(R.id.alpha_bar);
alphaText = (EditText) findViewById(R.id.alpha_text);
ok = (Button) findViewById(R.id.ok);
cancel = (Button) findViewById(R.id.cancel);
colorPreview = (ImageView) findViewById(R.id.color_preview);
//set initial colors
String[] colorVal = color.split(",");
int a = Integer.parseInt(colorVal[0]);
int r = Integer.parseInt(colorVal[1]);
int g = Integer.parseInt(colorVal[2]);
int b = Integer.parseInt(colorVal[3]);
colorPreview.setBackgroundColor(Color.argb(a, r, g, b));
redBar.setProgress(r);
greenBar.setProgress(g);
blueBar.setProgress(b);
alphaBar.setProgress(a);
redText.setText(colorVal[1]);
greenText.setText(colorVal[2]);
blueText.setText(colorVal[3]);
alphaText.setText(colorVal[0]);
redBar.setOnSeekBarChangeListener(this);
greenBar.setOnSeekBarChangeListener(this);
blueBar.setOnSeekBarChangeListener(this);
alphaBar.setOnSeekBarChangeListener(this);
redText.addTextChangedListener(this);
greenText.addTextChangedListener(this);
blueText.addTextChangedListener(this);
alphaText.addTextChangedListener(this);
ok.setOnClickListener(this);
cancel.setOnClickListener(this);
setCancelable(false);
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
switch (seekBar.getId()) {
case R.id.red_bar:
redText.setText(Integer.toString(progress));
break;
case R.id.green_bar:
greenText.setText(Integer.toString(progress));
break;
case R.id.blue_bar:
blueText.setText(Integer.toString(progress));
break;
case R.id.alpha_bar:
alphaText.setText(Integer.toString(progress));
break;
}
}
colorPreview.setBackgroundColor(Color.argb(alphaBar.getProgress(), redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress()));
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void afterTextChanged(Editable arg0) {
if(Integer.parseInt(redText.getText().toString()) > 255)
redText.setText("255");
if(!redText.getText().toString().equals("")){
if(Integer.parseInt(redText.getText().toString()) > 255)
redText.setText("255");
redBar.setProgress(Integer.parseInt(redText.getText().toString()));
} else
redBar.setProgress(0);
if(!greenText.getText().toString().equals("")){
if(Integer.parseInt(greenText.getText().toString()) > 255)
greenText.setText("255");
greenBar.setProgress(Integer.parseInt(greenText.getText().toString()));
} else
greenBar.setProgress(0);
if(!blueText.getText().toString().equals("")){
if(Integer.parseInt(blueText.getText().toString()) > 255)
blueText.setText("255");
blueBar.setProgress(Integer.parseInt(blueText.getText().toString()));
}else
blueBar.setProgress(0);
if(!alphaText.getText().toString().equals("")){
if(Integer.parseInt(alphaText.getText().toString()) > 255)
alphaText.setText("255");
alphaBar.setProgress(Integer.parseInt(alphaText.getText().toString()));
}else
alphaBar.setProgress(0);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.ok:
mListener.colorChanged(alphaBar.getProgress(), redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress());
dismiss();
break;
case R.id.cancel:
dismiss();
break;
}
}
}
Now make a main java File named MyActivity with implementing the ColorPickerDialog.OnColorChangedListener. And put the code in it.
public class MyActivity extends Activity implements ColorPickerDialog.OnColorChangedListener {
String color = "255,255,000,000"; //alpha, red, green, blue
ImageView image;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.color);
String[] colorVal = color.split(",");
image.setBackgroundColor(Color.argb(Integer.parseInt(colorVal[0]),Integer.parseInt(colorVal[1]),Integer.parseInt(colorVal[2]),Integer.parseInt(colorVal[3])));
Button btn = (Button) findViewById(R.id.select);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new ColorPickerDialog(MyActivity.this, MyActivity.this, color).show();
}
});
}
public void colorChanged(int a, int r, int g, int b) {
color = a + "," + r + "," + g + "," + b;
image.setBackgroundColor(Color.argb(a,r,g,b));
}
}
Labels:
Buttons,
Color.argb,
ColorPickerDialog,
Dialog,
EditText,
ImageView,
Seekbar,
TextView,
TextWatcher
Subscribe to:
Posts (Atom)

