Button one should aim to page one - java

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

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
}

I am new in android development. I have a problem

From the main page, i click the image and it will open the question 1. Now, I trying to open a new activity which is question 2 using image view from the question 1. But it has an error: question 1 is not an enclosing class.
Here is the code for Main Activity.
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//button video to video page
b = (ImageView) findViewById(R.id.btnPlayVideo);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, VideoPage.class);
startActivity(i);
}
});
//button start test to question1
b = (ImageView) findViewById(R.id.btnStartTest);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Question1.class);
startActivity(i);
}
});
//button question 1 ke question 2
b = (ImageView) findViewById(R.id.btn1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Question1.this, Question2.class);
startActivity(i);
}
});}}
This is question 1.java. it said that it is not an enclosing class.
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Question1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
}
}
This is question 2.java
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Question2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
}
}
Intent i = new Intent(Question1.this, Question2.class);
startActivity(i);
You are trying to start Question2 from the context of Question1 within MainActivity. The first parameter of new Intent() should be the context of the enclosing class that will start the next activity. Since you are defining this in MainActivity, the context should be MainActivity.this. There is a great introduction to this topic here.
Either change the first parameter of new Intent() to "MainActivity.this" or move the entire second OnClickListener to a view that is within Question1.

how to open multiple ulr in one webview (not once )

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

Buttons doenst work, no errors given

I am working on a new app, and copied a little bit of the code of my other working app. The errorlog and problems-log doens't give me any errors, but when I press the buttons (test/test1/test2) nothing is happening.
MenuScreen.java:
package com.example.random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MenuScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("DEBUG", "test");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
startActivityForResult(intent, 0);
}
});
findViewById(R.test1).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test1");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
}
});
findViewById(R.test2).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test2");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
}
});
findViewById(R.id.verlaat_app).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test3");
MenuScreen.this.finish();
}
});
}
}
MenuScreen.java:
package com.example.random;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class FotoMaker extends Activity
{
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.testpic);
iv = (ImageView) findViewById(R.id.imageView);
Button btn = (Button) findViewById(R.id.testpic);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick (View v){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 0)
{
Bitmap theImage = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(theImage);
}
}
}
CHECK OUT THIS ANSWER i have made some corresctions....
package com.example.random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MenuScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById (R.id.test);
Button btn2 = (Button) findViewById (R.id.test1);
Button btn3 = (Button) findViewById (R.id.test2);
Button btn4 = (Button) findViewById (R.id.verlaat_app);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("DEBUG", "test");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
startActivity (intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test1");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
startActivity (intent);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test2");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
startActivity (intent);
}
});
btn4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test3");
MenuScreen.this.finish();
}
});
}
}

Two buttons, it mix between the links

Two buttons, it mix between the links
I have a page with two buttons, when I had the first button, it took me to the link, when I had the second button, the first stopped and the second took me to the second link, help!
PageOne.jave
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
setContentView(R.layout.weoff);
}
}
MainActivity.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 button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.abus);
button = (Button) findViewById(R.id.weoff);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
}
}
Here
button = (Button) findViewById(R.id.abus);
button = (Button) findViewById(R.id.weoff);
It replaces the old reference to R.id.weoff so you will add the listener to weoff, two times which replaces the old listener and you set it with a listener which points to PageOne.class.
You should use two variables
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
and
button1.setOnClickListener(new OnClickListener() {
button2.setOnClickListener(new OnClickListener() {
Since the two listeners point to the same Intent, you could create the listener only one time and put the same?

Categories