How do I handle the click of an array of EditText? - android

I have a little problem with my app. In my app dynamically I create a table of EditText, obtaining an array of EditText, at this point would like to draw, when I click on the EditText my Keyboard instead of that of default. How do I handle the click of an array of EditText?

Try :
for(int i=0; i<etArray.lenght; i++) {
etArray[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}

Related

How do I remove numbers on phonepad if I press backspace button

I am trying to make a a phonepad in android and I need to know how I make my backspace button work when I press it, it remove my last number and so on.
b13.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int len=text.length();
text.setText(text.getText().toString());
String res=text.substring(0,len-1);
}
});
b13 is button id for remove button
You're on the right track. Try this:
#Override
public void onClick(View view) {
String newText = myTextView.getText().toString()
.substring(0, myTextView.getText.toString.length()-1);
myTextView.setText(newText);
}
Take a look at KeyEvent, there is a constant called KEYCODE_BACK that should achieve this behavior

How to add edittext and checkbox into listview android?

I have a problem, when edit-text is setted enable as false. I want to set the edit-text enable as true when the user check the check box in list view but it's not working properly
In the getView method of the customadapter add these lines:
holder.cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(holder.cb.isChecked())
{
//enable edit text
}
else
{
//disable edit text
}
}
});

How to make item visible and invisible by clicking same line?

I have one Linearlayout - totalincome and another TableLayout normalincometable, which should appear just below the totalincome. normalincometable will be invisible when the program runs. When the user clicks on "totalincome" the table should display. If the user clicks on "totalincome again", the table should disappear. I have tried this code, but It didnt work.
totalincome.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int x =0;
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
From this code, I can make the table visible in first click but It doesnt disappear in next click. Are there any options ?
Try this:
#Override
public void onClick(View v) {
if(normalincometable.getVisibility() == View.VISIBLE) {
normalincometable.setVisibility(View.GONE);
} else {
normalincometable.setVisibility(View.VISIBLE);
}
}
You have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.
because you have define x in the button click code so whenever button click it set to 0. define x outside the button click scope.
try this way:put x variable outside the button onclick() or defined x globally
int x =0;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
Use like this:
int x =0;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (x==0)
{
normalincometable.setVisibility(View.VISIBLE);
x=1;
}
else
{
normalincometable.setVisibility(View.GONE);
x=0;
}
});
}
Try this
Boolean isFirstTimeClicked=true;
totalincome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isFirstTimeClicked)
{
normalincometable.setVisibility(View.VISIBLE);
}
else
{
normalincometable.setVisibility(View.GONE);
}
isFirstTimeClicked=!isFirstTimeClicked;
});
}
and in your code you have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.
Easiest Method is
button.setVisibility(View.VISIBLE == button.getVisibility() ? View.GONE:View.VISIBLE);

Android Trouble with Edit Text Views

In my application,on press of a button "Add contact" phone book gets open and then user selects a contact that get displayed in Edittext View and when another button "Add More Contacts" is pressed, a another Edit-Text View gets displayed on the top for which i can again select the contact from phone book. But the problem that i am facing is that i want that user can only add up to 5 Edit Text .
I am using the following code, but its force crashing the application. Please help.
And also i want the Edit Text Views to be non editable, for which i tried editable=false but its working only for the first Edit Text View not on the other Views, that user adds afterwards.
int id = 1;
layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
btn_addmore_cntct.setOnClickListener(OnClick());
EditText editview = new EditText(this);
editview.setText("Add more");
editview.setEnabled(false);
editview.setFocusable(false);
}
// implementing OnClickListener OnClick() method for "btn_addmore_cntct"
// button
private OnClickListener OnClick() {
// TODO Auto-generated method stub
// changing return type "null" to "new OnClickListner"
return new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(layoutLinear.getChildCount()>5){
}else{
final EditText tab = new EditText(getApplicationContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tab.setId(id);
id++;
layoutLinear.addView(tab,0);
tab.requestFocus();
}
Just check number of layoutLinear child
if(layoutLinear.getChildCount()>5){
//nothing to do
}else{
//create new EditText and add to layoutLinear
}
user this code to disable EditText
edittext.setEnabled(false);
edittext.setFocusable(false);
You can use following way.
int temp;
public void onCreate(Bundle savedinstance) {
super.onCreate(savedinstance);
setContentView(R.layout.yourlayout);
temp=1;
layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
btn_addmore_cntct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(temp<=5){
EditText tab = new EditText(getApplicationContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tab.setEnabled(false);
tab.setFocusable(false);
layoutLinear.addView(tab);
temp++;
}else{
//Print message to user Cant add more editText.
}
}
});
}
Let me know its working or not.

Android Button Problem

i am making a app which generate buttons according to the value entered by user. each button have have there own function defined in XML. Now my main problem is how to shorten these codes.
name[0].setClickable(true);
name[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[0].setText("kjghjbjhb");
}
});
name[2].setClickable(true);
name[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[2].setText("kjghjbjhb");
}
});name[1].setClickable(true);
name[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[1].setText("kjghjbjhb");
}
});
and soo on.....writing these codes again and again is not possible as button generated are dynamic, i dunno how many buttons will be generated. Please tell if there is a some other way to do this.
Something like this?
createButton(int i){
name[i].setClickable(true);
name[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[i].setText("kjghjbjhb");
}
});
}
With this method you can also make a for-loop:
for (int i = 0; i<name.length; i++){
createButton(i);
}
Here I am specifying the steps to be executed.
You must be creating the buttons by new Button(); just hold its reference in a Collection say ArrayList
ArrayList ar = new ArrayList();
Button b1 = new Button();
ar.add(b1);
Now create a private inner class which is implementing the View.OnClickListener. Now as per rules implement theOnClick() and so the stuff which you want to be done at there
class A extends Activity{
// your stuff here for OnCreate and other business logic
private final class MyListener implements View.OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
v.setText("kjghjbjhb");
}
}
}
Notice that I am setting the text with the reference of object v in onClick. Also make this class singleton.
Now set create the instance of this class (as the MyListerner will be singleton the object will be one) in the setOnClickListener() like this:
MyListener listener = MyListener.getInstance();
b.setOnClickListener(listener);
You can opt this way when the buttons are created on some event or user action. In case if you need to create the buttons in loop you can use the 1st and 3rd step in loop.

Categories

Resources