Wednesday, 15 June 2016

Creating your first android app - installation, configurations and app coding

Download android studio from this link:
Keep clicking next and follow basic instructions and install studio.
Now , create a new project:



Give an application name and company domain. Company domain is pretty much like package in java.


Since we are creating a mobile app, click on mobile/tablet option:


Choose the design template you wish to build upon. I am selecting Empty activity:

Activity Name is named by default as MainActivity, you can change it to anything relevant. This is the piece of code where you write your events code and click on finsih.

On clicking finish editor opens, navigate to MainActivity.



since screen shot is unclear, ill put the entire final code here: [refer to it at the end]

package com.example.jb.khushisoysterdemo1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

        @Override        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        public void onClickEvent(View v)
        {
            TextView txt=(TextView) (findViewById(R.id.textID1));
            txt.setText("Hey there, this is my 1st android app with event");
        }
    }


Now navigate to gradle.properties 


Edit it by removing the last comment, and adding an extra line, so that the last two lines are:
org.gradle.parallel=true
org.gradle.daemon=true

Now gradle.properties should be like:


Now go to File > Settings > Compiler
Add –offline at command-line options to run your app offline with the connected device


Now go to File > Settings > Build, Execution, Deployment > Build Tools > Gradle .
Tick mark “Offline work”.

Click on Apply and press OK.

Now go to res > layout > activity_main.xml


Click on Widgets > Button and drag button to phone screen on the right side.
                Label the button as testClick and let the id be buttonID1:


Now drag a Plain TextView from widget. On running this app at the end, this text box should appear/ be made visible in screen on click of “testClick” button.
Give text as textPlaceHolder and give id name as textID1.





We will use button and text widget ids to fire event on button click.
Now, go back to MainActivity and change onCreate to public instead of protected and add a function “onCllickEvent” to trigger on click of button “buttonID1”.

Now go back to activity_main.xml and double click on “testClick” button widget. Navigate to “onClick” proprty and select “onClickEvent”. 


Check mark “clickable”



Now to test your app wirelessly on your android device, you have to do following configuartions:
(wireless test on android device is supported from version 5 as per my understanding)
On your android device, navigate to:
Settings > About device and Click on build number 7 times.
Now go to Settings > Developer Options
And check mark “Authorise wireless display” (Check on this point, im not sure if this is compulsary, but i did it)
Note your ip from ADB over network.
NOW,
On your computer, navigate to:
C:\Users\User Name\AppData\Local\Android\Sdk\platform-tools
Now press shift and right click at same time and then select “open command window here”


Now type “adb devices” and click enter. Then write “adb connect ip_address”. In place of “ip_adress”, type in the above noted ip address of your phone and click enter.

U’ll get a reply saying its connected to the ip:

Once connected,
Build and Run your app.

For USB test on android app
-          To test your app on device with USB connection:
1.       Make sure your device driver is installed in your computer. For example, Samsung device driver is downloaded from http://www.samsung.com/in/support/usefulsoftware/KIES/.
2.       In your computer, navigate to:
C:\Users\UserName\AppData\Local\Android\Sdk
And double click on SDK Manager  and navigate to Extras and check mark “Google USB Driver”:

Install it.
Then build the project:


Connect your android device with  your laptop. You should be able to see your connected device as blue highlighted in this screenshot:

Click OK.

Next, run it and you should be able to see this new app in your android device



Now click on TESTCLICK to see the text message



No comments:

Post a Comment