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);
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 am trying to add a button that will copy the text and i got this error. What is static content. what should i use if I can't use this?
cButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
String text;
text=EditText.getText().toString();
myClip=ClipData.newPlainText("this is the text", text);
Toast.makeText(getApplicationContext(),"Text Sucessfully Copied",Toast.LENGTH_SHORT).show();
}
});
You are calling EditText.getText(). getText() is not a static function. You need to call getText() on an instance of an EditText object.
Something like
(EditText)view.getText()
Same thing with ClipData.newPlainText. You've got to have an object instance before you can call that one.
Firstly, you need to bind your editText by using findViewByID() method and then;
Instead of this,
EditText.getText().toString();
Try to use this:
EditText yourEditText = (EditText) findViewById(R.id.yourEditTextId)
yourEditText.getText().toString();
Simply it's necessary to find the view by ID from your XML layout. Then, you do your work with it.
I'm writing code where in XML I'm using ImageViews and occasionally TextViews to act as buttons. I don't need to do anything with them other than setOnClickListeners in my Java code, so I'm wondering if there's any benefit to leaving them as View objects in Java as opposed to casting them to ImageView or TextView objects.
private ImageView mPlayButton;
mPlayButton = (ImageView) findViewById(R.id.playButton);
mPlayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Audio Play", Toast.LENGTH_SHORT).show();
}
});
as opposed to
private View mCurrentSongButton;
mCurrentSongButton = findViewById(R.id.currentSongIcon);
mCurrentSongButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SongInfo.class);
startActivity(intent);
}
});
I appreciate the assistance.
There is no need to cast a View to an ImageView just to set OnClickListener. Casting an object into a different class doesn't change the object's class i.e an ImageView object will remain an ImageView when cast as a View or even as an Object. And thus same methods would be called regardless the reference wrapper of the object itself.
Purpose of casting any object to a child class should only be to unlock the otherwise unavailable methods. So if you need to call any methods which are dedicated for an ImageView, you will need to cast it to an ImageView before hand (but only so you'd be able to call or use the method).
In short if there is no need to call a child specific method then don't cast the object, it is simply an overhead for the compiler.
If you only need to set click listener, then view type is enough but if you need specialized operations like setText(), setEnabled() etc, then you need to cast them
I'm trying to make a method that is activated when another method gives it an int, and at the same time, the method can also be activated by a view.
Here is the top line of the method and where in Java the method is called:
checkNum(theNumber, null);
public void checkNum (int num, View view){
I tried using "onClick" in the xml for a button, but checkNum did not appear as a suggestion and the app crashed when I ran it. How can I fix this?
Thanks so much!
When using the onClick attribute in XML, the correct signature to use is
public void checkNum (View view)
If you want to pass in a other parameters, I suggest that you set it in Java code.
Add a click listener to your button this way:
Button button = (Button) findViewById(R.id.your_button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something when the button is clicked
}
});
I have created a button in my Android application & I tried to set onclick listner to run onClick method like follows
...
Button btn_ok;
btn_ok = (Button)findViewById(R.id.button1);
btn_ok.setOnClickListener(this);
}
public void onClick() {
EditText uN = (EditText) findViewById(R.id.EditText04);
uN.setText("Clicked!");
}
But Eclipse shows an error & says that "setOnClickListener" need to Cast Argument. After casting it is like this
btn_ok.setOnClickListener((OnClickListener) this);
Then when I'm running the program Emulator says that "Program has stopped unexpectedly"...
How can I solve this problem ?
Make sure that your class implements View.OnClickListener. You can`t just add onClick method, you must implement interface
The signature of your onClick method is wrong, which leads me to believe you're not actually implementing the interface View.OnClickListener.
The signature should be:
public void onClick(View v)
{
//your implementation, v is your button that was clicked
}
Note that the View that was clicked is passed in as an argument, so there's no need to call findViewById from inside your onClick method.
implement the onClickListener from your activity and override the method:
#override
public void onClick(View v)
{
switch(v.getId()){
case R.id.button1:
EditText uN = (EditText) findViewById(R.id.EditText04);
uN.setText("Clicked!");
break;
case default:
break;
}
}
Hope it helps.
setOnClickListener take an OnClickListener instance as parameter and OnClickListener is an interface which content an onClick() method and you are passing here setOnClickListener(this); current context. so you have two option either implements OnClickListener in your activity and second use this way :
this.btn_ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do your work here
}
});
Make sure that you are implementing the interface View.OnClickListener and also pass View to onClick method