2/13/2011

Programmatically creating a Layout Part 1: LinerLayout vertical order

I guess there are a few tutorials on how to create Android layouts using only Java so I'm not really telling you anything totally new but I don't really care. You'll certainly not find any tutorial on this subject featuring an unicorn.

Me and my unicorn friend Fred will try to throw some light on the subject of creating layouts via code in a couple of somewhat shorter parts each dealing with another aspect of the layout creation process.

So lets start. First of all why would you create your layouts via code instead of just using the way more comfortable XML files? Well there is more than one answer to this question.

Some like XML others don't. It is a personal preference. There is absolutely nothing you can't do with Java compared to XML in terms of layouts.

Another point is that creating layouts via code might be more performant as Android does not have parse and create the layout for you as that is what happens in the background when using a layout defined in XML but I think that this is a marginal thing.

Now lets start off with the LinearLayout (yes that is a link to the reference page, use it!).

A short explanation of the concept. If you want to hold more than one object you will need a ViewGroup. That is some kind of container which will hold your views. This ViewGroup which in our case is a LinearLayout needs some layout parameters to know how it should scale inside of the window. After creating the container and defining its layout parameters you create a View like a TextView for example. Set some text and maybe another background color for that view. Define its layout parameters the way you did on the LinearLayout. On a View you have to set the layout parameters by calling the setLayoutParams() method. If all is set for that View add it to the container it should be displayed in using the addView() method on the LinearLayout. You have to pass that method the View you want to add and you may also add the layout parameters as a second parameter (we will cover both methods). Finally you set the LinearLayout containing all of your views as the content view of your Activity by setting it as a parameter on the setContentView() method, the second parameter is the optional LayoutParams object you have defined earlier.

So now that you know the rough concept we'll dive into some code. First we are going to look at a LinearLayout with a TextView, an ImageView and a Button in a vertical order.
public class CodeLinearLayoutVertical extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Grabbing the Application context
        final Context context = getApplication();
        
        // Creating a new LinearLayout
        LinearLayout linearLayout = new LinearLayout(this);
        
        // Setting the orientation to vertical
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        
        // Defining the LinearLayout layout parameters to fill the parent.
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.FILL_PARENT);
        
        // Creating a new TextView
        final TextView tv = new TextView(this);
        tv.setBackgroundColor(0xFFFF00FF);
        tv.setTextColor(0xFF000000);
        tv.setTypeface(null, Typeface.BOLD);
        tv.setText("Where is Fred?");
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        
        // Defining the layout parameters of the TextView
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
        
        // Setting the parameters on the TextView
        tv.setLayoutParams(lp);
        
        // Adding the TextView to the LinearLayout as a child
        linearLayout.addView(tv);
        
        // Creating Fred
        final ImageView iv = new ImageView(this);
        iv.setImageResource(R.drawable.fred);
        iv.setVisibility(View.GONE);
        
        // Defining the layout parameters of Fred
        lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
        
        linearLayout.addView(iv, lp);
        
        // Creating a new Button
        Button btn = new Button(this);
        btn.setText("Call Fred!");
        
        // Setting the Buttons OnClickListener
        btn.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick(View v) {
    
                if (!iv.isShown()) {
                    Toast.makeText(context, "Hello Fred", 
                        Toast.LENGTH_LONG).show();
                    iv.setVisibility(View.VISIBLE);
                    tv.setText("There is Fred!");
                } else {
                    Toast.makeText(context, "I'm already here!", 
                        Toast.LENGTH_LONG).show();
                }
            }
        });
        
        // Defining the layout parameters of the Button
        lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
        
        // Setting the parameters on the Button
        btn.setLayoutParams(lp);
        
        // Adding the Button to the LinearLayout as a child
        linearLayout.addView(btn);
        
        // Setting the LinearLayout as our content view
        setContentView(linearLayout, llp);
    }
}

As you can see most of it is already commented. So I'm not going to repeat all the above. What might be a bit confusing is why we need that Context on line 8. We need it for the Toast messages in the OnClickListener starting on line 57.

On line 23 and 24 I set colors. As you can see the color value is quite interesting. It is a hex value coded like this: 0xAARRGGBB the 0x highlights that it is a hex value. The A stands for the alpha channel, the R for the red channel, the G for the green channel and the B for the blue channel.

With setTypeface(null, Typeface.BOLD) on line 25 I'm making the text bold. Line 27 does nothing else than the android:gravity="center" would do; it sets the text gravity to center.

Line 35 sets the layout parameters for the TextView and line 38 adds our TextView to the LinearLayout. Another interesting line is line 44. Here we are setting the ImageViews visibility to gone which does two things. It will not be visible and it will not take up the space the view would normally take up. I guess you are asking yourself why it is not visible. Well unicorns are very shy creatures.

Line 50 is noteworthy. Remember when we set the layout parameters on the TextView and then added it to the LinearLayout? It is basically the same thing but in one step.

I've mentioned line 57 earlier when I told you why we need a Context object. Now here is what happens inside of this OnClickListener (which is an anonymous inner class in this example. You should be able to name the different elements of a class). First we check if the ImageView is not visible by calling isShown() on it. If it is not visible we first display a Toast message, then set the visibility of the ImageView to visible and change the text of the TextView. If it is visible and the Button is clicked again we display another Toast message.

Last but not least on line 86 we set the LinearLayout as our content view.

Here is how it initially looks like.

Initial state
 When you click the Call Fred! button it will look like this.

Fred appeared!

And this is how it looks like when clicking again.

You're pissing Fred off!

Thats pretty much it. If you want to have such a beautiful unicorn too then visit blapha's site Unicornify!

2/04/2011

Stack Overflow and how not to suck at using it

What is Stack Overflow?

StackOverflow (abbr. SO) is a top-notch programming Q&A site which is moderated by its own community. In fact I think that it is the programming Q&A site but that might be just me (and about 16 million unique visitor per month).

If you have a programming question then SO is the place to go. It is important to keep in mind that SO is not a forum but a Q&A site. So it is structured in questions and answers to the questions. There are no threads.

As a user one of the most important features on SO of course is the search function. You will search a lot with over 1 million unique questions!

If you want to find something related to Android your search syntax looks like this.
[tag] searchterms

You might have noticed the tag in the square brackets. Questions on SO are to be tagged with an appropriate tag. What is appropriate and not is a matter of common sense. If it is a question about Android for example then the tag would be android plus one or two catchwords like listview for instance. This whole tagging is there to group questions into categories and to make searching easier and more efficient. Every tag forms some sort of sub-site called Tag-wiki which is dedicated to the given tag. Those Tag-wikis are very useful. You have several tabs to display the tagged questions in different ways. You can view the newest questions, featured questions, frequently asked questions, questions by votes, questions with recent activity and unanswered questions.

There are a bunch of other functions on Tag-wikis which are beyond the scope of this article.

Now you know how and where to search for information. It is very important that you really perform an in depth search before posting a question. If you really can't find any information on your problem then you get to the most important part which is where you can screw up badly. We all know that English is not a native language for the entire world neither is it mine but that is no excuse for a bad structured question.

How do I ask a question?

Note that this is a blog dealing with Android stuff (and Unicorns) so the following is a bit Android-centric.

No one is going to yell at you just because you misspelled a word or because the grammar is not correct but people will get pissed off if your question is hard to read and has no structure.

Structure your question roughly with those five points in mind.

  1. A short but descriptive title. For gods sake please don't put the tag in the title. That is if you have an Android question then don't do it like the following example as you have the tags for tagging a question. Every time you do that a kitten will die!
    Android: How can I ...
  2. The description of your situation. Try to make the readers clearly understand what you problem is. You can also post images here or code for example.
  3. Post what you have tried so far and why it did not work.
  4. Post the expected result or what you are trying to accomplish.
  5. The question. In one single phrase and as short but descriptive as possible.



Try to use the markdown editor as it is meant to. It is a very powerful tool! There is even a description of what and how to do to format your question using the markdown syntax. Read it and use it!

This is also a very important point. I have mentioned inserting code. Now there are some DOs and DON'Ts.

DOs
  • Follow the Java™ Coding Style Guide (especially the 80 columns per line rule)
  • Only the most important part of your code
  • Correct your indention. If you use tabs instead of spaces to indent code shame on you
  • Short description of what the code is supposed to do if necessary

DON'Ts
  • Make the code box to scroll horizontally checkout the first DO point
  • Post entire files content. Every time you do that a whale will beach!
  • Post code of two different files into the same code block

Now to post well indented and formatted code you just copy and paste the code into the question at the position you want it to be then you select the entire code and hit the Code Sample button (that's the one with the brackets) or you hit Ctrl + K. That's it your code is now wrapped into a code block and highlighted.

If you keep all that in mind you will definitely get a decent answer soon enough. Another important thing to keep in mind is that you can write comments to your question and answers to your question regardless of your reputation (we will discuss reputation later).

By no means try to answer to someones answer or comment as a new answer or fur is going to grow on your palms.

Now if someone posted an answer to your question or possibly even more than one person then it is your duty to accept the most helpful answer by clicking on the green check mark on the left of the answer. If you have over 15 reputation you may also up-vote that particular question in order to signalize to other readers that this answer really helped you. We will talk about voting in a moment.

If your answer remains unanswered for a period of time you can always add more detail to your question by editing it. Add more information about the code or what other things you have tried and failed in the meantime for example.

If you happen to solve your problem yourself without receiving any helpful answer or no answer at all just post the answer to your question yourself! You are encouraged to do so.

How do I answer questions?

If you read a question and know the answer then take your time and answer the question as detailed as possible. Provide sources if you have found the answer on another site for example. The structure and formatting of your answer is equally important as the structure and formatting of your questions so all the above tips, DOs and DON'Ts apply here too.

Now if someone reading your answer thinks that it is helpful he will probably up-vote your answer which will signalize to other readers that this answer indeed is helpful and not just a meaningless collection of characters. It will also increase your reputation by 10 points on answers and by 5 points on questions. However if you post a wrong or unhelpful answer you will most likely get down-voted which reflects negatively on your reputation. If you get down-voted regularly you are doing it wrong.

You can and are even encouraged to up/down-vote answers and question. Don't just up-vote because someone has x reputation or because his avatar looks good. Vote based on the answers or questions content. If you happen to not have any clue about a certain domain don't vote. Only vote if you really understand the answer or question. Down-voting will decrease your own reputation by 1 point which is supposed to prevent people from just down-voting wildly because the feel like doing it. However if you encounter an answer or question earning a down-vote do it. The reputation is just a number but by down-voting where appropriate you will help the entire community but don't exaggerate!

Stack Overflow has a chat?

Indeed! Stack Overflow offers an amazing web chat which is structured into rooms. Most of the interesting programming areas and languages provide a room. You can enter the rooms and read what others are chatting about with 1 reputation and chat yourself with 20 reputation.

I don't know that much about the other rooms policies but I can tell you that there are a few DON'Ts in the Android chat room you should know about and follow in order to not get me mad at you.

DON'Ts
  • Enter the room and burst out your question without even saying hello! It is disrespectful!
  • Post code in the chat or puppies are going to die!
  • Repeat a question multiple times if you don't get an answer to it immediately. We are not there solely for your question!
  • Start a private chat with someone. There is the private room function for that purpose.
  • Post in any other language than English.
  • Post anything disrespectful, illegal, racist and pornographic.
  • Expect someone to answer your programming questions as it is not meant for that. You have to post a question on Stack Overflow and link to that question if you want to discuss something about it.
  • Neither post a question on Stack Overflow and bug every single person in the room to answer your question. You can post it once and wait for a response. If you receive no response then the above rules of how to post a question apply.
  • Pretend that there are no unicorns! You'd be lying and we all know it!
If you keep the rules above in mind we all are going to have a great time together!

Other stuff I should know about?

There is always more to know. Describing the entire site and its functionality is way beyond the scope of this article. There are other resources you should refer to in order to get more information about SO.

Here is a list of helpful resources:
Read and/or use them. No really do it!

I hope I didn't forget anything uber important. I'm sure I did but I'm not perfect. If you think that all the above is bullshit and that I'm making all that up then please leave a comment. Share your opinion on where my understanding of SO sucks.

Now happy asking and/or answering!

11/23/2010

Chromium, Android and the Search Engines

A busy developer should know how to get the most out the available resources and also how to save time where ever he can.

Luckily if you are using Chromium (or Google Chrome) you can at least optimize one part of your daily developer work. Searching for information and especially searching for a reference on a class for example.

The Search Engine function is exactly what you need to save time. You can simply add a search engine yourself which allows you to search directly from your browsers address bar.

To add one yourself click on the wrench icon (menu) and hit Preferences. On the Basic tab click the Manage button almost at the bottom of the windows down where you can set the Default Search. In the new window click the Add... button which again opens a new window with three input fields. Fill in the following lines:
The name you set is what you will see when entering the keyword you set and hitting the tabulator key. The URL is just the site specific search URL in this case the one of the Android developer site. Now just hit Add and you're done.

From now on if you need an information and you need it ASAP just enter dand, hit the tabulator key on your keyboard, enter the search keyword and hit enter on your keyboard. You will get the results you are looking for instantly which helps you saving a few clicks. It may sound like something not really worth it but you save a few clicks every time you need an information.