Saturday, November 9, 2019

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>                                                                                                                           






Sunday, October 20, 2019

Multiple File Picker in android

Multiple File Picker Library ,with this a user can pick multiple files for uploading to the server using retrofit or volley..


Dependency used for File Picker

implementation 'com.github.jaiselrahman:FilePicker:1.2.2'



Java File



package com.example.filepicker;

import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jaiselrahman.filepicker.activity.FilePickerActivity;
import com.jaiselrahman.filepicker.config.Configurations;
import com.jaiselrahman.filepicker.model.MediaFile;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<CustomMediaFile> imageArrayList;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==123 && resultCode==RESULT_OK){
            ArrayList<MediaFile> files = data.getParcelableArrayListExtra(FilePickerActivity.MEDIA_FILES);
            //Do something with files
            for (int i=0; i<files.size();i++){
                Log.d("MyPaths",files.get(i).getPath());
            }

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickFiles();
            }
        });

    }
    public void pickFiles(){
        imageArrayList = new ArrayList<>();
        Intent filePickerIntent = new Intent(MainActivity.this, FilePickerActivity.class);
        filePickerIntent.putExtra(FilePickerActivity.CONFIGS, new Configurations.Builder()
                .setCheckPermission(true)
                .setMaxSelection(1)
                .setShowImages(false)
                .enableImageCapture(false)
                .setShowVideos(false)
                .setShowAudios(false)
                .setShowFiles(true)
                .setSuffixes(new String[]{"pdf", "doc", "xls"})
                .build());
        startActivityForResult(filePickerIntent, 123);
    }
}

One ProgressBar for whole Project .

ProgressBar can be used in whole Project by creating a single class.THis is demonstrated By the following code...


XML for ProgressBar...

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"   
 tools:context=".MainActivity">

    <ProgressBar       
 android:layout_width="wrap_content"       
 android:layout_height="wrap_content"      
 android:id="@+id/progressbar"      
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"     
 app:layout_constraintRight_toRightOf="parent"       
 app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>



Java File


package com.a.progressbaee;

import android.app.Dialog;
import android.content.Context;

public class ProgressBar extends Dialog {
    public ProgressBar(Context context) {
        super(context);
        setContentView(R.layout.activity_main);

    }

    @Override    public void show() {
        super.show();
    }

    @Override    public void hide() {
        super.hide();
    }

    @Override    public void dismiss() {
        super.dismiss();
    }

    @Override    public void cancel() {
        super.cancel();
    }
}



This can be called in other classes By just 

providing the progressbar class name and creating its object..

ProgressBar progressbar;


TO show progressbar

progressbar.show();


To hide the progressbar

progressbar.hide();

To dismiss the Progrressbar

progressbar.dismiss();

To cancel the progressbar

progressbar.cancel();




Tuesday, July 30, 2019

Volley for loading data

Volley is a REST  network library which is  used to send and get data from the server we need to do json parsing to fetch data from the server below s the example for the same.


User permission

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

Dependencies

implementation 'com.android.volley:volley:1.1.0'

Java class

package com.vishalsingh.practice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class VolleyActivity extends AppCompatActivity {
    TextView textView;
    Button button;
    String url="your url ";
    ImageView imageView;
    String Imageurl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volley);

        textView=findViewById(R.id.textview);
        button=findViewById(R.id.button);
        imageView=findViewById(R.id.Imageview);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringRequest sr=new StringRequest(1, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jo=new JSONObject(response);
                            JSONArray jo1=jo.getJSONArray("Response");
                            JSONObject jo2=jo1.getJSONObject(0);
                            String data1=jo2.getString("Name");
                            Imageurl=jo2.getString("Image");
                            Picasso.get().load(Imageurl).into(imageView);
                            textView.setText(data1+"\n");



                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }){
                    @Override
                    public   Map<String,String> getParams(){
                        Map<String, String> params = new HashMap<>();
                        params.put("UserId", "0d770e2b-eeff-4652-8282-5c756941d280");
                        return params;
                }};
                RequestQueue rq= Volley.newRequestQueue(VolleyActivity.this);
                rq.add(sr);
            }
        });


    }
}

Xml file

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".VolleyActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="fetch"
        app:layout_constraintTop_toTopOf="parent"/>
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:id="@+id/Imageview"

        app:layout_constraintTop_toBottomOf="@+id/button"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textview"
        app:layout_constraintTop_toBottomOf="@+id/Imageview"/>

</android.support.constraint.ConstraintLayout>

Sample Json

{
"Status":true,
"Error":
{
"Code":0,
"Message":""
},
"Response":
[{"Name":"Shubham Shrivastava",
"Image":"http://103.115.194.111:906/Readers/uploads/Screenshot_20190424-202039.jpg",
"Gender":"Mr.",
"DateOfBirth":"26 Dec 2018",
"Address":"Noida",
"Country":"INDIA",
"PinCode":"485001",
"EmailId":"shri.shubham86@gmail.com",
"MobileNo":"9074762379",
"isPartner":"No",
"City":"",
"State":"Madhya Pradesh",
"Booth":"Other",
"Constituency":"Satna",
"Assembly":"Satna",
"Citizenship":null}
]
}

Monday, June 24, 2019

Soap in android #android #soap protocol #xml

Today I will explain to you how to load data using soap. Soap is not much used today but it has its own features like security so it is used even today

Points to Note
SOAP is a communication protocol designed to communicate via Internet.

SOAP can extend HTTP for XML messaging.

SOAP provides data transport for Web services.

SOAP can exchange complete documents or call a remote procedure.

SOAP can be used for broadcasting a message.

SOAP is platform- and language-independent.

SOAP is the XML way of defining what information is sent and how.

SOAP enables client applications to easily connect to remote services and invoke remote methods.


Manifest file


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    
package="com.vishalsingh.soap">
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
       <application        
 android:usesCleartextTraffic="true"  
 android:allowBackup="true"       
 android:icon="@mipmap/ic_launcher"      
 android:label="@string/app_name"       
 android:roundIcon="@mipmap/ic_launcher_round" 
 android:supportsRtl="true"       
 android:theme="@style/AppTheme">     
   <activity android:name=".MainActivity">       
     <intent-filter>            
    <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />    
        </intent-filter>    
    </activity>  
  </application>
</manifest>

Java file


package com.vishalsingh.soap;
import android.content.Context;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {

    SoapObject result;   
 Workertask workertask;   
 TextView textView; 
   String mytext;    
String SchId;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textview);

        workertask = new Workertask();
        workertask.execute();

    }

    private class Workertask extends AsyncTask {
        String[] separated;
       int count = 0; 
       String authenticated;

        @Override
        protected Object doInBackground(Object[] objects) {
            String SOAP_ACTION = "Your Soap Action";
            String NAMESPACE = "Your namespace";
            String METHOD_NAME = "your method";
            String URL = "your url";
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("un", "sumit");
            request.addProperty("pass", "billy");
            request.addProperty("DeviceId", "00");
            request.addProperty("Technology", "android");
            request.addProperty("ISNew", "0");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                result = (SoapObject) envelope.bodyIn;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();            }
            count = result.getPropertyCount();
            authenticated = result.getProperty(0).toString();
            separated = new String[count];
            separated = (authenticated).split("%");
            SchId = separated[0]; 
          String SchName = separated[1];
          String fName = separated[2];

            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            textView.setText(SchId);
            super.onPostExecute(o);
        }
    }
}


Xml file


<?xml version="1.0" encoding="utf-8"?><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"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:id="@+id/textview" 
       app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" 
       app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

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