Showing posts with label TUTORIALS. Show all posts
Showing posts with label TUTORIALS. Show all posts

Sunday 11 June 2017

How to use Buttons in Android App

No comments :


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();
      
    }

}

We will discuss the above concepts in detail in the upcoming posts.


Read More

Sunday 2 April 2017

Solve Couldn't resolve resource @string/your_text warning

1 comment :

resolve-Couldn't-resolve-resource-@string/your_text


Many of the beginners will find this error  Couldn't resolve resource @string/your_text , while you are trying to change the text hello_world in the activity_main.xml in the following line like this:




The actual coding will be like the above image but the beginner will try to change the hello_world like this:


The text may be anything , here i am using Welcome.

the error will come like this :


Why this is error is coming means , the @string/ resembles the string.xml under the values folder under the res folder  which i have discussed in the previous post.

Read here:

Project Structures in Android Eclipse

In order to add a new text we need to add the text in that string.xml folder.

Follow the following steps :

1.Open  res ----> values ----> string.xml 





In this add a new line as follows:

 <string name="welcome">Welcome</string>


Now save both string.xml and activty_main.xml and now see the error will be gone.


Thus the error   Couldn't resolve resource @string/your_text was successfully cleared.

read here:

Hello World app in Android
Read More

Android Project Structure in Eclipse

1 comment :


The Project Structure in Android ADT or Eclipse and also in Android Studio is very important to understand.

We need to be aware what are the all available project files when we create an application.

The Project Structure can be seen on the left side of the Eclipse or Android Studio window.




The Project Structure contains all the java packages, classes, xml files, icons, images which we use in an Android app.

This is the Complete Project Structure.


The Src folder is called as the Source folder. 

This folder contains all the Java Packages and Java Classes that we are creating and using.


The gen folder is called as Generated java files folder.

This Folder contains automatically created java files which contains

1.BuildConfig.java file and
2. R.java files.

In which the R.java file contains the automatically generated java class which contains the automatically generated integer values for every thing you are using in a project.

If you see the image you can see an integer value is generated for @string/hello_world which we have used in the xml file.


we should not modify anything in this R.java class , if you do anything changes in this class it will leads to error.

The next folder is the Android sdk or platform folder which contains android.jar file, we are not going to use this one and the next folder is Android Private Libraries which contains the necessary libraries.

The assets folder consists of static files , we are not going to use this folder also.

The bin folder consists of Compiled classes and executable files.

The next and the most important , which we are going to use in every android application development is the res folder.

The res folder is called as the resource folder.

It contains the Layout folder which will contain the xml file, which will be used to design the User Interface.

Under the res folder there are two main folders need to be known

1.Layout folder and
2. Values folder

the Values folder contains

1. dimens.xml
2. strings.xml
3.styles.xml

the dimens.xml file is used to specify dimensions(size) which can be used through out the developing the application.

the strings.xml file is used to specify every text that we are going to use in our application.

the styles.xml file is used to define some styles which can be used in designing the User Interface.

we will learn about these folders in more detail in the upcoming posts.

See the following image of res folder.

  
The last and the most important file is AndroidManifest.xml file. This file is used to set which activity to be run while the application starts , means that while you are creating an application you will use more than one activity.

An activity is said to be combination of  JAVA class+ XML file, so which activity we should run first we can specify using intent-filter in the AndroidManifest file.


we will learn about the intent-filter later, if you see in this Android Manifest we can give permissions like, if we want access our phone camera , then we need to add camera permissions, like that.

This AndroidManifest.xml file is also used to generate the apk files which we will be going to upload in Google Play Store.

These are the folders in a Project , and we saw what are its usages.

In upcoming post I will post more about these folders.

Read here:

Java Concepts required to develop Android Apps
Read More

Java Concepts required for developing Android apps

No comments :

In the previous posts, we have learned the software setup and emulator creations, which are very mandatory for the Android app development.

In this post i am going to tell about what are the java concepts required to develop android apps.

Yes, the android apps are created using java programming , so we need to learn the some Java Concepts to create Android apps.

The Android app development is done using two things.

1.Java code to implement the app functionality

2. XML code to design the User Interface( UI ).

In java there are lots of concepts but we do not need all the concepts to develop simple android apps.

Following concepts are enough to develop an android app:

A simple Info-graphic:



1. Basic programming Concepts.      ( this is just fundamentals)

2.Data Types 

3.Operators

4.Control Statements(this concept is very important)

 5.OOPS (Object Oriented Programming)

 6. String Handling

 7.Array List 

 8.Basics of Multi threading       (For advanced apps)

9.Basics of Exception Handling (For advanced apps)



If you want to learn java no need of any programming experience before. Just by reading some below post you can easily learn Java.

First you need to be familiar with programming for that purpose read some simple C programming...

1. How to run a C program
2.  How to add 2 numbers in C
3. Decision Control Statements
4. Loop Control Statements

If you are already familiar with this concept just, skip to start learning java.

After becoming familiar with this fundamentals let start to learn about java.

1. How to Run a Java Program
2.How to add 2 numbers in java
3.How to get input from the user
4. Classes and Objects in Java 

If you already know the above concepts just start learning the required concepts.

Practice the above concepts till you become familiar with that.

After that start to learn the Required Java Concepts for developing android applications.

There is a perfect blog to learn all Java concepts that is www.javatpoint.com .

This blog will provide A-Z knowledge in java.

You can learn what you want , try to first complete the above mentioned required java concepts required to develop android apps , then if you want you can learn more.


Read here:

How to develop Android apps






Read More

Saturday 1 April 2017

How to use Android Phone as an Emulator

No comments :
usb-debugging


In the previous post i have told you how to create an Emulator in Android Eclipse, but it is slow compared to USB Debugging.

What is USB Debugging?

Using our smartphones as an Emulator also called as Debugger is called as USB debugging .

It is very simple to use your phone as an Android Emulator.

I have created an Info graphics for the easy understanding.





usb-debugging-infographics
 usb-debugging-infographics
 usb-debugging-infographics

1.Go to settings in your phone

settings

Tap on About Phone option more than 5 to 10 times till a message will come like

YOU ARE AN DEVELOPER NOW!

After this you can see the Developer options above the About phone.

developer-options

Now open the Developer options and Enable it and also enable the USB Debugging option as shown in the image.

usb-debugging

2. Connect your phone to the laptop Using USB.

connect-usb

Then the phone will ask for Trust this Computer are not click yes.

Then after complete the coding go to Run configuration as shown in the image.

run-configurations-in-eclipse

Then click Target tab and enable Always Prompt to pick device  and then click Apply then click Run.

always-prompt-to-pick-device


Then select your phone and click OK.

running-android-device

Then within some seconds or minutes the output will be displayed in your phone.

phone-as-android-emulator

You have successfully used your phone as Emulator.

NOTE: If your Phone is not visible in the Eclipse then try the following options.

1. Reconnect the phone 
2. Download your Phone's Sync Manager.(For Example : My phone is HTC so I have downloaded    HTC SYNC Manager) . 

 Install it and then follow the above procedures from beginning.

It will work definitely.

Now try this:

Hello World app


If you find any difficulty in making your phone as an Emulator mail me your trouble or error i will mail you the solution. narain2829@gmail.com .

Also read:

How to create an Emulator .



Read More

Friday 31 March 2017

How to Create an Emulator in Android Eclipse

No comments :




In the previous post we saw how to create an sample Android app.

 Now we will see how to create an Emulator.

An Emulator is the software which will resemble our Android phone's features in order to run the applications we have created in Android Eclipse or Android Development Tool(ADT).

In Android Studio also we use the Emulator to Run the applications we have created.

Let us see how to create an Emulator also called as Android Virtual Device(AVD).

I have created an Info graphics for the easy understanding.

info-graphics

1. Open Android SDK Manager

 
android-sdk-manager



2. Install the required SDK platform and System Image.

    
sdk-manager

system-image



3. Open Android Virtual Device Manager.


avd-manager




4. Create New AVD

new-avd



5. Enter the AVD name , Target version and device then click OK

create-avd

avd-created


Then after clicking OK, You can start the created AVD or you can select the AVD at the time of running the application, but it is recommended to start before running the application, because the AVD takes more time to open.

So if you start the AVD before starting coding at the time of execution it will be opened so you can get the output fastly.

launch-avd

Then Launch the AVD...

AVD-loasding


It will take some time to load , so minimize it and start coding...

After the AVD or Emulator is opened it will look like..

Emulator

If the above step is followed to create an Emulator mostly the error wont come. If the Emulator is not working properly then close the emulator and re launch it.

We have successfully created the emulator , if you find any difficulties in creating the Emulator mail me to narain2829@gmail.com i will reply you the solution to your queries within a day...

But it is recommended to use our Android Mobile Phone as Emulator since it is faster than the AVD or Emulator.

In next post i will tell how to use our mobile as an Emulator.

Read Here: 

How to use Android Phone as an Emulator



Read More