How to set up working environment
For quick start of Android without any hick ups
go to : http://developer.android.com/sdk/index.html
Download the SDK ADT bundle for Windows.
After download find SDK Manager by selecting that we select on which version we want to develop our app.
Now that the whole set up is ready go to
adt-bundle-windows-x86-20130729\eclipse\eclipse
Start new project:Our sample application would be a simple calculator to multiply two integer/decimal values. It will take two inputs and on clicking 'Submit' it will show their multiply result.
1. Create Project
File->New->Andriod Application Project
enter Project Name as 'MyApp1', From Contents section
select 'Create New Project in workspace', Check Android 2.2 from Build Target section, Application
Name 'myapp1', Package Name 'com.sample', Create Activity 'Main' and '8' as Min SDK
Version.
2. Create Test Project
Check Create a Test Project and it will automatically fill rest of fields based on the last made project
(MyApp1), and click on Finish.
After successfully creating projects, our Project explorer screen should look like,
3. Understanding Project Architect
Expand the src directory then expand com.calculator
directory, MainActivity.java file contains application logic.
In res/menu directory we can define application's UI
interface.
In main.xml we can put controls on application
interface and in res/values string.xml we can define their string
values,which would be visible on UI.
We would not get into details, as its not in our scope
so far.
4. Designing Layout
In activity_main.xml enter following code and save it.
5. Designing Application Logic
In MainActivity.java enter following code and save it.
6.Configuring Strings:
In strings.xml enter following code and save it.
Our application is designed and its time to run it.
7:Run It:
Right click on project select Run As and then select Android Application & and wait for while.
It will load Android simulator, you need to wait for some time, it will launch application itself.
Our simple myapp1 is ready, enter some integer/decimal values and click on Submit, it will
show the result above Click button.
For quick start of Android without any hick ups
go to : http://developer.android.com/sdk/index.html
Download the SDK ADT bundle for Windows.
After download find SDK Manager by selecting that we select on which version we want to develop our app.
Now that the whole set up is ready go to
adt-bundle-windows-x86-20130729\eclipse\eclipse
Start new project:Our sample application would be a simple calculator to multiply two integer/decimal values. It will take two inputs and on clicking 'Submit' it will show their multiply result.
1. Create Project
File->New->Andriod Application Project
enter Project Name as 'MyApp1', From Contents section
select 'Create New Project in workspace', Check Android 2.2 from Build Target section, Application
Name 'myapp1', Package Name 'com.sample', Create Activity 'Main' and '8' as Min SDK
Version.
2. Create Test Project
Check Create a Test Project and it will automatically fill rest of fields based on the last made project
(MyApp1), and click on Finish.
After successfully creating projects, our Project explorer screen should look like,
3. Understanding Project Architect
Expand the src directory then expand com.calculator
directory, MainActivity.java file contains application logic.
In res/menu directory we can define application's UI
interface.
In main.xml we can put controls on application
interface and in res/values string.xml we can define their string
values,which would be visible on UI.
We would not get into details, as its not in our scope
so far.
4. Designing Layout
In activity_main.xml enter following code and save it.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="fill_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtFirstNumber" /> <EditText android:id="@+id/EditText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="5dp" android:ems="10" > <requestFocus /> </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtSecoundNumber" android:layout_marginTop="10dp" /> <EditText android:id="@+id/EditText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="36dp" android:layout_marginTop="10dp" android:text="Submit" /> <TextView android:id="@+id/txtResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtResult" android:layout_marginLeft="36dp" /> </LinearLayout> </ScrollView>
5. Designing Application Logic
In MainActivity.java enter following code and save it.
package com.example.myapp; import android.os.Bundle; import android.app.Activity; import android.text.Editable; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { EditText FirstValue; EditText SecondValue; TextView Result; Button Calculate; float num1 , num2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FirstValue = (EditText)findViewById(R.id.EditText1); SecondValue=(EditText)findViewById(R.id.EditText2); Result=(TextView)findViewById(R.id.txtResult); Calculate = (Button) findViewById(R.id.button1); Calculate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //Getting first & second values and passing to show result showResult(FirstValue.getText(), SecondValue.getText()); } }); } protected void showResult(Editable first, Editable second) { float num1 = Float.parseFloat(first.toString()); float num2 = Float.parseFloat(second.toString()); float result = num1 * num2; Result.setText(String.valueOf(result)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
6.Configuring Strings:
In strings.xml enter following code and save it.
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MyApp</string> <string name="action_settings">Settings</string> <string name="space_line">\n</string> <string name="txtFirstNumber">Enter First number:</string> <string name="EditText1">Enter First number</string> <string name="txtSecoundNumber">Enter Second number:</string> <string name="EditText2">Enter Second number</string> <string name="button1">Submit</string> <string name="txtResult">Result will be displayed here</string> </resources>
Our application is designed and its time to run it.
7:Run It:
Right click on project select Run As and then select Android Application & and wait for while.
It will load Android simulator, you need to wait for some time, it will launch application itself.
Our simple myapp1 is ready, enter some integer/decimal values and click on Submit, it will
show the result above Click button.
No comments:
Post a Comment