Android Trouble with Edit Text Views - android

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.

Related

How to add delete button on Touch event of dynamiclly generated view

I am working on an application, In this I have number of dynamically generated textviews and Image views. Now I want when I touch on any of the dynamically generated view it should come to front and should appear a cross button on it to delete. I am stucking on this for long time please help me. Thanks in advance.
this code will add view into LinearLayout
LinearLayout layout2=(LinearLayout )findViewById(R.id.relaGameZhaiGuoZi);
layout2.setOrientation(LinearLayout.VERTICAL);
Button btn1=new Button(this);
setContentView(layout2);
Button btn2=new Button(this);
btn1.setText("Button1");
btn2.setText("Button2");
layout2.addView(btn1);
layout2.addView(btn2);
hope it helps
LinearLayout layout2=(LinearLayout )findViewById(R.id.relaGameZhaiGuoZi);
layout2.setOrientation(LinearLayout.VERTICAL);
Button delbtn=new Button(this);
Button refreshbtn=new Button(this);
btn1.setText("Button1");
btn2.setText("Button2");
layout2.addView(delbtn);
layout2.addView(refreshbtn);
Then Add listener like this
delbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//del img
}
});
refreshbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//refresh img
}
});
private Button button;
onCreate(...) { //or any other method
button = new Button(this);
button.setText("touch me");
LayoutContainer cont = (LayoutContainer)findViewById(R.id.container); //your id here
cont.add(button);
button.addOnClickListener(new OnClickListener{
#Override
... onClick(){
cont.removeView(button);//and again, not sure of method name
}
}); // like this, i'm not sure
}

how to know which button called a method?

I'm using a method : button_click(View view) to set a text on an editText , and I have a lot of buttons and each one of them should set text on a specific editText .
Is there any way to know which button called the method , so that I can set text of the correct editText ?
here is the code of the method :
public void button_click(View view) {
// Create the dialog
final Dialog mDateTimeDialog = new Dialog(view.getContext());
// Inflate the root layout
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
final RelativeLayout mDateTimeDialogView = (RelativeLayout) inflater.inflate(R.layout.datepick, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView
.findViewById(R.id.DateTimePicker);
mDateTimePicker.setDateChangedListener(this);
// Update demo edittext when the "OK" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.SetDateTime))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.clearFocus();
// TODO Auto-generated method stub
String result_string = mDateTimePicker.getMonth()
+ "/"
+ String.valueOf(mDateTimePicker.getDay())
+ "/"
+ String.valueOf(mDateTimePicker.getYear())
+ " "
+ String.valueOf(mDateTimePicker.getHour())
+ ":"
+ String.valueOf(mDateTimePicker.getMinute());
edit_text1.setText(result_string);
mDateTimeDialog.dismiss();
}
});
// Cancel the dialog when the "Cancel" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.CancelDialog))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mDateTimeDialog.cancel();
}
});
// Reset Date and Time pickers when the "Reset" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.ResetDateTime))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mDateTimePicker.reset();
}
});
// Setup TimePicker
// No title on the dialog window
mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the dialog content view
mDateTimeDialog.setContentView(mDateTimeDialogView);
// Display the dialog
mDateTimeDialog.show();
}
The passed View is actually the button. All you need to do is switch the ID like so:
switch (view.getId()) {
case R.id.button1:
// Do something here related to button 1
break;
case R.id.button2:
// Do something here related to button 2
break;
}
Edit: Typo
the View you clicked upone is the argument of the onClick. If you need its id you can retrieve it through
int id = view.getId();
in this way you can switch upon the R.id.yourButtonId to understand which has been clicked
You're in luck - the View view that is passed to your handler is the actual view that was clicked. So the view itself is the button. Check out the docs.
In this method you have passed view which fired event (Button)v should give you reference
public void onClick(View v)

Programmatically created textviews not displaying correctly or saving

I have an activity setup with an edittext field and an add button. Essentially when the user makes changes to the edittext and hits Add, a new textview should be created with the edittext field. In addition i want the textviews created to be available even after i leave the screen or activity. The problem i have is that when i click add, a default values shows in the newly created textview each time, and when i leave the activity, all the textviews are gone altogether. Any suggestions?
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.adddocnote);
etDocNote = (EditText) findViewById(R.id.editDocNote);
btnAdd1 = (Button) findViewById(R.id.btnAdd1);
nLayout = (LinearLayout) findViewById(R.id.linearLayout);
TextView tvNote = new TextView(this);
tvNote.setText("");
btnAdd1.setOnClickListener(onClick());
etDocNote.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v1, int keyCode1, KeyEvent event1) {
if ((event1.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode1 == KeyEvent.KEYCODE_ENTER)) {
getSharedPreferences("myprefs", 0).edit().putString("etDocNote", etDocNote.getText().toString()).commit();
return true;
}
return false;
}
});
}
private OnClickListener onClick() {
// TODO Auto-generated method stub
return new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
String newDocNote = getSharedPreferences("myprefs", 0).getString("etDocNote", "" );
nLayout.addView(createNewTextView(newDocNote));
}
};
}
private TextView createNewTextView(String newText) {
// TODO Auto-generated method stub
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tvNote = new TextView(this);
tvNote.setLayoutParams(lparams);
tvNote.setText(newText);
return tvNote;
}
}
So your question has two parts:
How do you correctly add the textView with the text from the editText:
Right now you have a timing issue. You are trying to save in shared preferences the text when someone punched keys. This is not necessary since you can retrieve the editText contents when the add button is clicked.
Here is what you need to do: Remove setOnKeyListener and change your onClick() function to be
return new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
String value = etDocNote.getText().toString();
nLayout.addView(createNewTextView(value));
// Save to shared preferences
getSharedPreferences("myprefs", 0).edit().putString("etDocNote", value).commit();
}
};
How do you restore your view when you return to the activity.
Since your text is now saved in shared preferebnces. In your oncreate, you need to retrieve and restore your TextViews.

Add delete option with dynamically generated editText

I'm new in android and i have created editText dynamically with the following code while clicking add new button.Is it possible to add a delete button near editText so that each while clicking the delete respective editText will be removed?
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
EditText newPass = new EditText(getApplicationContext());
allEds.add(newPass);
newPass.setHint("Name of Label");
newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newPass.setWidth(318);
newPass.setTextColor(Color.parseColor("#333333"));
newPass.setId(textb);
rAlign.addView(newPass);
MY_BUTTON ++;
addSpinner();
}
});
Yes, create your remove button at the same time as your EditText and use the removeView() method just like the addView() method, maybe like this:
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
...
rAlign.addView(newPass);
MY_BUTTON ++;
addSpinner();
Button btnRemoveOld = new Button(this);
btnRemoveOld.setId(32); // arbitrary number
btnRemoveOld.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
rAlign.removeView(findViewById(textb));
}
});
// You will need to set parameters to define how the button looks
// and where it is in relation to the EditText here
rAlign.addView(btnRemoveOld);
}
});

I want to get EditText value in ListView

I have implemented Listview. I can see my string array inputs in the list view. There is one Edittext box now I want to add value in editText Box everytime addbutton is click it should be seen on the listview later on I have to retrieve the whole list or multiple selection list
I am stuck at how to add the edittext value to the string. Below is the code Please tell me why on clicking Addbutton the edittext is not showing up in the list and how it will refresh everytime I add ? ?
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.multi);
ListItem=new ArrayList<String>();
ListItem.add("000");
ipList=(ListView) findViewById(R.id.ListItem);
AddButton = (Button) findViewById(R.id.Add_Button);
AddItem.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ListIP.add(getText(R.id.Multi_Add_EditText).toString());
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,IP);
Just call
adapter.notifyDataSetChanged();
after
ListIP.add(getText(R.id.Multi_AddIP_EditText).toString());
to see the new value in the list.

Categories

Resources