Wednesday, February 19, 2020

Convert Multiple Date Format by Single Method #android #java #date

Create a method in a Global Class From where you can access this method in every class in Android(Java). 

public static String convertDateFormat(String inputFormat, String outPutFormat) {

List<SimpleDateFormat> knownPatterns = new ArrayList<SimpleDateFormat>();
knownPatterns.add(new SimpleDateFormat("dd/MM/yyyy"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd HH:MM:SS"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
DateFormat outputFormat = new SimpleDateFormat(outPutFormat);
Date date = null;
for (int i = 0; i < knownPatterns.size(); i++) {
try {
date = knownPatterns.get(i).parse(inputFormat);
} catch (ParseException e) {
e.printStackTrace();
}
}
return outputFormat.format(date);
}


As I have Created method convertDateFormat in Java class Utility.java as shown above.

Now Call this method in the main class where you want to format date

Utility.convertDateFormat(userProfile.getDateOfBirth(), "dd/MM/yyyy")

Now, here  Utility is java class where i have made method and i am calling that method in class where i want to convert Date Format.

Note:-where (userProfile.getDateOfBirth()) is input date which we want to convert and ( "dd/MM/yyyy") is output format date which you can give any format which is shown in method above.for another format which is not in method you can add that format in method.

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.

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