Thursday, November 21, 2019

Common Hide Keyboard in an application

public static void hideKeyboard(Context context) {
        try {
            InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {
            Log.e(TAG, "Sigh, can't even hide keyboard " + e.getMessage());
        }
    }

Saturday, November 9, 2019

Sending data from one activity to other using Intent



To send data from activity one


                Intent intent=new Intent(this,nextactivity.class);
                intent.putExtra("key ",value);
                context.startActivity(intent);

 To receive data in activity two

Intent intent=getIntent().getextras();

String s =intent.getstring("key provided in class one");


Showing pdf in a webview without downloading


import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.renderscript.Sampler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.amazonaws.Request;

import javax.security.auth.callback.Callback;

public class WebViewActivity extends AppCompatActivity {
    String url;
    WebView webView;
    ProgressDialog progressBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_pdf);
        url = getIntent().getStringExtra("url");
        webView = findViewById(R.id.webViewpdf);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        progressBar=ProgressDialog.show(this,"","loading");

//"https://docs.google.com/gview?embedded=true&url="  this is the online google viewer

        webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + url);


        webView.setWebViewClient(new WebViewClient() {

// this is used for showing the progress until the page stops loading
            public void onPageFinished(WebView view, String url) {
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }
        });


    }
}

Round image corners at runtime using Glide #android #Glide


Round image corners at runtime using Glide library

In this post, we will check how to make Image corners round at runtime.
               

 RequestOptions requestOptions = new RequestOptions();
   requestOptions = requestOptions.transforms(new CenterCrop(),newRoundedCornersTransformation(39,0,RoundedCornersTransformation.CornerType.TOP_RIGHT));
              
  Glide.with(this)
                        .load(userData.getCoverpicture())
                        .apply(requestOptions)
                        .into(coverpic);

Sending data from one activity to other using bundle

Send data from  Activity

Intent intent =new Intent(this,nextactvitiy.class);
Bundle bundle=new Bundle();
bundle.putString("key",value);
bundle.putString("another key",another value);
intent.putExtras(bundle);
startactivity(intent);


Get data in another Activity

Bundle bundle=getIntent().getExtras();
String s =bundle.getString("key");
String s2=bundle.getString("key2");

Friday, November 1, 2019

Comparing Two dates in Android

 SimpleDateFormat  simpledateformat  = new SimpleDateFormat("yyyy-MM-dd"); 
        Date d1 = simpledateformat  .parse("2018-03-31"); 
        Date d2 = simpledateformat  .parse("2012-03-31"); 
  
        if (d1before  d2) { 
  
            System.out.println("Date1 is before Date2"); 
        } 
  
        else if (d1 after d2) { 
              System.out.println("Date1 is afterrDate2"); 
  
        }



Kindly note:The date formats of both the dates must be same.

Tuesday, October 29, 2019

CardView in android

Cardview is another material added for designing a layout.It resprents your designin a card manner with the corner radius and  drop elevation (shadow).

Steps to Add CardView in your android studio:

1. Adding Design Dependency

       Build Gradle (Module:App)


dependencies {

implementation 'com.android.support:design:28.0.0'

}


2. Add the <android.support.v7.widget.CardView> widget to your layout and place UI widgets like TextViews, ImageViews inside it.


activity_main.xml

<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="match_parent"  
  android:background="@drawable/login_back"  
  tools:context=".LoginActivity">

    <android.support.v7.widget.CardView   
     android:layout_width="match_parent"   
     android:layout_height="wrap_content"    
    android:layout_margin="20dp"      
  android:background="@drawable/ic_cardview_corner"  
      app:cardCornerRadius="15dp"      
  app:cardElevation="20dp"     
   app:layout_constraintBottom_toBottomOf="parent"   
     app:layout_constraintLeft_toLeftOf="parent"     
   app:layout_constraintRight_toRightOf="parent"    
    app:layout_constraintTop_toTopOf="parent">


        <LinearLayout          
  android:id="@+id/linear_login"     
  android:layout_width="match_parent"  
  android:layout_height="wrap_content"  
  android:orientation="vertical">


 <android.support.design.widget.TextInputLayout     
           android:id="@+id/text_Username"       
         android:layout_width="match_parent"          
      android:layout_height="wrap_content">


                <EditText     
               android:id="@+id/username_l"  
                  android:layout_width="match_parent"    
                android:layout_height="wrap_content"     
               android:hint="Username"                
    android:textColorHint="#fff" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout      
          android:id="@+id/text_password"             
   android:layout_width="match_parent"              
  android:layout_height="wrap_content">


                <EditText                  
      android:id="@+id/password_l"              
      android:layout_width="match_parent"             
      android:layout_height="wrap_content"              
      android:hint="Password"                  
      android:textColorHint="#fff" />
            </android.support.design.widget.TextInputLayout>

            <Button                
   android:id="@+id/login"             
   android:layout_width="match_parent"         
   android:layout_height="wrap_content"
    android:text="Login" />

            <Button        
        android:id="@+id/newuser"    
        android:layout_width="match_parent"   
       android:layout_height="wrap_content"         
       android:text="Create New User" />

        </LinearLayout>
    </android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>








3.Adding Boundary line to the CardView                                                                                             


Drawable                                                                                                                                

ic_caredview_boundary.xml                                                                                                                     

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <stroke    
    android:width="2dp"
    android:color="#fff">
</stroke>

</shape>                                                                                                                           






Alert Dialog Box

Alert Dialog Box Android AlertDialog   can be used to display the dialog message with OK and Cancel buttons. It can be used to inter...