Share Android App Internal Storage Data

Two weeks ago I faced a problem, I was storing a pdf file in internal storage and I wanted to share it via another apps like Gmail, or any other sharing app. When I tried to attach it to Gmail for example I got this error “Permission denied for the attachment“.
The problem is that data saved in internal storage isn’t accessible by other applications.

After a while I figured it out, that the solution is in Content Providers.

Content Providers have a lot of uses one of them is providing a way to share data with other apps.

In manifest we declare the content provider :

manifest-provider

name attribute is my custom content provider class, authorities attribute is URI that identify data offered by the content provider, exported is true means that the content provider is available for other applications to use.

After that we create our CustomContentProvider :

custom-content-provider

Finally, We will use our custom content provider to share our pdf file :

sharing

CONTENT_URI means I am going to use content provider “content://” + provider’s_authority + “/” + absolute file path.

That will allow our app to share the pdf file.

Linking Activities Using Intents

An Android application can contain zero or more activities. When your application has more than one activity, you may need to navigate from one activity to another. In Android, you navigate between activities through what is known as an intent.

Today i will talk about how to navigate from your current activity to another one. We will create new android project.

Then we will add new Activity by right click on package name under src folder then choose New –> Other –> AndroidActivity, And give it a name (I will name it “Activity2”) and press finish button.

After that we should add this activity to our AndroidManifest.xml … We will add those lines

And as we said before @string used to add our program strings in one file called strings.xml .. So we will simply go to strings.xml file and add this line

Activity2 is the title of our new Activity .. or you can name it as you like 🙂

Then we go to our res –> layout –> right click –> New –> Other –> Android XML File … ( I will name it activity_activity2.xml ) … then add this code to it.

Return back again to strings.xml to add txtactivity by adding this line

We go to our first activity xml ( main.xml file ) and create a button by adding this code …

And as we do before go to strings.xml and add a text for button like this

Then go to our MainActivity.java class to link your button in .xml with your button in .java class, and after that when button pressed we write the code that enable us to navigate to another Activity.

Run your code and enjoy 😀 .. This is the first screen

After you press the button .. The second Activity will be shown

That’s all for now, and i hope it’s easy to understand … if you have any question don’t hesitate 🙂

Activites and Intents – Part 3

This is the third lesson in our series of Activities And Intents … Now we will learn how to make a Progress Dialog … A progress dialog is useful for showing the progress of some activities, such as the status of a download operation.

The first thing we will create a button in our main.xml as we do in the previous tutorial …

As we do before we will go to our .java class and override onCreateDialog() method that will be called when we call showDialog(int id) method. Create an instance of the ProgressDialog class and set its various properties, such as icon, title, and style. And set the two buttons Hide and Cancel.

Then we go to onCreate() method and connect our button in .xml  with our button in .java code .. and then implement the function that when button pressed is fired .. and inside it we will call showDialog(int id) method and give the dialog a unique id. To display the progress status in the progress dialog, you need to use a Handler object to run a background thread

The background thread counts up to 100, with each count delayed by 100 milliseconds. When the count reaches 100, the progress dialog is dismissed.

Now Run your program .. and see your great result .. tadaaaaaa 😀

That’s all for now .. and sorry for my delay in publishing this lesson 🙂

Activites and Intents – Part 2

Okay now we will continue our last lesson .. in Today’s lesson the first thing that we gonna learn is how to display a dialog window .. ?!

First we will create a button in our UI (main.xml file) ..

android:id

This provides a unique identifier for the view, which you can use to reference the object from your app code, such as to read and manipulate the object (you’ll see this in the next lesson).The at sign (@) is required when you’re referring to any resource object from XML. It is followed by the resource type (id in this case), a slash, then the resource name (btn_Show).The plus sign (+) before the resource type is needed only when you’re defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project’s gen/R.java file that refers to the Button element. Once the resource ID is declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts.

wrap_content value specifies that the view should be only as big as needed to fit the contents of the view.

fill_parent then the Button element would fill the screen, because it would match the size of the parent LinearLayout

The next step .. we will go to our .java class and write the code that respond to click event on our button .. Let’s first descripe the shape of our Dialog that we will create .. our dialog will contain a small picture, a word beside it, some check boxes and two buttons.

First we will create an array to contain check boxes’ names, and another array to contain a boolean that indicate if this item is checked or not .. then we will do as we do before .. we will connect our button in .xml file with our button in .java code .. and then implement the function that when button pressed is fired .. and inside it we will call showDialog(int id) method and give the dialog a unique id .. 🙂

Because we will show a dialog on our screen .. so we need to override onCreateDialog() method that will be called when we call showDialog(int id)  method. So now we will write this code in our onCreateDialog() method

The preceding code sets the Dialog Icon, Title and two buttons: OK and Cancel, using the setPositiveButton() and setNegativeButton() methods, respectively. We also set a list of checkboxes for users to choose via the setMultiChoiceItems() method. For the setMultiChoiceItems() method, you passed in two arrays: one for the list of items to display and another to contain the value of each item to indicate if they are checked. When each item is checked, you use the Toast class to display a message. A toast is a view containing a quick little message for the user. When the view is shown to the user, appears as a floating view over the application. And now when i run the App … Jerry Icon appears (i love tom & jerry :D)

The last thing i want to say the meaning of MainActivity.this that we use it as a parameter in Toast.makeText … On Android, a Context is used for many operations but mostly to load and access resources .. and MainActivity.this returns the current context of the activity, if activity is destroyed then it will be destroyed also.

That’s all for now … wait the other parts of Activities and Intents series 🙂

Activites and Intents – Part 1

Hello again .. i hope you all are fine 🙂

Today i will talk about something new .. in the last lesson we created an Activity that has a Button and a TextView .. but we didn’t define what’s activity ?!

Activity is a window that contains the user interface of your applications. An application can have zero or more activities, and the main aim of an activity is to interact with the user. From the moment an activity appears on the screen to the moment it is hidden, it goes through a number of stages, known as an activity’s life cycle.

And there’s another definition that i will talk about .. it’s called intent ..

An intent is basically the “glue” that enables different activities from different applications to work together seamlessly, ensuring that tasks can be performed as though they all belong to one single application.

Now let’s talk about how to create an Activity .. to create an Activity we will create a java class that extends an Activity base class.

Our Activity class then load its UI components(Buttons, TextView, EditText, … etc) from the xml file created in res/layout folder(which is here named activity_main.xml) .. and every Activity in our application must be declared in AndroidMainfest.xml file

The Activity base class defines a series of events that governs the life cycle of an activity. The Activity
class defines the following events:
➤ onCreate() — Called when the activity is first created
➤ onStart() — Called when the activity becomes visible to the user
➤ onResume() — Called when the activity starts interacting with the user
➤ onPause() — Called when the current activity is being paused and the previous activity is
being resumed
➤ onStop() — Called when the activity is no longer visible to the user
➤ onDestroy() — Called before the activity is destroyed by the system (either manually or by
the system to conserve memory)
➤ onRestart() — Called when the activity has been stopped and is restarting again

We will see an example for better understanding the Activity Life Cycle .. 🙂

First we will create a new android project .. by default we will found onCreate() method .. we will add to it a line that print a message on LogCat to know which method will be executed first …

Log.d(tag, “In onCreate() Event”);

and we will add this code that call the rest of events ..

And then we will start debugging … when we see the application .. we will see 3 messages in LogCat

When we press the Back button in android emulator … another 3 messages will be displayed in LogCat

And when we press Home button in android emulator .. another 3 messages will be displayed in LogCat

But when we press the Phone button on the Android Emulator so that the activity is pushed to the background .. we will see that LogCat contain 2 messages

Now press the Back button again to exit phone dialer .. the LogCat will display these messages

Notes :

  • The onPause() event is called in both scenarios — when an activity is sent to the background, as well as when it is killed when the user presses the Back button.
  • When an activity is started, the onStart() and onResume() events are always called, regardless of whether the activity is restored from the background or newly created.

Now we will talk about how to apply Styles and Themes to our Activity … Let’s see 🙂

To apply a dialog theme to an Activity .. we will just add one line in AndroidMainfest.xml inside our Activity

And this will produce the following output …

mmm but how to hide this title bar ?! .. The solution is to go to out onCreate method and under the line of super.onCreate(); .. we will put this line  requestWindowFeature(Window.FEATURE_NO_TITLE);  .. and this is the result … tadaaaa 😀

That’s enough for today .. i hope it’s useful .. enjoy learning 🙂

Hello World – Code Explanation

Okay now i am gonna explain the code in the previous example (HelloWorld) .. 🙂

First let’s explain what’s we write in main.xml file … we write a code for a Button and a TextView .. for the Button we specify its height and width .. and then specify the text that will be showed on it .. and the last thing we write this line
android:id=”@+id/btnShowText”
This line means that the name of this button in our java code will be btnShowText … Let me explain it in another way .. we have a main.xml file which is the UI and we have our .java class which will say “okay when this button is clicked i will show the text” .. how can we connect our UI with our code in .java ?!

The first step is to give the resource (aka Button, TextView, EditView, … etc) a name in our .xml file (as i show you above)

The second step is to tie this resource to a real Button in our .java class .. as this
Button myButton;
myButton=(Button)findViewById(R.id.btnShowText);

Now they are connected .. Congrats 😀 .. Let’s see the rest of our .java class

Here we say when myButton Clicked do the following .. make a Random number .. set the text of myTextView to be Hello World and set the size of the text to a random number from 0 to 74 .. and every time change the text color.

That’s all for now .. if you have any question don’t hesitate 🙂

Hello World

Today we will start our Hello World App .. but first let’s know some important files ..
main.xml — file defines the user interface (UI) of your application     —–>(main.xml exists in layout folder)
AndroidManifest.xml — This is the manifest file for your Android application. Here you specify
the permissions needed by your application, as well as other features
src — Contains the .java source files for your project
gen — Contains the R.java file, a compiler-generated file that references all the resources found in your project. You should not modify this file
assets — This folder contains all the assets used by your application, such as HTML, text files, databases, etc
res — This folder contains all the resources used in your application
strings.xml — file contains strings in our program    —–>(strings.xml exists in values folder)

Okay .. now i will explain a small program .. when we press a button a “Hello World” will be written in a TextView .. simple isn’t it ? 🙂

So the first thing we need to do is to add a button and a label to our UI .. so go to your main.xml file and wrtie the code below

And add this line to your strings.xml file

Show Text

And the last step is to add this code in MainActivity.java in our src folder

And then choose your project from Package Explorer and press F11 to run .. This is the result 🙂

Every time we press the button the text will change its size and color randomly ..

I will explain the code next time .. 🙂