I'm having a rather strange issue that I just can't solve.
I'm teaching myself Android Eclipse and I've created a very simple looking app with three buttons, 1)Facebook 2)LinkedIn and 3)Favourite Band. However, when I open the app on my phone, I can only press the buttons in order 1) --> 2) --> 3). For example, I can't open 2) unless i've already opened 1) and I can't open 3) unless I've already opened 2) etc. How can I change it so that I can open 3) first for example?
I've posted my java script below, any ideas would be great!
package tabletop.app4asli;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
MediaPlayer thesong;
Button main;
Button main2;
Button main3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = (Button) findViewById(R.id.btn_go);
main.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "https://www.facebook.com/asli.akidil?fref=ts";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
main2 = (Button) findViewById(R.id.button2);
main2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "https://www.linkedin.com/pub/asli-akidil/8a/719/55a.";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
main3 = (Button) findViewById(R.id.button3);
main3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.biffyclyro.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
It's because the code for the layout of button 2 is included in the onClick event of your button 1, and the layout of button 3 is included in the onClicked event of your button 2.
The code of onCreated() function should be looked like follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = (Button) findViewById(R.id.btn_go);
main.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "https://www.facebook.com/asli.akidil?fref=ts";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
main2 = (Button) findViewById(R.id.button2);
main2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "https://www.linkedin.com/pub/asliakidil/8a/719/55a.";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
main3 = (Button) findViewById(R.id.button3);
main3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.biffyclyro.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
You are setting ClickListener for button 3 in the ClickListener of button 2 and ClickListener for button 2 in the ClickListener of button 1. So, button 2 and 3 won't initialize until button 1 and 2 are clicked respectively. You can set OnClickListener to all Buttons in a for loop like this:
ArrayList<Button> buttonList = new ArrayList<Button>();
buttonList.add((Button) findViewById(R.id.button1));
buttonList.add((Button) findViewById(R.id.button2));
buttonList.add((Button) findViewById(R.id.button3));
ArrayList<String> urlList = new ArrayList<String>();
urlList.add("https://www.facebook.com/asli.akidil?fref=ts");
urlList.add("https://www.linkedin.com/pub/asli-akidil/8a/719/55a.");
urlList.add("http://www.biffyclyro.com");
for (int i = 0; i < buttonList.size(); i++) {
final int index = i;
final Button button = buttonList.get(index);
final String url = urlList.get(index);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
Related
Im new to android programming and I am trying to build an app with imageviews that open up a new activity for each image view clicked with an image view of their own. so far i have made my main activity and put 4 image views on it all below each other with constraints. all works fine in the sense of when i open the app, the image views are laid out how they should be and i can click on imageview1, then 2, then 3, then 4 and then any of them. but if i open the app and try to click on imageview 2 first or imageview 3 for example, or imageview 4 then it doesn't work. I have to start at 1 and then go on to 2 and then 3 and 4 and then finally i can navigate freely. How can I fix this? I have posted my main activity java file below before. Any help would be appreciated.
package com.ntq.ntqapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity1 extends AppCompatActivity {
private ImageView ts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
ts = (ImageView)findViewById(R.id.ts1);
ts.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent myIntent = new Intent(MainActivity1.this, imageView1.class);
startActivity(myIntent);
ts = (ImageView)findViewById(R.id.ts2);
ts.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this, imageView2.class);
startActivity(myIntent);
ts = (ImageView)findViewById(R.id.ts3);
ts.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this, imageView3.class);
startActivity(myIntent);
ts = (ImageView) findViewById(R.id.ts4);
ts.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), imageView4.class);
startActivity(myIntent);
}
}
);
}}
);
}});}});}}
You are using the same variable name ts for all the views.
Change to ts1, ts2, ts3, ts4.
Also there are some issues with closing parentheses.
You need to close the parentheses for the onClick method, for the inner classes and for the outer method each time.
ts1 = (ImageView) findViewById(R.id.ts1);
ts.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this, imageView1.class);
startActivity(myIntent);
} //you are missing this
}); //and this
// Notice how I change the variable name to ts2 here...
ts2 = (ImageView)findViewById(R.id.ts2);
//rinse and repeat...
You have two problems. First problem is your end brackets are in the wrong location. The second problem is you are using the same variable. Here is the code corrected:
package com.ntq.ntqapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity1 extends AppCompatActivity {
private ImageView ts1;
private ImageView ts2;
private ImageView ts3;
private ImageView ts4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
ts1 = (ImageView) findViewById(R.id.ts1);
ts1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this,
imageView1.class);
startActivity(myIntent);
}
});
ts2 = (ImageView) findViewById(R.id.ts2);
ts2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this,
imageView2.class);
startActivity(myIntent);
}
});
ts3 = (ImageView) findViewById(R.id.ts3);
ts3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this,
imageView3.class);
startActivity(myIntent);
}
});
ts4 = (ImageView) findViewById(R.id.ts4);
ts4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),
imageView4.class);
startActivity(myIntent);
}
});
}
You are using the same variable for all the views: private ImageView ts
Use 4 different variables (ts1, ts2...) that will fix your sequence issue !!
I am trying to build a login activity using android studio. They error keeps appearing and I don't know what to do about it. I have used } and still the last } has a red underlining.
This is the first time it has happened.
package co5025.example.com.noughtandcrosses;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button butLogin;
public boolean checkPassword() {
TextView edit_password = null;
TextView edit_username = null;
TextView butLogin;
if (edit_username.getText().toString().equals("test") &&
(edit_password.getText().toString().equals("1234")))
return true;
else
return false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Locate the button in activity_main.xml
Button butLogin = (Button) findViewById(R.id.butLogin);
// Capture button clicks
butLogin.setOnClickListener(new DialogInterface.OnClickListener() {
public void onClick(View arg0) {
Intent intent = getIntent();
String value = intent.getStringExtra("key"); //if it's a string you stored.
}
public void onClick(View v) {
if (checkPassword()) {
//Succeed: Load GameActivity.
Intent myIntent = new Intent(MainActivity.this,
GameActivity.class);
startActivity(myIntent);
} else {
//Fail: display error message.
AlertDialog alertDialog = null;
alertDialog.setMessage("Alert message to be shown");
}
}
} //here is the error
You are not closing the setOnClickListener properly
and you are missing one '}'
` #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Locate the button in activity_main.xml
Button butLogin = (Button) findViewById(R.id.butLogin);
// Capture button clicks
butLogin.setOnClickListener(new DialogInterface.OnClickListener() {
public void onClick(View arg0) {
Intent intent = getIntent();
String value = intent.getStringExtra("key"); //if it's a string you stored.
}
public void onClick(View v) {
if (checkPassword()) {
//Succeed: Load GameActivity.
Intent myIntent = new Intent(MainActivity.this,
GameActivity.class);
startActivity(myIntent);
} else {
//Fail: display error message.
AlertDialog alertDialog = null;
alertDialog.setMessage("Alert message to be shown");
}
} // end of onClick method block
*});* // end of DialogInterface.OnClickListener block
} // end of onCreate block
}// end of class block`
Look at the end of DialogInterface.OnClickListener block
you are missing a );
I'm trying to make an APK that saves passwords with the site using two different ArrayLists. This way, I can get the right indexnumber of the site and get the password based on this indexnumber. In the beginning of MainActivity, I add two random Strings to the ArrayLists, so that I don't have to work with empty ArrayLists, but this is utterly useless I think.
The problem is I can only view the last site-password I have put in. Previous combinations are "lost."
code:
MainActivity.java
package com.example.prive.passwordsafe;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public ArrayList<String> passwordList = new ArrayList<>();
public ArrayList<String> siteList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passwordList.add("ejifjejfijeifjeijfiejifjeijfiejfijefie");
siteList.add("iejfijeifjiejfiejidvjijijeijivjiejvijeivjejv");
Button addButton = (Button) findViewById(R.id.addButton);
Button showButton = (Button) findViewById(R.id.showButton);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
firstIntent();
}
});
showButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
secondIntent();
}
});
}
#Override
public void onResume(){
super.onResume();
add();
}
private void firstIntent() {
Intent intent = new Intent(MainActivity.this, addActivity.class);
intent.putStringArrayListExtra("passwordList", passwordList);
intent.putStringArrayListExtra("siteList", siteList);
startActivity(intent);
}
private void secondIntent() {
Intent intent = new Intent(MainActivity.this, showActivity.class);
intent.putStringArrayListExtra("passwordList", passwordList);
intent.putStringArrayListExtra("siteList", siteList);
startActivity(intent);
}
public void add(){
Bundle pickupData = getIntent().getExtras();
if(pickupData == null){
return;
}
String receivedPassword = pickupData.getString("Password");
String receivedSite;
receivedSite = pickupData.getString("Site");
passwordList.add(receivedPassword);
siteList.add(receivedSite);
}
}
addActivity.java
package com.example.prive.passwordsafe;
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.widget.Toast;
public class addActivity extends AppCompatActivity {
public EditText siteInsert, passwordInsert;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toevoeg);
siteInsert = (EditText) findViewById(R.id.siteInsert);
passwordInsert = (EditText) findViewById(R.id.passwordInsert);
siteInsert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast msg = Toast.makeText(getBaseContext(), "site", Toast.LENGTH_LONG);
msg.show();
}
});
passwordInsert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast msg = Toast.makeText(getBaseContext(), "password", Toast.LENGTH_LONG);
msg.show();
}
});
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String password = passwordInsert.getText().toString();
String site = siteInsert.getText().toString();
Intent intent = new Intent(addActivity.this, MainActivity.class);
intent.putExtra("Password", password);
intent.putExtra("Site", site);
startActivity(intent);
}
});
}
}
showActivity.java
package com.example.prive.passwordsafe;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class showActivity extends AppCompatActivity {
public EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toon);
Button showButton = (Button) findViewById(R.id.showButton);
Button backButton = (Button) findViewById(R.id.backButton);
editText = (EditText) findViewById(R.id.editText);
final TextView textView = (TextView) findViewById(R.id.textView);
Bundle pickupData = getIntent().getExtras();
final ArrayList<String> passwordList = pickupData.getStringArrayList("passwordList");
final ArrayList<String> siteList = pickupData.getStringArrayList("siteList");
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast msg = Toast.makeText(getBaseContext(), "site", Toast.LENGTH_LONG);
msg.show();
}
});
if (passwordenList != null && siteList != null) {
showButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int numberOfPasswords = passwordenList.size();
for (int i = 0; i <= numberOfPasswords; i++) {
String password;
String temporary = editText.getText().toString();
if (temporary.equals(siteList.get(i))) {
password = passwordList.get(i);
textView.setText(password);
}else{
password = "wrong input";
textView.setText(password);
}
}
}
});
}else{
return;
}
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
When you exit an Activity, all the data on it is lost. You have to persist your ArrayList in SQLite or use SharedPreferences instead.
SharedPreferences: https://developer.android.com/reference/android/content/SharedPreferences.html
SQLite: https://developer.android.com/training/basics/data-storage/databases.html
i was making an android app and i need to use one integer value in 2 activities. I tried using this code but it didn't work.
//Integer Sender
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("MyIntNameGoesHere", intValue);
startActivity(myIntent);
//Integer receiver
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
It says Cannot resolve symbol intValue and Cannot resolve symbol A and the same for B.
Here's the whole code.
MainActivity:
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int balance;
private SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Hide notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Click counter
final TextView text = (TextView) findViewById(R.id.balance_text);
assert text != null;
// to retreuve the values from the shared preference
preferences = PreferenceManager.getDefaultSharedPreferences(this);
balance = preferences.getInt("balance", 0);
text.setText(balance + " $");
final ImageButton button = (ImageButton) findViewById(R.id.click_button);
assert button != null;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
balance++;
text.setText("" + balance + " $");
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("balance", balance);
editor.apply();
}
});
final Button UpgradesButton = (Button) findViewById(R.id.upgrades_button);
assert UpgradesButton != null;
UpgradesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, UpgradesActivity.class));
}
});
//Balance Integer Sender
}
}
UpgradesActivity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class UpgradesActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upgrades);
//Hide notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
final Button e2u_button = (Button) findViewById(R.id.e2u_button);
assert e2u_button != null;
e2u_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
final Button back_button = (Button) findViewById(R.id.back_button);
assert back_button != null;
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(UpgradesActivity.this, MainActivity.class));
}
});
//TODO: Pass balance integer from MainActivity to here.
}
}
Error code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class UpgradesActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upgrades);
//Receive balance from MainActivity
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("key_int", 0);
//Hide notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
final Button e2u_button = (Button) findViewById(R.id.e2u_button);
assert e2u_button != null;
e2u_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(balance >= 300){ //ERROR ON THIS LINE
}
}
});
final Button back_button = (Button) findViewById(R.id.back_button);
assert back_button != null;
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(UpgradesActivity.this, MainActivity.class));
}
});
//TODO: Pass balance integer from MainActivity to here.
}
}
ALL ABOVE IS ANSWERED! ---------------------------------------------------------
Now i have problems with this part of the code:
#Override
public void onClick(View v) {
if(balance >= 300){
balance -= 300;
}
if(balance < 300){
final TextView text = (TextView) findViewById(R.id.not_enough_money_text);
assert text != null;
text.setText("You do not have enough money.");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
text.setText("");
}
}, 2000);
}
}
When i click the button it says i do not have enough money but i have over 300. Please help me.
I found out what the problem was but I'm not sure how to fix it. I need to send balance back to MainActivity. Can anyone help with that?
Send the data like this -
UpgradesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, UpgradesActivity.class);
intent.putExtra("key_int", balance);
startActivity(intent);
}
});
And fetch it in onCreate() of UpgradesActivity -
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("key_int", 0);
You're using different key when sending and receiving the Int.
Change this line:
int intValue = mIntent.getIntExtra("intVariableName", 0);
To this:
int intValue = mIntent.getIntExtra("MyIntNameGoesHere", 0);
Question 1: I have two activities. I was wondering how to optimize it. I can either create two activities with multiple listeners. Or create multiple Java files for each button(onclick listener)
Question 2: I have tried to create multiple listeners in one Java, but can only get one button to work. What is the syntax for multiple listeners in one Java file? Here is my *updated code: now the issue is no matter what button is clicked on it leads to the same page.
package install.fineline;
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 Activity1 extends Activity2 {
Button Button1;
Button Button2;
Button Button3;
Button Button4;
Button Button5;
Button Button6;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fineline);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
Button1 = (Button) findViewById(R.id.autobody);
Button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
Button2 = (Button) findViewById(R.id.glass);
Button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
Button3 = (Button) findViewById(R.id.wheels);
Button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
Button4 = (Button) findViewById(R.id.speedy);
Button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
Button5 = (Button) findViewById(R.id.sevan);
Button5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
Button6 = (Button) findViewById(R.id.towing);
Button6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
}
}
activity2.java
package install.fineline;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Activity2 extends Activity {
Button Button1;
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autobody);
}
Button Button2;
public void onCreate2(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glass);
}
Button Button3;
public void onCreate3(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wheels);
}
Button button4;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speedy);
}
Button Button5;
public void onCreate5(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sevan);
}
Button Button6;
public void onCreate6(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.towing);
}
}
From the code you posted, all your buttons are leading to the same page because you have the same code in each listener:
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
Where Activity1.class is the class object of the activity you wish to start. If you wanted to start Activity2 from that button, you need to put Activity2.class as the second parameter to Intent.
In your Activity2 example, note that only onCreate() is going to get called, because that method overrides the onCreate() method in the Activity base class. onCreate1, onCreate2, etc. will never be called unless you call them explicitly, because they do not override anything in the base class.
As per your original question, it is fine to create the OnClickListeners inline as you're doing in Activity1, or you could create them as class variables for better organization, and to keep the amount of inline code to a minimum. For example:
OnClickListener button1Listener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
and then you can set your click listener as:
Button1.setOnClickListener(button1Listener);
I would consider creating a new .java file for every OnClickListener overkill.