int[] etColorId = new int[] {R.id.etColor1, R.id.etColor2, R.id.etColor3, R.id.etColor4, R.id.etColor5,
R.id.etColor6, R.id.etColor7, R.id.etColor8, R.id.etColor9, R.id.etColor10, R.id.etColor11,
R.id.etColor12, R.id.etColor13, R.id.etColor14, R.id.etColor15};
for (int editText : etColorId) {
findViewById(editText).setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
v.setEnabled(false);
}
});
}
int[] btnEditId = new int[] {R.id.btnEdit1, R.id.btnEdit2, R.id.btnEdit3, R.id.btnEdit4, R.id.btnEdit5,
R.id.btnEdit6, R.id.btnEdit7, R.id.btnEdit8, R.id.btnEdit9, R.id.btnEdit10, R.id.btnEdit11,
R.id.btnEdit12, R.id.btnEdit13, R.id.btnEdit14, R.id.btnEdit15};
for (int button : btnEditId) {
findViewById(button).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setEnabled(true);
v.requestFocus();
}
});
}
How can I gain focus to EditText upon clicking a button? I have a bunch of EditTexts and Buttons. Each button corresponds to a specific EditText. What I want is, for example: if I clicked the button1, the EditText1 will be focused. And then when I click other button, the EditText corresponds to that button will be focused and then the other EditTexts will be disabled.
This code for OnFocusChangeListener has to be modified little bit, for anonymous classes you need to have final reference of any object which you wish to modify, since you want to modify EditText
final int[] etColorId = new int[] {R.id.etColor1, R.id.etColor2, R.id.etColor3, R.id.etColor4, R.id.etColor5,
R.id.etColor6, R.id.etColor7, R.id.etColor8, R.id.etColor9, R.id.etColor10, R.id.etColor11,
R.id.etColor12, R.id.etColor13, R.id.etColor14, R.id.etColor15};
Now for OnClickListener do like this
for (int i=0;i<btnEditId.length;i++)
{
final int counter=i;
int button=btnEditId[i];
findViewById(button).setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// HERE v refers to button itself on which you clicked,
// you need to update get edit text so
// based on ith position accessing the same edit as the button correspond too
EditText edt=etColorId[counter];
edt.setEnabled(true);
edt.requestFocus();
// and you are DONE!!!
}
});
}
UPDATE:
new OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if(!hasFocus)
{ // lost focus
v.setEnabled(false);
}
else
{
//you are already enabling on button click
}
}
});
Inside
onClick()
{
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
}
onFocusChange called every time when lost focus and gain focus.
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
v.setEnabled(false);
}
So, first you need to change code
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus) {// gain focus
// Code block
}
if(!hasFocus) { // lost focus
// Code block
}
}
You EditText is disabled every time, when press Button
Related
I am trying to use a Long-Click listener on a Toggle Button to lock/unlock the normal click action of the button (to avoid accidental clicking). The below code seems to have no effect. I have tried .isActivated, .isCickable and .isEnabled properties without luck... Is it possible?
final ToggleButton btnStartStop = (ToggleButton) findViewById(R.id.toggleAction);
btnStartStop.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
if (btnStartStop.isActivated()) {
btnStartStop.setActivated(false);
} else {
btnStartStop.setActivated(true);
}
return true;
}
});
Maybe use a boolean?
Boolean longPress = false;
final ToggleButton btnStartStop = (ToggleButton) findViewById(R.id.toggleAction);
btnStartStop.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
if (longPress) {
longPress = false;
} else {
longPress = true;
}
return true;
}
});
and onClick():
btnStartStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!longPress){
//Do stuff
}
else{
Toast.makeText(getApplicationContext(), "Button is locked!\nLong press button to unlock it",Toast.LENGTH_SHORT).show();
}
});
You need to change your snippet as
final ToggleButton btnStartStop = (ToggleButton) findViewById(R.id.toggleAction); btnStartStop.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
if (togglePref.isChecked()==(true))
{
// button is checked
}
else
{
// button is unchecked
}
return true;
}
});
I have a method with int parameter to getResources for a listView. I want to create a button to call one of each int one by one. How can I do that?
The method:
public void changeData(int i){
Resources res = getResources();
String[] descriptions = res.getStringArray(R.array.tips_description_details);
text.setText(descriptions[i]);
String[] titles = res.getStringArray(R.array.text_titles);
textTitle.setText(titles[i]);
imageTips.setImageResource(images[i]);
}
and this is the button's onClick method:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
You could try something like:
#Override
public void onClick(View v) {
int tagId = (int)v.getTag();
changeData(tagId);
}
Check out the docs for getTag() and android:tag.
I am Using the following code for my custom dialog box.
Code is here
I am using a new layout by setCustomView Method.That layout contains a 'Ok' button and a 'Cancel' button.
I need to close the dialog box when click on cancel.
buttonCancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick" , "YYYYY");
//up to this comes , here what I can wright
}
});
dialogObject.dismiss();
You can use this method
Why not you create custom dialog from here:
http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
Very clearly explained and easy to implement too.
try this :
buttonCancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick" , "YYYYY");
qustomDialogBuilder.dismiss();//this line will close the dialog
}
});
Replace your TestDialogActivity like below,
public class TestDialogActivity extends Activity {
private static final String HALLOWEEN_ORANGE = "#FF7F27";
private AlertDialog alertDialog;
private OnClickListener mShowDialogClickListener = new OnClickListener() {
public void onClick(View v) {
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(
v.getContext())
.setTitle("Set IP Address")
.setTitleColor(HALLOWEEN_ORANGE)
.setDividerColor(HALLOWEEN_ORANGE)
.setMessage("You are now entering the 10th dimension.")
.setCustomView(R.layout.example_ip_address_layout,
v.getContext())
.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
alertDialog=qustomDialogBuilder.create();
qustomDialogBuilder.setAlertDialog(alertDialog);
alertDialog.show();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(mShowDialogClickListener);
}
and replace setCustomView of QustomDialogBuilder like below
public QustomDialogBuilder setCustomView(int resId, final Context context) {
View customView = View.inflate(context, resId, null);
((TextView)customView.findViewById(R.id.ip_text)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
((FrameLayout)mDialogView.findViewById(R.id.customPanel)).addView(customView);
return this;
}
finally add below line into your QustomDialogBuilder
private AlertDialog alertDialog;
public void setAlertDialog(AlertDialog alertDialog)
{
this.alertDialog=alertDialog;
}
To close your dialog click on IP Address text.
Using the QustomDialog Source here in your activity class (TestDialogActivity), you can set the "Ok" and "Cancel" button by setting the Negative and Positive button of the dialog like this:
private OnClickListener mShowDialogClickListener =new OnClickListener(){
public void onClick(View v){
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()).
setTitle("Set IP Address").
setTitleColor(HALLOWEEN_ORANGE).
setDividerColor(HALLOWEEN_ORANGE).
setMessage("You are now entering the 10th dimension.").
setCustomView(R.layout.example_ip_address_layout, v.getContext()).
setIcon(getResources().getDrawable(R.drawable.ic_launcher));
qustomDialogBuilder.setNegativeButton("Cancel", new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
qustomDialogBuilder.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
/**
* Do something here...
*/
}
});
qustomDialogBuilder.show();
}
};
And it will look like this:
Hope you'll find this helpful. Thanks!
I'm trying to create a dynamic listview. I can add an item but i can't remove it right now. The code actually it's very simple and every guide i saw are too much complicated for me and my code. I want something simple to add in my MainActivity to remove the item selceted. I don't care in which way, swipe like gmail or by click or any other way.. I just want i simple way to remove an element of the list. This is the Activity
public class MainActivity extends Activity {
private EditText etInput;
private Button btnAdd;
private ListView lvItem;
private ArrayList<String> itemArrey;
private ArrayAdapter<String> itemAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpView();
}
private void setUpView() {
// TODO Auto-generated method stub
etInput = (EditText)this.findViewById(R.id.editText_input);
btnAdd = (Button)this.findViewById(R.id.addbtn);
lvItem = (ListView)this.findViewById(R.id.listView_items);
itemArrey = new ArrayList<String>();
itemArrey.clear();
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
etInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER) {
addItemList();
}
return true;
}
});
}
protected void addItemList() {
if (isInputValid(etInput)) {
itemArrey.add(0,etInput.getText().toString());
etInput.setText("");
itemAdapter.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText etInput2) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
etInput2.setError("Insert a value");
return false;
} else {
return true;
}
}
}
Is it possible insert some part of code to remove an item inside my activity code? Thanks
try this, While ListView item long ClickListener you can do it
lvItem.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
AlertDialog.Builder adb = new AlertDialog.Builder(
YourActivity.this);
adb.setTitle("Are you sure want to delete this item");
adb.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
itemArrey.remove(position);
itemAdapter.notifyDataSetChanged();
}
});
adb.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
adb.show();
return false;
}
});
This should help you.
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) {
itemArrey.remove(position);
itemAdapter.notifyDataSetChanged();
return true;
}
});
Put this method in your onCreate:
lvItem .setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
itemArrey.remove(position);
itemAdapter.notifyDataSetChanged();
});
}
Of course it is possible.
ArrayList.html#remove
Or if you don't know the index just iterate through the list.
Calling remove in foreach loop in Java
Do this way..
lvItem.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long id) {
itemArrey.remove(position);
itemAdapter.notifyDataSetChanged();
return true;
}
});
To delete item from list just do one thing, get onLongClickListener of listView.
Then open the context menu which has two options
Delete
Cancel
When user picks first item delete it from list. Then call listadapter notify datasetChanged method.
Use this library, I used this and it works very fine, if you prefer Swipe try this:
https://github.com/47deg/android-swipelistview
Try below code:
single click:
listview .setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
showDialog(int position);
});
}
public void showDialog(int position){
AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
alertDialog = builder.create();
alertDialog.setOnDismissListener(new myOnDismissListener());
alertDialog.setTitle(TITLE OF DIALOG);
alertDialog.setMessage(MESSAGE YOU WANT TO SHOW IN DIALOG);
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
YOUR ARRAY.remove(position);
YOUR ADAPTER.notifyDataSetChanged();
}
});
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alertDialog.show();
}
I have a page which consist of a spinner and a submit button. What I want to achieve is when user selects an item in the list and click on submit, it should take him to an other layout having a webview. Each item in the spinner should open different .html page in the layout.
What I have now is the item is being selected from the spinner, but I'm not sure how to perform onclick listener to it...
code for main activity is here
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beef);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*Nothing as of now*/
//I need to call the ID of the selected item from the spinner here and start new activity
}
});
}
}
code of CustomOnItemSelectedListener is here
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if (arg2 == 0) // First item selected
{
//Here I need to give an id for the .html file
}
else if (arg2 == 1) // Second
{
//Here I need to give an id for the .html file
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selItem = arg0.get(arg2); // String representation of the selected item
if (arg2 == 0) // First item selected
{
}
else if (arg2 == 1) // Second
{
}
// etc
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}