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 !!
Related
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 need to take the values of a and b from the user via editText and pass it to the next activity.This is the first activity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.content.Intent;
import android.widget.EditText;
public class activitysecond extends AppCompatActivity {
private String[] arraySpinner;
private String[] arraySpinner2;
int a=0,b=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
final EditText editText1=(EditText) findViewById(R.id.editText1);
final EditText editText2=(EditText) findViewById(R.id.editText2);
final Bundle bundle = new Bundle();
Button EnterButton=(Button) findViewById(R.id.Enterbutton);
this.arraySpinner = new String[]{
"None", "Lightly Active", "Moderately Active", "Very Active"
};
Spinner s = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, arraySpinner
);
s.setAdapter(adapter);
this.arraySpinner2 = new String[]{
"Male", "Female"
};
Spinner s1 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter1 = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, arraySpinner2
);
s1.setAdapter(adapter1);
editText1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = Integer.valueOf(editText1.getText().toString());
bundle.putInt("one",a);
}
});
editText2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b = Integer.valueOf(editText2.getText().toString());
bundle.putInt("two",b);
}
});
EnterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent z = new Intent(activitysecond.this, activitythird.class);
startActivity(z);
z.putExtras(bundle);
}
});
}
}
The next activity to which i want to pass the values is
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class activitythird extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thirdactivity);
}
public void thirdbutton(View view){
final TextView thirdtext=(TextView) findViewById(R.id.thirdtext);
Bundle seconddata=getIntent().getExtras();
int vala=seconddata.getInt("one");
int valb=seconddata.getInt("two");
thirdtext.setText(vala + " " +valb);
}
}
The values a and b are not passed and the thirdtext does not change
Please help!!
You start your activity before you add the bundle to your intent.
Try it like this:
Intent z = new Intent(activitysecond.this, activitythird.class);
z.putExtras(bundle);
startActivity(z);
Replace this
EnterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent z = new Intent(activitysecond.this, activitythird.class);
startActivity(z);
z.putExtras(bundle);
}
});
with
EnterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent z = new Intent(activitysecond.this, activitythird.class);
z.putExtras("one", Integer.parseInt(editText1.getText().toString()));
z.putExtras("two", Integer.parseInt(editText2.getText().toString()));
startActivity(z);
}
});
Remove ClickListener for the edit texts and add the following lines in onClick of EnterButton
a = Integer.valueOf(editText1.getText().toString());
bundle.putInt("one",a);
b = Integer.valueOf(editText2.getText().toString());
bundle.putInt("two",b);
At the below; putExtra comes before startActivity, that is the mistake.
EnterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent z = new Intent(activitysecond.this, activitythird.class);
startActivity(z);
z.putExtras(bundle);
}
});
try at below code;
EnterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent z = new Intent(activitysecond.this, activitythird.class);
z.putExtras(bundle); // first
startActivity(z); //second
}
});
Also editText onClickListeners should not be implemented for your case. You should get the variables via:
if (editText1.getText() != null) { // to avoid exception
a = Integer.valueOf(editText1.getText().toString());
bundle.putInt("one",a);
}
if (editText2.getText() != null) { to avoid exception
b = Integer.valueOf(editText2.getText().toString());
bundle.putInt("two",b);
}
Modify your code like this:
EnterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent z = new Intent(activitysecond.this, activitythird.class);
z.putExtras(bundle);
startActivity(z);
}
});
z.putExtras(bundle); must be higher that startActivity(z);
When I try running this code it says "Error:not an enclosing class:MainActivity" and "Error:Missing method body or declare abstract." And there's probably a really simple answer for this but I am new to Android and Java programming, so sorry for my noob-ishness.
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent myIntent = new Intent (MainActivity.this, MainActivity2.class);
startActivityForResult(myIntent, 0);
Button btn = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v);
});
}
//Activities
public void onStart() {
super.onStart();
Log.i("TaskActivity", "MainActivity Started");
}
public void onResume() {
super.onResume();
Log.i("TaskActivity", "MainActivity Resumed");
}
public void onPause() {
super.onPause();
Log.i("TaskActivity", "MainActivity Paused");
}
public void onStop() {
super.onStop();
Log.i("TaskActivity", "MainActivity Stopped");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i("TaskActivity", "MainActivity Destroyed");
}
}
The first argument of your Intent() should be the current (enclosing) class. The second argument is the Activity to load. Check the Developer Docs here
If you want to launch MainActivity from MainActivity2, reverse the arguments so your Intent looks like:
Intent myIntent = new Intent(MainActivity2.this, MainActivity.class);
startActivity(myIntent);
The error relating to your method likely comes from using the incorrect syntax when assigning your onClickListener. If you are trying to start MainActivity from a button press, you will need to re-write this section:
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
startActivity(intent);
}
});
You need to implement the onClick Method
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
}
});
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);
}
});
}
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.