Sunday, July 15, 2018

Starting With Android Studio Part-II | Addition of two numbers

  • Android basic tutorial for beginners 
Hello friends welcome to Tech Punch.
In this tutorial we will create app to add two number.
Hope you have referred my previous blog for starting with android studio.
Lets start!!

Designing part!!!

  • Go to activity-main.XML and select design view from bottom menu.
  • In left hand side from Palette panel select Text-view. Click And hold Text-view and drag into screen layout.
  • We will use Text-view to show result on addition.
  • Also drag and drop two Plain-text on layout for taking two number from user.
  • Also take one button for performing addition.
  • Click on Text-view which is previously dragged on layout, in right hand side all properties of Text-view are present.
  • Select text property and name it as RESULT.
  • Select 1st dropped Plain-text (EditText) on right hand side in hint property write “Enter 1st number”.
  • Do same for another Plain-text.
  • Select button and from properties change text to ADD

    Coding part!!!

  • Go to Main-activity.java
  • Create onClick method after onCreate method.
  • Pass object of View as method argument.
  • go to activity.main and select design view .
  • select button and in onClick properties select Onclick method which recently we have created.
  • get back to the MainActivity.java.
  • In onClick method create object of TextView and EditText which we had previously dragged on layout.
  • eg:-   TextView t1=(TextView)findViewById(R.id.textView);
  • Declare three variables for taking number and perform addition.
  • In 1st variable take 1st number from EditText.
  • In 2nd variable take 2nd number from EditText.
  • Perform addition between 1st and 2nd variable and store it into 3rd variable.
  • Using TextView object show result Which is stored in 3rd variable.

CODE:-

public void onClick(View v)

 {

 TextView t1=(TextView)findViewById(R.id.textView);

 EditText e1=(EditText)findViewById(R.id.editText);

 EditText e2=(EditText)findViewById(R.id.editText2);

int a,b,c

a=Integer.parseInt(e1.getText().toString());
b=Integer.parseInt(e2.getText().toString());
c=a+b;

t1.setText(Integer.toString(c));
}

No comments:

Post a Comment