Showing posts with label ANDROID. Show all posts
Showing posts with label ANDROID. Show all posts
Tuesday 13 June 2017
Working with multiple buttons in Android
Download Source Code
In this post you can learn how to work with multiple buttons , before that read how to work with buttons.
It is similar to that of working with buttons the difference is that we are going to work with multiple buttons .
We are just going to use a SWITCH case where the button id's are used in case: .
Let us start the project.
Open New Project and make a user interface by dragging dropping the buttons where you want , I recommend you to read the below two post to get grip on the Buttons.
How to display names using Button in Android
Adding two numbers in Android
After designing the user interface it will look like
Edit the names of the buttons as Button 1, Button 2, Button 3 respectively.
Right click on the button to Edit Text.
Do the same for all the buttons
After that we need to start java coding.
We need to register these three buttons and implement the interface and add unimplemented methods
After that inside the onclick method we need to write the switch condition.
This code is similar to that of switch case examples in c programs ,
the v.getId() will give you the id of the clicked button.
The other codes are similar to the previous posts.
The output will be:
How to use Android Phone as an Emulator
Working with multiple buttons in android will be useful in the upcoming examples so practice this example well and try your own ideas and practice that too.
Code of onClick() method:
public void onClick(View v){
switch (v.getId())
{
case R.id.button1:
Toast.makeText(getApplicationContext(), "Button 1 is clicked", Toast.LENGTH_LONG).show();
break;
case R.id.button2:
Toast.makeText(getApplicationContext(), "Button 2 is clicked", Toast.LENGTH_LONG).show();
break;
case R.id.button3:
Toast.makeText(getApplicationContext(), "Button 3 is clicked", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
Monday 12 June 2017
Adding two numbers in Android
Download Source Code
In this post you can learn how to add two numbers in android.Before that i recommend to read the below button example post to get an idea about working with buttons.
How to work with buttons in android
How to display names while button is clicked in android.
As in my last i have said that before starting the design we need to draw what we are going to design.
Create a New Project and drag and drop two EditText for getting inputs from the user and a Button to calculate addition operation and a TextView to display the Result.
Read this post to remove the warnings How to display names while button is clicked in android.
Next we need to start the coding part.
The registering the edittext , Textview, Button is same as the previous posts.
After that we need to declare variables to get the numbers and store the result value.
we define it in double.
double number1,number2,sum;
Then type the following code
the last lines in the code is just converting the string in to double for calculation.
Then after typing the code Run the program.
Output:
Read How to run android program in your mobile
Code of onClick() method:
public void onClick(View arg0)
{
number1=Double.parseDouble( num1.getText().toString());
number2=Double.parseDouble(num2.getText().toString());
sum=number1+number2;
result.setText(Double.toString(sum));
}
How to display names using Button in Android
Download source code
In this post we are going to display name which we are going to enter in a text field and when the button is clicked the entered name will be displayed.First we need to create a new Project in Eclipse IDE.
File ----> New ---> Android Application Project.
Give the project name and click Next--->Next-->Next--->Next---->Finish.
While creating any Project we need to make a simple design overview of that application.
It can be drawn in Paper or in Paint application.
Here I am using the first method , drawing the design in a paper.
Like this we are going to design the User Interface.
Open activity_main.xml file under DisplayingName(ProjectName)---->res---->activity_main.xml
Then find the Palette and Drag and drop the TextView element .
Then we need a place to enter the name that is called a plain text element which is found under the TextFields area.
Next we need a Button
After that we need to display the entered name for that we need text view.
Now our required elements are placed in our project, now we need to make some editing of the elements.
First we need to rename the textview, for that we need to right click on the textview--->EditText option.
Then we need to enter our desired text and click Ok.
After that you can see that the edited text will appear
Like that we can edit the Button name as Show and the last TextView to empty.
we can clear those warnings.
Go to values----> String .xml and open it.
Then type as in the image
After come to activity_main.xml and type as in the image.
Do the same for button and give the input type as text in Edit text
Now save all the errors and warnings will be gone now.
Next we need to start coding the java part for displaying the name.
Open MainActivity.java under the src folder.
Register the button and implement the interface and override the unimplemented methods as we saw in last post
Read Post
You can see the Button ID , Text Id in activity_main.xml
The coding till the last post is
We need to register the plaintext i.e edittext and text view to get the input and display the output.
Then we need to code the button part as in the image
The code is in the image.
Then Run the Program , right click on the Project Name and click Run as---> Android application
Read how to run Android app in your Mobile
The output is
input is the name of the edittext and getText() method is used to get what ever the text that the user gives inputs and settext is to set the text we want .
MainActivity.java
public class MainActivity extends Activity implements OnClickListener
{
EditText input;
TextView output;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input=(EditText)findViewById(R.id.editText1);
output=(TextView)findViewById(R.id.textView2);
Button show=(Button)findViewById(R.id.button1);
show.setOnClickListener(this);
}
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
String name=input.getText().toString();
output.setText(name);
}
{
EditText input;
TextView output;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input=(EditText)findViewById(R.id.editText1);
output=(TextView)findViewById(R.id.textView2);
Button show=(Button)findViewById(R.id.button1);
show.setOnClickListener(this);
}
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
String name=input.getText().toString();
output.setText(name);
}
This simple example will make you motivated to become an android app developer.
You find any error in this example send to my mail narain2829@gmail.com .
Sunday 11 June 2017
How to use Buttons in Android App
Download Source code
In this post you can learn the fundamental working concept of Buttons in Android .
I am using Eclipse IDE for this example but it does not differ too much with Android studio. So users who are using Android studio also can refer this example.
First create a New Project in Eclipse IDE.
It is same as that of creating Hello World app which I have already posted before.
Read How to develop Hello World app or Creating New Project in Eclipse IDE
In that post after creating a New Project and before going to run the Helloworld app we are going to create a Button.
Creating a button is very easy .
Look for the Pallet option near the work space area.
In that you can see the Button option in it.
Simply click and drag that Button and place anywhere in the work space.
You can remove that warning symbol by reading this post resolve warning.
Although it does not causes error it is better to avoid those warning, you can continue coding with that warning also.
Next we need to give action to that button when it has been clicked.
Now its time to java code.
You need to register your button with the MainActivity.java and then can write action code of button.
First go to MainActivity.java which can be found under the res folder.
Then open MainActivity.java file, it will like in the below picture
in the above you can delete the selected code which not going to useful for us in this example.
after deleting we need start writing the button code below setcontentview line
First we need to register the Button.
it can be done by:
Button b=(Button)findViewById(R.id.button1);
I will explain the code in detail later in this post.
After typing the code you may find some errors.
It is nothing but we need to import the Button widget, just move the mouse over that error you will see
Click Import 'Button' (android.widget)
Now the error is gone.
After this we have to write the code to detect when the button is clicked, this is similar to listening to the button click , we need to set the onClickListener to the button like:
b.setOnclickListener(this)
If you type the following code you will get error, just move over the error and you will see the options
In that scroll down and you will find Let MainActivity implements OnClickListener and click it.
Then after implenting the OnClickListener class again there will be an error to implement the overriden classes from the implemented interface
Move the mouse over the error and click add unimplemented methods
After clicking that you can see onClick() will be overrided
Inside the onClick() method only we are going write the code for the button action.
In this example i am going to print a message 'Hai' with the help of Toast.
Toast is a short message appears at the bottom of the screen for short time of period ex:3 sec or 5 sec .
This is the code for the Toast message.
That's it , after done the above run the program.
To Run the Program read
How to use Android Phone as an Emulator
How to Create an Emulator in Android Eclipse
Emulator is nothing but it is place to run the Android apps that we have developed.
The output will be
After clicking the button
Code Explanation:
Button b=(Button)findViewById(R.id.button1);
In the above code Button is an UI element in Android it is like a Data type.
b is a object or name of the button which used through out the program to use a particular button.
(Button) is a Type Casting the button we have created is of View type we have to convert into Button type.
Every button has its own id , in our example the id of our button is button1.
we can find it by findViewById method.
This the first step.
The next one is setting the OnClickListener method to the created button.
then we need to implement OnClickListener interface and add the unimplemented methods.
then the onClick method will be overridden, inside that we need write the code.
Toast is to display the short message , it is like a notification.
makeText is a method to describe where the text message should be appear , what is the message to be appear and the duration of the Toast message.
MainActivity.java
public class MainActivity extends Activity implements OnClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hai", Toast.LENGTH_LONG).show();
}
}
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hai", Toast.LENGTH_LONG).show();
}
}
We will discuss the above concepts in detail in the upcoming posts.
Subscribe to:
Posts
(
Atom
)