Saturday, February 9, 2013

Android: Find LogCat window



Can't find LogCat window to view Log messages.

Here are the steps to find it.































On your Eclipse.. In the bottom Panel you should see LogCat window now.





























Android: Printing console messages



First things first.. While coding everyone wants to log something or other in console for various purposes. iPhone has NSLog and Android has Log.d , Log.v .. In debug mode of app we can just use Log.d  and view the logs in LogCat Window. Can't find LogCat Window ?


This sample take values from an array and prints it in Log.

String itemValue=listArray[position]; Log.d("item value clicked is", itemValue);



How to print integer in Log.d ? Lets say you want to print length of listArray array which is an int.


Log.d("listArray length", String.format("array length = %d", listArray.length));

Android: Invoke a method after some delay



If you have a method which you want to call after some delay



        //Create a delay handler
        Handler delayHandler= new Handler();
        Runnable r=new Runnable()
        {
@Override
public void run() {

// Call this method after 1000 milliseconds
showCountryListViewIntent();
}
        
        };
        delayHandler.postDelayed(r, 1000);



    ///This method will be called after say 1000 milliseconds.
    //This will call the another Activity
    public void showCountryListViewIntent()
    {
    Intent countryListIntent=new Intent(this,CountryListActivity.class);
    startActivity(countryListIntent);
    }


You can find the sample code DOWNLOADABLE HERE.





Android: Navigate from one screen to another





From your current activity to want to switch to a new activity. You can do so as following



Intent newIntent=new Intent(this,SecondActivity.class);
startActivity(newIntent);



You can find the sample code if it is still not clear. LINK



Friday, February 8, 2013

Spotlight on Mac




Spotlight you can find anything on your Macintosh really quick.
Its a search icon on the top right on your macintosh screen.














ADB Error: The connection to adb is down




If you are getting following error while trying to run the android code on your AVD


The connection to adb is down, and a severe error has occured.
You must restart adb and Eclipse.
Please ensure that adb is correctly located at 'PATH' and can be executed.



Following tip may be help you :
Close your eclipse.Open the Activity Monitor and quit the process named abd process from the list.
I have highlighted the one you want to remove. Restart the eclipse and run your app now.




































Where to find the Activity Monitor ? Try Spotlight on your mac. Where is Spotlight ?




Outline Window in Eclipse ADT




Trying to find outline window ?

No worries if you closed it by mistake and you want to bring it back. Here is the path.


































Here is what Outline Window looks like, in right panel of Eclipse. You will be able to view all the child views in your xml file in Outline window.






Android 101- Part 2

Lets begin coding !

This is a very simple tutorial. In this tutorial we will learn how to add a simple listview (tableview) to our screen.
This is a one screen app and all it does it map strings of an array to each row in the listview.

Lets create our very first project. Android101. You can see the screenshots below to see what settings I used.











































































Now click on Finish. You will see Android 101 on left panel of Eclipse as shown in the screenshot below.





























Select activity_main.xml. On the right panel you will see Outline Tab. By default you will see RelativeLayout and TextView as its child. 


























Delete TextView. Your Outline tab will have only RelativeLayout now.























In the palette, Under Composite you can drag and drop the ListView on your activity_main.xml





























Finally  activity_main.xml will look like following






























Now go to your MainActivity.java file. You will see following code by default. Now we need to write more code  in it.































Note: If you remember when I dropped listview in xml file its name can be seen in Properties of activity_main.xml file, id is @+id/listView1 .We need to know this name to create the reference in .java file.

Don't know where the Outline Window is ? Check out this article.




























We need to create a reference in our code to listview and then create an array and connect array with listview. As you can see that while I am creating a reference to listview, I can use code completion technique to help me code faster.






















Here is the code for you
________________________________________________________________________________


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myListView=(ListView)findViewById(R.id.listView1);
//Create the array to fill the list view with
final String []listArray= new String[]{"Apple","Banana","Cherry","Lemon"};
ArrayAdapter<String> madapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,listArray);
myListView.setAdapter(madapter);
//When you tap on any row. it prints the string of that row.
myListView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   
  String itemValue=listArray[position];
  Log.d("item clicked at", itemValue);
  }


}); 
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}








________________________________________________________________________________



After you have added all the code in MainActivity.java .Hit the RUN green button.
You will see following pop up, Select Android Application.









































When you run the app for the first time on AVD. It will take long time to start  like 5 mins may be.
So you will see following screen for long time.
























After 5 mins or so, you will see home screen on your AVD.






















You can unlock your home screen. If you are able to run the app successfully; you will see following in console of Eclipse.

[2013-02-08 17:22:19 - Android101] ------------------------------
[2013-02-08 17:22:19 - Android101] Android Launch!
[2013-02-08 17:22:19 - Android101] adb is running normally.
[2013-02-08 17:22:19 - Android101] Performing com.reebok.android101.MainActivity activity launch
[2013-02-08 17:22:25 - Android101] Automatic Target Mode: Preferred AVD 'ReetuSim2' is available on emulator 'emulator-5554'
[2013-02-08 17:22:25 - Android101] Uploading Android101.apk onto device 'emulator-5554'
[2013-02-08 17:22:25 - Android101] Installing Android101.apk...
[2013-02-08 17:22:46 - Android101] Success!
[2013-02-08 17:22:46 - Android101] Starting activity com.reebok.android101.MainActivity on device emulator-5554
[2013-02-08 17:22:50 - Android101] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.reebok.android101/.MainActivity }






And you will be able to see listview in AVD screen finally !! Download the code.























If you have multiple AVDs and you want to switch it to see what your app will run like on another AVD with different dimension. Here is a little tutorial to help. LINK




How to switch between Android Virtual Device ?



You have multiple AVD in your list. The question is you want to switch to another AVD which has different dimension and settings.

Select your project in Package Explorer in left Panel. I have selected my project Android 101. On top menu you see the green arrow RUN button which you use to run the application. Click on the drop down arrow right next to RUN button, you will see following menu as shown in the screenshot below. Select Run Configuration.













After you select the Run Configuration in previous pop up.You will see the new pop up as seen in the screenshot below.  Select Target Tab. You will see your list of AVDs.





























You select AVD of your choice and click on Apply. After that you can Click on Run. This will run your project in AVD you have selected. As per the window in the screenshot. Android101 project will run on ReetuSim2 named AVD.


Android 101 - Part 1



In this post, we will learn how to start working on Android Apps. Its not difficult,Google has made it super easy now to begin the development.


Step 1:
You need to download the ADT kit which comes with eclipse.
http://developer.android.com/sdk/index.html
I run  mac,  it automatically downloaded the whole kit and I had following folders downloaded as shown in the screenshot above.. If you are running it on Windows, it will give you the windows compatible files to download.



















Now just open on Eclipse - the green button, it will ask you to create a workspace. You can choose some folder on your machine to be your workspace. For beginners I will suggest that don't move your downloaded folder as well as the workspace to any other place after this step till you get a hang of android development. Wait for some days, you don't want to mess up your set up which you just created.



Congratulations ! You have successfully finished step 1 of this tutorial !.

Step 2:

Lets create a virtual device(Simulator) which we will need to run our app. Under Window > Android Virtual Device Manager.



























Lets create a new virtual device. Click on New Button. Note: I already have a virtual device named ReetuAndroidSim" in my list. But since you are just starting so you will no virtual devices in your list.

































Clicking on New Button will open a new window as shown in the screenshot below.
You give the name, select the device of your preference- Note: Dimensions of every device is described so for now lets just select one. For example I  have given a sample details filled out for you. Then just click OK.


































This will add your virtual device in your list. You will be able to see it in the list as shown in the screenshot below.
































Congratulations ! You have finished step 2 as well. Now you are ready to start coding for Android.

Lets go to next Tutorial to start a project.