I have a class called Menu and inside that class I want to place a menu of buttons such as buttonA, buttonB, buttonC, and so on. However when I run the app on my phone I cant tap buttonB before I tap buttonA. If I tap buttonA first, I can choose buttonA or buttonB all I want. The question is how do you separate the buttons in the Menu class to be able to tap any button at any time?
package com.emods.app1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
}
});
}
}
You need to take your btnB and place it outside your onClick event for btnA. Currently you have your declaration for btnB inside your onClick event for btnA.
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
You defined the listener for the second button inside the listener for the first one.
Nesting issue.
Change:
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
}
});
to:
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
That should fix it.
Only the brackets are wrong:
package com.emods.app1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
}
}
You should activate the automatic formatting in your IDE.
Related
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.
I want to add a difficulty in my game, for example when I click a category like category "A" it will show to the next layout and the user has to pick a difficulty to play "easy, normal or hard" .
sorry for my bad sentence
here is my categories:
public class CategoryActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
Button A = (Button) findViewById(R.id.btn_a);
Button B = (Button) findViewById(R.id.btn_b);
Button C = (Button) findViewById(R.id.btn_c);
Button D = (Button) findViewById(R.id.btn_d);
Button E = (Button) findViewById(R.id.btn_e);
Button F = (Button) findViewById(R.id.btn_f);
Button G = (Button) findViewById(R.id.btn_g);
Button H = (Button) findViewById(R.id.btn_h);
Button I = (Button) findViewById(R.id.btn_i);
Button J = (Button) findViewById(R.id.btn_j);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
B.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg1) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
C.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg2) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
D.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg3) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
E.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg4) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
F.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg5) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
G.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg6) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
H.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg7) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
I.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg8) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
J.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg9) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
}
You should use radio buttons, so the user can select only one option and not two or more.
This is an example taken from the official android devs website.
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
PS: You're question is not really clear. It makes it difficult to help you. I hope my answer is acceptable. Refer to the manuals and do some tutorials.
I am new to coding and am trying to work on a project when my main Activity page has a series of buttons. From these buttons I would like each to open a different activity or command. I been searching and found what I thought SHOULD work, however, it does not. I get a crash when it comes loading the app and then clicking on the button. Below is the code. Any pointers to show my mistake somewhere would be kindly appreciated.
package com.example.finalproject2;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize Buttons
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button02);
Button b3 = (Button) findViewById(R.id.button03);
Button b4 = (Button) findViewById(R.id.button01);
Button b5 = (Button) findViewById(R.id.button04);
//Set OnCLickListeners
b1.setOnClickListener(chicagoListener);
b2.setOnClickListener(sanJoseListener);
b3.setOnClickListener(baltimoreListener);
b4.setOnClickListener(westPalmBeachListener);
b5.setOnClickListener(websiteListener); }
private OnClickListener chicagoListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Chicago.class));
}
};
private OnClickListener sanJoseListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, SanJose.class));
}
};
private OnClickListener baltimoreListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Baltimore.class));
}
};
private OnClickListener westPalmBeachListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, WestPalmBeach.class));
}
};
private OnClickListener websiteListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/")));
}
};
{
}
}
Mention your all activity in your app manifesto file (like this):
<activity
android:name="com.example.finalproject2.Chicago"
android:label="#string/app_name" >
</activity>
And try to do somthing like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
startActivity(new Intent(MainActivity.this, Chicago.class));
break;
case R.id.button02:
startActivity(new Intent(MainActivity.this, SanJose.class));
break;
case R.id.button03:
startActivity(new Intent(MainActivity.this, Baltimore.class));
break;
case R.id.button01:
startActivity(new Intent(MainActivity.this, WestPalmBeach.class));
break;
case R.id.button04:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/")));
break;
}
}
Button - Android developer
sample from developer site
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
another good example of how to use onclick
I'm trying to use multiple data on a single listview through different buttons, as from activity1 there are two buttons, if button1 is clicked on activity2 it shows different data and if button2 is clicked then diff data respectively
//main
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class) );
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class) );
}
});
}
}
//for second activity
public class NEXT extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
if (R.id.button1 == clicked) //did onClick="clicked"
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.acceptors)));
}
else
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.donars)));
}
}
}
You should use putExtra() method of the Intent class. Put boolean extra in your MainActivity with key acceptors:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class).putExtra("acceptors", true);
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class).putExtra("acceptors", false));
}
});
}
}
Then in your NEXT activity, get intent, and check whether you get acceptors or donors:
public class NEXT extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
boolean isAcceptors = getIntent().getBooleanExtra("acceptors", false);
if (isAcceptors) //did onClick="clicked"
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.acceptors)));
}
else
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.donars)));
}
}
}
Lets say my main screen has a lot of Buttons.
I want to make a class in which all the Button listeners are inside and call the onClick from my main method each time I press a button.
public class Buttons extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onClick(View v) {
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button3);
int viewId = v.getId() ;
if(viewId == R.id.button1){
Intent ScrollViewTest = new Intent(this, ScrollViewTest.class);
startActivity(ScrollViewTest);}
if(viewId == R.id.button2){
Intent ScrollViewTest = new Intent(this, CameraTest.class);
startActivity(CameraTest);}
}
}
}
Is there a way to call the onClick method like:
Buttons allButtons = new Buttons();
allButtons.onClick(btn1);
or is my logic wrong ?
........................................
Already checked
calling a method from an onClick listener
How to call onClick(View v) method explicitly in an Android? Is it possible?
Android onClick method
Is there a method to set methods for multiple objects in one go?
public class Buttons extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button3); // did you mean R.id.button2?
// Create the onClickListener for btn1
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent ScrollViewTest = new Intent(this, ScrollViewTest.class);
startActivity(ScrollViewTest);
}
});
// Create the onClickListener for btn2
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent ScrollViewTest = new Intent(this, CameraTest.class);
startActivity(CameraTest);
}
});
}