Monthly Archives: November 2017

View value of Password EditText while a button is pressed

yourButton.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

               switch ( event.getAction() ) {
                case MotionEvent.ACTION_DOWN: 
                   editText.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
                case MotionEvent.ACTION_UP: 
                    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
                }
                return true;
        }
    });

References
https://stackoverflow.com/questions/28185979/view-value-of-password-edittext-while-a-button-is-pressed

Transparent ProgressDialog with ProgressBar

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="net.pupli.lastlab.PupliProgressDialogFragment">

    <android.support.constraint.Guideline
        android:id="@+id/guideline12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <me.zhanghai.android.materialprogressbar.MaterialProgressBar
        android:id="@+id/progressbarMain"
        style="@style/Widget.MaterialProgressBar.ProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        app:layout_constraintEnd_toEndOf="@id/guideline12"
        app:layout_constraintStart_toStartOf="@id/guideline12"
        app:mpb_progressStyle="circular" />

</android.support.constraint.ConstraintLayout>
public class PupliProgressDialogFragment extends DialogFragment {

    MaterialProgressBar progressbarMain;

    public PupliProgressDialogFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_pupli_progress_dialog, container, false);

        // make dialogFragment transparent
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        // remove title of dialog fragment
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        // set dialogFragment non cancelable, so users can not dismiss progressbar from screen
        this.setCancelable(false);

        progressbarMain = view.findViewById(R.id.progressbarMain);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        // set width and hight of dialogFragment
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }
}

Hash String via SHA-256 in Android

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;
import java.math.BigInteger;

public class CryptoHash {
  public static void main( String[] args ) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance( "SHA-256" );
    String text = "Text to hash, cryptographically.";

    // Change this to UTF-16 if needed
    md.update( text.getBytes( StandardCharsets.UTF_8 ) );
    byte[] digest = md.digest();

    String hex = String.format( "%064x", new BigInteger( 1, digest ) );
    System.out.println( hex );
  }
}

References
https://stackoverflow.com/questions/3103652/hash-string-via-sha-256-in-java

Full screen background image in an activity

There are several ways you can do it.

Option 1:

Create different perfect images for different dpi and place them in related drawable folder. Then set

android:background=”@drawable/your_image

Option 2:

Add a single large image. Use FrameLayout. As a first child add an ImageView. Set the following in your ImageView.

android:src="@drawable/your_image"
android:scaleType = "centerCrop"

References
https://stackoverflow.com/questions/16135984/full-screen-background-image-in-an-activity

How to Use CommandLineRunner in Spring Boot Application

package com.therealdanvega;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
 
@Component
public class DataLoader implements CommandLineRunner {
 
    private final Logger logger = LoggerFactory.getLogger(DataLoader.class);
 
    @Override
    public void run(String... strings) throws Exception {
        logger.info("Loading data...");
 
    }
}

References
https://www.quickprogrammingtips.com/spring-boot/how-to-use-commandlinerunner-in-spring-boot-application.html
http://therealdanvega.com/blog/2017/04/07/spring-boot-command-line-runner

Integrate Facebook Login with your Android Application

Follow this QuickStart for Facebook Login for Android
https://developers.facebook.com/docs/facebook-login/android

Link the Facebook SDK

mavenCentral() 
compile 'com.facebook.android:facebook-login:[4,5)'

Edit Your Manifest

Open your /app/src/main/res/values/strings.xml file

<string name="facebook_app_id">id</string>
<string name="fb_login_protocol_scheme">protocol</string>

Open the /app/manifest/AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET"/>

Add the following meta-data element, an activity for Facebook, and an activity and intent filter for Chrome Custom Tabs after the application element. Replace @string/appname with the name of your Facebook App:

<meta-data android:name="com.facebook.sdk.ApplicationId" 
        android:value="@string/facebook_app_id"/>
    
    <activity android:name="com.facebook.FacebookActivity"
        android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name" />
    <activity
        android:name="com.facebook.CustomTabActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="@string/fb_login_protocol_scheme" />
        </intent-filter>
    </activity>

Provide the Development and Release Key Hashes for Your App

Download OpenSSL which is Requisite
https://slproweb.com/products/Win32OpenSSL.html

Add OpenSSL to path, so you can access it from command line

Go to JDK bin folder

cd C:\Program Files\Java\jdk1.8.0_151\bin

Add debug key hash

keytool -exportcert -alias androiddebugkey -keystore C:\Users\Mahmood\.android\debug.keystore | openssl sha1 -binary | openssl base64

Add release key hash

keytool -exportcert -alias lastlab -keystore C:\Projects\lastlab\keys\store.jks | openssl sha1 -binary | openssl base64

Read QuickStart to Add Facebook Custom Login Button

        buttonSignupFacebook = findViewById(R.id.buttonSignupFacebook);

        facebookLoginManager = LoginManager.getInstance();
        facebookCallbackManager = CallbackManager.Factory.create();

        facebookLoginManager.registerCallback(facebookCallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Toast.makeText(SelectSignupActivity.this, loginResult.getAccessToken().toString(),
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {
                Toast.makeText(SelectSignupActivity.this, error.getMessage(),
                        Toast.LENGTH_LONG).show();
                error.printStackTrace();
            }
        });

        buttonSignupFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                facebookLoginManager.logInWithReadPermissions(SelectSignupActivity.this,
                        Arrays.asList("email", "public_profile", "user_birthday"));
            }
        });
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        facebookCallbackManager.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

References
https://developers.facebook.com
https://www.youtube.com/watch?v=SrAXmZkOpJI
https://developers.facebook.com/docs/facebook-login/android
https://stackoverflow.com/questions/16965058/where-is-debug-keystore-in-android-studio
https://developer.android.com/studio/publish/app-signing.html
https://github.com/facebook/facebook-android-sdk
https://developers.facebook.com/docs/facebook-login/permissions/
https://androidammy.blogspot.com/2015/09/facebook-login-with-custom-button.html