Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Button Android


Google Android button example source code for developers.

// get handle
Button myButton;
myButton = (Button)findViewById(R.id.idMyButton);
//set focus
// set background image
// or
myButton.setBackgroundDrawable(getResources().getDrawable( R.drawable.someImage));

// set visibility
myButton.setVisibility(View.INVISIBLE); // VISIBLE

///////// SET LISTENER
       Button myButton =(Button)findViewById(R.id.button1);
        myButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "AHOJ",

// or set onClickListener
//end onCreate .....
   private OnClickListener myListener = new OnClickListener() {
        public void onClick(View v) {


397LW NO topic_id


Další témata ....(Topics)


Compressing a bitmap to JPG format Android example | compressing-a-bitmap-to-jpg-format-android-example

Codec, compress(), CompressFormat, quality, decodeByteArray(), Android example source code with image.

public class MainActivity extends Activity {
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(new SampleView(this));

	private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
			int quality) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		src.compress(format, quality, os);

		byte[] array = os.toByteArray();
		return BitmapFactory.decodeByteArray(array, 0, array.length);

	private static class SampleView extends View {

		public SampleView(Context context) {

		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
                        //  you need to insert some image flower_blue into res/drawable folder
			Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.flower_blue);
                        // Best of quality is 80 and more, 3 is very low quality of image 
			Bitmap bJPGcompress = codec(b, Bitmap.CompressFormat.JPEG, 3);
                   // get dimension of bitmap getHeight()  getWidth()
		   int h = b.getHeight();
           canvas.drawBitmap(b, 10,10, paint);
           canvas.drawBitmap(bJPGcompress, 10,10 + h + 10, paint);


jpg compression android

Unable to execute dex: Multiple dex files define Eclipse Android | unable-to-execute-dex-multiple-dex-files-define-eclipse-android

Try each step separately:

  1. Restart Eclipse

  2. Clean project and rebuild

  3. Create a new project and try it if the problem persists, if no move copy project to other folder, delete project from workspace , create new project same name and copy java, xml etc. files to new project

  4. Close Eclipse, backup folder c:\Users\myName\workspace\.metadata and delete it. Restart Eclipse try again import project to workspace

  5. Re-installing the Android Developer Tools

  6. Re-installing Eclipse - rename old folder with Android to Android_old, create new folder C:\Program Files\Android and copy new Eclipse with sdk into new folder

  7. Created a new project importing from the file system

  8. Created a new project from subversion


Activity Lifecycle if button Home pressed Android example | activity-lifecycle-if-button-home-pressed-android-example

If on Android emulator home button pressed.

// button home presssed
19:37:06.727: INFO/ActivityManager(60): Starting: 
Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] 
flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher } from pid 60

19:37:06.807: INFO/onSaveInstanceState(339): onSaveInstanceState()
19:37:06.848: INFO/onPause(339): onPause()
19:37:07.968: INFO/onStop(339): onStop()

Diagram of Activity lifecycle if button HOME pressed


Android Google Maps Night Mode | android-google-maps-night-mode

If you want save battery, select "Satelite map" from map menu and reduce the brightness of the display.


Get Context Java Android example | get-context-java-android-example

getContext() getApplicationContext() method Java Android example source

Context myContext_1 = ThisClassName.this; // to open a Dialog
Context myContext_2 = getContext();
Context myContext_3 = this.getContext();
Context myContext_4 = this;
Context myContext_5 = this.getApplicationContext ();

OnClickListener getImageBtnOnClick = new OnClickListener() {
public void onClick(View view) {
Context context = view.getContext();

// Toast
Toast.makeText(getApplicationContext(), "Context == getApplicationContext "
, Toast.LENGTH_SHORT).show();

// store Context in public class
public class MyActivity extends Activity {
public static Context myCnt = null;
protected void onCreate(Bundle icicle) {
myCnt = this;
// or
// MyStorage.setContext(this);

// cntxFromStorage == this
Context cntxFromStorage = MyStorage.getContext();

public class MyStorage
private static Context cntStorageContext = null;

public static Context getContext() {
return cntStorageContext;

public static void setContext(Context context) {
MyStorage.cntStorageContext = context;


class DataBaseHelper extends SQLiteOpenHelper {
// get MyActivity context
Context cnt = MyStorage.getContext();


Editace: 2011-09-28 07:45:52
Počet článků v kategorii: 397

11 Share