Friday, 22 November 2013

Android programming Tips & Tricks


How to Scroll up Screen page on EditText focus ?


ScrollView sv = (ScrollView) findViewById(R.id.LoginScrollView);
              // sv.scrollTo(0, sv.getBottom());
              unm.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {

                           sv.post(new Runnable() {
                                  public void run() {
                                         sv.fullScroll(ScrollView.FOCUS_DOWN);
                                  }
                           });
                     }

              });


How to Hide Soft keybord on page load of form or Sign In page?

 Set EditText property as 

    android:focusable="true"
    android:focusableTouchMode="true"

or set manifest property for that particular activity as 

  android:windowSoftInputMode="stateHidden"



Encode Bitmap to base64 ?

here is simple method you can use directly
      
       public static String encodeTobase64(Bitmap bitmap) {
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
              byte[] byteArray = baos.toByteArray();
              String imageEncoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

              Log.e("Base 64 String", imageEncoded);
              return imageEncoded;
       }


Decode Base 64 String to Bitmap?

       public static Bitmap decodeBase64(String input) {
              byte[] decodedByte = Base64.decode(input, 0);
              return BitmapFactory
                           .decodeByteArray(decodedByte, 0, decodedByte.length);
       }

Scale / Resize Bitmap to particular size?

      Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 640, 480, false);


Some useful commands:

adb devices – lists which devices are currently attached to your computer
adb install – lets you install an Android application on your phone
adb remount – Remounts your system in write mode – this lets you alter system files on your phone using ADB
adb push – lets you upload files to your phones filesystem
adb pull – lets you download files off your phones filesystem
adb logcat – starts dumping debugging information from your handset to the console – useful for debugging your apps
adb shell – drops you into a basic linux command shell on your phone with no parameters, or lets you run commands directly

Some advanced commands 
device commands:
adb push <local> <remote> - copy file/dir to device
adb pull <remote> <local> - copy file/dir from device
adb sync [ <directory> ] - copy host->device only if changed
(see 'adb help all')
adb shell - run remote shell interactively
adb shell <command> - run remote shell command
adb emu <command> - run emulator console command
adb logcat [ <filter-spec> ] - View device log
adb forward <local> <remote> - forward socket connections
forward specs are one of:
tcp:<port>
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
dev:<character device name>
jdwp:<process pid> (remote only)
adb jdwp - list PIDs of processes hosting a JDWP transport
adb install [-l] [-r] <file> - push this package file to the device and install it
('-l' means forward-lock the app)
('-r' means reinstall the app, keeping its data)
adb uninstall [-k] <package> - remove this app package from the device
('-k' means keep the data and cache directories)
adb bugreport - return all information from the device
that should be included in a bug report.


adb help - show this help message
adb version - show version num


DATAOPTS:
(no option) - don't touch the data partition
-w - wipe the data partition
-d - flash the data partition


scripting:
adb wait-for-device - block until device is online
adb start-server - ensure that there is a server running
adb kill-server - kill the server if it is running
adb get-state - prints: offline | bootloader | device
adb get-serialno - prints: <serial-number>
adb status-window - continuously print device status for a specified device
adb remount - remounts the /system partition on the device read-write
adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program
adb root - restarts the adbd daemon with root permissions
adb usb - restarts the adbd daemon listening on USB
adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port


networking:
adb ppp <tty> [parameters] - Run PPP over USB.
Note: you should not automatically start a PPP connection.
<tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
[parameters] - Eg. defaultroute debug dump local notty usepeerdns


adb sync notes: adb sync [ <directory> ]
<localdir> can be interpreted in several ways:
- If <directory> is not specified, both /system and /data partitions will be updated.
- If it is "system" or "data", only the corresponding partition
is updated.

Booting commands also hold their place and come in very handy while rooting processes and even while Android development. Below are stated such 3 commands:
adb reboot
The command simply reboots your device at any instance

adb reboot recovery
The command reboots a device into recovery mode

adb reboot bootloader
The command reboots a device into its bootloader (fastboot) mode

In case you’re in bootloader mode, your device shall never show any response to ADB commands. But, here comes fastboot in action, which has a limited, but a few and very useful commands. This is the same reason that bootloader mode is often referred to as fastboot mode.
fastboot devices
Displays the device ID of the Android device connected while in bootloader mode.

fastboot reboot
Reboots a device

fastboot reboot-bootloader
Reboots the bootloader

fastboot oem unlock
Initiates the bootloader unlocking sequence

fastboot oem lock
Re-locks the device bootloader

fastboot flash recovery <recovery_image_name.img>
Flashes a recovery image to the device
eg: fastboot flash recovery clockworkmod-6.0.3.1_mako.img

fastboot flash radio <radio_file_name.img>
Flashes the Radio (baseband) image file
eg. fastboot flash radio radio-crespo-i9020xxki1.img

fastboot flash bootloader <bootloader_file_name.img>
Flashes a bootloader image to the device partition
eg. fastboot flash bootloader bootloader-crespo-i9020xxlc2.img

Android - Run/install/debug applications over WiFi?

Connect device via USB and make sure debugging is working.
adb tcpip 5555
adb connect 10.0.0.2:5555 
(replace with device's IP address)
Disconnect USB and proceed with wireless debugging.
adb usb to switch back when done.
No root required!


or Try this 

- disconnect device from usb then tell it to listen on 4455
adb tcpip 4455
restarting in TCP mode port: 4455

- connect to the device using a specified ip:port. my device is using wifi
adb connect 192.168.1.103:4455
connected to 192.168.1.103:4455

- now do normal adb commands over tcp
adb shell

- when your done, you can put it back in USB mode
adb usb
restarting in USB mode



or try this 

If you want to enable wireless adb on the device without connecting with a usb cable at all:

Install a terminal app on the device (like Android Terminal Emulator) Type the following
su
setprop service.adb.tcp.port 5555
stop adbd
start adbd
Since you already have the terminal opened, you can find the find the ip of the device: ip addr show

Then in your debugging environment run command:

adb connect ip.of.the.device
This will make the device to be listed as any other emulators you might have. Note that the tcp will stay opened until you reset the device. You might also want to install WiFi Keep Alive app if you experience often disconnects.

Remember to close the tcp when connecting to public networks if you want to feel safe.You can do the following or simply restart the device:

su
setprop service.adb.tcp.port -1
stop adbd
start adbd

or try this link ( Don't worry All are same Methods :p  )

http://www.jessechen.net/blog/debugging-your-android-app-wirelessly-on-an-android-smartphone/