How to lock the spinner after button click in android - java

How can I prevent the user to change the value selected in spinner after some event like button press?
It is like after selecting the desired value from spinner the user will press a button after which they will not be allowed to see the values in the spinner except the top value i.e. the selected one so that no change be made in the selected value.
UPDATE:- And yes the spinners are also created dynamically on the press of that same button so it should lock only the previously created spinners not the new one.
Code for dynamic creation of Layout which contains the Spinner along with an EditText & a Button:-
View.OnClickListener addListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final RelativeLayout newView = (RelativeLayout) getLayoutInflater().inflate(R.layout.product_row_detail, null);
newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
container.removeView(newView);
}
});
container.addView(newView);

Simple just disable the spinner on button click
spinner.setEnabled(false);

In your onClick method, you can just set the visibility to GONE to remove the view as shown below:
View.OnClickListener addListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final RelativeLayout newView = (RelativeLayout) getLayoutInflater().inflate(R.layout.product_row_detail, null);
newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
newView.setVisibility(View.GONE); // remove (hide) your view
}
});
container.addView(newView);
And to enable it again, you can set its visibility to VISIBLE as shown below:
newView.setVIsibility(View.VISIBLE);
Or you could just show/hide your spinner as shown below:
your_spinner.setVisibility(View.GONE); // to remove (hide)
your_spinner.setVisibility(View.VISIBLE); // to make it visible
If you want to only disable the spinner:
your_spinner.setEnabled(false);
Hope it helps.

You need to create a variable (may be boolean flag = true)
On button click change to flag = false
And inside Listener to Spinner check if flag is true of false
Example
//inside listener of spinner
if(flag){
//do task
}else{
//restrict the task or don't do anything or display message
}
For Updated question
Then you should use disable() method. spinner1.disable() or spinner2.disable() or so on..

Related

Is it impossible to have ListvVew in Custom Dialog?

If the showChatList() function is not being called in the code, and the dialog is displayed normally.
When the listView is called via showChatList() function, it does not work.
To the original custom dialog
Is it impossible to bring up the listView?
public void callFunction() {
final Dialog dlg = new Dialog(context);
dlg.setContentView(R.layout.room_list);
dlg.show();
final Button okButton = (Button) dlg.findViewById(R.id.okButton);
final Button backbtn = (Button) dlg.findViewById(R.id.backbtn);
**final ListView chat_list = (ListView) dlg.findViewById(R.di.chat_list);**
- or
**chat_list = dlg.findViewById(R.id.chat_list);**
backbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dlg.dismiss();
}
});
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dlg.dismiss();
}
});
showChatList();
}
Your adapter depends on firebase.
It will wait until it gets data,
then I believe you need to call
adapter.notifyDataSetChanged();
after
adapter.add();
When dealing with views like dialogs, it is important to load all the views needed in the dialog before calling dialog.show().
Alternatively, I'll suggest the use of DialogFragment with this example: https://blog.mindorks.com/implementing-dialog-fragment-in-android.
DialogFragments allow you to manage your dialogs just like any other fragment.
Let me know which one you're able to use.

setOnClickListener - how to detect if clicked outside of view

I have button with setOnClickListener function, it works well, new view/form/window is opened and I can do some changes on this. But when I click somewhere around the view then my view is closing. How to detect that I clicked outside of view?
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final DeviceItem changedDeviceItemCopy = changedDeviceItem.clone(); // copy of changedDeviceItem before authorized numbers
if (changedDeviceItem.getAuthorized_numbers() != null) {
authorized_numbers_array_list = changedDeviceItem.returnAuthorizedNumbersAsList();
}
...
}
I expect that I can detect moment when I clicked outside of view.

How to change button text color programmatically on press?

I create button like this:
Button button = new Button(this);
button.setText("2012");
button.setBackgroundColor(Color.TRANSPARENT);
button.setOnClickListener(mCorkyListener);
layout.addView(dateButton);
On click listiner i have this method. Here i want to change button text color. Bu View don't have this method
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
//v.setBackgroundColor(Color.RED);
//so how to change pressed button text color ?
//v.setTextColor(colors);
}
};
There wouldn't be just one button. There would be many of those and i need to chnage text color when button pressed.
I know you asked about changing text color, and everyone else has pretty well covered that, but you could also change the button color itself (which I find much more visible than a text color change :p)...
import android.graphics.PorterDuff;
To set it to green (assuming you start with a standard gray button):
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Button aButton = (Button) view.findViewById(R.id.abutton);
aButton.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
}
}
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
Button button = (Button)v;
button.setTextColor(Color.RED);
}
};
button.setTextColor(Color.WHITE);
This will change the textcolor of the button.Do u want to give a press effet to button when click on it?
the best way to do this is not programmatically (using selector), but if you want to do it programmatically you can cast it to Button and then change the color.
public void onClick(View v) {
Button b = (Button) findViewById(v.getId());
b.setBackgroundColor(Color.RED)l
}
If you are interested to use the View of onClick(View v) then just cast it to Button
public void onClick(View v) {
if(v instanceof Button){
((Button)v).setTextColor(Color.WHITE);
}
}

Which context do i need?

I'm creating a dialog box and using the (this) isnt working. Up until now its just been a button calling a dialogbox but now the button within the called dialogbox needs to call another dialog. The Dialog dialogdelcon is the one with problem.
Here is the code:
case R.id.delappt:
//rmvall();
final Dialog dialogdelsel = new Dialog(this);
dialogdelsel.setContentView(R.layout.delsel);
dialogdelsel.setTitle("What would you like to do?");
dialogdelsel.setCancelable(true);
Button btndelsel = (Button) dialogdelsel.findViewById(R.id.btndelsel);
btndelsel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete selected code here.
}
});
Button btndelall = (Button) dialogdelsel.findViewById(R.id.btndelall);
btndelall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete all code here.
final Dialog dialogdelcon = new Dialog();
dialogdelcon.setContentView(R.layout.delcon);
dialogdelcon.setTitle("Deletion Confirmation");
dialogdelcon.setCancelable(true);
Button buttoncnclok = (Button) dialogdelcon.findViewById(R.id.btndelcon);
buttoncnclok.setOnClickListener(new OnClickListener() {
// on click for cancel button
#Override
public void onClick(View v) {
dialogdelcon.dismiss();
}
});
dialogdelcon.show();
}
});
dialogdelsel.show();
break;
getApplicationContext() or use YourActictyName.this Because this refers the button click listner ,not your class Object
If this code is in the onCreate() method, or similiar, add getApplicationContext() instead of this and you should be fine. That's because this in a Button-context will refer to the button environment.
To improve the isolation between the two dialogs, it would be best to call showDialog(R.id.delapptcon) from the onClick handler. Then load the new dialog in the onCreateDialog of your activity. In this way, you can create more reusable dialogs and avoid the scoping issue you have now.

how to calculate the number of touches on a button in android

hi i am a new developer. i am trying to design an app. In my app i want to calculate the no of touches in a particular button. Is this can be calculated by onTouch process if yes can anyone give me an example or idea.
Try below code
First Create an Global variable
int numberOfClick = 0;
Now for your button try following code
clickButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
numberOfClick++;
}
}
now you can get the number of clicks by this variable
A click on a button is sent to the app via the onClick event. So if you have a Button:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(myClickListener);
You can set up your onClickListener to do whatever you want when the button is clicked.
// Create an anonymous implementation of OnClickListener
private OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
// increment the counter on click
numberOfClicks++;
}
};

Categories

Resources