Google search

Basic and advanced computer skills like Excel with macros, How to speed up your PC, C, CPP, Java programming, HTML, JavaScript, PHP, Wordpress, all web tools, Android tutorials, MySQL Tutorials, WAMP server installation. etc.

Android Example1

Android : Simple program to show sum of two numbers.
Step 1. in activity_main.xml file copy the following code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Enter First number"
      android:id="@+id/t1" />

    <EditText
        android:layout_width="103dp"
        android:layout_height="wrap_content"
        android:id="@+id/n1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter second number"
        android:id="@+id/t2"
        android:layout_gravity="center_vertical"
        android:layout_below="@+id/n1"
        android:layout_toStartOf="@+id/n1"
        android:layout_marginTop="39dp" />

    <EditText
        android:layout_width="92dp"
        android:layout_height="wrap_content"
        android:id="@+id/n2"
        android:layout_gravity="center_vertical"
        android:layout_alignTop="@+id/t2"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="209dp"
        android:layout_height="wrap_content"
        android:id="@+id/n3"
        android:layout_gravity="center_vertical"
        android:layout_below="@+id/n2"
        android:layout_alignRight="@+id/n2"
        android:layout_alignEnd="@+id/n2"
        android:layout_marginTop="36dp" />

    <Button
        android:text="Click here"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:layout_gravity="bottom"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/t1"
        android:layout_toEndOf="@+id/t1" />
</RelativeLayout>
Step 2: in MainActivity.java file copy the following code.
package app.mitindia.com.aop;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       final EditText ed1=(EditText)findViewById(R.id.n1);
        final EditText ed2=(EditText)findViewById(R.id.n2);
        final EditText ed3=(EditText)findViewById(R.id.n3);

        Button btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int x=new Integer(ed1.getText().toString());
                int y=new Integer(ed2.getText().toString());
                int sum=x+y;
                ed3.setText(String.valueOf(sum));

            }
        });


    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Step 3: Now, Run the application see the following output. Screenshot_2015-06-24-17-41-58

2. Android Simple Menu Application
a) copy the following code to "activity_main.xml"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/save"
        android:layout_centerInParent="true"/>


</RelativeLayout>

b) Now copy the follwoing code to "Main_Menu.xml" file

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">



    <item android:id="@+id/item1"
        android:title="Item1"/>

    <item android:id="@+id/item2"
        android:title="Item2"/>

    <item android:id="@+id/item3"
        android:title="Item3"/>


</menu>

c) Now, copy the following code to "Main_Activity.java"

package app.mitindia.com.menuapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId())
        {
            case R.id.item1:
                Toast.makeText(getApplicationContext(),"Item 1 selected", Toast.LENGTH_LONG).show();
                return true;


            case R.id.item2:
                Toast.makeText(getApplicationContext(),"Item 2 selected", Toast.LENGTH_LONG).show();
                return true;

            case R.id.item3:
                Toast.makeText(getApplicationContext(),"Item 3 selected", Toast.LENGTH_LONG).show();
                return true;

default:
    return super.onOptionsItemSelected(item);


        }

    }
}
2) Run your application and observe the following output.
Screenshot_2015-06-29-14-10-33

1 comment: