Tuesday, November 10, 2015

Android Studio Shortcuts for Mac



 Color Picker
 shift + cmd + a (search color picker)
 Recent Files
 cmd + e
 Open class/file
 cmd + o
 Last edit Location
 cmd + shift + backspace
 Goto Declaration
 cmd + b
 Comment line
 cmd + \
Hide All Panels
fn + cmd + shift + f12
Switch tab
control + tab

Close structure panel
cmd  + 1
Code Format
cmd + opt + l
Search  files
shift + shift
Override method
cmd + shift + a
View switch
control +  `
for loop
fori + enter





Shortcuts:
https://stanfy.com/blog/use-android-studio-like-a-pro/?utm_source=reddit&utm_medium=referral




Reference:
http://www.developerphil.com/android-studio-tips-of-the-day-roundup-5/

http://javapapers.com/wp-content/uploads/2014/08/Android-Studio-Shortcuts-You-Need-the-Most.pdf




https://www.codementor.io/free
Codementor

Saturday, October 24, 2015

Android Tutorial: Share with Facebook, and with other apps.


Android: Share with Facebook

Facebook doesn't work well with normal sharing intents when sharing multiple content elements as discussed in this bug. To share posts with facebook, we need to:
  1. Create a new Facebook app here (follow the instructions)
  2. Add the Facebook SDK to your Android project:
SetUp:
a. App/build.gradle (add below)
repositories { mavenCentral() }

dependencies {
     
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'   
}

b. Add you Facebook app id to strings.xml
<string name="facebook_app_id">1698838388383</string>

c. Add Facebook activity to AndroidManifest.xml, also add <meta-data AppID />
Activity:
<!-- Share to Facebook -->
<activity android:name="com.facebook.FacebookActivity"></activity>
    Meta-data:
    <meta-data    
    android:name="com.facebook.sdk.ApplicationId"    
    android:value="@string/facebook_app_id" />

    b. Add you Facebook Library to your AndroidStudio project.
    1. Download SDK from here: Facebook SDK
    2. unzip
    3. On Android Studio go to: 

    •  File -> New -> New Module
    • Select "Import .JAR/.AAR package"
    • Go to your unzip Facebook SDK, and select facebook-android-sdk-4.7.0.aar

    • Click next. Done :)



    Share from Menu bar:
    ShareFB.java , menu_shareFB.xml
    a. edit menu_shareFB.xml:
    <menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto">
        <item        android:id="@+id/action_share"        android:icon="@android:drawable/ic_menu_share"        android:title="Some title"        app:showAsAction="always"        >
            <menu>            <item                android:id="@+id/action_share_Facebook"                android:title="Share with Facebook"/>            <item                android:id="@+id/action_share_other_Apps"                android:title="Share with other Apps"/>
            </menu>
        </item></menu>

    b. Now edit ShareFB.java:
    @Overridepublic boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_campaign_detail, menu);    return true;}
    @Overridepublic boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
    
            case R.id.action_share_Facebook:
            {
                setupFacebookShareIntent();            Toast.makeText(getApplicationContext(), "here facebok", Toast.LENGTH_LONG).show();        }
            break;      //  case  R.id.action_share:        case R.id.action_share_other_Apps:
            {
                setupShareIntent();            Toast.makeText(getApplicationContext(), "here 2", Toast.LENGTH_LONG).show();        }
            break;    }
        return super.onOptionsItemSelected(item);}
    
    //Share with Facebookpublic void setupFacebookShareIntent() {
        ShareDialog shareDialog;    FacebookSdk.sdkInitialize(getApplicationContext());    shareDialog = new ShareDialog(this);
        ShareLinkContent linkContent = new ShareLinkContent.Builder()
                .setContentTitle("SumOfUs")
                .setContentDescription(
                        "\"Title Of Test Post\"")
                .setContentUrl(Uri.parse("http://www.sumofus.org"))
                .build();
        shareDialog.show(linkContent);
        //MessageDialog.show(this, linkContent);}
    
    
    
    // Gets the image URI and setup the associated share intent to hook into the provider
    public void setupShareIntent() {
        // Fetch Bitmap Uri locally    ImageView ivImage = (ImageView)findViewById(R.id.ivCampaign);    // Get access to the URI for the bitmap    Uri bmpUri = getLocalBitmapUri(ivImage);    if (bmpUri != null) {
            // Construct a ShareIntent with link to image        Intent shareIntent = new Intent();        shareIntent.setAction(Intent.ACTION_SEND);        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);        shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Title Of Test Post");        shareIntent.putExtra(Intent.EXTRA_TEXT, "https://www.sumofus.org");        shareIntent.setType("image/*");        // Launch sharing dialog for image        startActivity(Intent.createChooser(shareIntent, "send"));    } else {
            Toast.makeText(this, "Some error occured during sharing", Toast.LENGTH_LONG).show();
        }
    
    }
    
    // Returns the URI path to the Bitmap displayed in specified ImageView
    public Uri getLocalBitmapUri(ImageView imageView) {
        // Extract Bitmap from ImageView drawable    Drawable drawable = imageView.getDrawable();    Bitmap bmp = null;    if (drawable instanceof BitmapDrawable){
            bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();    } else {
            return null;    }
    
        // Store image to default external storage directory    Uri bmpUri = null;    try {
            File file =  new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");        file.getParentFile().mkdirs();        FileOutputStream out = new FileOutputStream(file);        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);        out.close();        bmpUri = Uri.fromFile(file);    } catch (IOException e) {
            e.printStackTrace();    }
        return bmpUri;
    }


    TODO:
    add github repo here:

    Reference:

    1. Share using this code snippet:
     public void setupFacebookShareIntent() {
          ShareDialog shareDialog;
          FacebookSdk.sdkInitialize(getApplicationContext());
          shareDialog = new ShareDialog(this);
    
          ShareLinkContent linkContent = new ShareLinkContent.Builder()
                  .setContentTitle("Title")
                  .setContentDescription(
                          "\"Body Of Test Post\"")
                  .setContentUrl(Uri.parse("http://someurl.com/here"))
                  .build();
          
          shareDialog.show(linkContent);
      }

    http://guides.codepath.com/android/Sharing-Content-with-Intents#share-in-facebook

    Sunday, October 4, 2015

    Android webView Tutorial

    Basic app the opens a webView.

    Main.java
    webView = (WebView) findViewById(R.id.webview);webView.getSettings().setJavaScriptEnabled(true);webView.setWebViewClient(new MyBrowser());webView.loadUrl("https://github.com/login");

    activity_main.xml
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/webview"    android:layout_marginTop="60dp"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:focusable="true"  />

    Add Internet permission to Manifest.
    <uses-permission android:name="android.permission.INTERNET" />

    Done :-)

    Download code here: https://github.com/AndreSand/NoteGit

    Saturday, July 11, 2015

    Git Tutorials

    1. Read Git ebook
    2. Install Git GUI clients. (I use SourceTree)
    3. Have fun with Git

    Note: SourceTree makes you live easier and you don't have to remember git commands.

    Create a Repo:
    https://help.github.com/articles/create-a-repo/

    Must use tool: SourceTree
    SourceTree tutorial

    Do a Commit: push your code to github

    1.  From GitHub create new repo. It will give you a url: www.github.XYZ.MyNewRepo.git
    2. Go to the directory you want to push to git; $cd mydirX
    $git init
    $git add README.md
    $git commit -m "first commit"
    $git remote add origin https://github.com/AndreSand/gamma-android-app.git
    $git push -u origin master

    Now go to your new repo and you will see your code in there.

    How to Marge Code:

    .5 create new folder SOU
    1.  Clone team Repo: /SOU$git clone https://github.com/Org/MyApp.git
    2.  (Github web) Create fork: https://github.com/xxxx/MyApp.git
    3.  $git remote -v 
    4.  change pull origin to myFork url $git remote set-url --push origin https://github.com/xx/MyApp.git
    5.  open app in AndroidStudio.
    6.  make changes, 
    a. $git add . (add your files to the git repo)
    b. $git commit -m "First commit"

    c. $git push origin
    7.  make "Pull" request from GitHub fork (website my fork)

    8.  merge (website)

    Useful Commands:

    Remove your uncommitted changes:
    $git reset --hard to remove your uncommitted changes
    $git pull

    Show origin:
    $git remote show origin
    $git remote -v

    Remove origin:
    $git remote set-url origin git://new.url.here
    OR
    $git remote remove origin
    $git remote rm origin

    Change Fetch URL:
    git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git

    Remove commits:

    $git reset --hard origin/master

     Git log and show code commits:
    $git log branch (will return a list of the commits)

    $git show #commit_Number#


    GIT branch: switching between branches:

    Current branch: $git branch

    $git fetch
    $git checkout test (branch name test)

    To fetch a branch, you simply need to:
    $git fetch origin
    This will fetch all of the remote branches for you. You can see the branches available for checkout with:
     $git branch -v -a
    With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:
    $git checkout -b test origin/test

    Git Log info

    Command to print the commit Comment.
    $git log --oneline -3
    Prints commit# and commit comment


    To get more info about the commit you can do $git show (Commit#)
    $git show <commit#>


    Reference:

    http://zackperdue.com/tutorials/super-useful-need-to-know-git-commands
    http://www.gitguys.com/topics/temporarily-stashing-your-work
    http://stackoverflow.com/questions/16330404/how-to-remove-remote-origin-from-git-repo
    https://help.github.com/articles/changing-a-remote-s-url/
    http://stackoverflow.com/questions/1783405/checkout-remote-git-branch