Android Mid Beginner Cheat Sheet

by - 16:09

We both know that programming Android is mostly rewriting a number of times the same things.
Well here we are, save this page on the bookmarks and copy/paste when you need it.

FINDVIEWBYID

 mWeatherTextView = (TextView) findViewById(R.id.tv_weather_data);  

READ JSON DATA

 JSONObject weatherdata = new JSONObject (JSONstring);  
 JSONObject weather = weatherdata.getJSONObject("weather");  
 String condition = weather.getString("condition");  

BUILD URI AND CONVERT TO URL

 Uri builturi = Uri.parse(BASE_URL).buildUpon()  
     .appendQueryParameter(QUERY_PARAM, param)  
     .build();  
 URL url = null;  
 try {  
   url = new URL(builturi.toString());  
 } catch (MalformedURLException e) {  
   e.printStackTrace();  
 }  
 return url;  

EXECUTE ASYNCTASK

 new GithubQueryTask().execute(githubSearchUrl);  


ADD MENU IN TOP BAR

 @Override  
 public boolean onCreateOptionsMenu(Menu menu) {  
   MenuInflater inflater = getMenuInflater();  
   inflater.inflate(R.menu.forecast, menu);  
   return true;  
 } 

 @Override  
 public boolean onOptionsItemSelected(MenuItem item) {  
   if (item.getItemId() == R.id.action_refresh) {  
     mWeatherTextView.setText("");  
     loadWeatherData();  
     return true;  
   }  
   return super.onOptionsItemSelected(item);  
 }  

EXPLICIT INTENT

 Intent intent = new Intent(this, SettingsActivity.class);  
 startActivity(intent);  


INTENT FOR EXTERNAL MAP

 Intent intent = new Intent(Intent.ACTION_VIEW);  
 Uri uri = new Uri.Builder().scheme("geo").path("0,0").query("california").build();  
 intent.setData(uri);  
 if (intent.resolveActivity(getPackageManager()) != null) {  
   startActivity(intent);  
 }  

SHARE INTENT

 Intent shareIntent = ShareCompat.IntentBuilder.from(activity)  
  .setType("text/plain")  
  .setText(shareText)  
  .getIntent();  
 if (shareIntent.resolveActivity(getPackageManager()) != null) {  
  startActivity(shareIntent);  
 }  

ITEM IN THE ACTION BAR

 <item  
   android:id="@+id/action_share"  
   android:title="Share"  
   android:orderInCategory="1"  
   app:showAsAction="ifRoom"/>  

ASYNCTASKLOADER

(it handles lifecycle)
 @Override  
 public Loader<String[]> onCreateLoader(int id, Bundle args) {  
   return new AsyncTaskLoader<String[]>(this) {  
     String[] weatherData = null; //Cache data  
     @Override protected void onStartLoading() {}//Check cache data and show progress bar then forceLoad()  
     @Override public String[] loadInBackground() {}//Get data from prefences or bundle and do network request  
     @Override public void deliverResult(String[] data) {}//save data in cache  
   };  
 }  

USE PREFERENCEFRAGMENT

SHAREDPREFERENCES

 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);  
 sharedPreferences.getBoolean("show_bass", true);  


LISTEN TO SWYPE ON RECYCLEVIEW

 new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {  
   @Override  
   public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {  
     return false;  
   }  
   @Override  
   public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {  
     long id = (long) viewHolder.itemView.getTag();  
     removeGuest(id);  
     mAdapter.swapCursor(getAllGuests());  
   }  
 }).attachToRecyclerView(waitlistRecyclerView);  


Here some interesting images too:



You May Also Like

0 commenti