final variable in android programming - java

This is the code I am using in android programming
EditText pass1,pass2;
Button register=(Button) findViewById(R.id.register);
register.setOnCllickListener(new OnClickListener(){
public void onClick(View v)
{
passq=(EditText) findViewById(R.id.password_fill);
}
});
But i always get an error:
Cannot refer to non-final variable inside an inner class defined in different method.
Even after I declare the pass1 as final, I get the following error:
The final local variable pass1 cannot be assigned since it is defined in an enclosing type.
But why is this error coming and how can I remove it? I have been encountering it many times.

You have to declare edit text globally. The reason for this is in your activity class you have a method called "onCreate" where you declared the Edit text "pass1" and you trying to define by another pre defined method "setOnClickListener". This is not possible. So you have to declare it globally or as final.

When we use any variable in anynomus class then we have to use final variable.
So use
final EditText passq;
then use it in onClick method.

//declare your editext in global
or
final EditText pass1;
Button register=(Button) findViewById(R.id.register);
register.setOnCllickListener(new OnClickListener(){
public void onClick(View v)
{
pass1=(EditText) findViewById(R.id.password_fill);
or
EditText pass2=(EditText) findViewById(R.id.password_fill);
}
});

Related

Need understanding of implementation of onClick callback in android view

final EditText textview = new EditText(this);
textview.setText("Nothing to display as DB Read failed!!");
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText("Stop clicking me");
}
});
Ideally build should fail since textview object is defined local to my onCreate() method and the object is not passed to onClick() method.
I am relatively new to Java. Could anyone explain how the object is accessible?
1 more thing.. Why do I need to pass View v argument and how is it useful to this method?
The reference to the objects of the outer class, which are accessed from inside the anonymous class, are copied via an auto-generated constructor.
But you can only access variables which are declared final , so they can't possibly be modified by the rest of the outer class once copied.
As for the View v , like SimonR said, it's a reference to the textview itself.
But you will have to cast it to TextView before calling setText(...)
View v is the Element, on which the OnClickListener has been attached to. You can simply change textview within the method to v.
v.setText("Stop clicking me");

Change view on dialog when pressing a button in the dialog

Say i have two views in a layout - a Button and a TextView.
Is it possible to change the TextView text inside the button on click listener.
something like this:
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
textview.settext("changedText");
}
});
The obvious error is that textview is not recognized by the OnClickListener method, and making it final will make it impossible for changing.
you are making final textView so you can not assign this reference to another object
but you can do with any operation on that object.
you can change any property of textview
thanx.
Making the textview final will still allow you to set the text.
Only the assignment can be done once.
The final attribute is just restricting his initialization once. You can access is method after. (If that's what you need)
You can also make a field in your class and it will be available in the listener. Something like:
private TextView textview;
provide some ID to textview in the layout,
create new variable TextView tv=new TextView(); in the same activity where dialogue is created
tv=(TextView)findviewbyid(R.id.ID_OF_TEXT_VIEW);
Then you should be able to use tv.settext("change text.")
declare textview at class level as below
Class CLASSNAME{
public TextView textview;
//your onclickListener code inside method or wherever u have written
}

Android Java: View cannot be applied to ()

I have a couple of EditText that I would like to stay hidden until made visible by one of many methods. To achieve this I was hoping to make my controls visible in it's own public void and call it back.
public void showControls(View view) {
EditText showV = (EditText) findViewById(R.id.vedit);
EditText showH = (EditText) findViewById(R.id.hedit);
showH.setVisability(view.VISABLE);
showV.setVisability(view.VISABLE);
}
//calling on showControls
public void onMethodOne (View view) {
showControls();
}
My output reads:
com.rayman.raysgame.MainActivity.showControls(android.view.View) in com.rayman.raysgame.MainActivity cannot be applied to ()
I don't really understand the output, so I am not sure how am I screwing up the code. I'd like to understand my mistake.
You've declared showControls to take a View as an argument, but you're calling it with no arguments. You might want to change:
showControls();
to:
showControls(view);
However, I don't see that you are using the view itself in showControls. (You will also need to change the spelling of view.VISABLE to view.VISIBLE in that method.)
You need to call showControls(view); instead of showControls(); as your method takes a View as an argument
You have to call the correct function by adding its argument showControls(View view). Its a very basic in programming language although.
And you have to call the findViewById by a proper way. It should be a instance of view.
Use this:
public void showControls(View view) {
EditText showV = (EditText) view.findViewById(R.id.vedit);
EditText showH = (EditText) view.findViewById(R.id.hedit);
showH.setVisability(view.VISIBLE);
showV.setVisability(view.VISIBLE);
}
//calling on showControls
public void onMethodOne (View view) {
showControls(view);
}
Change your code
showH.setVisability(view.VISABLE);
showV.setVisability(view.VISABLE);
to
showH.setVisability(View.VISIBLE);
showV.setVisability(View.VISIBLE);

Android Final vs Private textviews

simple maybe stupid question. I have a login activity which launches another activity, and here's the code:
public class LoginActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
ActionBar actionBar = getActionBar();
actionBar.hide();
Button btnLogin = (Button) findViewById(R.id.btnLogin);
final TextView tvUsername = (TextView) findViewById(R.id.tvUsername);
final TextView tvPassword = (TextView) findViewById(R.id.tvPassword);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tvUsername.getText().toString().length()<1) {
Toast msg = Toast.makeText(LoginActivity.this, "Enter a Username", Toast.LENGTH_LONG);
msg.setGravity(Gravity.TOP|Gravity.LEFT, 0, 70);
msg.show();
tvUsername.requestFocus();
} else if (tvPassword.getText().toString().length()<1) {
Toast msg = Toast.makeText(LoginActivity.this, "Enter a Password", Toast.LENGTH_LONG);
msg.setGravity(Gravity.TOP|Gravity.LEFT, 0, 200);
msg.show();
tvPassword.requestFocus();
} else {
startActivity(new Intent(LoginActivity.this,CrewScheduleActivity.class));
finish();
}
}
});
}
}
My question is about the textviews. Eclipse basically said i had to make them final in order to use them in the onClick event of the button. NP so i did that and it worked.
The question is What is the difference between putting these above the #Override as private vs inside the OnCreate as final?
This has to do with closure in Java. Basically, when you use an anonymous class, the values (not objects themselves) used within it are copied to that class for usage. Therefore, it does not make sense to return or modify those variables within the class, hence they must be final.
However, if the variable is instead part of the class containing the anonymous class, that's different. Basically, your inner class has a reference to the LoginActivity object (as LoginActivity.this), and can use and modify its members and methods.
When you put them "above the #Override", you are making them member variables of the LoginActivity class. Therefore, they can be accessed by the anonymous class.
Succinctly, the difference is that: final variables are local to the method, and copied to the anonymous class; member variables are local to the containing class and are modified by the anonymous class.
If you want to reuse the data from the anonymous class later, use a member variable. If you only need it within onCreate() and the anonymous class, then a final variable will suffice.
When you declare the TextView field (or any field for that matter) as public, it can be accessed directly by any other class, which I don't believe is your intention; there's no reason to make the TextView variable public.
If it is set as private, there is a guarantee that its value won't be overridden in another class, which is what the final keyword was designed to do in the first place. So, simply set it as private, and you won't have to worry about Eclipse correcting you.
So, in summary: making the field private/final ensures that the value cannot be overridden from another class, which is a good design.
Hope this rambling helps. I'll be glad to clarify it better if it doesn't.

access variables of outer class in Java

in Java android application how can i access variables of outer class from the inner anonymous class ?
Example:
ProgressDialog dialog = new ProgressDialog(this);
.....
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
//here i'd like to do something with **dialog** variable
.......
}
});
If the dialog variable is a field of the outer class, you can use this prefixed with the outer class name (a qualified this):
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
ProgressDialog dlg = OuterClass.this.dialog;
.......
}
});
Alternatively, if the dialiog variable is a local variable it needs to be marked as final:
final ProgressDialog dialog = new ProgressDialog(this);
.....
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// The dialog variable is in scope here ...
dialog.someMethod();
}
});
Make the outer local variable (dialog) final so you can refer to it from the inner class.
If it's a local variable (like the signature suggests), it needs to be final for the inner class to be able to access it. If it's a member variable, the visibility modifier needs to be default (no modifier) or higher (protected or public). With private -modifier, it still works, but you might get a warning (depending on your compiler-settings):
Read access to enclosing field SomeClass.someField is emulated by a
synthetic accessor method

Categories

Resources