how can I click a button by another button without displaying it? - java

I want to send the data of a button1 by clicking another button2 is that possible in android, without showing button1 to the user?

You can programmatically "click" a button by calling its callOnClick method.
See this for reference.

You can do that either using View.performClick() or View.callOnClick().
But you should revisit your code. Is the code inside the buttons can be refactored to methods? Is the code can be reused can recall in another button?
What you're code now probably something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
buttonA.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button A clicked.
}
});
buttonB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button B clicked.
}
});
...
}
You can refactor the inside of each of your buttons to methods, something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
buttonA.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button A clicked.
processButtonA();
}
});
buttonB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button B clicked.
processButtonB();
}
});
...
}
private void processButtonA() {
// something here.
}
private void processButtonB() {
// something here.
}
then, if you need to call process related with the button B from button A, you can do the following:
buttonA.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
processButtonA();
processButtonB();
}
});

Related

OnClickListener for Multiple ImageButton

I'm super new in Java! Pardon for this question although there are similar ones but I'm completely clueless on fixing my problem.
I'm trying to set an OnClickListener for each ImageButton to open a new activity.
The first ImageButton works but not for the subsequent ones, it is unclickable in AVD.
Would greatly appreciate some help on it!
public class CharacterSelect extends AppCompatActivity {
ImageButton arrowbtnright;
ImageButton contchibtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_select);
arrowbtnright = (ImageButton) findViewById(R.id.arrowbtnright);
contchibtn = (ImageButton) findViewById(R.id.contchibtn);
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});
}
});
}
You are currently adding the listener of the second button, from inside the listener of the first button, this is probably not what you want to do .
Try putting both at the same level, i.e :
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
}
});
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});

Calling a button from a second xml file from the main .java file

I'm trying to make a text adventure game in android studio using buttons to progress through the map. I'm trying to call a button that is in the second page of the game. I know the problem is that it can't find the button since its on the second page and the page the .java file is looking at is page one. Is there a way to have the .java file look at all .xml page at the same time?
public class textAdven extends AppCompatActivity {
boolean male;
int gender;
private Button boyButt;
private Button girlButt;
private TextView genderBox1;
private Button shedButt, fieldButt, yardButt, lookAround1, cabButt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_adven);
boyButt =(Button) findViewById(R.id.boyButt);
girlButt =(Button) findViewById(R.id.girlButt);
shedButt=(Button) findViewById(R.id.shedButt);
cabButt =(Button) findViewById(R.id.cabButt);
lookAround1 =(Button) findViewById(R.id.lookAround1);
fieldButt=(Button) findViewById(R.id.fieldButt);
yardButt =(Button) findViewById(R.id.yardButt);
boyButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven2);
male = true;
genderBox1 = findViewById(R.id.genderBox1);
genderBox1.setText("boy");
}
});
girlButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven2);
male = false;
genderBox1 = findViewById(R.id.genderBox1);
genderBox1.setText("girl");
}
});
shedButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven3);
}
});
yardButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven2);
}
});
}
}

Different instructions for one button

I want to do different things for one button.
But I am changing instructions after the button was clicked every time.
First I am creating two buttons, after I clicked the button I want it to change its text. For each text I want to create different instruction.
I created an if/else for this. But I am not sure why my isn’t not working.
I only do instructions in the first statement.
Here is my code.
package com.example.iqbal.destini;
public class MAINPAGE extends AppCompatActivity {
Button mAnswer_1_Button;
Button mAnswer_2_Button;
TextView mTextbody;
int counter1 = 0;
int counter2 = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
mAnswer_1_Button = (Button) findViewById(R.id.answer_1);
mAnswer_2_Button = (Button) findViewById(R.id.answer_2);
mTextbody = (TextView) findViewById(R.id.text_body);
if(mTextbody.getText().toString().equals(getResources().getString(R.string.T1_Story)) && mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T1_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T1_Ans1)) ){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T3_Story);
mAnswer_1_Button.setText(R.string.T3_Ans1);
mAnswer_2_Button.setText(R.string.T3_Ans2);
}
});
mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T2_Story);
mAnswer_1_Button.setText(R.string.T2_Ans1);
mAnswer_2_Button.setText(R.string.T2_Ans2);
}
});
}
else if(mTextbody.getText().toString().equals(getResources().getString(R.string.T2_Story))&& mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T2_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T2_Ans1))){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T3_Story);
mAnswer_1_Button.setText(R.string.T3_Ans1);
mAnswer_2_Button.setText(R.string.T3_Ans2);
}
});
mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T4_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
}
else if(mTextbody.getText().toString().equals(getResources().getString(R.string.T3_Story))&& mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T3_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T3_Ans1))){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T6_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
**mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T5_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
}
}
}**
mTextbody.getText() always is empty where it's currently located (unless you have a default string in the TextView)
Move the if conditions into the onClick method body.
Conditionally perform the action. Don't conditionally add the listener.
And you only need one OnClickListener per button

How to set layout dynamically in android

Well, Suppose there is an Activity called MainActivity and there are two layouts called layout1 and layout2 both have few buttons. By default MainActivity layout is layout1 like following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
Now what I did actually is by clicking a button in layout1 the second layout is set like following:
someBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout2);
}
});
There are another button in layout2 to return back to layout1 like following:
someBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout1);
}
});
Problem is when I returned back to layout1 then OnClickListener of someBtn1 is not working. It seems I need to set OnClickListener again for someBtn1 of layout1.
How I can write code to make them work perfectly with best practices ?
The best practice is to use fragments instead of change the content view.
In your code, setContentView with layouts recreate (inflate) all your views every time, so the call setContentView(R.layout.layout1) in someBtn2 click listener will create a new button without the associated listener.
If you don't want to use fragments you can do this:
private View view1, view2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view1 = getLayoutInflater().inflate(R.layout.layout1, null);
view2 = getLayoutInflater().inflate(R.layout.layout2, null);
setContentView(view1);
The listeners will be:
someBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(view2);
}
});
someBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(view1);
}
});
If you just want to play around with your current code, a solution for your problem is that the listeners must be redeclared when the layout changes, as follows:
someBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout2);
someBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout1);
}
});
}
});
someBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout1);
someBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout2);
}
});
}
});
An alternative to avoid declaring the listeners twice is to declare two methods to handle the layout changes and use the onClick property of the button in each of the layouts, for example:
public void setLayout1(View view) {
setContentView(R.layout.layout1);
}
public void setLayout2(View view) {
setContentView(R.layout.layout2);
}
In layout1.xml:
<Button
android:id="#+id/someBtn1"
android:onClick="setLayout2"/>
In layout2.xml:
<Button
android:id="#+id/someBtn2"
android:onClick="setLayout1"/>
However, if you want to follow best practices, the best practice is not to mix layouts in the same activity, but instead declare two different activities (each one with its own layout) and call one activity or the other depending on the button that was clicked. Suppose that you are in Activity1 and want to call Activity2, then go back to Activity1:
In Activity1.java:
someBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(this, Activity2.class));
}
});
In Activity2.java:
someBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
One way of doing this is loading both views in onCreate(...), and then switching between them by making the one you don't want invisible. Something like the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams default_layout_params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
View view1 = inflater.inflate(R.layout.layout1, null);
addContentView(view1, default_layout_params);
View view2 = inflater.inflate(R.layout.layout2, null);
addContentView(view2, default_layout_params);
view2.setVisibility(View.INVISIBLE);
view1.setVisibility(View.VISIBLE);
view1.bringToFront();
At the time you callback layout1, data must be set again.
You could merge both layouts in one and then use ViewFlipper to switch between them.
When you are seting layout2, you should also set up OnClickListener to someBtn1 and vice versa, I'd suggest something like this. But as in prevoius answer, in general you should avoid mixing layouts in such manner.
public class MainActivity extends Activity {
private final View.OnClickListener setLayout1Listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout2);
((Button)findViewById(R.id.Btn2Id)).setOnClickListener(setLayout2Listener);
//do other stuff
}
};
private final View.OnClickListener setLayout2Listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout1);
((Button)findViewById(R.id.Btn1Id)).setOnClickListener(setLayout1Listener);
//do other stuff
}
};
#Override
public void onCreate(final Bundle savedInstance) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
((Button)findViewById(R.id.Btn1Id)).setOnClickListener(setLayout1Listener);
//do other stuff
}
}

How to close popup window?

I have an activity with custom popup window (quickaction style). There are some buttons leading to other activities. I want to close popup after pressing button (about or email button) in this popup (now when I go back popup appears again).
public class FirstActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some code
Button quickButton = (Button) findViewById(R.id.button_quickaction);
quickButton.setOnClickListener(this);
final ActionItem about = new ActionItem();
final ActionItem email = new ActionItem();
quickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
QuickAction qa = new QuickAction(v);
qa.addActionItem(about);
qa.addActionItem(email);
qa.setAnimStyle(QuickAction.ANIM_GROW_FROM_RIGHT);
qa.show();
}
});
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some code
}
});
email.setTitle("Email");
email.setIcon(getResources().getDrawable(R.drawable.email));
email.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some code
}
});
}
}
Please, help.
Added:
I need something like this:
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qa.dismiss();
}
});
But qa cannot be resolved. Even if I add final to QuickAction qa = new QuickAction(v);.
try finish() on button's onClick method.
updated:
QuickAction qa;
quickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
qa = new QuickAction(v);
qa.addActionItem(about);
qa.addActionItem(email);
qa.setAnimStyle(QuickAction.ANIM_GROW_FROM_RIGHT);
qa.show();
}
});
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(qa.isShowing())
qa.dismiss();
//some code
}
});
email.setTitle("Email");
email.setIcon(getResources().getDrawable(R.drawable.email));
email.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(qa.isShowing())
qa.dismiss();
//some code
}
});
you can also put private QuickAction qa; at your activity.

Categories