Android Image Button - java

Android app, i wrote to test image button doesn't work. I have created a image button and implement an event listener for that button. What's wrong with this source code?
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
public class ImageButtonTestApp extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void eventListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}

u have written setOnClickListener in different method but u didnot call that method any where call that method in oncreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eventListenerOnButton();
}
try this one,

Try it
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});

Related

'OnClickListener' is abstract; cannot be instantiated and Cannot resolve symbol 'v'

package com.example.application;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener());
#Override
public void onClick(View v)
Intent intent = new Intent(this, ClaimApplication.class);
startActivity(intent);
}
}
I have error such as 'OnClickListener' is abstract; cannot be instantiated and Cannot resolve symbol 'v'
I am a new programmer so please someone help!
You cannot instantiate abstract classes, so the new View.OnClickListener() gives the error.
As said in the Android Doc (https://developer.android.com/reference/android/widget/Button), try this
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here
}
});
and for keeping the code as simple as possible I suggest to call a method inside the onClick(),
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myMethod();
}
});
}
public void myMethod(){
//codehere
}

Android Webview doesn't load the Url from button listener

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

setOnClickListener shows error

First of all, please don't mark it a duplicate. I found similar question on stackoverflow.com but I could not find my solution. Please look at this image
Screenshot
I am trying to check that EditText should not be empty. But the setOnClickListener is displayed in red. I tried solutions suggested in other similar questions and mentioned import android.view.View.OnClickListener; but still it is not working.
So, any help that could help me solve the problem from anyone is really appreciated.
This is the java file
package com.example.rtrjs.abc;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
getSupportActionBar().setTitle("Sign up");
}
EditText edittext=(EditText)findViewById(R.id.etfn);
Button buton=(Button)findViewById(R.id.breg);
buton.setOnClickListener(new OnClickListener())
}
When it showed error I didnot proceed further.
First you are not inside any method, so you will not be able to assign the listener, second you are missing the onClick method inside de OnClickListener. Try this:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do your stuff
}
});
}
Because you're not in the onCreate() method
EDIT :
Try this way :
package com.example.rtrjs.abc;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
getSupportActionBar().setTitle("Sign up");
EditText edittext=(EditText)findViewById(R.id.etfn);
Button button=(Button)findViewById(R.id.breg);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//process
}
});
}
}
Also I suggest you to use the string.xml file for your "Sign up" in order to be prepared for multi language support ;)
You should write the OnClickListener function code for your Button and EditText in the OnCreate method like this
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
getSupportActionBar().setTitle("Sign up");
EditText edittext=(EditText)findViewById(R.id.etfn);
Button button=(Button)findViewById(R.id.breg);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//process
}
});
}
}
Update your code to this:
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
getSupportActionBar().setTitle("Sign up");
EditText edittext=(EditText)findViewById(R.id.etfn);
Button button=(Button)findViewById(R.id.breg);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
// logic here
}
});
}
}

What makes the app crash when launched on my phone?

I'm trying to figure out how progress bars work so i made a simple app where you have a plus and minus button which change the progress of the progress Bar. No errors in Android studio, but it crashes when launched on my phone (Honor 5X)
package net.gamepickle.rcap_new;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
int progressStatus = 50;
ProgressBar progressBar = (ProgressBar) findViewById(R.id.happiness_progress);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void run(){
Button plus = (Button) findViewById(R.id.happiness_plus);
Button minus = (Button) findViewById(R.id.happiness_minus);
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(progressStatus!=100){
progressStatus += 1;
progressBar.setProgress(progressStatus);
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(progressStatus!=0){
progressStatus -= 1;
progressBar.setProgress(progressStatus);
}
}
});
}
}
Please add your stack trace.
It seems that you are calling
ProgressBar progressBar = (ProgressBar)findViewById(R.id.happiness_progress);
on the class level.
You need to split it.
Declare: ProgressBar progressBar;
and on the onCreate function:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.happiness_progress);
}

Error: setOnClickListener cannot be resolved

I encountered an issue and couldn't resolve in Android Studio. The setOnClickListener remains red and doesn't work unless I get rid of my "loseStarter1" button name.
Note: Starter1 is a button, I'm trying to make it disappear when clicked by the user. My real code starts when I introduce the loseStarter1 button.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
}
Button loseStarter1;
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
})
}
Much appreciated.
You're missing a semicolon to end the new View.OnClickListener() { ... statement as well as that block not being inside of a method.
Not only move this code into the onCreate method, make sure you end it with a semicolon.
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); // Add the semicolon here
It should look like this:
public class game1 extends AppCompatActivity {
Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); //added semicolon
} // ends onCreate method
} // ends class
Your Button variable declaration and OnClickListener initialization is outside of the onCreate() method. Use the following code:
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
}
}
Move below code inside onCreate()
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
This is how i do it.
1) first make the class implement the interface View.OnClickListener, this let you handle the button clic event:
public class game1 extends AppCompatActivity implements View.OnClickListener;
2) second create the interface method to handle event.
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
3) OnCreate methods is the best way to find objects and set properties.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1= (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
all code :
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity implements View.OnClickListener {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
}
Advice:
Rename class to Game1.

Categories