Java / Android Developer, Trainer, Writer, Speaker

Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Wednesday, February 18, 2015

On 12:25 PM by Unknown in , ,    19 comments
Today, we are going to learn about Thread in JAVA.



simpleThread.java Class



 public class simpleThread implements Runnable {
 /*
  What is Thread in JAVA Programming? 
  Ans: Thread is a path of execution within a program. Multi Threading is multiple path
  of execution at the same time. Thread is a way, how your computer allows you to do
  multiple things at once. This is the simplest definition of thread.
   
   Now, how we will be working with Thread in practice?
   
   Step 1: Creating a thread object from thread class
   Step 2: Then we will start thread
   Step 3: Just to start, thread will look for RUN method.
   Step 4: While finds run method, the code into the RUN method
   will be executed.
  
   we will be implementing Runnable interface here.
  * */
 
 
 
 public static void main(String[] args) {

  System.out.println("Downloading two files..:");
  // we are creating a thread object below
  Thread t1 = new Thread() {
   /* 
    After t1.start , execution comes here to look for run
    Then the codes in run should be executed.
    
    */
   public void run() {
    for (int k = 0; k <= 20; k++) {
     System.out.println("File 1 downloaded " + k * 5 + "%");
    
     try {
      // Let the thread sleep for 1200 mili seconds.
      sleep(1200);
     } catch (InterruptedException threadExep) {
      threadExep.printStackTrace();
     }
    }

   }

  };
  // now, this will call run method
  t1.start();

  Thread t2 = new Thread() {
   public void run() {
    for (int k = 0; k <= 20; k++) {
     System.out.println("File 2 Downloaded " + k * 5 + "%");

     try {
      sleep(1000);
     } catch (InterruptedException threadExep) {
      threadExep.printStackTrace();
     }
    }

   }

  };

  t2.start();

  
  /*
   Using Runnable Interface(run method must be overridden):
   
   Step 1: We will Create an object of the class. Which is obj1 here.
   Step 2: Create a thread object and pass the class object through it
   Step 3: Start the thread, do operation.
   
   */
  simpleThread obj1 = new simpleThread();
  Thread t3 = new Thread(obj1);
  t3.start();
  
  
  /*
   
    This can be written as: 
    
   Thread t3 = new Thread(new simpleThread());
   t3.start();  
  
   * */
   
 }

 @Override
 public void run() {
  for (int p = 0; p <= 10; p++) {
   System.out.println("This is my T3 thread!");
  
  }
  
 }


}

/*
 
  What we are doing here:
  Creating couple of thread T1, T2. Then both starts and prints a for loop 
  while both sleeps for 1200 and 1000 mili seconds respectively.
 See the outputs!
 */




Output is:


Downloading two files..:
File 1 downloaded 0%
File 2 Downloaded 0%
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
File 2 Downloaded 5%
File 1 downloaded 5%
File 2 Downloaded 10%
File 1 downloaded 10%
File 2 Downloaded 15%
File 1 downloaded 15%
File 2 Downloaded 20%
File 1 downloaded 20%
File 2 Downloaded 25%
File 2 Downloaded 30%
File 1 downloaded 25%
File 2 Downloaded 35%
File 1 downloaded 30%
File 2 Downloaded 40%
File 1 downloaded 35%
File 2 Downloaded 45%
File 1 downloaded 40%
File 2 Downloaded 50%
File 1 downloaded 45%
File 2 Downloaded 55%
File 1 downloaded 50%
File 2 Downloaded 60%
File 2 Downloaded 65%
File 1 downloaded 55%
File 2 Downloaded 70%
File 1 downloaded 60%
File 2 Downloaded 75%
File 1 downloaded 65%
File 2 Downloaded 80%
File 1 downloaded 70%
File 2 Downloaded 85%
File 1 downloaded 75%
File 2 Downloaded 90%
File 2 Downloaded 95%
File 1 downloaded 80%
File 2 Downloaded 100%
File 1 downloaded 85%
File 1 downloaded 90%
File 1 downloaded 95%
File 1 downloaded 100%

Friday, December 26, 2014

Hello Google Play Android Developers! Recently, Google is forcing its developers to use Android Studio rather than using Android Development Tools ( ADT ). We were used to in ADT but now Google thinks it's better to develop in Android Studio. Android Studio came in March 2013. And in one and half years it gets good feedback and Google decides to take application developed in Android Studio for Google Play.

Watch in Video:


In ADT we used to set application icon when we were creating the application but that's not same in Android Studio. You have to set that letter. Point is how? Well, I'll show you!

Read More:  Career in Mobile Application Development for You : My Experience


If you go to AndroidMenifest.xml file you will be able to see something that I pointed with red arrow. We'll have to change that.

Changing Android App icon






Steps:

Step 1: See below Image, and  right click on 'res' of your android project. A new window will come up. Keep your mouse cursor on 'New' and select 'Image Asset' from there.

Android Studio

Step 2: Now, see this below image too. From Asset Studio, click on the three dotted area ( red marked ) and that will let you pick your designed icon for this Application. Then, Click next and do the rest.



Android Studio Developing Environment

Now, after building the application, you'll see the icon you set here.

Hope this helps you.
Subscribe for you!
Share for others!
Happy developing :)

Read More: Open up a Google Play Android Developer Account

Monday, December 15, 2014


Hello developers! Hope you are doing well. Many people are coming to application developing, specially in the Android arena. Now, What! They need a developer account.

How to Open a Android Developer Account?

A lot of people doesn't have Master card of VISA card for opening a account. But, you can easily do that with Payoneer Card.

There is another way out who doesn't have a  Payoneer Card. What's that? That's Neteller. Neteller is a virtual card which can be used for your purpose of opening up a Google Play account. In Neteller virtual card, you can pick any one Master Card, VISA, American Express coping up with your need. You can take real card too if you apply. But for opening up a Google Play account you don't need a card in hand.

At first go to www.Neteller.com and open up a account. For any kind of help / suggestion go here!





You will be given a Secure ID, same that somewhere.
Don't forget to email verify your account. Then, go to your Neteller account. For opening a virtual card, click on Net+ Cards from left side.




Now, click on Add a Virtual Card and create a card for you. Select any of VISA or Master Card. Put your name and save the information. We need those for our Google Play Developer Account. Not clear? See below example:

Cardholder Name: Mosharrof Rubel
Card Name: 1234 6756 3948 230948
CVC: 199
EXP: 1/80

Now, recharge dollar in your account. Recharge at least $30 USD for being safe. You only need $25 for Google Play account. You can buy dollar from your known personal or any where. Careful about cheating.

Now, go here in Google Play Developer Sign Up site and open your account.

Hope this helps you!
Subscribe for You!
Share for others!

Post Credit: Jakir

Monday, December 1, 2014

Hello mobile apps developers! if you are developing Mobile application in Phonegap platform. You might  face a problem in sometime  ' Connection to the server was unsuccessful. (file ///android_asset/www/index.html)  '


Phonegap problem


Well, I have couple of simple solution for you guys on this note.

Solution 1:
This might happen for extended loading time. You can add some time for loading. The problem will be solved. Process of doing that is :

    Just after  super.loadUrl("file:///android_asset/www/index.html");
Add below line:       

super.setIntegerProperty("loadUrlTimeoutValue", 7000);
   
Here, I'm giving 7 seconds to the system for loading the files. You can increase the time. But, users will face some kind of problem here. They will have to wait this amount of time after opening the application. For that I've another solution. I experimented this solution, and this worked nicely. You can give a try too.

Solution 2:
Call a simple HTML file from MainActivity.java and call your ' index.html ' file from that simple HTML file. For that:
Firstly, create a new html file naming rubel.html and paste below codes into that file:

<!doctype html>
<html>
  <head>
   <script>
     window.location='./index.html';
   </script>
</head>
  <body>
  </body>
</html>
Secondly, Rename the code line super.loadUrl("file:///android_asset/www/index.html"); to super.loadUrl("file:///android_asset/www/rubel.html"); from your MainActivity.java File. And keep the rest same!

Now, clean, refresh and build! Hope, this helps you!

Comment if you face any problem!
Subscribe for you!
Share for others!

Sunday, November 16, 2014


Well, one of my friend named OJANA MEGH told me to give some kind of basic Android Application Developing Guideline. So, I decided to write a post in simplest way for understanding for the guys who are new to Android Application Development. I'll offer full source code for you guys too. Here we go:-




Read More: Career in Mobile Application Development for You : My Experience








Tools you need for developing your first android Application:

1. ADT ( Download ADT from here )
2. JRE / JAVA Runtime Environment ( Download from here )

Updates you need to make: 


Open up Eclipse from ADT that you downloaded and extracted. From top menu Click on Menu > Window > Android SDK manager
1. Follow this image: SDK Updates 1
2. Follow this image: SDK Updates 2
Install those whose are installed here. Restart Eclipse.

Work Flow:
1. Go in Eclipse. Press Alt+Shift+N , a window will appear. Click on Android Application Project for new Android Application  Project.
2. Give a Application Name, as I named my app 'Simple App With Rubel'. For this project name your project exactly same as my one. Nothing Else needed. Click Next
3. Click Next Again. No need to do anything.
4. Here you will place icon for your application. Before starting create a icon and set that as your application icon. I placed the carnivalbd icon here. Click next.
5. Click Next here too, no need to do anything again.
6. Click on Finish! Your new project created successfully!



What to do next?
Well, I want a image of mine on my application and some description below. Then, My facebook and twitter link! Ok with you too? Well, after developing the app, it will look like below one:-

My Fitst App

Follow the below steps:

1. Right click on res folder from your project and create a new folder called 'drawable'. In drawable folder, keep your image naming 'kitten'. Keep a facebook icon naming 'fb' and a twitter icon name 'tw'. Not clear? Follow the below screenshot.

Drawable Files Setup






2. Now , place the below codes in your activity_main.xml file. Which is located in Project > res > layout folder.

Location of activity_main.xml file

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.simpleappwithrubel.MainActivity"
    android:background="#2b3c50" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="200dp"
            android:layout_height="200dp"           
            android:src="@drawable/kitten" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My Name is Ojana Megh. And I&apos;m developing my first application with Rubel. Visit the site of Rubel here in http://imRubel.blogspot.com"
            android:textColor="#f9f9f9"
            android:textSize="18dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp" >
       
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginRight="20dp"
            android:onClick="fbc"
            android:src="@drawable/fb" />
            
        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:onClick="twc"
            android:src="@drawable/tw" />
       
       
    </LinearLayout>

</RelativeLayout>




3. Place the below codes in your Project > src > com.example.simpleappwithrubel >  MainActivity.java file:

package com.example.simpleappwithrubel;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity  {

    public ImageButton fb,tw;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
       
        fb = (ImageButton) findViewById (R.id.imageButton1);
        tw = (ImageButton) findViewById (R.id.imageButton2);
       
    }

   
   
    public void fbc(View v) {
           
            Uri uri = Uri.parse("https://facebook.com/rubel007cse");
             Intent intent = new Intent(Intent.ACTION_VIEW, uri);
             startActivity(intent);

    }
   
    public void twc(View v) {
           
            Uri uri = Uri.parse("https://twitter.com/rubel007cse");
             Intent intent = new Intent(Intent.ACTION_VIEW, uri);
             startActivity(intent);

    }
   
   
}

Read More:  Google Adsense – Guideline to Success from my Experience


4. Click on Window from Menu. Then click on Android Virtual device. Click on Device Definition tab. Then, click on Create AVD from the right side. Then, Select and place options as shown in below. Then, click OK OK and come out. ( follow below images )

Select this from device definition




Select yours like this and click ok


5. Right click on your project. Find 'Run As' > 'Android Application'. Then your project will be built up with apk file. Find the apk folder on workspace > your project > bin folder. You can take this apk file to your Android Phone and run this application.


We are done! Happy developing! Happy coding!
Comment if you face any problem!
Subscribe for More!
Share for others!

Read More: 9 Ideas for Career ( Online Money Making ) based on your Love

Monday, November 10, 2014

Well! Hello Android Developers! It’s been amazing what Google doing since its birth! But, it’s also some kind of pain to cope up with its updates and newer things. It’s been painful for some people to set up new Admob for PhoneGap in new updated system. Previously we used to use an Admob jar file, but now system has been changed.  We’ll see this today. The developers who are from web developing background loves to code work with PhoneGap for Android Application development.



For the post, the first recommendation is reading this post: Google Play New Android SDK Setup for AdMob ( AdSense )


In this post I’ll just show the code placement along with file placement. PhoneGap files placement steps:

Step 1: Place cordova.js file in your project folder > assets > www > from workspace.

Step 2: Place XML folder to your project folder > res from workspace.



If you don’t know what I’m talking about, or what is cordova.js file / xml folder, please follow this: http://phonegap.com/install/


Read More: 9 Ideas for Career ( Online Money Making ) based on your Love



Now, The code placement steps:


Step 1: First of all, place the below code in import section, for PhoneGap cordova:

import org.apache.cordova.DroidGap;



Step 2: We need more importing for AdMob Advertisement. Place the below codes in import section too:

import com.google.android.gms.ads.*;



Step 3: Now rename this line ‘public class Second extends Activity ‘ to ‘public class MainActivity extends DroidGap ‘ for using Phonegap



Step 4: Now declare a private variable ‘private AdView adView;’ just before onCreate method.

Step 5: Into onCreate method place this line   super.loadUrl("file:///android_asset/www/index.html"); right after super.onCreate(savedInstanceState);

Step 6: Make the onCreate Method public from protected.



Step 7: Now, place the below codes into onCreate method.

            // Create the adView. [ www.imrubel.blogspot.com ]

            adView = new AdView(this);

            adView.setAdSize(AdSize.BANNER);

            adView.setAdUnitId("ca-app-pub-bbbbb5816378148090/6054xxxxxxxxx");

            LinearLayout layout = super.root;

            layout.addView(adView);

            //  [ www.imrubel.blogspot.com ]

            AdRequest request = new AdRequest.Builder()

          .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)

          .build();         

            //  [ www.imrubel.blogspot.com ]

            // Start loading the ad in the background.

            adView.loadAd(request);


Replace setAdUnitID with your Ad Code.

We are done! Now you can compile and see output. If you are not clear yet, you can follow the below image.



admob ad placement in phonegap for android



After successful placement, Admob add will appear like below image. I took a screenshot for you guys.

PhoneGap Admob add



Read More: R cannot be resolved to a variable error in Android Development and Solution

I hope you did it successfully, leave a comment if you face any problem. Share this post for others then others will share something good for you too. Subscribe for more.

Read More: Career in Mobile Application Development for You : My Experience

Saturday, November 8, 2014



Hello  Android developers! This is one of the common problems in Android Application development. Just previous day, while I was developing 7 March Speech Application, I faced this problem for a awkward reason. Normally, this is not a big problem but this hurts. This could be happening for few reasons. I'll tell about the problems and solution as well as.
Basically, when you start and open a project java automatically generates a folder file called 'gen'. After your build there are a file generated called R.java , but if your have error in somewhere that couldn't be generated and problem appears.

R.JAVA file


Read More: Google Play New Android SDK Setup for AdMob ( AdSense )


Point 1: First of all, this happens mostly if you have an error in your any of XML file. So, first check your XML files ( could be in layout folder, because you edit that most ) and solve the red pointed error. Solve the error the problem will be gone.

Point 2: Sometimes, after fixing error, this could show up again and again. Just go to Project > clean project and clear the project. You can restart your ADT ( Android development Tools )

Point 3: In my previous project, I named 'if' an image and kept it to my resources folder. The problem R cannot be resolved to a variable type appeared for that! As soon as i renamed the file, the problem went away. Clean and build again.

Point 4: Name your files careful, like your new XML file, new image. This error could be appearing for that too. Write your resource names in lower case, make sure those are not conflicting with anything. You won't find R cannot be resolved to a variable type anymore.


Read More: Career in Mobile Application Development for You : My Experience


Point 5: Make sure your are working with latest SDK  ( Software Development Kit ). Go to menu and click on SDK manager. Check if any update are available. If available, make sure you updates those. That could be a reason for this error too.

Point 6: Make sure you ADT (  Android Development  Tools ) are latest and updated. This could help the error to come up.

Point 7: Right click on your project. Go to Java Build Path. Then, in Order and Export tab look for your latest Android version ( Ex: Android 5.0 ), check that and click OK. Then from your

Point 8: After making changes don't forget to clean your project and then build.


Those are Eight points with eight various problems. You could create a  new one and face this problem! Just kidding g :P If you find anything new on this type problem, don’t forget to leave a comment. Share this post for others. Others will help you too.

Happy Programming, Happy developing!

Read More: 9 Ideas for Career ( Online Money Making ) based on your Love

Wednesday, October 22, 2014

Hello Mobile Application developers! Ya, Google has moved to a change and we developers will have to cope up with that change. To set up Admob in new updated SDK , we need to do some tasks. Few of them are tricky, so be with me and follow my instruction. I faced some problems and that ruined my few days to fix it. If you follow this , I hope you won't have to face the same way.

Previously, we used to use GoogleAdMobAdsSdk-6.4.1.jar in our application for monetization. But now we have to go up. Now, we don't need any .jar file but update our SDK. I would suggest to download a new one.





So, Let's start:

Task 1: Download the fresh and new Android SDK from Here. Make sure, you are using a computer with 64 bit, otherwise it won't show up the Android Virtual Device  ( AVD  ).

Now you need to make some updates.

Task 2: Open up eclipse from the Android SDK and go to Window > Android SDK Manager. Or, click on the icon shown in below image.

Android SDK Manager
   A new window will show up SDK. Here we will make some updates. Look at the below images, and check those ( Which are installed in below images ) on your Android SDK Manager and click on Install x packages after accepting license agreement.

You need to install those Installed Packages

You need to install those Installed Packages




After installation, restart Eclipse. Now, we are done with installation.

Task 3: Now, go to your ADT bundle package on your PC and we need to copy a folder from there. Go to, ADT Bundle > SDK > Extras > Google > google_pay_services_libproject . Look at the file path at the below image. Copy the folder name google-play-services_lib.


See the file path and Copy this folder


Task 4: Done with copying? Now , we need to paste this folder on the project you are doing or the Application you are developing. Go to Eclipse and in project explorer paste this folder in to your project > libs. Now clear? Follow the below image.
Paste the folder into libs




Done with pasting? I guess thats easy! :3

Task 5: Now, we need to import that file. Look on the top left corner on your eclipse, click on File > Import > Android > Existing Android Code into Workspace . You need to import your full libs folder. Through this window, go to Workspace > Your Project > Libs . Select that and the click OK. Not clear yet? See, below screen shots.





After clicking on libs, click OK




Task 6: This very simple. Just check the imported file and click on finish. we are almost done! Below image will help you more,

See the Root Directory Path

Now, if the Imported file show an error and ask for number of SDK in L ( Android Lollipop  ) preview, go there and make it L in the place of 8. Like below image:-

Buddy, Make it L , replace 8



Task 7: Now, go to your AndroidManifest.xml file, We need to add some permission for showing admob Ads. Add bellow codes after <application />

<meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
Then, add another activity code, just after the first activity. Not clear? just copy below codes as the image suggests place:

<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|
screenSize|smallestScreenSize"
/>





Task 8:  Now, we will place the add codes in our main xml file. Open up , res/layout/activity_main.xml. See below image. Place <com.google.android.gms.ads.AdView> xml tag as show below and replace, adUnitId with your adUnitId , generated from admob.com

go to admob.com and generate an add, get adUnitId



Task 9: You XML might be showing some errors till now. Add xmlns:ads = "http..." line on the <ScrllView> or <RelativeLayout> tag. The errors will be gone!

Add xmlns:adds line


Task 10: Go to the MainActivity.java file. And couple of imports at the top :

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

Now, add couple more lines  into onCreate method:

AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);


Well, I guess we are done! :D

There the ads should be shown up! Like the below image: -

Ads are appearing!


 Task 11: If yet not shown, you can go right click on Your Project > Android and select things like below image:


I hope no more error left! If you find more errors, go die! Just kidding, you can leave a comment below, I'll try to solve!

If the post helps you, comment, share for others, subscribe for you and buy me a MacBook Air :P

Read More:  Career in Mobile Application Development for You : My Experience