I made a basic homepage for my app.
The page consists of a sign in and sign up button.
Only the signup button works. I'm fairly new to android development and I guess I just can see why this is happening.
I checked other questions on stack overflow and decided to make a private handler and then cases however I still have the same problem.
Here is the code. Any help and insight as to why this is happening is much appreciated.
package net.citrusdynamics.citrus;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button signin = (Button) findViewById(R.id.signin);
Button signup = (Button) findViewById(R.id.signup);
signin.setOnClickListener(onclicklistener);
signup.setOnClickListener(onclicklistener);
}
private View.OnClickListener onclicklistener = new View.OnClickListener() {
#Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.signin:
setContentView(R.layout.activity_signin);
Intent signin_intent = new Intent(MainActivity.this, SigninActivity.class);
startActivity(signin_intent);
break;
case R.id.signup:
setContentView(R.layout.activity_signup);
Intent signup_intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(signup_intent);
break;
}
}
};
}
Try to add the click listener like in this code below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button signin = (Button) findViewById(R.id.signin);
Button signup = (Button) findViewById(R.id.signup);
signin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent signin_intent = new Intent(MainActivity.this, SigninActivity.class);
startActivity(signin_intent);
}
});
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent signup_intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(signup_intent);
}
});
}
Related
I am developing an login and register app. The app is supposed to check if the entered data is correct or not while login or register. The login validation works fine but register doesn't validate data. The register button is supposed to check validations after clicking but nothing happens when I click it. There is no error shown in android studio and the app runs fine.
package com.example.investas;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
private EditText inputUsername,inputEmail,inputPassword,inputCpassword;
Button btnRegister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
TextView btn=findViewById(R.id.alreadyHaveAccount);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView btn=findViewById(R.id.alreadyHaveAccount);
inputUsername=findViewById(R.id.inputUsername);
inputEmail=findViewById(R.id.inputEmail);
inputPassword=findViewById(R.id.inputPassword);
inputCpassword=findViewById(R.id.inputCpassword);
btnRegister=findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validations();
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
}
});
}
});
}
private void validations() {
String username=inputUsername.getText().toString();
String email=inputEmail.getText().toString();
String password=inputPassword.getText().toString();
String cpassword=inputCpassword.getText().toString();
if(username.isEmpty() || username.length()<7)
{
showError(inputUsername,"Your username is not valid");
}
else if(email.isEmpty() || !email.contains("#"))
{
showError(inputEmail,"Email is not valid");
}
else if(password.isEmpty() || password.length()<7)
{
showError(inputPassword,"Password must be 7 characters");
}
else if(cpassword.isEmpty() || !cpassword.equals(password))
{
showError(inputCpassword,"Passwords are not matching");
}
else
{
Toast.makeText(getApplicationContext(),"Registration
Successful",Toast.LENGTH_SHORT).show();
}
}
private void showError(EditText input, String your_username_is_not_valid) {
input.setError(your_username_is_not_valid);
input.requestFocus();
}
}
Try to move the following lines out of the OnClickListener, they should be in the onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//...
inputUsername = findViewById(R.id.inputUsername);
inputEmail = findViewById(R.id.inputEmail);
inputPassword = findViewById(R.id.inputPassword);
inputCpassword = findViewById(R.id.inputCpassword);
btnRegister = findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validations();
}
});
// ...
}
Doing it this way is necessary for it to be cleaner and to work.
private EditText inputUsername,inputEmail,inputPassword,inputCpassword;
Button btnRegister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
TextView btn=findViewById(R.id.alreadyHaveAccount);
inputUsername=findViewById(R.id.inputUsername);
inputEmail=findViewById(R.id.inputEmail);
inputPassword=findViewById(R.id.inputPassword);
inputCpassword=findViewById(R.id.inputCpassword);
btnRegister=findViewById(R.id.btnRegister);
TextView btn=findViewById(R.id.alreadyHaveAccount);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validations();
}
});
}
It loads the site but only when it's not in the Action Listener.
Even if I load the url way down in the onCreate method it works.
I already checked if the Webview is invisible, the button listener works also.
The problem began after a few hours of coding other things
WebView wbvCheckURL;
Button btnSearch;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wbvCheckURL = findViewById(R.id.wbvCheckURL);
btnSearch = findViewById(R.id.btnSearch);
wbvCheckURL.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
...
}
#Override
public void onReceivedTitle(WebView view, String title) {
...
}
});
wbvCheckURL.getSettings().setJavaScriptEnabled(true);
wbvCheckURL.setWebViewClient(new WebViewClient());
// When i do it like this it works:
// wbvCheckURL.loadUrl("http://www.google.de");
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
wbvCheckURL.loadUrl("http://www.google.de");
hideKeyboard(v);
LoadCode();
}
});
}
The Solution is to load it in a Thread
Create instance and start the thread:
LoadURL t = new LoadURL();
t.start();
Thread class:
class LoadURL extends Thread {
public void run() {
Classname.wbvCheckURL.post(new Runnable() {
public void run() {
Classname.wbvCheckURL.loadUrl("http://www.google.de");
}
});
}
you can try mine. Mine is another kind of webview with Drawer Layout. Hope you get the idea on how to insert the webview.
package com.example.appname;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ShareActionProvider;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuItemCompat;
public class Provokinc extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provokinc);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://stackoverflow.com/questions/63438455/android-webview-doesnt-load-the-url-from-button-listener");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
ImageButton ImageButton = (ImageButton) findViewById(R.id.imageButtonA);
ImageButton ImageButton1 = (ImageButton) findViewById(R.id.imageButtonB);
ImageButton ImageButton2 = (ImageButton) findViewById(R.id.imageButtonC);
ImageButton ImageButton3 = (ImageButton) findViewById(R.id.imageButtonD);
ImageButton ImageButton4 = (ImageButton) findViewById(R.id.imageButtonE);
ImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int1 = new Intent(Provokinc.this,Main3Activity.class);
startActivity(int1);
}
});
ImageButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int2 = new Intent(Provokinc.this, Main2Activity.class);
startActivity(int2);
}
});
ImageButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int3 = new Intent(Provokinc.this, Provokinc.class);
startActivity(int3);
}
});
ImageButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int4 = new Intent(Provokinc.this, ActivityItemDetails.class);
startActivity(int4);
}
});
ImageButton4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int5 = new Intent(Provokinc.this, MainActivity.class);
startActivity(int5);
}
});
}
#Override
public void onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
} else{
super.onBackPressed();
}
}
}
$ second activity.java
package com.example.applincatio.buttonclick;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;
public class Main2Activity extends Activity {
ImageButton imageButton1;
ImageButton imageButton2;
ImageButton imageButton3;
ImageButton imageButton4;
private WebView mWebView;
ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webviewmoddle);
imageButton1=(ImageButton)findViewById(R.id.MyButton1);
imageButton2=(ImageButton)findViewById(R.id.MyButton2);
imageButton3=(ImageButton)findViewById(R.id.MyButton3);
imageButton4=(ImageButton)findViewById(R.id.MyButton4);
mWebView = (WebView) findViewById(R.id.moddlewebview);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mWebView.setWebViewClient(new myWebclient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("https://www.kluniversity.in/");
}
public class myWebclient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
$ main activity.java
package com.example.applincatio.buttonclick;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton button1;
ImageButton button2;
ImageButton button3;
ImageButton button4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the button in activity_main.xml
button1 = (ImageButton) findViewById(R.id.MyButton1);
button2 = (ImageButton) findViewById(R.id.MyButton2);
button3 = (ImageButton) findViewById(R.id.MyButton3);
button4 = (ImageButton) findViewById(R.id.MyButton4);
// Capture button clicks
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(myIntent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(myIntent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(myIntent);
}
});
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(myIntent);
}
});
}
}
I want to load multiple urls in one webview
for example
if i click on image 1 then i want to open url 1
if i click on image 2 then i want to open url 2
like this each button should have different url
here I loaded only one URL, but I want to add three more URL by using if statements, but I cant change that.please anyone help me
thanks in advance
Use listeners for ur imageViews.
imageButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mWebView.loadUrl("your url");
}
});
I have a page that has two buttons to "two" other pages, the thing is, button 1 and button 2 lead to a second page, thought button 1 should lead to the first page and button 2 should lead to the second page, here is the java file, I don't know how to put it!
ActiveMain.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button1;
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTwo.class);
// you made a mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
}
}
PageOne.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
}
}
PageTwo
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageTwo extends Activity {
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weoff);
}
}
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTWO.class); // you made a
mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
This is my starting activity. I'm trying to get this button to work but it's been giving me this error.
Line 15 button can not be resolved.
package com.synamegames.giveaway;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GiveawayActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button register = (Button) findViewById(R.id.register);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
setContentView(R.layout.main);
}
}
Please try this..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
}
The problem is You have defined the Button instances as register in the line final Button register = (Button) findViewById(R.id.register); But you are setting onclick listener to the button instance which is not defined. You should have
register.setOnClickListener(new OnClickListener() {
instead of
button.setOnClickListener(new OnClickListener() {
U can use android:onClick from xml and pass a the view into the .java file
eg:
android:onClick="bactToList"
in java:
public void bactToList(View view){
}