Android - Trying to give links to open web in two button - java

I'm new android development, i tried to give links to open webpage for two buttons and i get the error "} expected at line xx" can some one just check any whether i got problem with my java? and fix the closing tag issue too.
package com.domain.app;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public class MyAndroidAppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button_Red);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com"));
startActivity(browserIntent);
}
});
button = (Button) findViewById(R.id.button_Blue);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
}
}

Try these:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
//Try putting one here } OR (see bottom)
public class MyAndroidAppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button_Red);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com"));
startActivity(browserIntent);
}
});
button = (Button) findViewById(R.id.button_Blue);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
}
}
//Or here }

Related

New to android programming, trying to create login button that leads to login screen. When I click the login button, the app says "stopped working"

The Home Activity where the login button is
package com.example.james.assignment1_18094969;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//findview for the login button
findViewById(R.id.button_login).setOnClickListener(new login());
}
onClickListener for the login button to be clicked and take the user to the login page.
class login implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(Home.this, login.class);
startActivity(intent);
}
}
}
The login screen:
package com.example.james.assignment1_18094969;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
Try This and please Confirm Login Activity is declared in the manifest
package com.example.james.assignment1_18094969;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
public class Home extends AppCompatActivity implements View.OnClickListener {
Button Login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Login=findViewById(R.id.button_login);
login.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
}
}
this will be more correct approach to put click Listener's instead of defining inner class for click Listener. Or you can use functions instead.
public class Home extends AppCompatActivity {
Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
loginButton = (Button) findViewById(R.id.button_login);
loginButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
}
});
}
Acitivity names should be used correctly.
Do this!
findViewById(R.id.button_login).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
}
}

Android Studio multiple onCreate methods

I've been trying to make two buttong on my main layout. The buttons are supposed to open a second layout, called barcode_scanner.xml and vragen.xml
However, only the first button opens the scanner. The second button does not do anything.
This is my current code from MainActivity.java
package com.kvprasad.zbarbarcodescanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button scannerButton;
//#Override
//protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// scannerButton = (Button) findViewById(R.id.scannerButton);
//scannerButton.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// Intent intent = new Intent(v.getContext(), BarcodeScanner.class);
// startActivity(intent);
// }
// });
//}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
}
//bovenbouw knop
public class AutoBodyActivity extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.bovenbouw);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);
}
});
}};
}
This is the specific part with the onCreate methods:
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
}
//bovenbouw knop
public class AutoBodyActivity extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.bovenbouw);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);
}
});
}};
}
The button with id: 'scannerButton' works fine, but the button with id: 'bovenbouw'
I don't see any mistakes in the code. What is possibly wrong?
You should declare both buttons in the MainActivity.onCreate(..) method
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
and then register your onClick listeners to open a new Activity.
For your second activity you'd have an own class SecondActivity with its own onCreate method to take care of stuff.
To open the second Activity you just open a new Intent like so
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent)
hope this helps :)

Android Studio Multiple onCreate methods in MainActivity.java

For a school project I am creating a simple app. However, I run into one problem to finalize the app.
I've been trying to make two buttons on my main layout. The buttons are supposed to open a second layout, one called barcode_scanner.xml and another called vragen.xml
However, only the first button opens the scanner. The second button does not do anything.
This is my current code from MainActivity.java
package com.kvprasad.zbarbarcodescanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button scannerButton;
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class bovenbouw extends MainActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.bovenbutton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);}});}}}
I don't see any problems in the code. What am I possibly doing wrong?
Thank you.
Do not make multiple onCreate methods for the same activity. Handle all your button clicks in the same onCreate method. What you can do is this:
package com.kvprasad.zbarbarcodescanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button scannerButton;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next1 = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
Button next2 = (Button) findViewById(R.id.bovenbutton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);}
});
}
}
You don't need 2 onCreate() methods for two buttons. As both of them are in the same layout, you can set onClickListener on both of them in one method only.
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(this, BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
Button next1 = (Button) findViewById(R.id.bovenbutton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(this, Vragen.class);
startActivityForResult(myIntent, 1);
//Make sure the second parameter is not 0, so that you can differentiate between them in onActivityReult method.
}
});
}
}
You don't have to create another class for vragen.class after bovenbutton is pressed.
just create another button for bovenbutton in the main class oncreate method besides the scanner_button
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
Button vragen = (Button) findViewById(R.id.bovenbutton);//button for vragen
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
//here goes the onclicklistener for vragen button
vragen.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);
}
});
}
You're not supposed to register two Activities in single .java file. You must create separate Activity.
package com.example.photosapp
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Cannot Resolve Symbol "OnClickListener"

I am getting the error Cannot Resolve Symbol "OnClickListener" and "Cannot Resolve Symbol "V""
I am starting to get into Java so I am not very good at this and not so familiar with the language
My code:
package com.emiliogaines.counter.counter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I'm not sure why you did that but please move the initializing and listeners to onCreate():
Simply cut these lines of code:
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
and paste them in onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.More);
final TextView mTextView = (TextView)findViewById(R.id.Antal);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mTextView.setText("Some Text");
}
});
}
And then go read the basis of coding and it's standards.
U must store this into onCreate method :
#Override
protected void onCreate(Bundle savedInstanceState) {
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView)findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
}

My Android-Emulator displays emulator error

I made an Android Application. Eclipse isn't reporting me what errors are there in my code.
But if I run my project then emulator displays:
emulator error http://s2.ipicture.ru/uploads/20121106/466GCZH9.png
My Java code is (MainActivity.java):
package ru.startandroid.develop.AppLog;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
TextView myText = (TextView) findViewById(R.id.myText);
Button myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
Button myBtnOK = (Button) findViewById(R.id.myBtnOK);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.myBtnCancel:
myText.setText("Нажата кнопка Cancel");
break;
case R.id.myBtnOK:
myText.setText("Нажата кнопка ОК");
break;
}
}
}
You can only use findViewById() after calling setContentView():
public class MainActivity extends Activity implements OnClickListener{
TextView myText;
Button myBtnCancel;
Button myBtnOK;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}
You need to bind the Widgets (TextView and Buttons) calling findById after the setContentView method, because depends of it:
TextView myText;
Button myBtnCancel;
Button myBtnOK;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}
Change your code like below:
Declare variables at instance level and assign values inside onCreate()
public class MainActivity extends Activity implements OnClickListener{
TextView myText = null;
Button myBtnCancel = null;
Button myBtnOK = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}

Categories

Resources