How to code multiple button navigation with Java activities - java

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.

Related

Issue with Image View and onClickListener

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 !!

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 :)

Simple Hello World app

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

Android Eclipse - Buttons only open one after the other

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

How to add two buttons in eclipse one for playing an mp3 file and the other for stopping it

I am a newbie in android app development. For now my challenge is to add two buttons in my UI one for playing an mp3 file and the other for stopping it. I am able to do the first successfully and as I try to do the stop one, I see no result. My code is as following:
package ir.polyglotcenter.childrenmostfrequentwords;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer media = MediaPlayer.create(Main.this, R.raw.audio);
media.start();
}
});
//this is to stop the audio flow
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer media = MediaPlayer.create(Main.this, R.raw.audio);
media.stop();
}
});
}
}
Try this code:
public class Main extends Activity {
protected MediaPlayer mMediaPlayer;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mMediaPlayer.prepare();
mMediaPlayer.start();
}
});
//this is to stop the audio flow
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(mMediaPlayer.isPlaying())
mMediaPlayer.stop();
}
});
}
}
Play and Stop all song for all rows.
The Point is implement MediaPlayer in the loop of if or any loop you have:
if(playandstop==true){
mp=MediaPlayer.create(MainActivity.this, resourceIIIDD);
mp.start();
playandstop=false;
}else{
mp.stop();
playandstop=true;
}

Categories

Resources