I have two button b1 and b2 .If button b1 is pressed then perform a query q1 else if button b2 is pressed then perform an another query q2.
if(b1_click)
{
mCursor=//query
}
else if(b2_click)
{
mCursor=//query
}
Please tell me how can i implement this.How to implement b1_click method or any inbuilt method which tell that button is pressed.I tried
Cursor c;
c=//querys
if(b1.isPressed())
{
next.setOnClickListener
(
new View.OnClickListener()
{
#Override public void onClick(View v) {
c=db.getData1(); (getData1 method return cursor)
}
}
);
}
tv.append(c.getString(column_number) (tv=TextView)
"Same as above for b2"
It is saying that cursor (c) should be final
Help?
First of all I would recommend going through some tutorials Hello Android and you could also follow the code and samples in Common Tasks and How to do them in Android.
If you would read this carefully you would know that is very simple, something like this:
public class MyActiviy extends Activity implements OnClickListener{
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.myLayout);
findViewById(R.id.Button1).setOnClickListener(this);
findViewById(R.id.Button2).setOnClickListener(this);
//more code...
}
public void onClick(View v){
switch(v.getId()){
case R.id.Button1:
//Button1 pressedd...do stuff
break;
case R.id.Button2:
//Button2 pressed...do some other stuff
break;
default:
break;
}
}
}
Try creating your OnClickListeners as private classes:
private class Button1ClickedListener implements OnClickListener {
public void onClick( View v ) {
//Do what needs to be done when button 1 is clicked.
}
}
private class Button2ClickedListener implements OnClickListener {
public void onClick( View v ) {
//Do what needs to be done when button 2 is clicked.
}
}
Then you set the onClick-listeners:
b1.setOnClickListener( new Button1ClickedListener() );
b2.setOnClickListener( new Button1ClickedListener() );
If you need the OnClickListeners to be able to use the cursor, you just need to declare it as a private field in the parent class of the OnClickListeners.
Related
Let's say i have a button which inserts a number to the front of a linked list and a button that inserts a number to the end of the linked list. I have this AlertDaialog and I'm trying to use it with both buttons. How can i differentiate the buttons from one another so that when i press "OK" on the dialog, the onClick method know where to insert the number(front or end).
I've tried somthing like this but it doesn't work, only the default case gets activated.
ok.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String input = inputText.getText().toString().trim();
int numberInput = Integer.parseInt(input);
switch (v.getId()){
case R.id.btn_front: //insert to the beginning
break;
case R.id.btn_end: //insert to the end
break;
default: Log.e("DIALOG_ERROR", "Error!");
}
}
});
Use a class that extend dialog like:
DialogClass obj=new DialogClass(context,true);
obj.show();
class will be:
public DialogClass(final Context context,Boolean isAddToFront) {
super(context);
setContentView(R.layout.dialog_layout);
setTitle(Title);
TextView Ok = (TextView) findViewById(R.id.ok);
Ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isAddToFront){
/// Add to front
}else{///add to back}
});
}
I just started to learn Java. I know some C++, but you know, I am just a novice. I have a problem with a button. I a main activity there are 3 buttons with onClick discovered by switch. By clicking on one of the buttons you're redirected to another activity where I need to create a new button.
The code responsible for MainScreen buttons looks like this (and it works):
public class MainScreen extends Activity implements View.OnClickListener {
Button act_2x2, act_3x3, act_4x4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
act_2x2 = (Button) findViewById(R.id.Activity_2x2);
act_3x3 = (Button) findViewById(R.id.Activity_3x3);
act_4x4 = (Button) findViewById(R.id.Activity_4x4);
act_2x2.setOnClickListener(this);
act_3x3.setOnClickListener(this);
act_4x4.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.Activity_2x2:
Intent inent1 = new Intent(this, macierz_2x2.class);
startActivity(inent1);
break;
case R.id.Activity_3x3:
Intent inent2 = new Intent(this, macierz_3x3.class);
startActivity(inent2);
break;
case R.id.Activity_4x4:
Intent inent3 = new Intent(this, macierz_4x4.class);
startActivity(inent3);
break;
}
And it is okay, I can normally enter the new activity, for example Activity_2x2.
Here, in 2x2 class I've created a new OnClickListener and when I click on it, nothing happens. I am sitting here for two hours with debugger, it is saying that I don't have permissions, but It is impossible, because it is just a simple button. I am using Android Studio and just don't know how to debug correctly.
Here is the definition:
public class macierz_2x2 extends MainScreen implements View.OnClickListener{
Button b_2x2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2x2);
b_2x2 = (Button) findViewById(R.id.button_2x2);
b_2x2.setOnClickListener(this);
}
public void OnClick(View view) {
what happens after clicking
}
I know, that this problem is somewhere in overriding and extending, but no idea, why the compiller is letting this being compiled.
If someone have any idea, I will be grateful.
ps. I don't need an answer, just a point, what is wrong.
public class macierz_2x2 extends MainScreen implements View.OnClickListener{
MainScreen already implements View.OnClickListener. Remove it from the definition of your class.
public class macierz_2x2 extends MainScreen {
is enough. You can override onClick on your macierz_2x2 activity
#Override
public void onClick(View view) {
switch(view.getId()) {
R.id.button_2x2:
// do something
break;
default:
super.onClick(view);
break;
}
}
I was wondering If anyone knew of a way getting the On Click Listener a button is set to. Sort of like...
btn1.getOnClickListner
I want to make an IF Statement like this...
if (button.Onclick == onClick1) {
do this...
} else {
do this...
}
Any Help would be much Appreciated
I found the easiest way to do this was to take CommonsWares advice and create one onClick, using booleans to give it separate methods to execute. Below is an example in case anyone else gets stuck trying to accomplish this task. Thank you to everyone that helped and shared their thoughts.
public class MainActivity extends Activity implements View.OnClickListener {
boolean separateOnClickActive;
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(this);
separateOnClickActive = false;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
if (!separateOnClickActive) {
// Do Something Here
separateOnClickActive = true;
} else {
//Do Something Here
}
break;
}
}
}
This is the first time I'm ever dabbling in Android development so please bear with me.
My requirement is this:
I have two buttons on screen, A and B. If the user presses both buttons (order doesn't matter), I need another page to be displayed. Pressing either A or B should do nothing.
Is this possible? If so, how would I achieve this?
Thank you.
This is possible if you take a flag. (boolean)
You should set a flag in your button listeners.
public class Mtest extends Activity {
Button b1;
Button b2;
boolean flag_1 = false;
boolean flag_2 = false;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler1);
b2.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
flag_1 = true;
doSomething();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
flag_2 = true;
doSomething();
}
};
}
public void doSomething(){
if(flag_1 && flag_2)
{
//DO SOMETHING
}
}
}
Create two boolean's like button1isClickedand button2isClicked,then set an onClickListener for each Button. When the the Button is clicked set the value of these two boolean's to true, then simply create an if() statement that will chekc to see if both buttons have been clicked, like this:
if(button1isClicked == true && button2isClicked == true){
//display your new page
}
I know there are similar questions asked before in SO, but sorry to say that, none of them are serving my purpose.
I have a button in an activity class, and I want to give its functionality in another class.
Below is my HomeActivity code:
// Tile Button
tileButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TileButton tileView = new TileButton();
tileView.tile();
}
});
And here is TileButton.java class code:
public class TileButton {
HomeActivity homeActivity = new HomeActivity();
View view = homeActivity.hometabView;
public void tile(){
if(view.isShown()){
view.setVisibility(View.INVISIBLE);
}else{
view.setVisibility(View.VISIBLE);
}
}
}
Now when I press the tile button, a Null Pointer Exception is thrown. Below is the LogCat entry.
10-04 10:32:07.833: E/AndroidRuntime(5330): java.lang.NullPointerException
How do I solve this problem? Please help
Change:
public class TileButton {
public void tile(View view){
if(view.isShown()){
view.setVisibility(View.INVISIBLE);
}else{
view.setVisibility(View.VISIBLE);
}
}
}
// Tile Button
tileButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TileButton tileView = new TileButton();
tileView.tile(v);// you can pass any view from here
}
});
If you want to have same operation in both the activities, Create a public method in one of the activity and just call the method onClick of both buttons. But you cannot control the visibility of an activity which is not even on screen.