• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Making an Android App for NOOBS!

tomsfacts

Newbie
Alright folks, I am new to development. I have spent countless hours reading through development guides, practicing in eclipse, reading the android developers guide etc.

I always wished that there was a guide that showed me a simple tutorial that did more than display hello world, but also wasn't too complex for a complete noob to understand.

That's why I have created this simple guide to give a jump start to those who are in the position I once was.

This will show you how to make an app that has two text entry fields (called EditText's), take the user input from those fields, and when a button is pressed it will display the user inputted text on two seperate text display fields (called textView's).

Step 1 - Setup Eclipse and Android SDK

There are lots of simple to follow guides on how to do this so I won't waste my time here. I would look at the guide on developer.android.com

Step 2 - Open Eclipse and Create New Android Application

Go ahead and open Eclipse, and go to file-->New-->Android Project
Project Name: Change Text
Create New Project in Workspace [X]
Use Default Location [X]
Build Target: Use whatever target you can choose, it really does not matter for this tutorial
Application Name: Change Text
Package Name: com.tomsfacts.changetext (this is where you use your dev name/company name, for instance if Motorola made this app, they would put Motorola instead of tomsfacts).
Create Activity: [X] mainActivity
Min SDK Version:Use the corresponding number to what build target you used. (I am using Android 2.0 so I enter in "5" in this field)

Click Finish


Step 3 - Make the XML Layout File

Alright so Android uses Java for all of its coding guts, but for the actual user interface it uses an XML file, which makes it a lot easier to format your applications.

So in the left navigation area you will need to expand your folder called "ChangeText". Navigate to res-->layout-->and double click the main.xml

You should now be looking at the xml file. Make sure the tab in the bottom left is on graphical layout. You should be staring at a TextView that has something like "hello world" displayed on it.

Go ahead and locate the palette on the left hand side. Scroll down to TextView, and drag it over to the xml, placing it below the "hello world".

Now drag over two EditTexts and place them below the TextViews.

Now drag a button over to the xml and place it below the EditTexts.

Essentially what has just happened is Eclipse automatically generated an xml layout file for your application with no need for coding! To see the generated code click on the main.xml tab.

Go to the main.xml tab, and find the first TextView code. It starts with <TextView and ends with /> or sometimes</TextView>. In the first textview, add this line of code:
android:id=
 
Hey, I have a question.. I'm really new to this and I try to understand where I'm wrong.

I have followed your demo from step 3, that because I want to display the text from one EditText box to one TextView.

My main.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="@string/hello"
android:id="@+id/textView"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_marginTop="30dip"/>
<EditText
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:text=""
android:layout_width="fill_parent"
android:layout_marginTop="50dip"
android:maxLines="1"/>
<Button
android:text="Process"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_alignParentRight="true"/>
</RelativeLayout>

> The first TextView is there just to say hello so you can ignore it.

..and my java file looks like this:

package com.androidforums.inputprocess;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class InputProcess extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

};
public void setText(String et){
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText(et);
setContentView(R.layout.main);

final Button but1=(Button)findViewById(R.id.button1);
final EditText et=(EditText)findViewById(R.id.editText1);
but1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setText(et.getText().toString());
}
});
}
}

> There's my problem, it says "Duplicate local variable et"
> I have a quick fix available, to rename it to something else but then the app won't do anything..


Any thoughts?
Thanks in advance
 
Your code confuses me, you have all of these items outside of the onCreate. I'm not sure what this will do, but you should have it be something like

Code:
package com.androidforums.inputprocess;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class InputProcess extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

public void setText(String et){
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText(et);
setContentView(R.layout.main);

final Button but1=(Button)findViewById(R.id.button1);
final EditText et=(EditText)findViewById(R.id.editText1);
but1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setText(et.getText().toString());
}
});
}
}
};

Still not sure if that will fix anything.
 
The reason it says duplicate local variable et, is because in your method setText(String et) you have allready defined et in the method header, and when you then try to define another variable called et, it crashes. so try to rename the final EditText variabel to something else than et and i think you are good to go.
 
Thank you guys for your help!

@norton5315 > Good tip with "outside of the onCreate", that made me think.
@miXer > I knew I should change the name but keeping the code inside the "setText" was wrong :)

so, the working code is:
public class InputProcess extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button but1=(Button)findViewById(R.id.button1);
final EditText et1=(EditText)findViewById(R.id.editText1);
but1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setText(et1.getText().toString());
}
});

};
public void setText(String et){
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText(et);
}
}

Cheers!
 
Thank you guys for your help!

@norton5315 > Good tip with "outside of the onCreate", that made me think.
@miXer > I knew I should change the name but keeping the code inside the "setText" was wrong :)

so, the working code is:
public class InputProcess extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button but1=(Button)findViewById(R.id.button1);
final EditText et1=(EditText)findViewById(R.id.editText1);
but1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setText(et1.getText().toString());
}
});

};
public void setText(String et){
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText(et);
}
}

Cheers!


That looks right, sorry I guess I should have posted my source too. Now that my laptop died it is impossible though :(
 
Back
Top Bottom