Thursday 1 February 2018

Android Studio - Creating A Simple Silent Toggle App (Beginner-Friendly)

                                           Silent Toggle Application
Today, we are going to develop a Silent Toggle App in Android Studio. This App is going to enable users to toggle their Ringer Mode.

Get your Android studio running.
Create a new project - Use "Silent Toggle " as the Application name - Minimum SDK version shouldn't be below 15 - Choose Empty Activity . Lets Begin ...

Your Platform Should look like this,

NB: If You Are Using Android Studio 2.3 and above , Please Change From ConstraintLayout to Relative Layout.

Now we are going to modify the  activity_main.xml and place these Views in it
-An ImageView
-Button

We are going to use two Images to aid the User of your Application in determining whether the phone is silent or not. These Images were downloaded from google.
Click Here to Download Them.

*After the images have been downloaded, you need to copy and paste them in the drawable folder of your application.
To do that, you need to:
1-Left-Click on "app"
2- Left-Click on "res"
3-Right-Click on drawable
4-A Dialog will appear and you click on "show in explorer"
5- A Folder containing several folders will appear. You should see a drawable Folder, open it and paste the images depicting ringer-mode visuals.


Now, I presume you have both images in the Drawable folder. Lets set the ball rolling.
If you are not in "activity_main.xml" please go there now.
We are going to add  xml-codes which :
-Add an image view which will give a visual appeal to the Users
-Sets the image-width to match the parent layout
-Sets the image-height to its original height
-Give an  id  to the image so that we can reference or use it from other activity.  id name = image
-Also We are going to include a Button view which the user can tap to change the ringer-mode of her app
-Align the Button below the parent's layout
-Give an id to the button so that we can reference it from other activities . id name = btn_Toggle
-Set the Button text to "Silent mode". The text changes in respect to the state of the ringer-mode.

Your XML code should look like this:








Now run your application.
Everything appears as expected. What you see is just the User Interface that the User would be interacting with. It does nothing now. We are going to respond to click event on the button. When the user clicks the button, the user expect her phone to either change to silent-mode when the phone is in normal-mode or change to normal-mode when the phone is in silent-mode. This all will happen in the java class. Now lets get our hands dirty with some java-codes.

NB: If you are not in "MainActivity.java" Please Go There Now.
Inside "MainActivity.java" we are going to use codes which:
-Gets a Reference of the Button and Image views we have in "activity_main.xml" . We first use a Class-wide variable so that we can access them in the various methods we create. If You don't know what Class-wide variable is , Click here
-Sets an "OnClickListener" to the button .








Now that you have set the an on-click listener to the button, you can add this code below the comment "Method which toggles the ringer mode and modifies the visual appeal of the...." to see if you button will respond when you click the button.
Toast.makeText(MainActivity.this,"Button works",Toast.LENGTH_LONG).show();

Okay, run the app now. You should see a Toast Message whenever Button is clicked. This means our click-listener is working perfectly.


Great - Now, You can call yourself an Android Developer.

Your application still is dull - we are going to create a method which is going to check the states of the phone's ringer-mode and base on the state it gives us , we will change the ringer-mode to the opposite state. That is , if the phone is on silent, we change it to normal and vice-versa.
NB: We will be checking the phone's ringer-state. We need to include a class-wide variable for "Android Manager" which is a built-in class which allows Programmers to access phone's ringer-mode and volume Services or controls. After creating the class-wide variable, you need to get a reference of it by stating the controls or Services you will like to have access to: We only want the Ringer-mode service and not the Volume service.

NB: Volume Service is used in media application for increasing the volume of the sound you are playing.





-Outside of your "onCreate()" method , create a private void method with the name "toggleStates". Pay close attention here, this method will check for the phone's current ringer-mode state. In this method we use an "if statement" to check whether the phone's ringer-mode state is in silent, and if it is in silent we will change the phone's ringer-mode state to normal and change the text of the button to "Silent", and change the image to a silent-logo. I know! A lot of changes is going one here- Let me structure it well.
*When Button is Clicked and Phone's Ringer mode State is Silent:
-Change the ringer-mode to normal
-Change the ImageView to the image depicting a Normal state
-Change the Button text from "Normal" to "Silent"

I will elaborate on each step so that you get to know and have a solid understanding of what the various codes do.
CHANGE THE RINGER-MODE TO NORMAL
To change the ringer mode to Normal. You use the Audio manager to change the phone's ringer mode. After the Phone's Ringer mode is changed, we will use a Toast Message to display a success message. We use Toast Message to inform the user that something has happened to her phone.

NB:We will have to ask permission from the "Great" Android System of the phone for it to inform the user that  our app wants to access its Ringer-mode states. If we don't provide this permission in our code , Android System running in the User's phone will Kill the application for security reasons. We are Legal Programmers so we are going to follow Android's "10 - commandment".  If your phone or Android Emulator is not above  API: 21 You wouldn't have much problem But if its above that API then - Scroll-Down to "API: 21 Security Stricture" - I will provide a way you can get around this issue.

Now , remember we placed a Toast message when the button is clicked , just to see if our button clicks is working. I want you to remove that code and let your "onCreate()"  look like this,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Referencing (Initialization)    btnToggle = (Button) findViewById(R.id.btn_toggle);
    imageView = (ImageView) findViewById(R.id.image);

    //Getting a reference of the Audio Service    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

    //Setting an onClickListener to the button    btnToggle.setOnClickListener(new View.OnClickListener() {
        @Override        public void onClick(View view) {
           
            toggleState();
        }
    });

}

This is a Beginner's Friendly tutorial so I am going to explain why the "toggleState()"
is placed into the onCreate(). The compiler compiles code in onCreate only, it doesn't 
extend outside of the onCreate(){"compiler compiles codes within these curly brackets"}.
The Only instance a Compiler will compile beyond the onCreate closing brackets is when 
there is a method in the onCreate() method - it then searches for that particular method 
and in our case , it will search for the toggle() method and compile the codes inside the
toggle() curly brackets.
Yes that's all it is about. You don't have to be Einstein for you to understand this. 


Now Run Your Application , Your Phone state should change when the button is pressed.
NB: Your Phone must be intentionally-placed on silent for the "toggleState()" to work. This is because we have only one "if statement" which is checking to see whether the phone is in Silent-mode so that it changes it to Normal-mode. We will proceed to changing the Phone state when the phone is not on silent.

When The Phone is not on silent then it has to be set to silent right? If yes then Your toggleState() method should be like this:
private void toggleState(){
    if(audioManager.getRingerMode() == audioManager.RINGER_MODE_SILENT){
        //Change to Normal when phone is silent        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        Toast.makeText(MainActivity.this, "Normal-mode", Toast.LENGTH_LONG).show();
        //Change to Silent when phone is not silent    } else{
        audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        Toast.makeText(MainActivity.this, "Silent-mode", Toast.LENGTH_LONG).show();

    }
}
Now Run Your Application and it will change the phone ringer-mode when the button is clicked.

Well technically, it works alright, but Your App will also give users anxieties because it doesn't provide any reasonable visual appeal. Remember Software Development is Engineering , the visuals of your application must be an eye-candy to your millions of users. Henceforth, we are going to change the text and the image when the button is clicked.

CHANGING THE IMAGE:
-Just include this line of code in the toggleState() method :

private void toggleState(){
    if(audioManager.getRingerMode() == audioManager.RINGER_MODE_SILENT){
        //Change to Normal when phone is silent        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

        //change image to one that is depicting that the phone is in a normal-state        imageView.setImageResource(R.drawable.normal);

        Toast.makeText(MainActivity.this, "Normal-mode", Toast.LENGTH_LONG).show();
        //Change to Silent when phone is not silent    } else{
        audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        
        //change image to one that is depicting that the phone is in a silent-state        imageView.setImageResource(R.drawable.silent);
        
        Toast.makeText(MainActivity.this, "Silent-mode", Toast.LENGTH_LONG).show();
    }
}
Now when you run your app , it is going to change its image relative to the phone's ringer mode. Now its left with changing the text of the button and the user won't be getting anxieties anymore when using your application .

CHANGE BUTTON TEXT BASED ON RINGER-STATE
We expect the text of the button to provide a directional aid as to the state we are going to put the phone in when the button is clicked. So "nous allons continuer"
Add this line of code below the imageView.setImageResource of "if statement":

//change button text to Silent when phone is in Normal-statebtnToggle.setText("Silent");

And add this line of code to the "else statement":

//change button text to Silent when phone is in Silent-statebtnToggle.setText("Normal");

Run!!!
Sorry Run the Application.

 API: 21 Security Stricture
Your App is Ready to be Shipped!! umm, only for Developers Using API below 21. 
Developers using API's which is greater than 21 - You need to ask permission from the User. There is this "Do Not Disturb mode" which is not switched on for your application - Your application can not turn on the "Do no disturb" mode by itself. What you can do is to add a code which will direct the user to where that particular mode is, for the User to just click on the "Do not Disturb" radio button for your application to successfully lunch. When this is turned on for your application , Users wouldn't have to go through that process again, the application can run with no interruption from the Android System.
Well, to access this, we have to include this code in the onCreate() method . I recommend you place it above the toggle() method in the onCreate(). :

NotificationManager notificationManager =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (!notificationManager.isNotificationPolicyAccessGranted()) {

        Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
        startActivity(intent);
    }
}


NB:I am going to explain what is happening in the code above . NotificationManager is a class which allows developers to access the isNotificationPolicyAccessGranted().a We use this in the "if statement" to check if the "Do not Disturb mode" is not granted for our app, and if it is not granted then We use an "Intent class". This class enable us to "move" in and out of the application.
For example, when you are using any social media app and you want to post or change your profile picture, as soon as you click on choose picture from gallery, it is an "Intent Class" which takes you to the gallery application. So the "Intent" used here will direct the user to her phones settings where the "Do not Disturb" radio button is located.

That's about it. You might think your app is ready, but if you showcase it in the app store, negative feedbacks are going to rain in your notification inbox. This is because, when your application is not used or in focus and the User uses the phone's manual button to put her phone on silent , Your application might display the wrong Image and also button text- You continue to give your Users anxieties.
Do not worry, this problem is a nut everyone can crack. To solve this problem, you will need to override the onStart() and onResume() by pressing ctrl + O (for windows) and just place codes we have already written in it.
onPause() method : This is a method which is called when your app is not in focus. For instance, a User is using your application and her mother calls, the Android System has placed high priority on phone calls so any current application which has focus will be killed instantly. If you have overridden the onPause()method and you have codes which save the states of the application then when the call comes, Your application quickly saves everything before your app is killed so that when User resumes the application again -Your data will be put in order as you left by using the onResume();
With the Silent Toggle App - all you need is to do is place the "toggleState() method" in onPause() and onStart() methods. 
       THE SILENT TOGGLE SOURCE CODE :


package com.business.etfs.silenttoggle;

import android.app.NotificationManager;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //Class-wide variables for Button and Image views    Button btnToggle;
    ImageView imageView;
    AudioManager audioManager;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Referencing (Initialization)        btnToggle = (Button) findViewById(R.id.btn_toggle);
        imageView = (ImageView) findViewById(R.id.image);

        //Getting a reference of the Audio Service        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

        //Setting an onClickListener to the button        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!notificationManager.isNotificationPolicyAccessGranted()) {

                Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                startActivity(intent);
            }
        }


        btnToggle.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                toggleState();
            }
        });

    }

    @Override    protected void onStart() {
        toggleState();
        super.onStart();
    }

    @Override    protected void onResume() {
        toggleState();
        super.onResume();
    }

    private void toggleState(){
        if(audioManager.getRingerMode() == audioManager.RINGER_MODE_SILENT){
            //Change to Normal when phone is silent            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

            //change image to one that is depicting that the phone is in a normal-state            imageView.setImageResource(R.drawable.normal);

            //change button text to Silent when phone is in Normal-state            btnToggle.setText("Silent");

            Toast.makeText(MainActivity.this, "Normal-mode", Toast.LENGTH_LONG).show();
            //Change to Silent when phone is not silent        } else{
            audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

            //change image to one that is depicting that the phone is in a silent-state            imageView.setImageResource(R.drawable.silent);

            //change button text to Silent when phone is in Silent-state            btnToggle.setText("Normal");

            Toast.makeText(MainActivity.this, "Silent-mode", Toast.LENGTH_LONG).show();
        }
    }

}


NOW YOU CAN GO BRAG TO YOUR FRIENDS AND SHOWCASE YOUR NEW ANDROID APPLICATION. COME BACK LATER AND I WILL SHOW YOU HOW TO INCLUDE WIDGET TO THIS APPLICATION.  I AM GOING TO TAKE YOU THROUGH THE DEVELOPING PROCESSES OF BUILDING MAJOR AND COMPLICATED APPS BUT I DO SO IN A MANNER WHICH WOULD NOT BE LIKE AN INEFFABLE TASK .



Wednesday 10 January 2018

ETFs Blog - Android Programming , Software Engineering, Technology.

WHAT ETFs is ABOUT
Notice to all Visitors: This blog is not associated nor post about trading with ETFs treasury bill. ETF here is an Acronym for Entertainment , Technology and Finances . Yeah Maybe we might post something about ETF(trade) because we also provide advice on being financially stable with current opportunities we deem great.

       Now that you know what our ETFs acronym is, lets jump into what we will be publishing on this blog.
       Programming in Android Studio tidbits examples. We are going to provide readers with in-depth understanding about how "things" work in Android Programming, what I mean by "things" is what the "getInstance()","startService()","startIntent()","build.gradle.", all those codes involved in android programming . We are going to go in-depth describing what they are supposed to do, why they do what they are supposed to do , why can't they do what "blah blah" can do and who the hell even made this code and what the hell was he(they) thinking. Yes!!! . I feel like a Priest now😃!. Okay, back to reality. We are going to talk more about programming with Android and if you continue with us, we are going to grow together to the extent where we know everything there is to know to become a Professional and God-mode Programmer in Android . By the time you read this , you might have been directed from my YouTube Channel to come here for a Source Code. But for now(As I write this post) , My YouTube Page is not ready yet, I am still working to get more tutorial videos ready before I post them all on YouTube. Android is amazing, thanks to Java. God I Love these two. They make  life easier. Those of you newbie's in Programming - Welcome ,I have a very special tutorial video made for YOU. I don't want to disclose the name here before anyone steals my precious idea that I have to get you started as an ANDROID DEVELOPER as quick as a sonic BOOM!.  Also I am going to give tutorials on some cool stuffs and techniques Big companies are using on their User interface . We are going to CLONE . We are going to CLONE. WE are GOING to CLONE every popular applications available on Planet Earth and when the Aliens finally let us in on their source code , we are going to clone theirs also . For now , our focus is on |Popular Apps like Uber, Facebook, Snapchat, Instagram and When we grow enough , we will go into some high tech military softwares. (After learning these, better know Kim Jong Hunn (whatever the name is ) might nabb you to teach his "technical know-not" how to get his rocket responding to their software). When You all follow through , We are going to dabble in the so-called coding Crytocurrency. Yeah , Long way ahead but , just obey and follow my instructions.Remember , I am starting from the time Android was born and how they got to where they are in terms of software. When I tell you to sleep for only 3 hours, you do so. lol. (I intend to include "lol" to every writings that may sound ........).lol
    Programming-related news.  News like, Tesla using a software which can detect objects and their latest software. I am trying to get a link into NASA so that I get to know what kind of people (programmers) they are, what air do they breath, how many heart do they have and what kind of energy do they use. 🙂. 

  Conclude: A lot of activities are going to happen but I can not continue to elaborate on them because I have about 9 tabs opened (work-based) and they are my highest priority for now , Time will not permit me to keep writing about what I am going to do on this blog  but I have to publish this little information I have for you , just to get information out there that "A new Force is coming " and this FORCE is like none you've seen before. If you have  friends, siblings or non-human race entities 😶interested in the topics I have elaborated on ,then link this blog to them and let Ye save ,bookmark or memorize this web name.  Also, there is Entertainment included, it is not the music, movie kind of entertainment , rather Entertainment in the Software Engineering Spectrum.

Thank You.