AlertDialog - MessageBox - Alert - Toast - Android sample
AlertDialog like MessageBox by WinApi:
If error: Unable to add window -- token null is not for an application
try change get Context.
If error: Unable to add window -- token null is not for an application
try change get Context.
// you can put this text into some function body or case in switch statement
new AlertDialog.Builder(this)
.setMessage("Hello boys!!!")
.setPositiveButton("OK", null)
.show();
Context context = getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Select font size");
final CharSequence[] chsSize= { "Small", "Medium", "Large"};
builder.setSingleChoiceItems(chsSize, 0 /*sel.item*/,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(context, "Hello from dialog!!!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
396LW NO topic_id
AD
Další témata ....(Topics)
Cut, shear, clip, snip, crop a bitmap, picture, image Android example
public class ApokusActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawColor(Color.YELLOW);
// you need to insert a image flower_blue into res/drawable folder
paint.setFilterBitmap(true);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.flower_blue);
Bitmap croppedBmp = Bitmap.createBitmap(bitmapOrg, 0, 0,
bitmapOrg.getWidth() / 2, bitmapOrg.getHeight());
int h = bitmapOrg.getHeight();
canvas.drawBitmap(bitmapOrg, 10, 10, paint);
canvas.drawBitmap(croppedBmp, 10, 10 + h + 10, paint);
}
}
}
Generate random number Android Java example source code.
Random rand = new Random();
int i = rand.nextInt() % 256; // range -255 +255
System.out.print(i + "
"); // -184
i = Math.abs(rand.nextInt() % 12); // range 0 +11
System.out.print(i); // 7
// Math.random() start with 0. e.g. 0.35981234
int nRan = (int) (Math.random()*10); // 0 - 10
// nextDouble(), nextFloat(), nextInt(), nextLong() returns 0 - 10
import java.util.Random;´
Random r = new Random();
int nRan = r.nextInt(); // 0 - 10
double dRan = r.nextDouble() * 10; // e.g. 7.496285271597397
nextDouble – return 0 - 1
nextFloat – same as double
nextInt – -2147483648 +2147483647
nextLong – -922337203685775808 +9223372036854775807
nextGaussian – 0.0 aberation 1.0.
Try this code:
final String ERROR = "my error message....";
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
// some code and method ..... see AsyncTask
@Override
protected String doInBackground(String... urls) {
URL urlL = null;
try {
urlL = new URL(url);//"//chmi.cz..../"
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) urlL.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
response = readStream(in);
return response;
} catch (IOException e) {
//throw new RuntimeException(e);
} finally {
if(urlConnection != null)
urlConnection.disconnect();
return ERROR;
}
} catch (MalformedURLException e) {
e.printStackTrace();
return ERROR;
}
}
return response;
}
private String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
Nejmenší telefon od Samsungu s Androidem se někdy přidává i jako dárek k zakoupenému zboží, přesto tento telefon toho za svou cenu něco přes 2 000 korun umí hodně.
Technické parametry Samsung Galaxy Pocket
Rozměry a hmotnost: 103,70 × 57,50 × 12 mm, 97 g
Sítě GSM 850 / 900 / 1800 / 1900 MHz, W-CDMA (3G) 900 MHz / 2 100 MHz
Displej TFT TN, 2,80" (320 × 240 px), dotykový: kapacitní
Procesor 832 MHz
Paměť RAM 256 MB, vnitřní paměť 3 GB, paměťové karty microSD
Operační systém Android 2.3, údajně nelze upgradovat
Hudba: mp3, aac, wav, wma
Video: mp4, 3gp, 320 × 240 px, 15 FPS
Fotoaparát 2 Mpx
Navigace vestavěná GPS, elektronický kompas
Baterie 1 200 mAh, pohotovostní doba: 312 hodin
Cena něco přes 2 000 korun / léto 2012
Technické parametry Samsung Galaxy Pocket
Rozměry a hmotnost: 103,70 × 57,50 × 12 mm, 97 g
Sítě GSM 850 / 900 / 1800 / 1900 MHz, W-CDMA (3G) 900 MHz / 2 100 MHz
Displej TFT TN, 2,80" (320 × 240 px), dotykový: kapacitní
Procesor 832 MHz
Paměť RAM 256 MB, vnitřní paměť 3 GB, paměťové karty microSD
Operační systém Android 2.3, údajně nelze upgradovat
Hudba: mp3, aac, wav, wma
Video: mp4, 3gp, 320 × 240 px, 15 FPS
Fotoaparát 2 Mpx
Navigace vestavěná GPS, elektronický kompas
Baterie 1 200 mAh, pohotovostní doba: 312 hodin
Cena něco přes 2 000 korun / léto 2012
private SensorManager mSensorManager;
private PowerManager mPowerManager;
private WindowManager mWindowManager;
private Display mDisplay;
// onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get an instance of the SensorManager
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// Get an instance of the PowerManager
mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
// Get an instance of the WindowManager
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mDisplay = mWindowManager.getDefaultDisplay();
setContentView(R.layout.main); // main.xml or your xml file name
}
Editace: 2011-10-27 20:09:56
Počet článků v kategorii: 396
Url:alertdialog-like-messagebox-android-sample