Issue
Directory structure of my activity is like below
That's why to get the button id, I wrote below code
View rootView = getLayoutInflater().inflate(R.layout.activity_login, null, false);
btnLogin = (Button)rootView.findViewById(R.id.btnLogin);
But, due to some reasons, when I click button, it does not come inside hander. Am I missing something? My code is below.
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
Complete code of Activity
package com.example.pankajgarg.android.Activities.UserManagement.Auth.Login;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.pankajgarg.android.R;
public class LoginActivity extends AppCompatActivity {
Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = getLayoutInflater().inflate(R.layout.activity_login, null, false);
btnLogin = (Button)rootView.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_SHORT).show();
}
});
setContentView(R.layout.activity_login);
}
}
Solution
The best way you can go about it is by using setContentView(R.layout.activity_login)
in your onCreate()
method and then pull the button with findViewById(R.id.btnLogin)
.
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//insert this line here
setContentView(R.layout.activity_login);
//Pull the button with the id
btnLogin = (Button) findViewById(R.id.btnLogin);
//set onclicklistener
btnLogin.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_SHORT).show();
}
});
}
Answered By - landrykapela
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.