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
}
Related
When we have the following:
final TextView view = (TextView) layout.findViewById(R.id.some_view);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
view.setText(“blah”);
}
}
//etc
view unless is final can not be accessed by the onClick method.
If this snippet is inside a method then view is allocated on the stack holding a reference to the heap. So in the end of the method call view is lost. Since the onClick is in the same scope the view is still visible. My question is: The onClick can be called at any time. Marking it as final does it ensure that view will not be null when the listener runs? Or is a check for null required? And why is that the case?
It's possible that findViewById() will return null, if your layout doesn't contain R.id.some_view. So you probably need to check that for null.
Where to check for null depends on your app logic. Anonymous OnClickListener object will create inner "view" copy, so even if outer "view" goes out of scope, the listener will still hold reference to the "view" variable.
Where to check for null is your choice, but if you findViewById() return null then you probably should not create click a listener.
I tested your final code
But This Textview is not null.
Maybe layout don't have textview
You have to check textview your layout.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R
.layout.activity_main);
Button button =(Button)findViewById(R.id.button);
final TextView view = (TextView)findViewById(R.id.some_view);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.setText("Hello world");
}
});
}
I mean is like this
button = (Button)findViewById(R.id.button1);
button = (Button)findViewById(R.id.button2);
button = (Button)findViewById(R.id.button3);
button = (Button)findViewById(R.id.button4);
button = (Button)findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//Do same logic
}
});
}
it is possible to define like that? or it can causes force close?
and one question again, it is okay to use copying ID from current xml to another xml? i mean the xml was different but the Widget and ID is same, and define the ID in diferrent class. Because it is more simple to copying than make the Same widget with new Id, it is okay?
example :
so in activityone.xml
i had this
so i was copy the widget to activitytwo.xml so they have a same Widget and ID
it is okay if i do like that?
That will only set the listener for button5. Every time you assign button you lose the previous assignment, so when you set the listener the variable button doesn't know that it used to be pointing to button1 to 4.
So no, it will not work.
I put a comment about your second question.
For that you should have single listener but five different buttons (Objects). Currently you are assigning every button one by one to same button reference. In that way your button will have R.id.button5 and in further code you won't have reference to your previous buttons butons1-4. So, while you add listener to the button you are actually adding listener to only button 5 and any other buttons will not have that listener.
First of all you need five different buttons and you should create class which implement the listener and add instance of that listener to your setOnClicklistener method or in a better way you can just specify android:onClick="onClick" in your xml and add onClick method in your activity.
FOR EXAMPLE :
//.. on create
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
Button button5 = (Button)findViewById(R.id.button5);
ListenerClass listener = new ListenerClass();
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
//..and so on
//Create class
public class ListenerClass implements View.OnClickListener {
//...
#Override
public void onClick(View view) {
//You can determine which button is clicked by view.getId()
//Add logic here
}
}
OR
public class YourActivity implements View.OnClickListener {
//...on create
button1.setOnClickListener(this);
button2.setOnClickListener(this);
//..and so on
//...
#Override
public void onClick(View view) {
//You can determine which button is clicked by view.getId()
//Add logic here
}
}
it is okay to use copying ID from current xml to another xml?
Yes it is fine but you should avoid using that as it may create confusion in a long run. In a wide project working with a team can create problem in understanding that. Other than that you can have same id because when you call findViewById it will refer to the current view and does not get influenced by ids of other views.
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");
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);
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);
}
});