Sunday, November 13, 2011

Change a textView field after creating an Activity

Problem:
I could not remember how to change the textView field after the start of the program. I got an error that was caused by the int to string conversion. I finally got my code to work and I decided to share it.
I added a button to make the application more interactive


Code:

public class Main extends Activity
{

Button add;
TextView tv;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        add = (Button) findViewById(R.id.button1);
        tv = (TextView) this.findViewById(R.id.TextView01);
     
        tv.setText("0"); //the starting value
     
        add.setOnClickListener(new View.OnClickListener()
        {
        int counter = 0;
        String s = "";
       
@Override
public void onClick(View v)
{
counter++;
s =  Integer.toString(counter); //change to string
tv.setText(s); //the text will be changed here.
}
});
}
====

I am sure that I ran into a better method of adding text to the TextView. I just wanted to publish this here as it might help others getting something running. If you can see any areas where I can refine my code then please post it in the comments.
Monday, November 7, 2011

Interview Questions by Chris Foley - Starcraft 2

A teammate in my UNCC SC2 team (and a friend of mine) wanted some help with his paper. I liked the questions and I think that the answers would make a good blog post.

Starcraft Questions
What is Starcraft to you?
A game. A very good one.

What got you into Starcraft?
The pro scene. I always enjoyed video games but I had never looked at videogames as competitive until I found out youtube videos of tournaments played in Korea and WCG. I picked up the game afterward and the more I tried to improve myself (by looking strategies , practicing with mentors and applying stratagies I watched others do). The better I got in the game the more I  enjoyed it.

If you could have time to truly dedicate to Starcraft would you attempt and try to go pro with it?
Not really.. I have a huge respect for the proscene but I don't think that I would enjoy it much.

Do you see yourself playing starcraft for as long as possible?
Not starcraft, But I will surley continue playing videogames forever as long as I can find the games that would let me practice those skills

What is the most difficult part of starcraft for you?
Improve my analytic skills. I tend to get frustrated at games that I lose without knowing the reason why I lost or what should I have done better.

What motivates your to become better at starcraft?
I think a lot of the concepts that I learn from SC can be applied in real life. I see a lot of concept in my real life as game concepts. An example would be time management and how spending more effort on one part would have to be sacrificed on the other side.
Also I realized that I get the same feeling while playing as the one that I get when I am taking a test (a headache from the lack of oxygen and long period concetrating).
There is a lot of pressure when I play and I become extreimly tired. I had felt my heart beats many times and I could not sleep after games because of the adrilanin in my body. Knowing that I can become better at those situations by playing more "games" can only make me happier.


Social Questions
What is your reaction to people who call you a nerd?
I have always liked it and I embrace it more now. I know that it can be looked at negetivily, but I have always enjoyed who I am and I love it when I find other nerds to hang out with. I think that we should all be passionate about something and become nerds for what we love.


Do you have a girlfriend? If not is there any particular reason why not that you care to share?
lolno because of video games!. Ok not really but I know that is the answer that you wanted.

I am from a different culture (I moved to the US 7 years ago) and I don't have a lot in common with most of the people that I meet. It is hard enough for me to make friends due to my narrow interest (but I try!). I see relationships as a higher level of friendship so that narrow it even more. The pool is very small and to be honest, I enjoy the freedom now.

Can I just go and find a random girlfriend? of course, but I do not want to watch a crappy movie and eat dinner somewhere every weekend. I guess there are things I need to scarafice.

Do you consider yourself to have a social life outside of gaming and starcraft?
Yes. I go to clubs whenever they are having any Electronic DJs and tend to be the one bringing everyone together on the go outs.

Do you go or want to go to big gaming events like Blizzcon, MLG, Gamescon, E3, Dream Hack, etc?
YES! Money and time are my only limits. I finally made it to MLG last year even that my crappy car was broken. It was worth evey pennie.

Have you found starcraft or gaming in general to sometimes distract your from other commitments like work or school? If so how often does it happen?
I only play videogames because I don't want to do my work. If it wasn't gaming then I would find something else to proscastinate (like writing this paper)

Do you go to parties or social events outside of gaming like house parties, sports events, frat parties, clubs, etc.?
Yes. It mainly depends on if I enjoy the people going or not. If you know any house parties coming soon then let me know.

Information Questions
What is your current student status (freshmen, sophomore, etc.)?
Senior
Do you have a job?
Yes. I had 2 this semester but dropped one due to an awesome senior project that I want to put more time into it.
Are you a provider/supporter of a family (help pay bills, etc.)?
It is complicated. But yes I support myself and I didn't ask for money in over 5 years now. I am also expected to support my parents once I graduate school.
Thursday, November 3, 2011

An android App in one day (Under construction)

This blog will document my creation of an app in  2 days  one sitting. I will be using my notes on upcoming presentation that I will be giving in a UNCC student Club (C-Programming Union).

9:45 - I started a bit too late due to homework and lots of house shores. I need to work on my time planning better.

What you need:
Set up your enviorment:
I am cheating with this a bit since I have the IDE setup. But none of the steps are hard. Eclipse takes care of lots of the hard work. Use this link to have your up running and build on it.
http://developer.android.com/resources/tutorials/hello-world.html

I say follow the tutorial to get something running. My tutorial will follow the same steps but for another project as I think it would be a waste of time to write the tutorial again.

===
THE PROJECT!
For this project, I will do a greeting card app. The app will be the greeting card and I will just send it to my friend to tell him happy birthday. He is a nerd so he will understand.

Quick sketch of the final result!


LETS GET STARTED!
Create a new Android project following the steps on the hello world sitehttp://developer.android.com/resources/tutorials/hello-world.html#create
These are the Fields that I used ( withg comments //javastyle)

Project name: Greeting Cards //Name of the project
Target: Select a platform version that is equal to or lower than the target you chose for your AVD. (what version to work with. I just put 7 which is the same as 2.1. It doesn't matter much)
Application name:  Greeting Cards // The application name
name: com.cola.greetingcards // this is complicated. think of it as making your own package for use later. 
Create Activity: Main // your main class name


Once you fill up everything, Click on "Finished" and you will be directed to the main class.



package com.cola.GreetingCard;


import android.app.Activity;
import android.os.Bundle;


public class main extends Activity  //I made a mistake with the name of the class. Can you guess it?
{
    /** Called when the activity is first created. 
     * You will put in your code inside it. Think of it
     * As a constructor
     * */
@Override
    public void onCreate(Bundle savedInstanceState) 
    {
//inherit the onCreate from the on in the Activity class    
        super.onCreate(savedInstanceState);   
        //Show 
        setContentView(R.layout.main);
    }


}





Run your code and it would ask you where do you want to show the application. I use the emulator (if it is your first time, It would ask you to create an emulator environment. Just choose a 2.1 one or follow the instructions on the wiki website).

WORKING WITH XML!
Using XML can come pretty handy when creating the UI for android apps as it frees the programmer from the annoyance that comes with dealing with positioning. Separating the layout and display from the behind the scenes can save a lot of time. I hated having to deal with XML at the start but now I will never go back.


Jump to the main.xml file and change the values to make the app display what you want.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Happy Birthday Enrique"
    />
</LinearLayout>


Now run your application and it will display the new changes.
YES!

Now Lets try adding more resources( like text and images) on the canvas

Adding an image:
We need to put the image in the project somehow. We need to put the image in the drawable folder (there are 3 of them, for HD, small display and very small display. Just paste the image in all).

I added an image named android to the HD folder. Then I changed the main.XML by adding this after the text view


<ImageView android:layout_width="wrap_content"
android:layout_height="600px" 
android:src="@drawable/android" 
/>




Run your program and you will see this magic
THIS IS SOME CUTE STUFF

Adding interactivity
I tried adding sound but it doesn't seem as straight forward as I hoped to put in a tutorial. Instead, I will add a button and a toast.
And just to get it out of the way. Toast is a notification that appears to let the user know something. It looks like this:








So to add a button, We should go to the XML and add this part of the code under the ImageView block

<Button 
     android:id="@+id/button" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "Random Fact"
/>


You can compile your code. The button will be added. I will go and add the Toast before I compile.


So back to the main.java (man it have been a while. We didn't do anything in java for a while).

Insert this code in the toCreate block (under the part where you load the R.layout.main)


        //Code to handle what happens when we click on the buttons
        Button randomFact = (Button) findViewById(R.id.button);  //Create the button class
        
        //Create a listener that would detect any clicks and preform action
        randomFact.setOnClickListener(new View.OnClickListener() 
        {
        public void onClick(View v)
        {
        //This 1 line will display the toast
        Toast.makeText(getBaseContext(), "Enrique rocks!", Toast.LENGTH_LONG).show();
        }
        });


AND DONE!

It took 4 hours. I ran into a bug that I didn't understand with the button that just worked magically as well as a half hour or so trying to look into adding audio by using the XML but decided to not bother adding it here. Documenting also took some time with all the screen shots getting organized. I need to come back and clean up this blog a bit more.
Edit: I am aware that the "random fact" button is not really random. I think anyone who knows how to code in java would be able to do it. Let me know in the comments if you want me to add that code.


The app is located here:
http://dl.dropbox.com/u/6446381/android/GreetingCard.apk