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) ..


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/ 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
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;

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 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 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 .. 🙂

Now I am 22

mmm … Today 14 – 8 – 2013, I am 22 years old .. Yes I hear some people over there say Happy Birthday To You, But no I am not writing this post for celebrating with my Birthday.

I have some weird thoughts, Why people celebrate with their Birthday even they know that another year passes from their life Which means your life on earth decreases by one year .. And that is not a happy thing to celebrate with !!

There is a hidden conversation between Me and Myself .. I am asking Myself >_>

1) What do you spend your life in ?

2) What do you spend your youth in ?

3) What do you spend your money in ?

4) Do you increase your knowledge in Religion and Science ?

5) Are you Useful for someone ? Helpful for someone ?

6) What do you offer for your Religion ?

7) What do you offer for helping your country and your Islamic Ummah ?

8) Do you know that Allah is keeping an eye “يراقب” on you in all your actions ?

9) What is the changes that you want to make in your life ? and in others life ?

10) What books you plan to read ?

11) When you will improve your behaviours and morals to gain Allah’s Satisfaction and Mercy ?

12) Will you be an effective human ? Will you participate in any charity organization (Resala, Life Makers, … etc.)

A lot of other questions in my mind for you ‘Myself’ .. Because I know that

نفسك إن لم تشغلها بالحق شغلتك بالباطل — ابن تيمية

So the last thing I want to say to Myself .. ” I hope to see you better in all life sides in the next years 🙂 and don’t forget there is another year that passes from your life, you are growing and life doesn’t wait for anyone and always remember Allah is watching you, Keep Moving Forward To Your Goals 😀 ”


Willpower & Determination

Today, I will talk about the meaning of Willpowerقوة الإرادة” and Determinationالعزيمة” and how they could change our life and i will use a real life example to prove my point of view.

The Willpower قوة الإرادة” and Determination “العزيمة” are inner power that enable us to face the challenges and to continue in achieving our GOALS 🙂

“If you think you can do a thing or think you can’t do a thing, you’re right.” – Henry Ford

So, It is all about what you believe and think .. If you have a strong motiveدافع“, You can do it 😀

Try to always motivate and encourage yourself … Set Your GOAL, and Go On You Can Achieve It .. You Can Do It — Ask Allah To Help You, And Trust That He Will — 🙂


And the last thing I want to talk about is the real life examples that I watched and they Motivate, and Inspire me. Here you are this great two videos 🙂

The first example was about Khaldoon Sinjab a Software Engineer that has an accident and he give all of us a lesson in life. A lesson about Never Give Up and Yes You Can whatever obstacles you face.

The second example was about Nick who was born with no arms and no legs. See him when he say ‘I Love Living Life .. I am Happy’ 🙂

I hope that we all change our mind and our life, Stay positive, have faith in Allah and thank him for his graces 🙂


How To Read A Book ?!

When we are kids, a lot of us doesn’t have family that encourages reading. So when we go to study we are always bored and thinking of high marks only and didn’t care about understanding the subject.

Boy studying

Reading is important in our life, with reading we can gain knowledge and success. Many of us(including me :D) doesn’t read in a regular way, Sometimes we read a hole book in a small time .. and other times we don’t read for several months maybe years :(. We need to change our life in this point, We need to care more about Reading … We need to make “Reading A Way Of Life” — “القراءة منهج حياة“.

Today, I am going to talk about How To Read A Book ?! and Can We Really Have A Time For Reading In Our Busy Life ?!

Steps To Read A Book :-

  1. Divide the book to sections or chapters.
  2. When you read, you need to stabilize yourself. (Make yourself comfortable :))
  3. Read the book chapter by chapter or section by section.
  4. Make thing that you want to read portable (Like to have a hard copy of the book, or have it on your own phone or tablet) to read it when you have some free time.
  5. Pay attention to what you read and get rid of distractions “الملهيات” like (phone, TV, Music, … etc).
  6. Get a pencil on your hand while reading to take notes.
  7. Don’t judge a book from the first 1 or 2 chapters (because in most of books they are introduction).
  8. Use your time efficiently, and read in any small chunks of time (Like when you are waiting someone, or in the transportation or anything else).

We need also to implant the love of Reading in children’s life .. If they are used to read, They will never have our problem with reading and maybe they become a great researchers or scientists 😀


By the end of this topic, I will let you watch a great video about How To Read A Book That You Don’t Want To Read It …