Button working only on second click [duplicate] - java

This question already has answers here:
Android Button Works Only on the second Click
(2 answers)
Closed 7 years ago.
I have a simple, but annoying problem:
In my fragment, I have a button which should open another activity (intent) on click. However I have to click the button twice, and only the second time it's opening the activity.
Here is the xml layout of the button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go!"
android:onClick="login"
android:id="#+id/bt_SignIn"
android:layout_below="#+id/pass"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
And here is the code for onClick:
public void login(View view){
bt_SignIn = (Button) findViewById(R.id.bt_SignIn);
bt_SignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
}
}

You're setting the onClick listener for the button twice. You're declaring the onClick in the XML so your method can just be.
public void login(View view){
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}

Why are you defining Onclick listener for btn_signIn again? As you already define in your xml login method will call on button click.
Use only below code.
public void login(View view) {
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}

That is because of thee fact that in the function defined in xml you are setting the listener for the button. Do it from either the JAVA code or from the xml code.
This shall suffice
public void login(View view) {
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
What was happening earlier was that on the first click the function defined in the xml was getting invoked which inturn was setting the listener for the button, and so on the next click your activity was getting triggered

You should avoid to use onClick directly in the XML, it's a bad idea.
Remove onClick from the XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go!"
android:id="#+id/bt_SignIn"
android:layout_below="#+id/pass"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
put this code in the onCreate of your Activity:
bt_SignIn = (Button) findViewById(R.id.bt_SignIn);
bt_SignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
}

Change your login(View view) method as follows
public void login(View view){
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
Or much better put your code inside onCreate() method
bt_SignIn = (Button) findViewById(R.id.bt_SignIn);
bt_SignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
}
and remove onClick from your layout
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go!"
android:id="#+id/bt_SignIn"
android:layout_below="#+id/pass"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />

Related

How do I click on a button that makes an invisible button appear in android? [duplicate]

This question already has answers here:
how to show/hide layout on button click
(5 answers)
Closed 5 years ago.
I need to do a quiz with levels and I would like the next level button to appear only when a certain button was clicked, how to do it? (Please be detailed, because I'm new to java)
This should be in your java code:
certain_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button_next_level.setVisibility(View.Visible);
}
});
In your xml layout file, make the button_to_appear as gone.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_next_level"
android:visibility="gone"
/>
Activity A:
btnA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra("isAnswerCorrect",true);
startActivity(i);
}
});
Activity B:
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
boolean isAnswerCorrect = getIntent().getBooleanExtra("isAnswerCorrect", false);
if (isAnswerCorrect) {
next_level_btn.setVisibility(View.VISIBLE);
} else {
next_level_btn.setVisibility(View.GONE);
}
}
...
In this case, Activity A is where you are pressing the button and Activity B is the activity you are showing your next level button on,

Button not working (android studio) [duplicate]

This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 5 years ago.
My app runs well and shows no errors, but my button is not working ( is unClickable in device)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button11=(Button)findViewById(R.id.button11);
button11.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
new Intent(MainActivity.this, Main2Activity.class);
}
});
}
}
and XML file :
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50px"
android:layout_marginTop="200dp"
android:background="#color/colorPrimary"
android:text="صفحه اصلی"
android:onClick="onClick"/>/>
On the real device when I click on this button nothing happens !
try this way
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
EDIT: I wanted to add a bit of an explanation to help
Hi MRAK,
First things first, you do NOT need to create an onClick listener. Remove all of that completely. Android studio is a beast and automatically does that for you using the XML file. When you set "onClick" in the XML file, it automatically calls the name of whatever method you put in there. You should change it so it is not also called "onClick." I would prefer to call it "startAcitivty2" or so on so you are not confused later. I stuck with your method name for now.
See below for corrected code:
public void onClick(View v){
// note in the below line i'm just using "this"
Intent myIntent = new Intent(this, Main2Activity.class)
// Secondly, you need to end the current activity
finish();
// Third, you need to start your new activity...
// Creating an Intent does not the activity alone
startActivity(myIntent);
}
Also, this has so many downvotes because this has been asked 1000+ times. Please use google or the search bar above before asking. Google will reroute you to stackoverflow anyway :)

buttonOnClick switch isn't working

One of the activities in my app has three buttons in it and I used a switch to code them. I've used nearly identical code several other times in my app, but this particular one doesn't work. When I navigate to this screen and click any of the three buttons, nothing happens.
Here's the code that isn't working:
public void buttonOnClick(View view){
switch(view.getId()){
case R.id.generalPrefabButton:
Intent generalPrefabScreen = new Intent();
generalPrefabScreen.setClass(this, General_Prefab_Order.class);
startActivity(generalPrefabScreen);
break;
case R.id.conduitBendButton:
Intent conduitBendScreen = new Intent();
conduitBendScreen.setClass(this, Conduit_Bend_Order.class);
startActivity(conduitBendScreen);
break;
case R.id.safetyReportButton:
Intent safetyReportScreen = new Intent();
safetyReportScreen.setClass(this, Safety_Report.class);
startActivity(safetyReportScreen);
}
}
One way of achieving this is to make your class implement OnClickListener and then add it to your buttons like this:
Example:
//make your class implement OnClickListener
public class MyClass implements OnClickListener{ ... //Create your buttons and set their onClickListener to "this"
Button generalPrefabButton = (Button) findViewById(R.id.buttonplay);
generalPrefabButton.setOnClickListener(this);
Button conduitBendButton = (Button) findViewById(R.id.buttonstop);
conduitBendButton.setOnClickListener(this); ...
//implement the onClick method here
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.generalPrefabButton:
Intent generalPrefabScreen = new Intent();
generalPrefabScreen.setClass(this, General_Prefab_Order.class);
startActivity(generalPrefabScreen);
break;
case R.id.conduitBendButton:
Intent conduitBendScreen = new Intent();
conduitBendScreen.setClass(this, Conduit_Bend_Order.class);
startActivity(conduitBendScreen);
break;
case R.id.safetyReportButton:
Intent safetyReportScreen = new Intent();
safetyReportScreen.setClass(this, Safety_Report.class);
startActivity(safetyReportScreen);
break;
}
}
Button button = new Button(R.id.generalPrefabButton);
button.setOnClickListener(this);
This sets the onClickListener activating the action in the onClick method.
Add this line to your Buttons in the layout .xml file:
android:onClick="buttonOnClick"
<Button
android:id="#+id/button"
android:onClick="buttonOnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

Passing string to setOnClickListener

WAIT BEFORE REPORTING!!! I read everything about "passing parameter with setOnClickListener", but I didn't found a solution to my problem.
String citazione = new String(data[position]);
share_btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, citazione);
sendIntent.setType("text/plain");
v.getContext().startActivity(Intent.createChooser(sendIntent, v.getResources().getText(R.string.send_to)));
}
});
I should put the string "citazione" in the 6 line, but I can't edit View v because I'm using it in the 8 line.
I tried everithing (getContext(), startActivity(), the v before) but nothing.
What should I do?
Just make it final:
final String citazione = new String(data[position]);
First of all I would declare the onClick in the XML like so:
<Button
...
android:onClick="onButtonClick"
....
/>
Then I would make citazione a class variable
private String citazione;
The buttons onClick in the activity would be
public void onButtonClick(View view) {
//do what you want here with citazione
}

How to navigate from one screen to another screen

How to navigate from one Activity screen to another Activity screen? In the first screen I'm having one button if I click the button it has to move to another Activity screen.
The most trivial case (called from activity):
startActivity(new Intent(this, ActivityToLaunch.class));
More details here: http://developer.android.com/guide/topics/fundamentals.html
OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(action));
}
};
Button button = (Button) findViewById(id);
button.setOnClickListener(onClickListener);
Button x.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent i = new Intent(y.this, Activity.class);
startActivity(i);
}
});
Here we've defined a listener for Button x. The OS will call this method and start the Activity referenced in Intent i.
Here's the official tutorial example:
http://developer.android.com/guide/tutorials/notepad/notepad-ex2.html
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(TestActivity.this,second.class));
}
});
public void onClick(View v)
{
Intent myintent = new Intent(currentclass.this, nextactivity.class);
startActivity(myintent);
}
This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html
public void startActivity (Intent intent) -- Used to launch a new activity.
So suppose you have two Activity class and on a button click's OnClickListener() you wanna move from one Activity to another then --
PresentActivity -- This is your current activity from which you want to go the second activity.
NextActivity -- This is your next Activity on which you want to move.
So the Intent would be like this
Intent(PresentActivity.this, NextActivity.class)
Finally this will be the complete code
public class PresentActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);
// currentContext.startActivity(activityChangeIntent);
PresentActivity.this.startActivity(activityChangeIntent);
}
});
}
}
This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener() at any place where you want to switch between your activities.
final Context cont = this;
Button btnClickABC =(Button)findViewById(R.id.btnClickABC);
btnClickABC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(cont, NextActivity.class));
}
});
Use following code..I hope this will help you.
Button button = (Button)findViewById(R.id.xxx);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
startActivity(intent);
}
});
xxx is id from your xml of your Button.
startActivity(new Intent(this,newActivity.class));
Switching from one activity to another is really simple, but tricky for a new one.
Your next class must be defined in AndroidManifest.xml. This is tester class:
<activity
android:name=".Tester"
android:label="#string/title_activity_tester" >`enter code here`
</activity>
final Button button = (Button) findViewById(R.id.btnGo);// btnGo is id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(CurrentClass.this, Tester.class);
startActivity(i);
}
You can navigate to the next screen using these code snippets:
Kotlin
startActivity(Intent(this, LoginActivity::class.java))
Java
startActivity(new Intent(this, LoginActivity.class))
Here's a reference: Android Developers - Starting another activity
Intent intentobj=new Intent(FromActivity.this,ToActivity.class);
startActivity(intentobj);
or you can simply use
startActivity(new Intent(FromActivity.this,ToActivity.class));
For Kotlin (if you are in an activity)
buttonToClick.setOnClickListener{ startActivity(this,YourDestinationActivity::class.java)
}
If you are in a fragment
buttonToClick.setOnClickListener{
startActivity(requireActivity, YourDestinationActivity::class.java)
}
In your method fun onCreate(savedInstanceState: Bundle?) add this.
your_btn_id.setOnClickListener{
val intent = Intent(this, yourpagename::class.java)
startActivity(intent)
}
Until now if it doesn't work then, check if these two files are added or not,
import android.content.Intent
import kotlinx.android.synthetic.main.activity_otp.*
Try this code:
Button my_btn;
my_btn = findViewById(R.id.submit_btn);
my_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_2);
}
});
Button navigate;
navigate = findViewById(R.id.button);
navigate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Navigate another Activity",Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
});
Just go to XML file and add Onclick = "opennewactivity" in button xml.
Then go to java code and create class opennewactivity you can just make it my clciking alt+Enter in xml code's "opennewactivity". in that just write
Intent intent = new Intent(this, newacivity.class);
startActivity(intent);
and if you want user to not get back to first activity again then just write this
Intent intent = new Intent(this, newactivity.class);
startActivity(intent);
finish();
Cartoon_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
newActivity();
}
});
}
public void newActivity()
{
Intent selectClass= new Intent(getApplicationContext(), com.example.fyp.videoplayer.class);
startActivity(selectClass);
}

Categories

Resources