Download the large file from the server asynchronously in android with the below code....
call this:
new DownloadFileAsync(YourActivity.this).execute();
And your DownloadFileAsync is as below:
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
// URL url = new URL(DOWNLOAD URL);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
File filedir = new File(path);
// have the object build the directory structure, if needed.
filedir.mkdirs();
OutputStream output = new FileOutputStream(path + name + ".zip");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile),
name);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC", progress[0]);
mProgressDialog.setMessage("Downloading " + progress[1] + ".....");
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
new UnzipFile(BookDownloadActivity.this, path + name + ".zip", path)
.execute();
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file.....");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
This is how you can download the whole file from the server or any other URLs.
Search This Blog
Showing posts with label Dialog. Show all posts
Showing posts with label Dialog. Show all posts
Saturday, November 23, 2013
Sunday, March 31, 2013
DialogFragment
android.app.DialogFragment, was introduced from API Level 11 (Android 3.0), displays a dialog window, floating on top of its activity's window.
public class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
String title = "My Fragment";
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
f.setArguments(args);
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
Dialog myDialog = new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setPositiveButton("OK",
static MyDialogFragment newInstance() {
String title = "My Fragment";
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
f.setArguments(args);
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
Dialog myDialog = new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((AndroidDialogFragmentActivity)getActivity()).okClicked();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((AndroidDialogFragmentActivity)getActivity()).cancelClicked();
}
})
.create();
return myDialog;
}
}
<?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="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/opendialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Dialog" />
</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonOpenDialog = (Button)findViewById(R.id.opendialog);
buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
OpenDialog();
}});
}
void OpenDialog(){
MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.show(getFragmentManager(), "myDialogFragment");
}
public void okClicked() {
Toast.makeText(AndroidDialogFragmentActivity.this,
"OK Clicked!",
Toast.LENGTH_LONG).show();
}
public void cancelClicked() {
Toast.makeText(AndroidDialogFragmentActivity.this,
"Cancel Clicked!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
((AndroidDialogFragmentActivity)getActivity()).okClicked();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((AndroidDialogFragmentActivity)getActivity()).cancelClicked();
}
})
.create();
return myDialog;
}
}
in main.xml modify the following code.
<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="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/opendialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Dialog" />
</LinearLayout>
now in your activity just change the following code and see the dialog
public class DialogFragmentActivity extends Activity {
/** Called when the activity is first created. */@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonOpenDialog = (Button)findViewById(R.id.opendialog);
buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
OpenDialog();
}});
}
void OpenDialog(){
MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.show(getFragmentManager(), "myDialogFragment");
}
public void okClicked() {
Toast.makeText(AndroidDialogFragmentActivity.this,
"OK Clicked!",
Toast.LENGTH_LONG).show();
}
public void cancelClicked() {
Toast.makeText(AndroidDialogFragmentActivity.this,
"Cancel Clicked!", Toast.LENGTH_LONG).show();
}
}
Labels:
Alert Dialog,
Bundle,
Dialog,
DialogFragment,
Toast
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
Wednesday, February 8, 2012
Date Picker & Time Picker Dialog and set it into the String Format
First of all make an simple interface to open the Dialog and make that dialog open on the button click. For that you have to take two text view for storing the date and time and two button for opening the date and time dialog.
<?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"
>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="30dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview2"
android:text=""
android:textSize="30dp"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="time"
/>
</LinearLayout>
Now make a java file with the DatePickerDemo and put the following code into it.
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class DatePickerDemo extends Activity {
private static final int DATE_DIALOG_ID=1; // for date
private static final int TIME_DIALOG_ID=2; // for month
private int d,mo,y,h,m; // for date & month variables
Button b1,b2; // button objects
TextView e1,e2; // textview objects
// execution starts from here
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datepicker); // calling main.xml
e1=(TextView)findViewById(R.id.textview1); // getting textview1 id from main.xml
b1=(Button)findViewById(R.id.button1); // getting button id from main.xml
b1.setOnClickListener(new OnClickListener() // setting listener for button one
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID); // generating dialog box
}
});
final Calendar cal=Calendar.getInstance(); // allocating memory for calendar instance
d=cal.get(Calendar.DAY_OF_MONTH); // present date
mo=cal.get(Calendar.MONTH); // present month
y=cal.get(Calendar.YEAR); // present year
updateDate(); // update date
b2=(Button)findViewById(R.id.button2); // getting listener for button2
e2=(TextView)findViewById(R.id.textview2);
b2.setOnClickListener(new OnClickListener() // setting listener for button2
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(TIME_DIALOG_ID);
}
});
h=cal.get(Calendar.HOUR); // getting present hour & minute
m=cal.get(Calendar.MINUTE);
updateTime(); // updating time
}
public void updateTime()
{
e2.setText(new StringBuilder().append(h).append('-').append(m));
}
public void updateDate()
{
e1.setText(new StringBuilder().append(d).append('-').append(mo+1).append('-').append(y));
}
private DatePickerDialog.OnDateSetListener datelistener=new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view,int year, int monthofyear, int day)
{
y=year;
mo=monthofyear;
d=day;
updateDate();
}
};
private TimePickerDialog.OnTimeSetListener timelistener=new TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourofday, int minute)
{
// TODO Auto-generated method stub
h=hourofday;
m=minute;
updateTime();
}
};
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this,datelistener , y, mo, d);
case TIME_DIALOG_ID:
return new TimePickerDialog(this,timelistener,h,m,false);
}
return null;
}
}
This will show you how to pick the date from date picker android widget and time from TimePicker widget and convert Those date and Time into String format and set it to the TextView.
Labels:
Buttons,
Calender,
DatePickerDialog,
Dialog,
StringBuilder,
TimePickerDialog
Saturday, January 21, 2012
How to Show Alert Dialog in Android
Creating alert dialog is very easy. In this tutorial i will be discussing about creating different alert dialogues with one button(ok button), two buttons(yes or no buttons) and three buttons(yes, no and cancel buttons).
Android alert dialog with One button
The following code will create a simple alert dialog with one button. In the following code setTitle() method is used for set Title to alert dialog. setMessage() is used for setting message to alert dialog. setIcon() is to set icon to alert dialog.
AlertDialog alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to typicaljava.blogspot.com");
// Setting Dialog Message
alertDialog.setMessage("Welcome to typicaljava.blogspot.com");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK",
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK",
Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
The following code will create alert dialog with two button. setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Confirm Delete...");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want delete this?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.delete);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
Toast.makeText(getApplicationContext(), "You clicked on YES",
Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
Here setNeutralButton() is used to create a neutral cancel button
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Save File...");
// Setting Dialog Message
alertDialog.setMessage("Do you want to save this file?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.save);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on YES",
Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed No button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
}
});
// Setting Netural "Cancel" Button
alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Cancel",
Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
After making the proper screenshot of this code i will be uploading on it....
}
});
// Showing Alert Message
alertDialog.show();
Android alert dialog with two button
The following code will create alert dialog with two button. setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Confirm Delete...");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want delete this?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.delete);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
Toast.makeText(getApplicationContext(), "You clicked on YES",
Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
Android alert dialog with three button
Here setNeutralButton() is used to create a neutral cancel button
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Save File...");
// Setting Dialog Message
alertDialog.setMessage("Do you want to save this file?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.save);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on YES",
Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed No button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
}
});
// Setting Netural "Cancel" Button
alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Cancel",
Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
After making the proper screenshot of this code i will be uploading on it....
Labels:
Alert Dialog,
Buttons,
Dialog,
Toast
Subscribe to:
Posts (Atom)