Thursday, October 27, 2016

iPhone - First App (Happy Diwali) / Check u r Eligibility to play with Crackers ...

Dear All....

Are you crazy for iPhone Development .......................
OOOKKkkk.......   So  let's start something new on this Diwali .........

As you know my way ....... i m always adding the stuff in which code is more......
Just for your info.........
I m using Xcode 7.3 as a IDE and Swift as a programming language.
I would like to suggest you to prefer Apple machine to develop App for iPhone.
If you r using Window OS then You may be stuck due to VMWare and can't enjoy the real feel ...
But it's OK with Window OS also... but u should have patience......
So.... Good Luck ....

Few steps are.....

Step - 1. Launch your Xcode
Step - 2. Create a new project with Single View App options
Step - 3. Design whatever you want to add as a UI Component on Main.storyboard
Step - 4. Add All labels , Buttons with the Controller file which is ViewController.swift
Step - 5. Add Appropriate code in this controller file.
Step - 6 . Select Emulator as you wish and run the app.

See My First App with the following functionalities....
1. Diwali Greetings by Text Message
2. Image Addition
3. Just to take input from user i added a TextField to insert value
4. Also added a button to add event
5. Just twice you entered value and display a message for fun........

A. Output will be like .....

1.

2.

3.

B. Swift code is as

import UIKit

class ViewController: UIViewController {

    @IBOutlet var label: UILabel!
    @IBOutlet var msg: UILabel!
    @IBOutlet var textField: UITextField!
    @IBAction func submit(sender: AnyObject) {
     
        var data = Int(textField.text!)!
        
        data = data * 2
        
        if data>=18
        {
        msg.text = "You can Play with Crackers..."
        }
        else
        {
            msg.text = "Not Eligible"
            
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}



  

Sunday, October 16, 2016

Social Integration (FaceBook) in your App- Part II

Hi..... Dear All ....  Hope you know how to integrate FaceBook in your Android App. if not please visit
http://androidclue4u.blogspot.in/2016/08/social-integration-facebook-in-your-app.html

know in this post i am carrying the same example which is done on mentioned link.
Know i am trying to make it more realistic .... Like
@ How you can retrieve Profile pic from FB account
@ How you can get user information   Name   , First Name , Last Name from FB a/c

For this i have added two new classes like ProfileTracker and AccessTokenTracker 
Another important component is Android Query (aquery).
I had change activity_main layout also .... now the output will be like
A ... Output
a.


b.

c.

B. Layout file is as

<?xml version="1.0" encoding="utf-8"?>

<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"

      tools:context="com.exam.ravi.socialintegration.MainActivity" >

    <com.facebook.login.widget.LoginButton

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/loginFB"

        android:layout_alignParentBottom="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:layout_marginBottom="137dp" />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="20dp"

        android:text="New Text"

        android:id="@+id/name"
        android:layout_centerVertical="true"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true"

        android:textAlignment="center" />

    <ImageView        android:layout_width="200dp"

        android:layout_height="200dp"

        android:id="@+id/imageView"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true" />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="20dp"

        android:text="New Text"

        android:id="@+id/fname"

        android:layout_below="@+id/name"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:textAlignment="center" />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="20dp"

        android:text="New Text"

        android:id="@+id/lname"

        android:layout_below="@+id/fname"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:textAlignment="center" />

</RelativeLayout>

C. Java code is as

package com.exam.ravi.socialintegration;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class MainActivity extends AppCompatActivity {
    TextView name,fname,lname;
    LoginButton loginButton;
    CallbackManager callbackManager;
    private ProfileTracker profileTracker;
    private AccessTokenTracker accessTokenTracker;
    AQuery aQuery;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        callbackManager = CallbackManager.Factory.create();
        aQuery = new AQuery(this);
        loginButton = (LoginButton) findViewById(R.id.loginFB);
        loginButton.setReadPermissions("public_profile");
        name = (TextView) findViewById(R.id.name);
        fname = (TextView) findViewById(R.id.fname);
        lname = (TextView) findViewById(R.id.lname);
        loginButton.registerCallback(callbackManager,
                              new FacebookCallback<LoginResult>() {
            @Override

            public void onSuccess(LoginResult loginResult) {

               Profile profile = Profile.getCurrentProfile();
                if(profile==null)
                {
                    profileTracker = new ProfileTracker() {
                        @Override

                        protected void onCurrentProfileChanged(Profile oldProfile, 
                                                    Profile currentProfile) {
                      if(currentProfile!=null) {
                          showProfile(currentProfile);
                          Log.v("Ravi", "Logging Current Profile");
                      }
                      if(oldProfile!=null)
                      {
                          showProfile(oldProfile);
                          Log.v("Ravi", "Logging Old Profile");
                      }
                        }
                    };

                    accessTokenTracker = new AccessTokenTracker() {
                        @Override

                        protected void onCurrentAccessTokenChanged
                         (AccessToken oldAccessToken,AccessToken currentAccessToken) 
                        {
                            Toast.makeText(MainActivity.this,"Token Changed",
                                                      Toast.LENGTH_LONG).show();
                        }
                    };
                    accessTokenTracker.startTracking();
                    profileTracker.startTracking();
                }
                else                {
                    showProfile(profile);
                }
           
            }

            @Override            public void onCancel() {
                Log.v("Ravi", "User Cancelled");
            }

            @Override            public void onError(FacebookException error) {
                Log.v("Ravi", "Something Wrong");
            }
        });
    }
    public  void showProfile(Profile recProfile)
    {
        name.setText("Name is =  " + recProfile.getName());
        fname.setText( " First Name = " + recProfile.getFirstName());
        lname.setText("Last Name = " + recProfile.getLastName());
        String image_string = recProfile.getProfilePictureUri(200,200).toString();
        aQuery.id(R.id.imageView).image(image_string);

    }
    @Override 

    protected void onStop() {
        super.onStop();
        if(profileTracker!=null)
            profileTracker.stopTracking();
        if(accessTokenTracker!=null)
            accessTokenTracker.startTracking();
    }

    @Override 

   protected void onActivityResult( int req, int res, Intent intnt)
        {
            callbackManager.onActivityResult(req,res,intnt);
        }

}
    

Tuesday, October 4, 2016

Update UI Thread by Worker Thread using Handler and Messages

Hi .... Dear All ....  Today I am uploading a superb example of Multi-threading. 
In this example i have one Main Thread / UI Thread and a worker thread.

Worker thread updates the status on UI thread by using Handler and Messenger.

Handler ???? 

 Handler is class it's  object is associated  with the thread in which it is created. 
 It provides a way to send data to this thread.
 A Handler is particular useful if you  want to post multiple times data to the main thread.

Message 

•       A message is an object that contains a description and arbitrary data object that can be sent to a Handler.
•       Create by calling Message.obtain() or Handler.obtainMessage() methods.
See the example
A. Output will be like this 

B. Layout files are 
<?xml version="1.0" encoding="utf-8"?>

<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"

     tools:context="com.exam.ravi.servicedemo.MessangerHandler">
    <ProgressBar

        style="?android:attr/progressBarStyleHorizontal"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/progressBar"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true"

        android:layout_alignParentStart="true"

        android:layout_alignParentEnd="true"

        android:indeterminate="false"

        android:max="100"

        android:progress="0"/>

    <TextView

        android:text="TextView"

        android:textSize="20dp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/textView"

        android:layout_below="@+id/progressBar"

        android:layout_centerHorizontal="true" />
</RelativeLayout>
C. Java code is as 
package com.exam.ravi.servicedemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MessangerHandler extends AppCompatActivity {

    Thread thread;
    Handler handler;
    ProgressBar progressBar;
    TextView textView;

    @Override
          protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_messanger);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        textView = (TextView) findViewById(R.id.textView);
        thread = new Thread(new MyThread());
        thread.start();
        handler = new Handler() {
            @Override            public void handleMessage(Message rcvmessage) {
                super.handleMessage(rcvmessage);
                progressBar.setProgress(rcvmessage.arg1);
                textView.setText((String.valueOf(rcvmessage.arg1)) 
                                                  + "% Completed ");

            }     };    }

    class MyThread implements Runnable {

        @Override        public void run() {

            for (int i = 1; i <= 100; i++) {
                Message sendmsg = Message.obtain();
                sendmsg.arg1 = i;
                handler.sendMessage(sendmsg);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
          }
}