multiple click on single button while opening from one fragment to another? - java

I have created an application which uses the fragment, I am opening a fragment on the click of the first fragment with custom animation, meanwhile the animation is going on I am able to click the button on the first fragment and it creates 2 fragments. how can I not click on my button while moving from one fragment to another, I just don't want double click of the same button.
can anyone help me?

Try below
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.setEnabled(false);
}
});
// on animation complete, enable it
// button.setEnabled(true);

You could try set android:clickable in your XML layout to determine whether a button can be clicked.

You could implement the following method into your code and call it when needed.
public void myMethod(boolean isLoading){
myButton.setEnabled(!isLoading);
}

button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {view.setEnabled(false);}
});

Try using myButton.setEnabled(false) in your click callback function.

Try the below kotlin snippet
view.setOnClickListener {
val tag = "my_dialog"
val oldFragment: Fragment? = supportFragmentManager.findFragmentByTag(tag)
if(!(oldFragment?.isAdded == true)) {
val myDialogFragment = MyDialogFragment.newInstance()
myDialogFragment.show(supportFragmentManager, tag)
}
}
In case a fragment(with the tag specified) is already added to the activity then this code prevents new fragment creation and adding it to the activity.

Related

How to capture the AutoCompleteTextView arrow click event

How do I capture the click on the AutoCompleteTextView arrow.
OnClick works for the rest of the component, but it doesn't work when you specifically click the arrow.
It took a while, but I found the solution.
The event is in the parent of the AutoCompleteTextView.
XML:
XML
Solution:
public com.google.android.material.textfield.TextInputLayout txtGleba;
...
txtGleba = findViewById(R.id.txtGleba);
txtGleba.setEndIconOnClickListener (new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

Switching activities loses functionality

I have an app that consists of 3 Activities
MainActivity
CalculatorActivity
InformationActivity
My MainActivity has a confirm button that onClick starts the CalculatorActivity and everything is done correct and working as intended.
CalculatorActivity has 2 buttons, one calculateButton that checks something and shows a message and a learnMorebutton that starts the InformationActivity.
When I am on the
CalculatorActivity for the first time everything is fine and working.Pressing the learnMoreButton navigates me to the InformationActivity.That activity looks like this :
InformationActivity:
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchActivity();
}
});
}
public void switchActivity(){
final Intent intentObj = new Intent(this,CalculatorActivity.class);
startActivity(intentObj);
}
A goBack button that gets me back to CalculatorActivity.Going back seems to break the functionality.Although the layout is there and everything looks as it should be, pressing the buttons (calculateButton,learnMoreButton) does nothing.
CalculatorActivity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
final Button calculateButton = (Button) findViewById(R.id.calculateId);
final Button learnMoreButton = (Button) findViewById(R.id.learnMoreButtonId);
there are some more TextView and EditText that dont show up here but you get the point.Some more methods that do the calculations ,getters and setters.
This method
public void switchActivity(){
final Intent intentObj = new Intent(this,Information_activity.class);
startActivity(intentObj);
}
But I am not using onResume() , onPause() or any methods from the lifecycle apart from onCreate().
From some search that I have done I found out that I am doing something wrong with how I manage the activity lifecycle but I still can't find the solution.The dev documents didn't help me that much and a post with kinda the same problem as mine is old.
My question is, how the navigation from InformationActivity to CalculatorActivity should be done, so the functionality doesn't break when CalculatorActivity comes back to interact with the user.Which method should be called onResume()? , onRestart()? and how should it look like?
Thanks anyone who is willing to help.
P.S: As I mentioned , I have read the documents for the lifecycle of an Activity but I haven't found the solution.
instead of starting new activity everytime, finish the informationactivity.
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
You are creating too much activities moving going back and forth this way. You can use either destroy the activity with finish(); or you can also go back to previous activity using onBackPressed();
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
Try this out
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InformationActivity.this.finish();
}
});
}
Instead of saying where to go back, you can just finish the activity and it will automatically switch you to the previous one.
I think your activities are hierarchical thus you should be able to do the following from your main calculator activity:
Intent i = new Intent(this, InformationActivity.class);
startActivityForResult(i);
Your back button add this code:
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setResult(Result.OK);
finish();
}
});
You are all suggesting the same thing.Adding
Information_Activity.this.finish()
fixed the broken functionality , though you are all correct I can pick only one answer.
Thanks

Using buttons on android - newbie

I am a newbie to android development, trying to get buttons working. every time i use this code below, the error message "unfortunately the app has stopped". but when i remove the code the app runs but obviously the buttons do nothing. here is the code ive tried
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.ExerciseButton);
button1.setOnClickListener (new View.OnClickListener(){
public void onClick(View v) {
setContentView(R.layout.exercises);
}
});
}
}
anybody able to help me out there? thanks
Don't try to load another View in the current activity. Navigate to a new ExercisesActivity.
Use:
public void onClick(View v) {
Intent intent = new Intent(ExercisesActivity.this, WcActivity.class);
startActivity(intent);
}
You can't call setContentView anymore after the view has loaded (which it obviously has to receive button clicks). Use a different approach, like showing and hiding views or using a ViewFlipper (see Calling setContentView() multiple times), using fragments (see Fragments) or starting a new activity.
Well, from your code, I see a couple of things:
I am usually familiar to using the onClickListener of the Button class if I want to use it for a button. Makes sense, doesn't it?
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Do stuff here
}
Second thing:
Start a new Activity (if that is what you want) by using an Intent:
Intent myIntent = new Intent(this, exercises.class);
startActivity(myIntent);
You CAN absolutaly call setContentView on any event, even after the view has loaded
I tried your code in a demo project and it is working fine. So, i think the error will be some where in your layout.(Let me know more if you need more help on this)

Creating buttons and separating there functionality android

my app have action bar on top of windows. Where are some buttons. Buttons count and there functions is changing depending on activity user are.
I want to write a class with methods addFirstButton, removeFirstButton and so on.
So i other classes i want to do this:
MyButtons myButtons = new MyButtons();
myButtons.addFirstButton();
So there is everything alright, but how to create a listener button if i want to do this ?
Normally i would do this:
Button backButton = (Button) customNav.findViewById(R.id.back);
backButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(Action_Bar_TestingActivity.this, "BACK", Toast.LENGTH_SHORT).show();
}
});
But i want that this would be in MyButtons class and method somehow would return a listener to that action.
So any ideas if this possible ?
Thanks.
If you're programming an Action Bar, then you can handle its "buttons" in onOptionsItemSelected(). For more information, see here: http://developer.android.com/guide/topics/ui/menus.html
If you are supporting Android 1.6-2.x, you can make a copy of the ActionBarCompat sample app. It will use some of the same XML flags as >=3.x ActionBar, but not all functionality is emulated. You may also consider using Action Bar Sherlock.
If you want to set and get your onClickListeners, you can. Nothing says you have to instantiate the click listener inside the button. But you'll have to do some bookkeeping. At the least, instantiate the listener outside your button array and pass it in.
Here's how I make a standalone click listener:
Button.OnClickListener mTakePicOnClickListener =
new Button.OnClickListener() {
public void onClick(View v) {
dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B);
}
};
And here's where I attach it to a button (trivial example):
private void setBtnListener(
Button btn,
Button.OnClickListener onClickListener ) {
btn.setOnClickListener(onClickListener);
}
(If you want to see what this function really looks like, it's part of the Capturing Photos sample app.)
But I think you can see how you could use this function internal to MyButtons.
Or the hard way to code:
final Button backButton = null;
final LinearLayout navBar = (LinearLayout) customNav.findViewById(R.id.root);
Button addButton = (Button) customNav.findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
if (backButton == null)
{
backButton = new Button(this);
backButton.setText("Back");
backButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Toast.makeText(Action_Bar_TestingActivity.this, "BACK", Toast.LENGTH_SHORT).show();
}
});
navBar.addView(backButton);
addButton.setText("Remove Back button");
}
{
navBar.removeView(backButton);
backButton = null;
addButton.setText("Add Back button");
}
}
});

Can I reference an OnClickListener's button from inside the listener? (android)

I have an android program where I have multiple buttons using the same OnClickListener, and I want to be able to reference the button's dynamically assigned text from inside the listener. Is there some way to reference the button that was pushed to get its text? I don't want to have to make multiple button-specific listeners that do the same thing.
In your onClick(View v) you can cast it to a button:
#Override
public void onClick(View v) {
Button clickedButton = (Button)v;
// do stuff with it here.
}
use the View which comes as the argument to the onClick(View v)
this can be casted to a button & worked with.
The argument to onClick is the View that originated the click, which will be the button to which you attached the listener. Cast it to Button to get the button object.
Yes, there should be a way.
public abstract void onClick (View v)
You'll notice that the View that was clicked is passed into the onClick() method. So if you have a reference to the View (Button) available (for example, as an instance variable in the Activity) then you can do this:
public abstract void onClick (View v) {
if (v == firstButton) {
//Do some stuff
}
else if (v == secondButton) {
//Do some other stuff
}
}

Categories