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-Async-DownloadWebContent.java
Created April 1, 2019 09:35
Downloading Web Content - HttpConnection - URL - AsyncTask
public class MainActivity extends AppCompatActivity {
// We Are going to download Web Content.
// 1) we start by creating a new class.
public class DownloadTask extends AsyncTask<String, Void, String> {
// a class is a collection of methods and variables that we can use in our app.
// AsyncTask: is a way of running our code on a different thread than the main thread.
// so far we used onCreate method thread which known as ui thread. it is advised to run
// any code that gets a bit of time on a different thread rather than the main thread.
// so we are creating some code that will be run in background.
// AsyncTask usually gets 3 parameter.
@MohammadSamandari
MohammadSamandari / Android-MediaPlayer.java
Created April 1, 2019 09:46
Working with Media Player
// Defining a Media Player:
MediaPlayer mediaPlayer;
// Giving the Media Player a sound to play
mediaPlayer =MediaPlayer.create(this,R.raw.music);
// Getting lenght of file in media player.
int musicLength = mediaPlayer.getDuration();
// getting the current position of sound while playing
@MohammadSamandari
MohammadSamandari / Android-AudioManager.java
Created April 1, 2019 09:50
Working with Audio Manager
// Defining Audio Manager:
AudioManager audioManager;
// Getting System Service of Audio Manager
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Getting the MaxVolume of Phone
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// Getting the Current Sound of Phone
@MohammadSamandari
MohammadSamandari / Android-DecimalFormat.java
Created April 1, 2019 10:46
Formatting Int so that it is 2 digit number
txtScore.setText(new DecimalFormat("00").format(noCorrectAnswer) +
" / " +
new DecimalFormat("00").format(noAllQuestions));
@MohammadSamandari
MohammadSamandari / Android-FullScreenActivity.xml
Created April 2, 2019 17:41
Making a activity full screen.
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
@MohammadSamandari
MohammadSamandari / Android-MultipleActivity.java
Created April 2, 2019 20:53
intent - Multiple Activity - Moving from activity to another one
// Defining the second activity for moving to.
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
// putting and extra into an intent so that on the other activity could be recieved.
intent.putExtra("editText", editText.getText().toString());
// Starting the second activity
startActivity(intent);
// On the Second Activity: how te recieve the intent that opened this activity.
Intent intent=getIntent();
// How to extract the extra that has been put in the intent before starting the activity.
@MohammadSamandari
MohammadSamandari / Android-SharedPreferences.java
Created April 3, 2019 14:21
Permanent Data Storage - Shared Preferences
/*
Permanent Data Storage
There are different ways of storing data on the app, the most simple one iscalled
Shared Preferences.
this is a way of storing just very simple short items of data like username or ideally
a preference that user has choosen for the app.
it is great for storing short information.
*/
SharedPreferences sharedPreferences = this.getSharedPreferences("com.mohammadsamandari.sharedpreferences", Context.MODE_PRIVATE);
@MohammadSamandari
MohammadSamandari / ObjectSerializer.java
Created April 3, 2019 15:08
Serialize and Deserialize Objects for saving into shared preferences.
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectSerializer {
@MohammadSamandari
MohammadSamandari / Android-ObjectSerializer-SharedPreferences.java
Created April 3, 2019 15:10
Using ObjectSerializer to save arraylist into shared preferences
// Shared Preferences With arraylist.
// We want to save an arraylist into a shared preferences.
// the thing is shared preferences is limited in the objects in can get. so we are going to go around it.
// To do this, we are going to do a process called Serilization which is essentially
// like converting a json to a string to save it like a string.
// to Do this, we are going to add a new class called, ObjectSerializer.
SharedPreferences sharedPreferences1=this.getSharedPreferences("com.mohammadsamandari.sharedpreferences",Context.MODE_PRIVATE);
ArrayList<String> friends=new ArrayList<>();
friends.add("Monica");
friends.add("Chandler");
@MohammadSamandari
MohammadSamandari / Android-CustomiseActionBar-Menu.java
Created April 4, 2019 18:38
Adding Menu to the activity, customising the action bar
// For Creating a menu in android, we need to add a new file. and we need to customise
// MainActivity.java file.
// The new file is a xml layout file which only gives the layout for our menu
// We created a folder in res as menu an inside that created a menu resource file.
// We customise the xml file and added items for the menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/settings"
android:title="Setting"/>