Skip to content

Instantly share code, notes, and snippets.

View MohammadSamandari's full-sized avatar
💭
Android Is SO MUCH FUN

Mohammad Samandari MohammadSamandari

💭
Android Is SO MUCH FUN
View GitHub Profile
@MohammadSamandari
MohammadSamandari / Android-GoogleMap.java
Created March 31, 2019 12:07
Learning: Working With Google Map
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
// Start By Creating a new location to center our map On:
@MohammadSamandari
MohammadSamandari / Android-LocationPermission.xml
Created March 31, 2019 12:13
Asking For Permissions In Android Studio
// this coarse location is for location base on wifi and phone signal
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
// This fine location is the wxact location with GPS
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
@MohammadSamandari
MohammadSamandari / Android-Location.java
Created March 31, 2019 16:03
Learning: Getting users location - Asking for permission to access location
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@MohammadSamandari
MohammadSamandari / Android-Showing User Location On Map.java
Created March 31, 2019 21:07
Learning : Maps and Getting Location
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
LatLng newLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@MohammadSamandari
MohammadSamandari / Android-ReverseGeoCoding.java
Created March 31, 2019 21:36
Learning: Getting Address From LatLon
// Reverse GeoCoding
// Getting The address from a Location coordinates.
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
// Locale: format for the address. getDefault() use the Locale of the user.
try {
List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
// We have to say how many result we want, so that integer is for that.
// we do a check to see if we got something back or not.
if (listAddresses != null && listAddresses.size() > 0) {
String countryName = listAddresses.get(0).getCountryName();
@MohammadSamandari
MohammadSamandari / Android-JSON-Part1
Created April 1, 2019 09:18
Learning: Working With JSON - Extracting Info Out Of It.
try {
//Converting result To Json and extracting Parts of it.
JSONObject myJSON = new JSONObject(weatherInfo);
// Checking if the City is found:
int resultCod = Integer.parseInt(myJSON.getString("cod"));
if (resultCod == 200) {
Log.i("Lord", "resultCod Is 200 - Continuing . . .");
//Getting The Information We Need.
@MohammadSamandari
MohammadSamandari / Android-HideKeyboard
Created April 1, 2019 09:20
Learning: Hiding Keyboard After a button is clicked
// To Close The Keyboard as soon as button is clicked.
InputMethodManager mgr= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(edtCity.getWindowToken(),0);
// edtCity is the EditText that has the focus and keyboard is writing in it.
@MohammadSamandari
MohammadSamandari / Android-EncodeStringToUrlString
Created April 1, 2019 09:22
Learning: Encode String So that they are URL friendly
// To Encode the text of Edit Text So that cities with space are converted to url type and no error happens:
String encodedCityName= URLEncoder.encode(edtCity.getText().toString(),"UTF-8");
@MohammadSamandari
MohammadSamandari / Android-Async-DownloadImage.java
Created April 1, 2019 09:27
Learning: Downloading Image from internet with async task
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... strings) {
URL url;
HttpURLConnection httpURLConnection;
try {
url = new URL(strings[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream in = httpURLConnection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
@MohammadSamandari
MohammadSamandari / Android-StringManipulation-Regex.java
Created April 1, 2019 09:32
Learnig : String Spilit - SubString - Regex
public class StringManipulation {
//Split:
// this is a method for spiliting a strings
String myString = "Mohammad x Faeze x Tommy x Mobina";
// if i wanted to convert above string into an array which contained each of the name:
String[] splitString = myString.split(" x ");
// this takes myString and split it by x into an array;
//----------------------------------------