Android: Passing 2 Input Parameters Through A Method Activated By A View - java

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
}
});

Related

How to select a button by default on activity run?

I want to include two simple buttons in my activity, for example, "Start" and "End".
Now i want "Start" button to be selected by default (with any background color) when i run my activity. How to do that through java in Android studio?
Note:- i am not talking about radio button's small circle. I am talking about simple button.
use setBackgroundColor method.
btn1.setBackgroundColor(Color.GREEN);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// your code here
}
});

Opening datepicker dialog on click of a programmatically created editext?

I've found numerous tutorials on how to accomplish this when you can grab the textview from the xml but nothing on how to implement such a function when you have multiple edit texts created programmatically that all need the same functionality.
After you create the EditText, you would add a click listener the same way you would to any other view. You may also need to disable the focusable attribute to prevent clicks from activating the keyboard:
editText.setFocusable(false);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Open dialog here
}
});
If you have all EditText's, simply create one OnClickListener, call setOnClickListener on every EditText and pass the OnClickListener Object.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//open dialog here;
}
};
editText1.setOnClickListener(listener);
editText2.setOnClickListener(listener);
You should maybe store the EditText's in an array, so that you can just loop through all of them.

Double Click on Button Android

First off this question has been asked multiple times, however, none of these questions have been answered to any extent. I have one example that works in the main activity class:
final Button button = (Button) findViewById(R.id.viewcatalog);
button.setFocusable(true);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.find_item);
}
});
But all of my other attempts to replicate this in sequential pages has resulted in failure. I know the reason that they won't work the same way is that my buttons are instantiated in other classes and not in the host class. What is the correct way to fix this error?
The method that doesn't work for reference:
public void OnClickSearch(View view) {
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText text = (EditText)findViewById(R.id.editText);
String value = text.getText().toString();
setContentView(R.layout.search_results);
}
});
}
It sounds like you are mis-understanding how the UI works in Android.
It is not normally expected that you will change an Activity's view on the fly as your are doing in your OnClickListener.
Instead, you should do one of two things. Either switch to a new Activity, using an Intent and the Activity's startActivity method, or use Fragments, and replace a Fragment in your Activity with a new Fragment.

Android onClickListener - Android Studio bug or my own mistake?

The previous snippet of code has been written down by the aid of random sites and answers from StackOverflow, but somehow not working. I should add that I am an absolute beginner at making apps and my experience with Java is very limited as well.
The errors is the following:
"setOnClickListener": Marked red.
"public void onClick(View v) {": Here "v" is marked red, for some
reason. It continues being red in "String text = v.toString();".
The program also finds my semicolon redundant at the end of the
snippet.
I am using the beta of Android Studio on Elementary OS, using OpenJDK.
Button button_1 = (Button) findViewById(R.id.btn_1);
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String text = v.toString();
displayPassword(text);
}
});
I could have made any number of mistakes, that's for sure. But any nudge in the right direction would be very appreciated.
My suggestion is:Set the onClick in the XML file, and create the method in the current class.
<Button
android:id="#+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click" />
public void click(View arg0) {
}
Agree with Graph that you should have to #Override the onClick method. Not sure what's wrong with it there. In fact, when I typed your example into Android Studio, I got 3 letters into OnClickListener and it automatically filled in the rest, including the #Override.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = v.toString();
// do something with the text.
}
});
Also, I don't think v.toString() is going to get you any useful information. If you want the text off the button, you're going to want to cast it to a button then call getText():
Button button = (Button) v;
String text = button.getText().toString();
or, you could do:
String text = ((Button)v).getText().toString();
Simply calling v.toString() is going to get you a description of that button, not the text on it.
I believe you need to #Override the onClick method.
Button button_1 = (Button) findViewById(R.id.btn_1);
button_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = v.toString();
displayPassword(text);
}
});
Just press " ctrl+shift+o " and add 1 library which you shows on screen prob solved

Android setOnClickListener(this) Error

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

Categories

Resources