Buttons only work when used after one another - java

So I've got multiple buttons in one activity, and I can only use them one after another. I've read I need to do something with the (OnClickListener) part but I'm unsure what or how to do it. Any help is greatly appreciated. Code is below:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
B5 = findViewById(R.id.button11);
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
EDIT: thanks guys for all the help- out at the moment so when I get home I’ll test these. They look very similar to what I’ve read so they should work.. thanks!

You are setting your click listeners sequentially. If you want all the buttons to be clickable at any time, move the listeners into your onCreate():
protected void onCreate() {
B3.setOnClickListener(){};
B4.setOnClickListner(){};
// etc.
}
A click listener is just that – it "listens" for clicks. Your B3, for example, is the only one listening when the Activity gets created, so all the other buttons will ignore your clicks on them. When B3 is clicked, moveToDanceScheduleMenu() is invoked and B4 starts listening.
I hope that clears things up a bit.
Complete code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5 = findViewById(R.id.button11);
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
}

You can put all your code in OnCreate() method like this
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}

You are making Buttons depended of other Buttons in a Sequential order.
Try Binding Button and their OnclickListners in oncreate.Like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}

I think this code is work
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
private a=false,b=false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
a=true ;
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(a==true ){
moveToWhatsOn();
}
}
});
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
b=true ;
B5 = findViewById(R.id.button11);
}
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(b==true){
moveToMainResultsMenu();
}
}
});
}

If you want to use a button only after a previous button was pressed, you should disable all next buttons in the begining in the onCreate() method and enable them later when the previous button was pressed
B3.setEnabled(true);
B4.setEnabled(false);
B5.setEnabled(false);
And then enable the next button after onClick
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
B4.setEnabled(true);
}
});
And as far for your code
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
private void moveToDanceScheduleMenu() {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn() {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
}

There are several ways to interact with the buttons:
First method: Class implementation
public class Main2Activity extends AppCompatActivity implements OnClickListener {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(this);
B4.setOnClickListener(this);
B5.setOnClickListener(this);
}
public void onClick(View v){
if(v.getId()==B3.getId()){
moveToDanceScheduleMenu();
}else if(v.getId()==B4.getId()){
moveToWhatsOn();
}else if(v.getId()==B5.getId()){
moveToMainResultsMenu();
}
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
Second Method: Private variable (large blocks)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findViewById(R.id.button3).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.button10).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.button11).setOnClickListener(mGlobal_OnClickListener);
}
//Global On click listener for all views
final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
public void onClick(final View v) {
switch(v.getId()) {
case R.id.button3:
moveToDanceScheduleMenu();
break;
case R.id.button10:
moveToWhatsOn();
break;
case R.id.button11:
moveToDanceScheduleMenu();
break;
}
}
};
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
Third Method: Online (small blocks)
public class Main2Activity extends AppCompatActivity{
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}

Related

setOnClickListener button doesn't work Android studio

I'm new in java and I have tried to make a food app in android studio. I have a bar in app and when I click on cart my app is crashing.
Here is my MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView.Adapter adapter, adapter2;
private RecyclerView recyclerViewCategotyList, recyclerViewPopularList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerViewCategoty();
recyclerViewPopular();
bottomNavigation();
}
private void bottomNavigation() {
LinearLayout homeBtn=findViewById(R.id.homeBtn);
LinearLayout cartBtn=findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,CartActivity.class));
}
});
}
And I have this error: at com.example.food.Activity.MainActivity$2.onClick(MainActivity.java:48)
This is CartActivity
public class CartActivity extends AppCompatActivity {
private RecyclerView.Adapter adapter;
private RecyclerView recyclerViewList;
private ManagementCart managementCart;
private TextView totalFeeTxt, taxTxt, deliveryTxt, totalTxt, emptyTxt;
private double tax;
private ScrollView scrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
managementCart = new ManagementCart(this);
initView();
initList();
bottomNavigation();
calculateCard();
}
private void bottomNavigation() {
LinearLayout homeBtn = findViewById(R.id.homeBtn);
LinearLayout cartBtn = findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
}
I don't know what's wrong and why my setOnClickListener doesn't work.
Thank you.
I think your button R.id.homeBtn is not LinearLayout, can you show your xml file?
If your buttons are Button classes, try this
private void bottomNavigation() {
Button homeBtn = findViewById(R.id.homeBtn);
Button cartBtn = findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
}

How to access array of drawables from a method

I have researched similar problems but none is a fit for this problem. Am trying to access the arrays of drawables from the method name called nextQuestion(), but i keep getting the error cannot resolve symbol 'ballArray' any help please. I just feel that this should work but don't know why. Here is my code.
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
The reason you can't access ballArray from within nextQuestion() is because it's declared in a separate method. If you want it accessible from within nextQuestion(), you would need to make it visible at that level:
ImageView mBallDisplay;
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
Do like this:-
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}

Edit the TextView in custom AlertDialog

I have a layout with 10 buttons. When I click one of them a custom dialog shows up, that has 2 textviews - one for the title and one for the quote.
There's always an instant crash when I try to edit the TextView programmatically. The program crashes when I call tv1.setText.
I tried to create an another function called Qdialog. When I call it the program crashes as well.
package com.example.lud.fortunecookie;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Menuslection extends AppCompatActivity {
Button btback, bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10;
TextView tv1,tv2;
AlertDialog quotesalert;
AlertDialog.Builder quitbuilder,quotesbuilder;
int numb = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menuslecton);
char stline;
bt1 = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt4 = (Button) findViewById(R.id.button4);
bt5 = (Button) findViewById(R.id.button5);
bt6 = (Button) findViewById(R.id.button6);
bt7 = (Button) findViewById(R.id.button7);
bt8 = (Button) findViewById(R.id.button8);
bt9 = (Button) findViewById(R.id.button9);
bt10 = (Button) findViewById(R.id.button10);
btback = (Button) findViewById(R.id.buttonback);
btback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
quitbuilder = new AlertDialog.Builder(Menuslection.this);
quitbuilder.setMessage("Are you sure to quit ?");
quitbuilder.setTitle("Are you already got my advise.");
quitbuilder.setCancelable(false);
quitbuilder.setNegativeButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//finish(); //dung de thoat khoi activity hien tai
Intent iquit = new Intent(getApplicationContext(), MainActivity.class);
startActivity(iquit);
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
finish();
}
});
quitbuilder.setPositiveButton("no", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog Alertquit = quitbuilder.create();
Alertquit.show();
}
});
//View layout = inflater.inflate(R.layout.dialoglayouttest,null);
//View layout = inflater.inflate(R.layout.customdialog, null);
// quotesbuilder.setView(tv1);
// View content = inflater.inflate(R.layout.dialoglayouttest, null);
// quotesbuilder.setView(content);
//TextView tv1 = (TextView) content.findViewById(R.id.dia_tit);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numb=1;
Qdialog();
quotesalert.show();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(2);
quotesalert.show();
}
});
bt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(3);
quotesalert.show();
}
});
bt4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(4);
quotesalert.show();
}
});
bt5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(5);
quotesalert.show();
}
});
bt6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(6);
quotesalert.show();
}
});
bt7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(7);
quotesalert.show();
}
});
bt8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(8);
quotesalert.show();
}
});
bt9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(9);
quotesalert.show();
}
});
bt10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Qdialog();
// numb=10;
// quotesalert.show();
}
});
}
public int numbslection(int numb)
{//Slection Tittle Classic setup
if(numb==10)
numb=1;
//tv1.setText("");
else
if(numb==1||numb==2)
tv1.setText(R.string.Pgud);
else
if(numb<7&&numb>2)
tv1.setText(R.string.OK);
else
tv1.setText(R.string.Upset);
return numb;
}
public void Qdialog()
{
quotesalert.setContentView(R.layout.dialoglayouttest);
tv1 =(TextView) findViewById(R.id.dia_tit);
numbslection(numb);
quotesbuilder = new AlertDialog.Builder(Menuslection.this);
quotesalert=quotesbuilder.create();
}
}
To use custom layout to AlertDialog, you have to inflate layout and set view to AlertDiaolog:
public void Qdialog()
{
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialoglayouttest, null);
quotesbuilder = new AlertDialog.Builder(Menuslection.this);
quotesbuilder.setView(dialogView);
EditText editText = (EditText) dialogView.findViewById(R.id.dia_tit);
quotesalert = quotesbuilder.create();
quotesalert.show();
}
ALTERNATIVE:
If you want to use custom layout, use **Dialog** instead of **AlertDialog** :
public void Qdialog()
{
Dialog dialog = new Dialog(this)
dialog.setContentView(R.layout.dialoglayouttest);
dialog.setTitle("Title...");
TextView text = (TextView) dialog.findViewById(R.id.dia_tit);
dialog.show();
}
Hope this helps.

Get certain intents

So lets say i have 4 buttons , and each button contains an intent which navigates to an activity.They all navigate to a single same activity . When i click the first button i want that new activity to show "Hi" , When i click the second button i want it to show "Bye" and so on . How do i do that ?
Here is a simple code to start with
public class Intentt extends Activity {
Button b1,b2,b3,b4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button)findViewById(R.id.button2);
b2 = (Button)findViewById(R.id.button3);
b3 = (Button)findViewById(R.id.button4);
b4 = (Button)findViewById(R.id.button5);
b1.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b2.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b3.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b4.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
}
Just pass some data through intent extras, or intent extra Bundle:
Intent i=new Intent(context,MainActivity.class);
i.putExtra(id, 453);
context.startActivity(i);
Example:
Button b1, b2, b3, b4;
Context context = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button) findViewById(R.id.button2);
b2 = (Button) findViewById(R.id.button3);
b3 = (Button) findViewById(R.id.button4);
b4 = (Button) findViewById(R.id.button5);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, MainActivity.class);
i.putExtra(MainActivity.ID_ACTION, MainActivity.ACTION_1);
startActivity(i);
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, MainActivity.class);
i.putExtra(MainActivity.ID_ACTION, MainActivity.ACTION_2);
startActivity(i);
}
});
}
And then in that activity you can switch on that values getting them like that:
String id = intent.getStringExtra("id");
int id = intent.getIntExtra("id",-1);
and then use the if-elseif-else or switch statement(s) to change the actions based on the passed action, so you can display your messages
To properly recieve them you need to set activity's launchMode to "standard" because of this bug in AndroidManifest.xml and create
public static final int ACTION_1 = 1;
public static final int ACTION_2 = 2;
public static final int ACTION_NULL = -1;
public static final String ID_ACTION = "action_id";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int id = getIntent().getIntExtra(ID_ACTION, -1);
if (id == ACTION_NULL) {
Log.d("TAG", "id is null");
Toast.makeText(MainActivity.this, "id is null!", Toast.LENGTH_SHORT).show();
} else if (id == ACTION_1) {
Log.i("TAG", "ALLOHA! from button 1");
Toast.makeText(MainActivity.this, "Aloha from button 1!", Toast.LENGTH_LONG).show();
} else if (id == ACTION_2) {
Log.i("TAG", "Hello from button 2");
Toast.makeText(MainActivity.thi,"Hello from button 2!", Toast.LENGTH_LONG).show();
}
}
In each intent you could pass parameterts
Intent i=new Intent(context,SendMessage.class);
i.putExtra("KEY_MESSAGE", "Hola amigo");
In the other activity
Intent i = getIntent();
String message = i.getStringExtra("KEY_MESSAGE")
Toast.make(this,message,Toast.LENGTH_SHORT).show()
Re write the button click listener as follows :
b1.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","Hi");
startActivity(i);
}
});
b2.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","Hello");
startActivity(i);
}
});
b3.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","Bye");
startActivity(i);
}
});
b4.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","See you");
startActivity(i);
}
});
In your MainActivity class onCreate Method, you can access the passed data by :
String passedData = getIntent().getStringExtra("Data");
you can use this passedData to display on screen.
Do it like this:
public class Intentt extends Activity implements View.OnClickListener {
Button b1,b2,b3,b4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button)findViewById(R.id.button2);
b2 = (Button)findViewById(R.id.button3);
b3 = (Button)findViewById(R.id.button4);
b4 = (Button)findViewById(R.id.button5);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button2:
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra('"STRING_I_NEED"', "Hi");
startActivity(i)
break;
case R.id.button3:
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra('"STRING_I_NEED"', "Bye");
startActivity(i)
break;
case R.id.button4:
break;
.
.
.
}
}
}
After that in your mainActivity retrieve string that you sent with:
Bundle extras = getIntent().getExtras();
String s = extras.getString("STRING_I_NEED");

illegal start of expression

It says Expression expected but no recommendations as to what symbol is missing. I'm using android studio. I suppose it's a simple fix I'm just not sure what symbol.
Heres my code:
public class CloudActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
},
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
}, // Where the problem is
));
}
#Override
public void onClick(View v) {
}
}
You are closing your anonymous inner classes (new View.OnClickListener() {) wrong. You shouldn't be separating them with a comma but closing them off with a );.
For example,
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
match up your {} and () for all of them. So you will want to do the same thing for button3.setOnClickListener(new View.OnClickListener() { and any others you might have.
You can read more about them in the Java Docs
Try This. You weren't closing your inner classes properly. I imagine you accidentally put commas in place of semi-colons?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
});
}

Categories

Resources