Wednesday, 1 February 2017

Calculations of Integers in Android | Very Simple Calculator App | Add sub mult Div - Part 1

Here you can learn how to perform simple Airthmatic operations in Android. This is applicable for only integer data type.



Coding is below -

package com.example.username.SimpleCalculatorforInt;

}

// Similarly we perform same coding for rest of operations ( subtraction , multiplication, division)

public void onSubClick(View view) {
        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        TextView res = (TextView)findViewById(R.id.textView4);

        int num1 = Integer.parseInt(e1.getText().toString());
        int num2 = Integer.parseInt(e2.getText().toString());

        int sub = num1 - num2;
        res.setText(Integer.toString(sub));

    }
    public void onMultClick(View view) {
        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        TextView res = (TextView)findViewById(R.id.textView4);

        int num1 = Integer.parseInt(e1.getText().toString());
        int num2 = Integer.parseInt(e2.getText().toString());


        int mult = num1 * num2;
        res.setText(Integer.toString(mult));

    }
        public void onDivClick(View view) {
        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        TextView res = (TextView)findViewById(R.id.textView4);

        int num1 = Integer.parseInt(e1.getText().toString());
        int num2 = Integer.parseInt(e2.getText().toString());

        int div = num1 / num2;
        res.setText(Integer.toString(div));

    }



}


Explanation of Code -


1. Variable declaration - EditText e1, EditText e2, TextView res

2. Variable Casting -  EditText e1 =(EditText)findViewById(R.id.editText); 
                       (same for other variables)
User entered number values in edit text widgets which 
are in integer format and are converted into string format 
and stored into num1 and num2. and for that we need to 
use code e1.getText().toString()and e2.getText().toString()

Now we need to perform add operation so numbers which
 are stored into num1 and num2 are in string format are again
 converted into integer format by using code Integer.parseInt



Tuesday, 31 January 2017

How to Run Audio File when you Click on Image Button in Android

Here I am sharing explanation of layout and coding ...

Step 1: Create new project and do basic activities..
Step 2 : Go to drawable folder and paste .jpg file over there. 
Step 3 : Go to res folder, Create raw folder, paste .mp3 file over there. 

Step 4 : Pasting here screen shot


Step 4 : Go to MainActivity.java and code is below


   package com.example.username.runaudio;

Explanation of Layout - 

1 . Library created automatically
2.  Dragging and dropping of imagebutton (widgest from palette).
3. saving image file (pink.jpg) on drawable folder. ( which is inside res folder)
4. created raw folder to save audio file. (inside res folder)and paste itspink.mp3 file over
   there.
 
Explanation of code - 

1. declare variables inside main method
2. created one method clickImageButton() ( we need to create listner when we press on 
imagebutton.    in this method we casted (assign) our widgest. 
3. we need to create onClickListner on our click action and we created this on code and there
 onclick() method is created and this is bydefault created by eclips. 

4. Now MediaPlayer.create method fetches audio file from raw folder and starts audio by calling 
method mdp.start()