New to kotlin/android so excuse my beginner skills.
However, why is View being passed in onClick(v: View) where the type should usually be? (e.g Int, String) etc.
I didn't think this was possible/allowed.
My code:
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
choose_1_player.setOnClickListener(this)
}
override fun onClick(v: View) {
when (v.id){
R.id.choose_1_player -> println("Hello")
R.id.choose_2_player -> println("Hello")
}
}
}
Is it because the function is written in Java and not Kotlin?
Because
1.We click on a View it may be a Button , TextView ,EditText or any layout so on the basis of Integer ID difficult to match with actual views.
2.View has many information including its Id ,that can be used like (Tag , Text etc).
3.Also view has its parent Layout Information can be used .
Please find more details at below link:
Why do we have to add 'View' as parameter in onClick method and what it does?
View class has interface with method onClick(View view).
/**
* Interface definition for a callback to be invoked when a view is clicked.
*/
public interface OnClickListener {
/**
* Called when a view has been clicked.
*
* #param v The view that was clicked.
*/
void onClick(View v);
}
MainActivity class is implementing View.OnClickListener interface, which has the following signature:
public interface OnClickListener {
void onClick(View v);
}
So onClick method is implemeted and it receives View as the parameter.
v - is a view which was clicked.
Addition:
The syntax of the overridden onClick method in Kotlin is the following:
override fun onClick(v: View) { ... }
where v - is a view which was clicked; View - is the data type of object v from Android SDK. It is a parent class for many children, such as Button, TextView etc. That means onClick method can receive a Button, TextView, ImageView and other views that inherit from View. Here works one of the principles of OOP - Polymorphism.
The View attribute inform which View you clicked (every visual component of Android is a View) so you can use this information to identify "who" is calling the onClick.
If in my layout I have 3 buttons (button1, button2, button3) I can do something like this (in Java):
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch(view.id) {
case R.id.button1:
button1Action();
break;
case R.id.button2:
button2Action();
break;
case R.id.button3:
button3Action();
break;
}
}
Or you can do something like this and ignore the View attribute
public class MainActivity extends AppCompatActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button1Action();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button2Action();
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button3Action();
}
});
}
PS.: I didn't run this code. Probably it has some errors.
Related
I am new to Android Programming and I am making a simple browser in which I want to open my web activity by clicking the button but I am amazed to see that setOnClicklistener is not available in Android Studio 3.5 as I have just updated
You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener().
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filters);
Button button = findViewById(R.id.google);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO()
}
}
}
Access/init Your button inside onCreate() method.
private Button btn;
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.home_search_layout);
btn = findViewById(R.id.someId);
setClickListener();
}
private void setClickListener() {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
All the functional elements' code is to be initialized in the activity's onCreate() method. Since you want to make a button clickable, you need to add the setOnClickListener() in the onCreate() method, like so:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectionactivity);
Button button = findViewById(R.id.google); //this id should be of a button not a view
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO do something
}
}
}
I highlighted some text because from the image, you can see that the 9th that you get from the code completion menu is the setOnClickListener but the parent is from the group android.view.View but not from android.widget.Button. Make sure, that R.id.google is a Button and also put the code inside onCreate()
I made one of the simplest programs that creates a login page, however, I cannot add an OnClickListener to my button, and I do not know why. I'm really new to Android Studio and have no idea what to do. When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #Override does not override what's above.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
regist1 = (Button)findViewById(R.id.btnregister1);
regist1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent teachtoregist = new Intent(TeacherInJ.this, TeacherRegisterInJ.class);
startActivity(teachtoregist);
}
});
etUsername = (EditText) findViewById(R.id.editText2);
etPassword = (EditText) findViewById(R.id.editText3);
bLogin = (Button) findViewById(R.id.btnlogin);
bLogin.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #override does not override what's above.
Both the above problems are because you didn't add the implements keyword to your Activity. You need to add it so the Activity can be regarded as OnClickListener interface by the button.
You need to do something like this (See the comments inside the code):
// see below the implements View.OnClickListener line that
// need to be added so the Activitiy can be regarded as the listener.
public class TeacherInJ extends AppCompatActivity implements View.OnClickListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
...
// now you can use this as the listener. It's because you have
// set the current Activity class as the View.OnClickListener
// this is refer to current Activity object.
bLogin.setOnClickListener(this);
}
// Now you can add the #Override to the onClick method from
// the View.OnClickListener.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
}
}
}
i am creating two java file 1st main activity.java file 2nd fragment.java file create button on fragment.java how to click listener written on activity.java help me
fragment.java
public class fragment extends fragment{
Button btn;
// some code
btn = (Button)layout.findviewbyid(R.id.btn1);
}
}
activity.java
public class activity extends Activity
{
// how to access the click action btn here
btn.setOnclicklistner(new View.OnClickLisitner(){
public OnClick(){
}
To use the button in activity from the fragment, you have to use getActivity()
In your fragment,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_select, container, false);
btn = (Button) getActivity().findViewById(R.id.btn);
}
btn is the button in activity
getActivity() in a Fragment returns the Activity the Fragment is currently associated with. (see http://developer.android.com/reference/android/app/Fragment.html#getActivity()).
You can define custom clickListener class and create it's instance in fragment and set listener instance there. Now you can write code in that class. Hope it will help you.
public class MyCustomListener implements OnClickListener{
#override
public void onClick(View v){
// you stuff
}
}
then in your fragment call this
MyCustomListener listener=new MyCustomListener();
btn.setOnClickListener(listener);
Here is my take on the issue, both in Java and Kotlin.
Java:
public final class YourActivity extends AppCompatActivity {
/***/
public final void yourMethod() {
printIn("Printing from yourMethod!")
}
}
public final class YourFragment extends Fragment {
/***/
#Override
public void onViewCreated(View v, Bundle savedInstanceState) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourActivity yourActivity = (YourActivity) getActivity();
yourActivity.yourMethod();
}
}));
}
}
Kotlin:
class YourActivity : AppCompatActivity() {
/***/
fun yourMethod() {
print("Printing from yourMethod!")
}
}
class YourFragment: Fragment() {
/***/
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
button.setOnClickListener {
val yourActivity = activity as YourActivity
yourActivity.yourMethod()
}
}
}
I hope it helps someone out there =)
If I understand your problem correctly, you want to delegate a button click inside a fragment back to its parent activity.
Keep a reference to the parent activity inside your fragment. Then set the listener to your button in your fragment like this:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
parentActivity.doStuff();
}
};
In your parent Activity, define method doStuff():
public void doStuff() {
// handle button click event here
}
i have dynamically added fragment(which is inflated from secfrag.xml) in main activity. there are two buttons in my main activity. one for adding fragment and the other is for changing text of my fragment. i have successfully added fragment to my activity but i can not change the textview text of my fragment. how can i do that.
public class MyActivity extends ActionBarActivity {
Button addFragment;
Button changeFragText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
addFragment = (Button) findViewById(R.id.mybtn);
changeFragText = (Button) findViewById(R.id.mybtn2);
addFragment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Secfragjava secfragjava = new Secfragjava();
getFragmentManager().beginTransaction().add(R.id.mainholder, secfragjava).commit();
}
});
changeFragText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//how can i have the fragment and change its textview
TextView frv=(TextView) getFragmentManager().findFragmentById(R.id.container).getView().findViewById(R.id.mysecText);
frv.setText("raton");
}
});
}
You should create a method inside of your fragment for changing the text.
e.g.
public void changeText(){
//this textview should be bound in the fragment onCreate as a member variable
TextView frv=(TextView) getView().findViewById(R.id.mysecText);
frv.setText("raton");
}
Then in your onClickListener you can just call that method:
((Secfragjava)getFragmentManager().findFragmentById(R.id.container)).changeText();
What is the difference between:
public class MainActivity extends Activity {
public void onCreate (Bundle savedInstanceState) {
button1 = (Button) findViewById(R.id.btn1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Click code
}
)};
}
}
And:
public class MainActivity extends Activity implements OnClickListener {
public void onCreate (Bundle savedInstanceState) {
button1 = (Button) findViewById(R.id.btn1);
button1.setOnClickListener(this);
}
public void onClick(View arg0) {
switch(arg0.getId()) {
case R.id.button1:
// Click code
break;
}
}
}
They have both the exact same functionality and results.
The first method uses an anonymous inner class that implements the interface method. By using this approach, you receive events only for that particular View.
In the second method, you entire Activity class implements the OnClickListener interface. You can set the OnClickListener of every View to this, and receive all the click events in one method, where you can then filter them and act upon them.
The first method translates to:
Button.OnClickListener anonymous_listener = new Button.OnClickListener() { ... };
button.setOnClickListener(anonymous_listener);
Which is to say that it dynamically creates and stores a new OnClickListener instance when you use it.
In the second method, your entire class uses one single instance of the OnClickListener, that is passed to all the Views you want to listen for clicks on.