Monday, 28 October 2013

Android ADB over wifi - Portable wifi device

Bored of connecting your Android device to ADB with traditional way wired/USB cable?
Here is the solution, connect your device to ADB over wifi & deploy your application for testing, how cool is that?
In order to make this successful you should 

There are two ways to connect,
1. Non-Rooted  Devices
2. Rooted Devices


For Non-Rooted Device

1. Connect your device to Computer


2. Once it is connected to ADB, 

Go to command prompt locate your Android sdk/platform-tools as shown below


3. Type the below command

adb tcpip 5555
adb connect 192.168.0.101:5555
Make sure you will replace your IP (Device IP) 

4. Remove USB cable 

Now your device is connected via wifi ADB. 

5. To revert back to USB

Just type 
adb usb



For Rooted-Device

1. Download apps

ADB wifi https://play.google.com/store/apps/details?id=com.ryosoftware.adbw&hl=enor Wifi ADB https://play.google.com/store/apps/details?id=com.ttxapps.wifiadb&hl=enon your device. 

2. Go to command prompt

locate your Android sdk/platform-tools as shown below



3. open your any of the ADB wifi app 

now type in command adb connect 192.168.2.77 (as shown in your device)





4. Now you will be able to see 

connected to 192.168.2.77:5555



5. Now if you check you in DDMS you will be able to see your device connected in Device tab.


Enjoy ADB over wifi :) 
like it ? Please comment

Sliding Activities like Gallery ImageView

Have you ever tried of combining all 4 or more activities(layouts)  into single activity?
If not here is a sample which uses Image Gallery to load activities. 
I have used total 5 layouts (1 main & other 4 for 4 different activity) 
  • sliding_activity.xml
  • layout_1.xml
  • layout_2.xml
  • layout_3.xml
  • layout_4.xml
Also 5 classes (1 Main & 4 others)
  • SlidingActivity.java
  • layout_1.java
  • layout_2.java
  • layout_3.java
  • layout_4.java





















layout_1.java
Has a simple toast notifcation on click of a button



layout_2.java
Has a alert dialog to be opened on click of a button



layout_3.java
Has a image to be changed on click of a button 



layout_4.java
Has simple toast 



Java class code as follows

SlidingActivity.java

package com.rnd.slidingactivity;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.LinearLayout;

public class SlidingActivity extends Activity {
    /** Called when the activity is first created. */
Gallery my_gallery;
Integer[] integer_layouts= 
{
R.layout.layout_1,
R.layout.layout_2,
R.layout.layout_3,
R.layout.layout_4

};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.sliding_activity);
        
        my_gallery = (Gallery)findViewById(R.id.my_gallery);
        my_gallery.setAdapter(new LayoutAdapter());
    }
    
    public class LayoutAdapter extends BaseAdapter {
   
        
        
        public int getCount() {
             
            return integer_layouts.length;
        }

        public View getView(int arg0, View convertView, ViewGroup arg2) {

         LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View layoutViews = (LinearLayout)inflater.inflate(integer_layouts[arg0], null, false);
        
         layoutViews.setLayoutParams(new Gallery.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)) ;
         
         if(arg0==0)
         {
          LayoutActivity1 la = new LayoutActivity1(SlidingActivity.this, layoutViews);
         }
         else if(arg0==1)
         {
          LayoutActivity2 la = new LayoutActivity2(SlidingActivity.this, layoutViews);
         }
         else if(arg0==2)
         {
          LayoutActivity3 la = new LayoutActivity3(SlidingActivity.this, layoutViews);
         }
         else if(arg0==3)
         {
          LayoutActivity4 la = new LayoutActivity4(SlidingActivity.this, layoutViews);
         }
           return layoutViews;
        }

        
        public Object getItem(int position) {
         // Do nothing
            return null;
        }

        
        public long getItemId(int position) {
         // Do nothing
            return position;
        }
    }
}


LayoutActivity1.java


package com.rnd.slidingactivity;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class LayoutActivity1 {
Context context;
View vw;
public LayoutActivity1(Activity act,View view)
{
context = act;
vw=view;

operations();
}

public void operations()
{
Button btn = (Button)vw.findViewById(R.id.btn_ll_1);

btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
Toast.makeText(context, "You have clicked the second activity Control", Toast.LENGTH_LONG).show();

}
});
}

}


LayoutActivity2.java
package com.rnd.slidingactivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class LayoutActivity2 {
Context context;
View vw;
public LayoutActivity2(Activity act,View view)
{
context = act;
vw=view;

operations();
}

public void operations()
{
Button btn = (Button)vw.findViewById(R.id.btn_ll_2);

btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

getAlert();

}
});
}

public void getAlert()
{
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Layout 2");
alert.setMessage("Alert for 2nd layout");

alert.setPositiveButton("Ok",new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});

alert.show();

}

}


LayoutActivity3.java
package com.rnd.slidingactivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class LayoutActivity3 {
Context context;
View vw;
public LayoutActivity3(Activity act,View view)
{
context = act;
vw=view;

operations();
}

public void operations()
{
Button btn = (Button)vw.findViewById(R.id.btn_ll_3);

btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

changeImage();

}
});
}

public void changeImage()
{
ImageView img = (ImageView)vw.findViewById(R.id.img_ll_3);
img.setImageResource(R.drawable.droid);
}
}

LayoutActivity4.java
package com.rnd.slidingactivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class LayoutActivity4 {
Context context;
View vw;
public LayoutActivity4(Activity act,View view)
{
context = act;
vw=view;

operations();
}

public void operations()
{
Button btn = (Button)vw.findViewById(R.id.btn_ll_4);

btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Toast.makeText(context, "You are into Activity 4", Toast.LENGTH_LONG).show();

}
});
}


}


Layouts 
sliding_activity.xml
<?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" >

    <Gallery
        android:id="@+id/my_gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

layout_1.xml
<?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" android:background="#222288" android:gravity="center">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="50dp" >


        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1st Layout"
            android:textSize="50dp"
            android:gravity="center"
            android:textColor="#ffffff"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <Button
        android:id="@+id/btn_ll_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Toast Notification" />

</LinearLayout>


layout_2.xml
<?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" android:background="#228822" android:gravity="center">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="50dp" >


        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2nd Layout"
            android:textSize="50dp"
            android:textColor="#ffffff"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <Button
        android:id="@+id/btn_ll_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Alert Dialog" />

</LinearLayout>

layout_3.xml
<?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" android:background="#881122" android:gravity="center">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="50dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="3rd Layout"
            android:textSize="50dp"
            android:textColor="#ffffff"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageView
            android:id="@+id/img_ll_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

    </LinearLayout>

    <Button
        android:id="@+id/btn_ll_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Change Image" />

</LinearLayout>

layout_4.xml

<?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" android:background="#311100" android:gravity="center">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="50dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="4th Layout"
            android:textSize="50dp"
            android:textColor="#ffffff"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge" />

       
    </LinearLayout>

    <Button
        android:id="@+id/btn_ll_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Toast Notification" />

</LinearLayout>

Manifest file 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rnd.slidingactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SlidingActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Now enjoy your sliding activity like gallery imageview 




Friday, 25 October 2013

Android EditText to TextView conversion (UI only)

In order to make your EditText to function like Textview(look & feel) follow the below steps. 

Step 1: 
Assign ID to your EditText object
EditText edtText = (EditText)findViewById(R.id.YourID);

Step 2: 
Disable the edtText

edtText.setEnabled(false);

Step 3:
Disable cursor

edtText.setCursorVisible(false);

Step 4: 
Set Key listner as null.

edtText.setKeyListener(null);

Step 5:
 Make background Transparent

edtText.setBackgroundColor(Color.TRANSPARENT); 

Now check your EditText 

Thursday, 26 September 2013

control your android from your pc - how to control your android phone from pc

Hi Guys,

Bored of operating both PC & mobile at a time, seems like little irritating right?. Here is the solution. 

How about using a Windows software that can control your phone? Sounds interesting right. 

Just have look at the following video.




Wanna give a try ? 

1. Download the free MyPhoneExplorer http://www.fjsoft.at/en/ also you can find it in Google play for Android https://play.google.com/store/apps/details?id=com.fjsoft.myphoneexplorer.client&hl=en
2. Connect your phone through USB cable to PC. 
3. Little task for you just go through all the tabs you will get it :) 

Cheers... 

Please ask your doubts and also feel free to give your feedback. 

Tuesday, 24 September 2013

Android apk generation through command line (HTML to APK)

Android APK generation using command line
In order to generate, we need to follow the following steps
1.      1.  Folder
Create these folders inside your parent folder named AndroidTest. Folders you need to create inside manually are

·         Assets
This folder contains all html folder.

·         Res
o   Folders you need to create manually inside this folder
o   Inside res/drawable copy app icon image, rename the image as mylogo.png
o   Inside res/values create a xml file named as string.xml
Paste the following code

  

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="myApplicationName">My App Name</string>
</resources>


Where myApplicationName sets the name of your app.
·         Src
o   This folder contains the package including java source
Ex: com.tt.rnd (i.e. com\tt\rnd)
 

o   Create a file named MyActivity.java inside your package
Ex : com\tt\rnd\MyActivity.java Paste the following code inside MyActivity.java 
package com.tt.rnd;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       WebView   webview = new WebView(this);
        webview.getSettings().setJavaScriptEnabled(true);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webview.loadUrl("file:///android_asset/index.html");
        setContentView(webview);
    }
}

 2.       AndroidManifest.xml You need create this file inside your parent folder. Copy the below code 
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mycompany.package1"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-sdk android:minSdkVersion="8"  android:targetSdkVersion="15"/>
    <application android:icon="@drawable/mylogo"
                 android:label="@string/myApplicationName">
        <activity android:name="com.tt.rnd.MyActivity"
                  android:label="@string/myApplicationName">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>




3. Download Java
The Java Standard Edition (Java SE) is available from Oracle at http://www.oracle.com/technetwork/java/javase/downloads.
Default installation location will vary between Java versions and OS platforms, so create a pointer to its location:
JAVA_HOME = C:/Program Files/Java/jdk1.6.0_26

5. Download Android
The Android Software Development Kit (Android SDK) is available from Google at http://developer.android.com/sdk.
Default installation location will vary between Android versions and OS platforms, so create a pointer to its location:
ANDROID_HOME = C:/Program Files/Android/android-sdk

6. Choose project
Locate the path to your project named- AndroidTest
DEV_HOME = C:/Users/johnd/dev/AndroidTest

7. Create keystore
A keystore is a database of private keys and their associated X.509 certificate chains authenticating the corresponding public keys. An Android program must be signed in order to execute on a device, and the program is signed by using a key from a keystore.
Use the keytool program to create a keystore:
JAVA_HOME/bin/keytool
                -genkeypair
                -validity 10000
                -dname "CN=company name,
                        OU=organisational unit,
                        O=organisation,
                        L=location,
                        S=state,
                        C=country code"
                -keystore DEV_HOME/AndroidTest.keystore
                -storepass password
                -keypass password
                -alias AndroidTestKey
                -keyalg RSA
                -v

Replace the distinguished name (dname) information with your own. Make sure to specify passwords as indicated. The key is accessed through the specified alias which can be any string.
The output of the command will be the DEV_HOME/AndroidTest.keystore file which contains one key identified by the supplied alias.
8. Create R.java
In order for the application source code to be able to access the resources within the res/ directory, a class called R.java (for Resources) is created.
Use the Android Asset Packaging Tool (aapt) to create the R.java file:
ANDROID_HOME/platform-tools/aapt
                        package
                        -v
                        -f
                        -m
                        -S DEV_HOME/res
                        -A DEV_HOME/assets
                        -J DEV_HOME/src
                        -M DEV_HOME/AndroidManifest.xml
                        -I ANDROID_HOME/platforms/android-15/android.jar
The destination location of R.java within the src/ tree is determined by the package attribute of the manifest file.


9. Compile code
Use the javac tool to compile java source code in a package:
JAVA_HOME/bin/javac
                -verbose
                -d DEV_HOME/obj
                -classpath ANDROID_HOME/platforms/android-15/android.jar;DEV_HOME/obj
                -sourcepath DEV_HOME/src
                DEV_HOME/src/com/tt/rnd/0*.java
The command must be applied for each existing package. 3rd-party .jar files from the lib/ directory must be listed in the -classpath entry. Note that on UNIX-like operating systems the classpath entry delimiter should be a colon (":").
The output of the command is .class files in the obj/ tree.
Non-java files within the src/ tree must be copied to the associated location in the obj/ tree.

10. Create DEX file
DEX ("Dalvik Executable") is the specific bytecode format understood by the Dalvik virtual machine (VM) present in all Android devices.
Use the dx tool to bundle the content of the obj/ directory as well as 3rd-party .jar files from the lib/ directory into a single .dex file:
ANDROID_HOME/platform-tools/dx
                --dex
                --verbose
                --output=DEV_HOME/bin/classes.dex
                DEV_HOME/obj
                DEV_HOME/lib
This will create the classes.dex file in the bin/ directory. The content of a .dex file can be inspected using the ANDROID_HOME/platform-tools/dexdump tool.
11. Create APK file
The Android package format (APK) is the .jar equivalent for Android. The package contains the manifest file, the resources and the classes.dex file.
Use the aapt tool to create the .apk file:
ANDROID_HOME/platform-tools/aapt
                         package
                         -v
                         -f
                         -A DEV_HOME/assets
                         -M DEV_HOME/AndroidManifest.xml
                         -S DEV_HOME/res
                         -I ANDROID_HOME/platforms/android-15/android.jar
                         -F DEV_HOME/bin/AndroidTest.unsigned.apk
                         DEV_HOME/bin
This will create the AndroidTest.unsigned.apk file in the bin/ directory. Note that APK is an ordinary archive format that can be inspected by tools like WinZip or unzip -l.
12. Sign APK file
In order to execute on an Android device, the Android package needs to be signed.
Use the jarsigner tool and the key from the keystore created above to create a signed version of the package:
JAVA_HOME/bin/jarsigner
                -verbose
                -keystore DEV_HOME/AndroidTest.keystore
                -storepass password
                -keypass password
                -signedjar DEV_HOME/bin/AndroidTest.signed.apk
                DEV_HOME/bin/AndroidTest.unsigned.apk
                AndroidTestKey
The signing process adds the META-INF/ directory to the APK archive including the signature (.SF) file and the associated PKSC file (.RSA).
The signed APK is stored as AndroidTest.signed.apk file in the bin/ directory.
13. Zip-align APK file
zipalign is an archive alignment tool that provides important optimization to Android packages. This step is optional, but recommended:
ANDROID_HOME/tools/zipalign
                -v
                -f
                4
                DEV_HOME/bin/AndroidTest.signed.apk
                DEV_HOME/bin/AndroidTest.apk

This will create the AndroidTest.apk which is the final product delivered in one self-contained unit.
Guys give your views about the article.