How to make 'if button clicked, do this' in Android Studio? - java

I am new to Android Studio. I am working on a ArCore sample project.
I want to add a button, when clicked the plane finding mode will be vertical otherwise horizontal.
Before voting negative, I have gone through these posts:
How can I write “if button clicked” in an If statement in android studio?
Checking if a button has been clicked
The following is the snippet of my code:
if(y > 0){
Config config = new Config(session);
//config.setPlaneFindingMode(Config.PlaneFindingMode.VERTICAL); If button clicked, this
config.setPlaneFindingMode(Config.PlaneFindingMode.HORIZONTAL); //otherwise this
session.configure(config);
Globals.notRecording = true;
}

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Just use this code in OnClickListener.
when you tap button call this
config.setPlaneFindingMode(Config.PlaneFindingMode.VERTICAL);

Related

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

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.

OnClickListener in custom dialog

This is what is working correctly
>>The user swipes on a part of the main activity ui we will call the MainTitleBar.
>>This creates a dialog with stuff in it
=----------------------------------------------------------------------------=
Now I need to make it so when someone clicks on the buttons or imageviews within that dialog STUFF happens. I cannot figure out for the life of me how to handle this.
Code I have currently inside the maintitlebar.java class (which extends view) and is the view they are swiping on to create the dialog box.
--- bunch of stuff like ui stuff and swipe calculations etc
public void onDownSwipe() {
// custom dialog object created
Dialog ntd = new Dialog(getContext(),R.style.lightbox_dialog);
ntd.setContentView(R.layout.create_task_dialog);
ntd.show();
ImageView cancelbutton = (ImageView) ntd.findViewById(R.id.cancelbutton);
cancelbutton.setOnClickListener(new OnclickListener(){
public void onClick(View v) {
ntd.dismiss();
}
}
);
In the xml for the dialog, the create_task_dialog is the overall xml for the dialog.
the cancelbutton (id) is the id for the cancel button (which is properly added with + sign into resource file).
Should I be putting the onclicklistener and clicky stuff somewhere else in my code?
So confused as to how to make my dialog buttons/imageviews clickable.
THANKS!

code to turn on transition setting on phone within app

I have this app where when it navigates to another activity it does a transition animation. Now I have already got that working properly but, to get it to work i have to turn the animation setting on my phone on. I have been searching on Google and I can"t seem to find solution to my problem. So my problem is this,is it possible to turn this setting on from within the app, with a code, instead of having to manually do it?
Here is the code but as i have said above this works perfectly and I'm using a Telstra next G Huawei phone, just want to no if i can code it so i can turn the animation setting on if i decide to put it on a different phone.
//Button to restart from beginning:
//Declaring the button for OnClickListener and EditText to send data:
private void Restart() {
Button Next = (Button)findViewById(R.id.restart3);
Next.setOnClickListener(new View.OnClickListener() {
//The action the button takes when clicked:
#Override
public void onClick(View v) {
//Goes back to the start of app:
Intent i = new Intent(Height.this, Page_1.class);
//clears the stack of all Activities that were open before this one so that they don't stack up:
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
//The transition animation when going onto next page:
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
// to end the current activity:
finish();
}
});
}

Display different Button/Button Background depending on SharedPreferences Value

Basically i want a Button to change to another one depending on the int Value stored in sharedprefs.
I have stage select in my game, if the user got enough score in level, then he can start the next one and i want to change the Button depending on that.
I'm setting up my Button's with custom Background created in XML Selector file located in drawable folder.
After that how i can call it in Java?
I tried a bit with if statement but cant find the right solution.
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
Button button = (Button) findViewById(R.id.button_id);
if (settings.getBoolean("nextLevel", true) {
button.setText("Level_2");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
else {
//Everything should be the same.
}
Check also the developer docs concerning SharedPreferences in android: http://developer.android.com/guide/topics/data/data-storage.html#pref

Choosing database using radio buttons

I'm teaching myself to write an Android app. I use the following code to successfully choose the different database for my app:
public void manageDB()
{
setContentView(R.layout.dbmanager);
ScrollView ll = (ScrollView) findViewById(R.id.lstDb);
final RadioGroup rg = new RadioGroup(this);
rg.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT));
rg.setOrientation(RadioGroup.VERTICAL);
for (int i=0; i < mDBList.items.size(); i++)
{
RadioButton rb = new RadioButton(this);
rb.setId(VIEW_RADIO_ID + i);
rb.setText(mDBList.items.get(i).dictionaryName);
rb.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rg.addView(rb,i);
if (mDBFile != null && mDBFile.fileName.equals(mDBList.items.get(i).fileName))
{
rg.check(VIEW_RADIO_ID + i);
}
}
ll.addView(rg);
Button btnOk = (Button) findViewById(R.id.btnOk);
btnOk.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.main);
int selectedIndex = rg.getCheckedRadioButtonId();
if (selectedIndex < 0)
{
selectedIndex = 0;
}
mDBFile = mDBList.items.get(selectedIndex - VIEW_RADIO_ID);
savePreferences();
setContentView(R.layout.main);
menuMain();
}
});
}
With this code, I can click on a radio button to choose a database, and the click on the OK button to confirm and return to the main screen. Now I want to assign a button on the main screen to do this task so that I don't have to leave the main screen when choosing a database.
Since I have two pieces of database, ideally I want a two-way button to do this task. I mean when I click on the button, db_1 is chosen. When the button is clicked again db_2 is chosen, and vice versa.
I have no idea how to adapt this code to meet this requirement of mine. I wonder if you guys can give me a little help. Thank you very much in advance.
If your need is to change database in one click instead of two, you could use a radiogroup containing your 2 radiobuttons, and listen for checked radiobutton change.
Then in onCheckedChange() you load the right database.
Why don't you use "Switch" I think it will solve your purpose. Look at the link [here:][1]
[1]: http://developer.android.com/reference/android/widget/Switch.html Or you can also use the toggle buttons. Hope it helps.

Categories