-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search bar full functionality complete; ready for location functionality
- Loading branch information
danielj0nes
committed
Apr 9, 2021
1 parent
75759f5
commit 1640222
Showing
7 changed files
with
122 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 103 additions & 6 deletions
109
app/src/main/java/com/example/priisvergliich/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,138 @@ | ||
package com.example.priisvergliich; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.appcompat.widget.SearchView; | ||
|
||
import androidx.core.app.ActivityCompat; | ||
import android.Manifest; | ||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.view.Gravity; | ||
import android.view.Menu; | ||
import android.view.MenuInflater; | ||
import android.view.MenuItem; | ||
import android.view.WindowManager; | ||
import android.location.Location; | ||
import android.location.LocationListener; | ||
import android.location.LocationManager; | ||
import android.widget.Toast; | ||
|
||
import com.google.android.gms.location.FusedLocationProviderClient; | ||
import com.google.android.gms.location.LocationServices; | ||
import com.google.android.gms.tasks.OnCompleteListener; | ||
import com.google.android.gms.tasks.Task; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
protected LocationManager locationManager; | ||
protected LocationListener locationListener; | ||
FusedLocationProviderClient fusedLocationProviderClient; | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
// Permissions handling | ||
if (requestCode == 100 && grantResults.length > 0 && (grantResults[0] + grantResults[1] | ||
== PackageManager.PERMISSION_GRANTED)) { | ||
getCurrentLocation(); | ||
} else { | ||
Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
private void getCurrentLocation() { | ||
LocationManager locationManager = (LocationManager) getSystemService( | ||
Context.LOCATION_SERVICE | ||
); | ||
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) | ||
|| locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { | ||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | ||
== PackageManager.PERMISSION_GRANTED | ||
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) | ||
== PackageManager.PERMISSION_GRANTED) { | ||
fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() { | ||
@Override | ||
public void onComplete(@NonNull Task<Location> task) { | ||
Location location = task.getResult(); | ||
if (location != null) { | ||
double longitude = location.getLongitude(); | ||
double latitude = location.getLatitude(); | ||
Toast.makeText( | ||
MainActivity.this, | ||
String.valueOf(longitude) + " " + String.valueOf(latitude), | ||
Toast.LENGTH_SHORT).show(); | ||
System.out.println("Success"); | ||
} else { | ||
Toast.makeText(MainActivity.this, | ||
"Could not get location, please try again", | ||
Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); | ||
setContentView(R.layout.activity_main); | ||
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient( | ||
MainActivity.this | ||
); | ||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
MenuInflater inflater = getMenuInflater(); | ||
inflater.inflate(R.menu.pvg_menu0, menu); | ||
MenuItem.OnActionExpandListener onActionExpandListener=new MenuItem.OnActionExpandListener() { | ||
inflater.inflate(R.menu.pvg_menu0, menu); // Toolbar | ||
if (menu.getItem(1).getActionView().isFocused()) { | ||
menu.getItem(3).setVisible(false); | ||
} else { | ||
menu.getItem(2).setVisible(true); | ||
} | ||
// Search functionality | ||
MenuItem.OnActionExpandListener searchListener = new MenuItem.OnActionExpandListener() { | ||
@Override | ||
public boolean onMenuItemActionExpand(MenuItem item) { | ||
return true; | ||
} | ||
|
||
|
||
@Override | ||
public boolean onMenuItemActionCollapse(MenuItem item) { | ||
return true; | ||
} | ||
}; | ||
menu.findItem(R.id.search).setOnActionExpandListener(onActionExpandListener); | ||
menu.findItem(R.id.search).setOnActionExpandListener(searchListener); | ||
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); | ||
searchView.setQueryHint("Search for a product..."); | ||
|
||
// Location functionality | ||
MenuItem.OnMenuItemClickListener locationBtnListener = new MenuItem.OnMenuItemClickListener() { | ||
@Override | ||
public boolean onMenuItemClick(MenuItem item) { | ||
if (ActivityCompat.checkSelfPermission(MainActivity.this, | ||
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED | ||
&& ActivityCompat.checkSelfPermission(MainActivity.this, | ||
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
getCurrentLocation(); | ||
} else { | ||
Toast.makeText( | ||
MainActivity.this, | ||
"Automatic location is not supported", | ||
Toast.LENGTH_SHORT).show(); | ||
} | ||
} else { | ||
ActivityCompat.requestPermissions(MainActivity.this, | ||
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, | ||
Manifest.permission.ACCESS_COARSE_LOCATION}, | ||
100); | ||
} | ||
return true; | ||
} | ||
}; | ||
menu.findItem(R.id.cur_location).setOnMenuItemClickListener(locationBtnListener); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#FFFFFF" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M12,8c-2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4 -1.79,-4 -4,-4zM20.94,11c-0.46,-4.17 -3.77,-7.48 -7.94,-7.94L13,1h-2v2.06C6.83,3.52 3.52,6.83 3.06,11L1,11v2h2.06c0.46,4.17 3.77,7.48 7.94,7.94L11,23h2v-2.06c4.17,-0.46 7.48,-3.77 7.94,-7.94L23,13v-2h-2.06zM12,19c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
include ':getlocation' | ||
include ':app' | ||
rootProject.name = "priisvergliich" |