PDA

View Full Version : Android help needed for "onClickListener" demo



youbuntu
April 11th, 2011, 04:04 PM
Hi there. Here is my code:



package com.button.click;

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

public class MainScreen extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView showText = (TextView)findViewById(R.id.showText);
Button genText = (Button) findViewById(R.id.genText);
genText.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showText.setText("Hello");

}
}


The line: showText.setText("Hello");

Shows an error, and does not work. Could someone suggest what is wrong here? I merely want a label to update when I click the button "genText". Thanks

simeon87
April 11th, 2011, 04:22 PM
Does it compile at all? showText is a local variable in onCreate so how do you intend to access it in onClick?

youbuntu
April 11th, 2011, 04:29 PM
I have no idea :( would you mind showing me?

thanks


[_EDIT_]

I changed the code to this:



package com.button.click;

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

public class MainScreen extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button genText = (Button) findViewById(R.id.genText);
genText.setOnClickListener(this);


}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText showText = (EditText)findViewById(R.id.showText);
showText.setText("hello");


}
}


And now it seems to work... but is this the correct way?

Shpongle
April 11th, 2011, 10:36 PM
I recently coded an android project for my final year project.

your better off putting your edit text outside the onCreate just under the
public class MainScreen extends Activity implements OnClickListener line.

Make it private too unless it needs to be public. This way all the required variables are in scope , have the correct access control and it saves code later on. Only create local variables when they are needed


package com.button.click;

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

public class MainScreen extends Activity implements OnClickListener {

//declare ui components
private Button genText = (Button) findViewById(R.id.genText);
private EditText showText = (EditText)findViewById(R.id.showText);


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
genText.setOnClickListener(this);


}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showText.setText("hello");


}
}

youbuntu
April 12th, 2011, 03:01 AM
I recently coded an android project for my final year project.

your better off putting your edit text outside the onCreate just under the
public class MainScreen extends Activity implements OnClickListener line.

Make it private too unless it needs to be public. This way all the required variables are in scope , have the correct access control and it saves code later on. Only create local variables when they are needed


package com.button.click;

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

public class MainScreen extends Activity implements OnClickListener {

//declare ui components
private Button genText = (Button) findViewById(R.id.genText);
private EditText showText = (EditText)findViewById(R.id.showText);


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
genText.setOnClickListener(this);


}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showText.setText("hello");


}
}

So "local" means local access within the method, and not accessible anywhere else, even within that class? And private means accessible anywhere within within the class, but not outside it?

I just dusted off my old Java book - didn't realise HOW rusty I have become! :$

Shpongle
April 12th, 2011, 11:08 AM
So "local" means local access within the method, and not accessible anywhere else, even within that class? And private means accessible anywhere within within the class, but not outside it?

I just dusted off my old Java book - didn't realise HOW rusty I have become! :$

Private means its not accessible from other classes by using MyClass.myvariable , you can only get at from another class it by using a getter method like

getVar(){
return myvariable;
}


your correct about local , its to do with the scope. You can still define them at the top and then initialize them in the onCreate method. But you wont have to re define them anywhere else.

youbuntu
April 13th, 2011, 04:13 PM
Private means its not accessible from other classes by using MyClass.myvariable , you can only get at from another class it by using a getter method like

getVar(){
return myvariable;
}


your correct about local , its to do with the scope. You can still define them at the top and then initialize them in the onCreate method. But you wont have to re define them anywhere else.

Dude, I did what you suggested but I just get a blank screen with no elements...



package com.on.click;

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

public class ScreenOne extends Activity implements OnClickListener {
private Button setText = (Button)findViewById(R.id.setText);
private EditText showText = (EditText)findViewById(R.id.showText);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setText.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showText.setText("Clicked!");
}
}

unknownPoster
April 13th, 2011, 06:10 PM
I just dusted off my old Java book - didn't realise HOW rusty I have become! :$

I believe this is why many people recommended that you go over basic programming in your other thread rather than undertaking such a significant task like Android Development.

youbuntu
April 13th, 2011, 06:35 PM
I believe this is why many people recommended that you go over basic programming in your other thread rather than undertaking such a significant task like Android Development.

Agreed :)

However, this IS basic programming!

Shpongle
April 27th, 2011, 09:23 PM
package com.on.click;

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

public class ScreenOne extends Activity implements OnClickListener {

// declare ui components
private Button Btn1;
private EditText Textbox;

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

//get ui component references
Btn1 = (Button)findViewById(R.id.setText);
Textbox showText = (EditText)findViewById(R.id.showText);

// add listeners
Btn1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Textbox.setText("Clicked!");
}
}

this should work now, I was tired the last time , sorry.