Jump to content
crezzy

Introduction into Android Development [Making a button do something]

Recommended Posts

This tutorial will be a introduction into android development.
This is a basic tutorial and is only meant to help a begginer programmer on the feel of android applications and how they are developed. So don't expect a big application because this is not. Also this will help you to begin with setting the Android into eclipse est.

[B]This application will contain:[/B]
[LIST]
[*]A button
[*]A Toggle Button
[*]A text box
[*]A text label.
[/LIST]

[B]This application will:[/B]
[LIST]
[*]Once you click the toggle button it will change the text in the textbox to a password like text Ie ****=pass
[*]Once you click the normal button it will:
[*]Check what text is in the textbox then:
[*]Change the text in the text label according to what is in the textbox.
[/LIST]


As you can see nothing fancy but it should help some people get started into android development.

[B]What an android application is:[/B]
[LIST]
[*]A mobile software application developed for use on devices powered by Google's Android platform.
[*]This can be anything from a game to a calculator and so on.......
[/LIST]


[B]NOTE:[/B]
I will not be posting loads of pictures. Instead I will use the printscreen application on my computer and I will post the link for you to view if you wish. This makes it allot faster for me to make this tutorial.

[B]Difficulty:[/B] Not hard at all
2/10 Depending on if you know a little about java or not.


[B]1st off what your going to need:[/B]
[LIST]
[*][B]Eclipse IDE[/B]: Which can be downloaded from [url=http://Eclipse.org]Eclipse - The Eclipse Foundation open source community website.[/url]
[*][B]The android SDK[/B]: Which can be downloaded from [URL="http://developer.android.com/sdk/index.html"]developer.android.com[/URL]
[*][B]The eclipse plugin[/B]: I will show you how to get that now:
[/LIST]
[LIST=1]
[*]Start Eclipse, then select Help > Install New Software.
[*]Click Add, in the top-right corner.
[*]In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location:
[CODE]https://dl-ssl.google.com/android/eclipse/[/CODE]
[*]Print screen of what it should look like:[url=http://prntscr.com/12ycvv]Screenshot by Lightshot[/url]
[*]Click OK.
[*]If you have trouble acquiring the plugin, try using "http" in the Location URL, instead of "https"
[*]In the Available Software dialog, select the checkbox next to Developer Tools and click Next.
[*]In the next window, you'll see a list of the tools to be downloaded. Click Next.
[*]Accept the license agreements, then click Finish.
[*]Once the installation completes, restart Eclipse.
[/LIST]


And your done you should now be able to start making [B]android applications Via though eclipse[/B] :)

What I wont be showing you is how to setup the android emulator as I've not set it up my self. I just use my Android phone instead.
[B]Anyway on with the Tutorial:[/B]

[U][B]Part 1: Setting up the project folder[/B][/U]
If this is your 1st time ever starting up eclipse your going to be a bit puzzled at all the features but do not worry you wont be using most of them :p
Well with any project you will 1st have to start a new project(obviously lol).
The way I do it is:
[LIST=1]
[*]Right click on the project explore(The left panel) This should be blank for the moment.
[*]Hover over > new > then click other. Printscreen: [url=http://prntscr.com/12ydjv]Screenshot by Lightshot[/url]
[*]Once you clicked other a wizard will pop up asking you which type of project you would like to create. In the wizard click the android folder and choice the option Android application project. Printscreen: [url=http://prntscr.com/12ydvc]Screenshot by Lightshot[/url]
[*]On the next screen it will ask you for a project/application/package name you should understand what these mean as the names say what they are :P If you don't just copy what I've got Printscreen: [url=http://prntscr.com/12yee3]Screenshot by Lightshot[/url]
[*]From here on just keep clicking next until you can click finish.
[*]Once you have clicked finish you will see your project folder in the project explorer to the left Printscreen:[url=http://prntscr.com/12yesm]Screenshot by Lightshot[/url]
[/LIST]


[B][U]Part 2: Setting up the layout[/U][/B]
This is the boring part and I really don't even want to explain this as its so easy and you should be able to understand what it means just from what each line says.
So I'm not going to lol :D
I will just post the code and you can copy and paste but if you would like to know then ask and I would be happy to explain it to you.
[LIST]
[*]Anyway open up the folder called: layout
[*]and open the file named: activity_main.xml Printscreen: [url=http://prntscr.com/12yg22]Screenshot by Lightshot[/url]
NOTE: Your file may not have that name. Just open the only XML file in that folder.
[*]Once it opens make sure you are in XML view by clicking the file_name.xml Next to the graphical layout at the bottom. Printscreen: [url=http://prntscr.com/12ygbh]Screenshot by Lightshot[/url]
[*]Copy and paste this code into it: [/LIST]
[CODE]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="25dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/etCommands"
android:hint="Type A Command"
android:password="true"
/>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >

<Button android:text="I'm a button! Click me lol!"
android:layout_weight="20"
android:id="@+id/bResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ToggleButton
android:paddingTop="8dp"
android:layout_weight="80"
android:id="@+id/tbPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ToggleButton"
android:checked="true"/>

</LinearLayout>

<TextView android:text="Invalid"
android:id="@+id/tvResults"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>


</LinearLayout>[/CODE]

If you now click on the graphical layout you will now see what it will look like on a mobile screen. Printscreen: [url=http://prntscr.com/12ygmb]Screenshot by Lightshot[/url]
If yours looks like that then you have did it correctly :)

Part 3: On with the java :D
I will post the code with comments all the way though it saying what each line dose:
[CODE]package com.example.rune_server_tut; //PACKAGE NAME.....

//CLASSES THAT WE WILL BE USING
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.InputType;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

//public class means any other class can use it
//MainActivity is the class name
//extends Activity means we will use some methods from the activity class
//implements .......... Means we will use all methods from that class
//Not going to explain fully just use google for full deff
public class MainActivity extends Activity implements View.OnClickListener {

Button button; //Creates a new variable
ToggleButton togButton; //Creates a new variable
EditText input;//Creates a new variable
TextView display;//Creates a new variable

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//Sets the screen view to the activity_main.xml
getXmlVaribles(); //gets the xml variables defined below
togButton.setOnClickListener(this);//checks if the toggle button is clicked
button.setOnClickListener(this);//checks if the button is clicked
}
private void getXmlVaribles() {
button = (Button) findViewById(R.id.bGetResults);//gets the id of the button
togButton = (ToggleButton) findViewById(R.id.tbToggle);//gets the id of the toggle button
input = (EditText) findViewById(R.id.etCommands);//gets the id of the command textbox
display = (TextView) findViewById(R.id.tvResults);//gets the id of the results textbox
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View v) {//Once clicked
switch (v.getId()) { //If what it clicks id is =
case R.id.tbToggle: //If id is the togglebutton
if (togButton.isChecked()) { //if its checked
input.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
//change type of text to *********
} else { //otherwise do this
//change text type to normal
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
break; //end statement for the togglebutton
case R.id.bGetResults: //if id is the normal button
String check = input.getText().toString(); //change textbox data into a string
display.setText(check);
if (check.contentEquals("left")) { //if text in box = left
display.setGravity(Gravity.LEFT); //change text label results location to the left
} else if (check.contentEquals("center")) {//if text in box = center
display.setGravity(Gravity.CENTER);//change text label results location to the center
} else if (check.contentEquals("right")) {//if the text in box = right
display.setGravity(Gravity.RIGHT);//change text label results location to the right
} else if (check.contentEquals("blue")) {//if the text in the box = blue
display.setTextColor(Color.BLUE); //change text label colour to blue
} else if (check.contentEquals("random")) {//if the text in the box = random
Random crazy = new Random(); //Creates a new variable which is random
display.setText("MADNESS :D"); //sets the text to madness :d
display.setTextSize(crazy.nextInt(75));//randomly change text size
display.setTextColor(Color.rgb(crazy.nextInt(265),crazy.nextInt(265),crazy.nextInt(265)));
//randomly changes text color
switch(crazy.nextInt(3)) {
case 0://random number = 0
display.setGravity(Gravity.LEFT); //change text label location to the left
break;
case 1:
display.setGravity(Gravity.CENTER); //change text label location to the center
break;
case 2:
display.setGravity(Gravity.RIGHT);//change text label location to the right
break;
}
} else {//if the textbox dose not contain any of what we have defined then :
display.setText("INVALID"); //text in the label = INVALID
display.setGravity(Gravity.CENTER); //Center the label text
}
break;
}
}
}
[/CODE]

Well that's about it I would make a video and show what it exactly dose but I don't see the need for it tbh lol Can't really see anyone actually making this application. This tutorial is more for understanding some of the text methods that are available to you when you are making an Android application.

[SIZE=4][COLOR="#AFEEEE"]Hope some of you learnt something this :)
Any help just post and I will try to help :D[/COLOR][/SIZE]

Share this post


Link to post
Share on other sites
You could just use android:onClick="methodname" in the layout file when you make the button. Next you should show them how to use [MENTION=85483]String[/MENTION] resources instead of hardcoding the string values.

Share this post


Link to post
Share on other sites
[quote name='Steve']You could just use android:onClick="methodname" in the layout file when you make the button. Next you should show them how to use [MENTION=85483]String[/MENTION] resources instead of hardcoding the string values.[/QUOTE]

Umm 1st part:
I could do but I prefer doing this way :p
2nd part:
That would of made the tutorial more complicated that it should be :p

Thanks for the feedback and how.

Share this post


Link to post
Share on other sites
[quote name='crezzy']Umm 1st part:
I could do but I prefer doing this way :p
2nd part:
That would of made the tutorial more complicated that it should be :p

Thanks for the feedback and how.[/QUOTE]

More complicated? It's considered bad practice doing what you're doing it. You should have a constants class that holds all string variables/titles like you're doing. Such as strings.xml, then you just make calls to them.

This is a good resource for EVERYTHING getting started: [url]http://coding.smashingmagazine.com/2010/10/25/get-started-developing-for-android-with-eclipse/[/url]

Share this post


Link to post
Share on other sites
[MENTION=1045]Emily[/MENTION] must be smoking crack. Enough said.. [MENTION=277]crezzy[/MENTION] Your code is messy but not horrible. Its just her way of saying I don't like you and your code is shit. If she had the intention of giving you real advice she wouldn't be linking you to a tutorial for a total beginner. She probably looks at that site for her own jerba codes l0ols Edited by jediz

Share this post


Link to post
Share on other sites
[quote name='Emily']More complicated? It's considered bad practice doing what you're doing it. You should have a constants class that holds all string variables/titles like you're doing. Such as strings.xml, then you just make calls to them.

This is a good resource for EVERYTHING getting started: [url]http://coding.smashingmagazine.com/2010/10/25/get-started-developing-for-android-with-eclipse/[/url][/QUOTE]

Yes more complicated lol.... It's was more of a java tutorial than me showing you how to write strings in a XML file. I'm sure most people can do that without my help as the code tells you exactly what its doing. I do know it is bad practice but tbh but I prefer to hard code things than to put them in a XML file and then reference them.

[quote name='jediz'][MENTION=1045]Emily[/MENTION] must be smoking crack. Enough said.. [MENTION=277]crezzy[/MENTION] Your code is messy but not horrible. Its just her way of saying I don't like you and your code is shit. If she had the intention of giving you real advice she wouldn't be linking you to a tutorial for a total beginner. She probably looks at that site for her own jerba codes l0ols[/QUOTE]

The code is messy because of the fact I wrote notes all the way though it to explain what is going on lol :p It's a basic java thing its nothing special I think the thread took me longer to write than the actual code lol :p I'm OK at programming android apps but I don't really like doing them. They are boring to code if you ask me. And about her above shes prob never made a android application before she just prob saw the tutorial and went and looked it up lmao :p If she wants to see my coding then she can go to the other RSPS coding forums as I've gave up these forums as they are now dead lol I only came back today to check the Toplist and noticed I had two notifications :L Edited by crezzy

Share this post


Link to post
Share on other sites
[quote name='jediz'][MENTION=1045]Emily[/MENTION] must be smoking crack. Enough said.. [MENTION=277]crezzy[/MENTION] Your code is messy but not horrible. Its just her way of saying I don't like you and your code is shit. If she had the intention of giving you real advice she wouldn't be linking you to a tutorial for a total beginner. She probably looks at that site for her own jerba codes l0ols[/QUOTE]

Or maybe Emily took a class on Android Development? So you might not want to argue with someone who understands more about what they're talking about than you.

[quote name='crezzy']Yes more complicated lol.... It's was more of a java tutorial than me showing you how to write strings in a XML file. I'm sure most people can do that without my help as the code tells you exactly what its doing. I do know it is bad practice but tbh but I prefer to hard code things than to put them in a XML file and then reference them.



The code is messy because of the fact I wrote notes all the way though it to explain what is going on lol :p It's a basic java thing its nothing special I think the thread took me longer to write than the actual code lol :p I'm OK at programming android apps but I don't really like doing them. They are boring to code if you ask me. And about her above shes prob never made a android application before she just prob saw the tutorial and went and looked it up lmao :p If she wants to see my coding then she can go to the other RSPS coding forums as I've gave up these forums as they are now dead lol I only came back today to check the Toplist and noticed I had two notifications :L[/QUOTE]

Commenting, not notes, has nothing to do with how messy your code is. I didn't say any thing regarding your code, but seeing as you think saying you need a constants class means your code is shit, then let us examine your code?

For starters, holy damn learn how brackets format. Secondly, why are you displaying errors? Android has a built in message console called Toast. It's a static access class meaning you can call it directly. Oh wait, "shes prob never made a android application before" so I probably don't have a clue what I'm doing, right? I've been away for awhile in these forums, I know a lot more shit here than most of you uneducated rsps 'programmers' have combined. Want to take up something better than Android Development? I would suggest xCode. The emulator for Android is ridiculous with how slow it is and android merely tries to capture the functionality that IOS offers. In overall terms I can't comment on how shitty your code is, because it is so incredibly easy, that I think the first week of Android Development covers it. I would suggest a 100's English course though, if you really want to get smart. Edited by Emily

Share this post


Link to post
Share on other sites
Lol thanks o.O

Share this post


Link to post
Share on other sites
Niggas should design frontend using HTML, CSS, and Javascript. I can't believe making UI using XML like that is still done like this.

Share this post


Link to post
Share on other sites
[quote name='Emily']Or maybe Emily took a class on Android Development? So you might not want to argue with someone who understands more about what they're talking about than you.



Commenting, not notes, has nothing to do with how messy your code is. I didn't say any thing regarding your code, but seeing as you think saying you need a constants class means your code is shit, then let us examine your code?

For starters, holy damn learn how brackets format. Secondly, why are you displaying errors? Android has a built in message console called Toast. It's a static access class meaning you can call it directly. Oh wait, "shes prob never made a android application before" so I probably don't have a clue what I'm doing, right? I've been away for awhile in these forums, I know a lot more shit here than most of you uneducated rsps 'programmers' have combined. Want to take up something better than Android Development? I would suggest xCode. The emulator for Android is ridiculous with how slow it is and android merely tries to capture the functionality that IOS offers. In overall terms I can't comment on how shitty your code is, because it is so incredibly easy, that I think the first week of Android Development covers it. I would suggest a 100's English course though, if you really want to get smart.[/QUOTE]

To be honest, the emulator is nearly useless. You can just use a real device, and designing one layout using weights is easy enough so you don't really need to worry about screen sizes(sometimes you might have specific cases, but I haven't run into any problems really).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×