title: how to implement these Activity code on Fragment?
body: <p>confuse to implement these code to Fragment.java
  could you please to implement these code from Activity to Fragment? I want to make auto update location and show the address by click the "Fetch" button.</p>
  
  <p>I have these MainActivity.java code, I want to implement these to Fragment.java but i don't understand how to do it.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.example.parsaniahardik.addresscurrentlocation;
  
  import android.Manifest;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.location.Address;
  import android.location.Geocoder;
  import android.location.Location;
  import android.location.LocationManager;
  import android.net.Uri;
  import android.provider.Settings;
  import android.support.v4.app.ActivityCompat;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.api.GoogleApiClient;
  import com.google.android.gms.location.LocationRequest;
  import com.google.android.gms.location.LocationServices;
  import com.karumi.dexter.Dexter;
  import com.karumi.dexter.MultiplePermissionsReport;
  import com.karumi.dexter.PermissionToken;
  import com.karumi.dexter.listener.DexterError;
  import com.karumi.dexter.listener.PermissionRequest;
  import com.karumi.dexter.listener.PermissionRequestErrorListener;
  import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
  
  import java.io.IOException;
  import java.util.List;
  import java.util.Locale;
  
  public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
  
      private static final String TAG = "MainActivity";
  
      private GoogleApiClient mGoogleApiClient;
      private Location mLocation;
    //  private LocationManager mLocationManager;
  
      private LocationRequest mLocationRequest;
      private com.google.android.gms.location.LocationListener listener;
      private long UPDATE_INTERVAL = 2 * 1000;  /* 10 secs */
      private long FASTEST_INTERVAL = 2000; /* 2 sec */
      private LocationManager locationManager;
  
      private Double latitude = 0.00;
      private Double longitude = 0.00;
      private TextView textView, tvCountry, tvCity, tvState, tvPincode, tvFeature;
      private Button btn;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          requestMultiplePermissions();
  
          mGoogleApiClient = new GoogleApiClient.Builder(this)
                  .addConnectionCallbacks(this)
                  .addOnConnectionFailedListener(this)
                  .addApi(LocationServices.API)
                  .build();
  
        //  mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
          checkLocation();
  
          textView = findViewById(R.id.tv);
          tvCountry = findViewById(R.id.tvCountry);
          tvState = findViewById(R.id.tvState);
          tvCity = findViewById(R.id.tvCity);
          tvPincode = findViewById(R.id.tvPincode);
          tvFeature = findViewById(R.id.tvFeature);
          btn = findViewById(R.id.btn);
  
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  setAddress(latitude,longitude);
              }
          });
  
      }
  
      @Override
      public void onConnected(Bundle bundle) {
          if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
              // TODO: Consider calling
              //    ActivityCompat#requestPermissions
              // here to request the missing permissions, and then overriding
              //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
              //                                          int[] grantResults)
              // to handle the case where the user grants the permission. See the documentation
              // for ActivityCompat#requestPermissions for more details.
              return;
          }
  
          startLocationUpdates();
  
          mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  
          if(mLocation == null){
              startLocationUpdates();
          }
          if (mLocation != null) {
  
              // mLatitudeTextView.setText(String.valueOf(mLocation.getLatitude()));
              //mLongitudeTextView.setText(String.valueOf(mLocation.getLongitude()));
          } else {
              Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
          }
      }
  
      @Override
      public void onConnectionSuspended(int i) {
          Log.i(TAG, "Connection Suspended");
          mGoogleApiClient.connect();
      }
  
      @Override
      public void onConnectionFailed(ConnectionResult connectionResult) {
          Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          if (mGoogleApiClient != null) {
              mGoogleApiClient.connect();
          }
      }
  
      @Override
      protected void onStop() {
          super.onStop();
          if (mGoogleApiClient.isConnected()) {
              mGoogleApiClient.disconnect();
          }
      }
  
      protected void startLocationUpdates() {
          // Create the location request
          mLocationRequest = LocationRequest.create()
                  .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                  .setInterval(UPDATE_INTERVAL)
                  .setFastestInterval(FASTEST_INTERVAL);
          // Request location updates
          if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
              // TODO: Consider calling
              //    ActivityCompat#requestPermissions
              // here to request the missing permissions, and then overriding
              //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
              //                                          int[] grantResults)
              // to handle the case where the user grants the permission. See the documentation
              // for ActivityCompat#requestPermissions for more details.
              return;
          }
          LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                  mLocationRequest, this);
          Log.d("reque", "---&gt;&gt;&gt;&gt;");
      }
  
      @Override
      public void onLocationChanged(Location location) {
  
          String msg = "Updated Location: " +
                  Double.toString(location.getLatitude()) + "," +
                  Double.toString(location.getLongitude());
  
          latitude = location.getLatitude();
          longitude = location.getLongitude();
  
          Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
  
      }
  
      private boolean checkLocation() {
          if(!isLocationEnabled())
              showAlert();
          return isLocationEnabled();
      }
  
      private void showAlert() {
          final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
          dialog.setTitle("Enable Location")
                  .setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
                          "use this app")
                  .setPositiveButton("Location Settings", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface paramDialogInterface, int paramInt) {
  
                          Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                          startActivity(myIntent);
                      }
                  })
                  .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface paramDialogInterface, int paramInt) {
  
                      }
                  });
          dialog.show();
      }
  
      private boolean isLocationEnabled() {
          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
                  locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
      }
  
      private void  requestMultiplePermissions(){
          Dexter.withActivity(this)
                  .withPermissions(
                          Manifest.permission.ACCESS_COARSE_LOCATION,
                          Manifest.permission.ACCESS_FINE_LOCATION)
                  .withListener(new MultiplePermissionsListener() {
                      @Override
                      public void onPermissionsChecked(MultiplePermissionsReport report) {
                          // check if all permissions are granted
                          if (report.areAllPermissionsGranted()) {
                              Toast.makeText(getApplicationContext(), "All permissions are granted by user!", Toast.LENGTH_SHORT).show();
                              //startLocationUpdates();
                          }
  
                          // check for permanent denial of any permission
                          if (report.isAnyPermissionPermanentlyDenied()) {
                              // show alert dialog navigating to Settings
                              openSettingsDialog();
                          }
                      }
  
                      @Override
                      public void onPermissionRationaleShouldBeShown(List&lt;PermissionRequest&gt; permissions, PermissionToken token) {
                          token.continuePermissionRequest();
                      }
                  }).
                  withErrorListener(new PermissionRequestErrorListener() {
                      @Override
                      public void onError(DexterError error) {
                          Toast.makeText(getApplicationContext(), "Some Error! ", Toast.LENGTH_SHORT).show();
                      }
                  })
                  .onSameThread()
                  .check();
      }
  
      private void openSettingsDialog() {
  
          AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
          builder.setTitle("Required Permissions");
          builder.setMessage("This app require permission to use awesome feature. Grant them in app settings.");
          builder.setPositiveButton("Take Me To SETTINGS", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
                  Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                  Uri uri = Uri.fromParts("package", getPackageName(), null);
                  intent.setData(uri);
                  startActivityForResult(intent, 101);
              }
          });
          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
              }
          });
          builder.show();
  
      }
  
      private void setAddress(Double latitude, Double longitude){
  
          Geocoder geocoder;
          List&lt;Address&gt; addresses = null;
          geocoder = new Geocoder(this, Locale.getDefault());
  
          try {
              addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          if(addresses.size() &gt; 0) {
              Log.d("max", " " + addresses.get(0).getMaxAddressLineIndex());
  
              String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
              String city = addresses.get(0).getLocality();
              String state = addresses.get(0).getAdminArea();
              String country = addresses.get(0).getCountryName();
              String postalCode = addresses.get(0).getPostalCode();
              String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
  
              addresses.get(0).getAdminArea();
              textView.setText(address);
              tvFeature.setText(knownName);
              tvCountry.setText(country);
              tvState.setText(state);
              tvCity.setText(city);
              tvPincode.setText(postalCode);
          }
      }
  }
  </code></pre>
  
  <p>fragment_myloc.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout 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:orientation="vertical"
      tools:context=".MainActivity"&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/btn"
          android:text="fetch address"/&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="20dp"
          android:orientation="horizontal"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Full Address : "
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
          &lt;TextView
              android:id="@+id/tv"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello World!"
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp"
          android:orientation="horizontal"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Feature : "
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
          &lt;TextView
              android:id="@+id/tvFeature"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello World!"
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp"
          android:orientation="horizontal"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Country : "
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
          &lt;TextView
              android:id="@+id/tvCountry"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello World!"
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp"
          android:orientation="horizontal"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="State : "
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
          &lt;TextView
              android:id="@+id/tvState"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello World!"
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
      &lt;/LinearLayout&gt;
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp"
          android:orientation="horizontal"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="City : "
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
          &lt;TextView
              android:id="@+id/tvCity"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello World!"
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp"
          android:orientation="horizontal"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Pincode : "
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
          &lt;TextView
              android:id="@+id/tvPincode"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello World!"
              android:textColor="#000"
              android:textSize="18sp" /&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt; 
  </code></pre>