Skip to content

Instantly share code, notes, and snippets.

View Bahaaib's full-sized avatar
🎯
Focusing

Bahaa Abdelal Bahaaib

🎯
Focusing
View GitHub Profile
@Bahaaib
Bahaaib / Shared Preferences.java
Last active June 11, 2017 22:22
How to store a small amount of data using Shared Preferences
Add the following lines to onCreate method..
SharedPreferences sharedPreferences = this.getSharedPreferences("package name", Context.MODE_PRIVATE); //let the mode private to prevent
//..any other App to access into it
sharedPreferences.edit().putString("variable_name", "variable_value").apply(); //@ putString() it could be any other variable type.
String variable_name = sharedPreferences.getString("variable_name",""); //"" refers to a default value for variable_name in case there wasn't
@Bahaaib
Bahaaib / Get User Location.java
Last active June 11, 2017 22:22
How to get User location information via android system
package com.example.robpercival.locationdemo;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
@Bahaaib
Bahaaib / JSON Script.java
Last active June 11, 2017 22:22
Downloading web content via JSON
.
.
.
.
protected void onPostExcute(String result){ // A Function to work in background and have the permission to change in UI simultaneously
super.onPostExcute(result);
try{
JSONObject jsonObject = new JSONObject(result);
String weatherInfo = jsonObject.getString("weather");
@Bahaaib
Bahaaib / Advanced String Manipulation.java
Last active June 11, 2017 22:22
String Manipulation further more..
import java.util.Arrays;
import java.util.Pattern;
import java.util.Matcher;
public class HelloWorld {
public static main (String[] args){
String mystring = "Mississippi";
String [] splitter = mystring.split("s"); // to split the phrase to words depends on a specific letter
String riverPart = mystring.substring(2, 5); // to prints the letters starting from letter 2 to 5 "which is not including"
@Bahaaib
Bahaaib / Download content from web.java
Created July 18, 2016 07:18
How to bring content from internet to your App
public class DownloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls){
String result = ""; // send Link to Method
URL url; // initialize URL variable
HttpURLConnection urlConnection = null; // initialize HttpURLConnection which later will be responsible for establishing Conn.
try{
url = new URL(urls[0]); //calling (urls member) number 0 from the (String...)
urlConnection = (HttpURLConnection).openConnection(); // initialize the connection to the URL
InputStream in = urlConnection.getInputStream(); // connecting the HTML page to the input stream
@Bahaaib
Bahaaib / Android Timers.java
Created July 17, 2016 11:53
How to initialize an Android timer code
final Handler handler = new Handler(); // A handler is a way used to run the code every certain delayed time
Runnable run = new Runnable (){ // A Runnable is the that code
@Override
public void run (){ // An override fuction describe exactly the action to be taken within that time
//Insert a code to be run every certain "delayed time"
handler.postDelayed (this, 1000); // initialize the handler delayer for 1000msec= 1 second
}
@Bahaaib
Bahaaib / List Views.java
Created July 17, 2016 10:20
How to initialize A List View
ListView myList = (ListView) findViewById(R.id.mylist)
ArrayList<String> myFamily = new ArrayList<String>();
myFamily.add("XXX"); // adding new items
myFamily.add("YYY");
// OR ArrayList<String> myFamily = new ArrayList<String>(asList"XXX", "YYY");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.id.simple_list_item_1, myFamily);
// Insert the following lines below your package name
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
@Bahaaib
Bahaaib / NumberShape.java
Last active February 17, 2016 06:16
Number Shape App (New Algorithm)
public class MainActivity extends ActionBarActivity {
class Number {
double x;
String message = "";
public void numTest() {
{