I am new to android development.
I want to add EditText and a remove button ( X ) when they click on a button (called “contact”). So that there will be multiple contacts. On click of the remove button the corresponding added EditText with remove button should be disappeared.
It will be something like this,
————————— X
————————— X
————————— X
————————— X
———————
|contact|
———————
If I use any Adapter, how to add the empty EditText and a remove button ( X ). If not any other better options?
Finally I need to get all the values entered in the EditText.
NOTE: On click of “contact” it should be adding EditText and a remove button ( X ).
Kindly give me a clear and simple way to do this.
Thanks!
Create a LinearLayout and button. On the button click call the following method. Also, create a List<EditText> in your onCreate Method to track all added edittexts & retrieve texts later.
private void createNewEditText() {
EditText editText = new EditText(this);
editText.setMaxLines(1);
editText.setSingleLine(true);
editText.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, VectorDrawableCompat.create(resources, R.drawable.ic_clear_black_24dp, null), null);
editText.setOnTouchListener(new View.OnTouchListener {
#Override
Boolean onTouch(View view,MotionEvent event) {
val DRAWABLE_BOTTOM = 3
val DRAWABLE_RIGHT = 2
val DRAWABLE_LEFT = 0
val DRAWABLE_TOP = 1
if (event.getRawX() >= (editText.right - editText.getCompoundDrawables()[DRAWABLE_RIGHT].bounds.width())) {
editTextList.remove(editText);
parentLinearLayout.removeView(editText);
return true;
}
return false;
}
})
editText.requestFocus();
editText.setText(voicetext);
editTextList.add(editText);
parentLinearLayout.addView(editText);
}
You can use a LinearLayoutwith orientation set to vertical. you can then create another layout with yourEditTextand Button and add them dynamically to the LinearLayout within the onClick method for contact button.
Do not forget to generate different IDs for your newly added views to be able to find them later
The X button will then just remove its parent from the layout
Related
I have a fragment with a scrollview. Initially there are 3 edit texts which I add programatically not through the xml. The desired functionlty is when the user clicks the last edit text i.e number 3 it will automatically add a 4th edit text directly below it. When the user clicks on the 4th edit text it will add a 5th and so on. My code which is shown below is almost working. When the user clicks on edit text 3 it will add a 4th and when they click on the 4th it will add a 5th, however after that it will not add anynore edit texts.
I have an onFocusChange listener and I also notice this listener only activates on the 3rd and 4th edittexts. For some odd reason the listener doesnt activate when clicking on any other edit texts.
// Adds 3 edit texts to the view which are stored in an array list.
for (int etId = 0; etId <= 3; etId++) {
etChallenges = new EditText(getContext());
editTextList.add(etChallenges);
mLinearLayout.addView(etChallenges);
}
etChallenges.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
int last = 0;
for(last =0; last <= editTextList.size()-1; last++){
if(last == editTextList.size()-1){
etChallenges = new EditText(getContext());
editTextList.add(etChallenges);
mLinearLayout.addView(etChallenges);
break;
}
}
An optional route out is have a recyclerview with an edittext and an initial item size of 3 and then you can add more edittexts once the last item on the recyclerview is clicked or active.
I have the following peace of code on a Xamarin Android app
_editText = new EditText(context);
_editText.Text = textData.Text;
_editText.Gravity = GravityFlags.Top;
_editText.Clickable = true;
_editText.Enabled = false;
_editText.Focusable = true;
_editText.FocusableInTouchMode = false;
_editText.SetBackgroundResource(Color.Transparent);
AddView(_editText);
The EditText widget gets added inside a framelayout
I want that when the EditText is not editable or disabled, the events to propagate to the parent layout, a RelativeLayout in this case.
If I click on the EditBox the Touch event of the parent RelativeLayout never gets called
If I change the EditBox to a TextView, the event reaches the RelativeLayout's touch event.
How can I set the EditText to disabled but still allow events to propagate to the parent?
Even though this is Xamarin.Android the solution is probably the same in Kotlin or Java.
Thanks!
If you set _editText.Enabled = false this touch event will not executed. If you want to achieve the not editable, disable result and need touch event, you can use following code.
EditText _editText = new EditText(Context);
_editText.Text =" textData.Text";
_editText.Gravity = GravityFlags.Top;
//make the color like EditText is disabled
_editText.SetTextColor(Color.Argb(80,88,88,88));
_editText.Clickable = true;
_editText.Enabled = true;
//cannot get the focus, cursor cannot be seen, text cannot be selected
_editText.Focusable = false;
_editText.SetCursorVisible( false);
_editText.SetTextIsSelectable(false);
_editText.SetBackgroundResource(Color.Transparent);
If I add a click lisener, it executed as normal.
I am working on a project that has 5 buttons when I click on any of them it should show another 3 buttons. When I click on any of the 3 buttons it should open an activity.
I have tried working with MaterialArcMenu it doesn't give me what I need it gave me a circle button .
Can I receive any advice on this?
Button 1 ---> Nested Button 1 --> Nested Button 1
---> Nested Button 2
Button 2
Button 3
Button 4
Button 5
Have 8 buttons in your xml file. 5 of which are visible, 3 of which are hidden & disabled
Have the 5 implement the same onClickListener (using xml onClick attribute is easiest for this)
Have the 3 implement the same onClickListener as well (using xml onClick attribute is easiest for this)
In the onClickListener for the 5 buttons, write code that makes the other 3 buttons visible & enabled
In the onClickListener for the 3 buttons, write code that opens the new activity
If you are creating the button from a layout, then what I would suggest to do is add the activity or fragment as the click event listener. Secondly, set a tag for the button. Then when the button is clicked, get the tag for the view and handle accordingly.
Here is an example:
protected CustomImageButton getSmartLinkImageButton(int buttonImageResource, int colorToChangeTo, boolean isLargerImage, ActionType buttonAction) {
LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
SmartLinkImageButton button = (SmartLinkImageButton) inflater.inflate(R.layout.footer_smartlink_image_button, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mFooterButtonWidth,
isLargerImage == true ? mFooterButtonHeight / 2 : mFooterButtonHeight);
params.gravity = Gravity.CENTER;
button.setLayoutParams(params);
if(isLargerImage)
button.setScaleType(ScaleType.FIT_CENTER);
button.setImageResource(buttonImageResource);
if(colorToChangeTo > 0) {
changeImageViewImageColor(button, null, colorToChangeTo);
button.setDefaultColorFilter(colorToChangeTo);
}
button.setTag(buttonAction); //HERE IS THE TAG CREATION
button.setOnClickListener(this);
//optional method for mapping image buttons
mapImageButton(button, buttonAction);
inflater = null;
return button;
}
Then the onClick:
#Override
public void onClick(View view) {
view.getTag(); //DO ACTION BASED ON VIEWS TAG
}
I have an ImageButton with the image resource R.drawable.img1 and I want to change the image from R.drawable.img1 to R.drawable.img2 or from R.drawable.img2 to R.drawable.img1 everytime I click on the ImageButton.
I was trying this way but it doesn't work:
Integer img1 = (Integer) gridlayout.getChildAt(index).getTag(R.drawable.img1);
Integer img2 = (Integer) gridlayout.getChildAt(index).getTag(R.drawable.img2);
if(img1 == R.drawable.img1){
//code...
}else if(img2 == R.drawable.img2){
//code...
}
I set ImageButton's dynamically with this code:
ImageButton imgBtn1 = new ImageButton(this);
imgBtn1.setId(id);
imgBtn1.setImageResource(R.drawable.img1);
imgBtn1.setTag(R.drawable.ing1);
imgBtn1.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgBtn1.setPadding(5,5,5,5);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.setMargins(-7,-7,-7,-7);
params.width = fieldSize;
params.height = fieldSize;
gridlayout.addView(imgBtn1, index, params);
Thanks!
I am just giving one of the several solutions available ,what i would is in the onCreate i would load the imagebutton with one of images available ,then i would create a class variable i and would set it to 1, when ever i click the imagebutton ,on the onClick of it ,i would increment i and do this
if(i%2==0)
{
setImage1;}
else
{set image2;}
So, if you want to display a different button each time, I would create two buttons in the layout, hide one and show one. When the visible one is clicked, hide it and show the second button. I think that will be easier. You can alternatively use a local class variable to keep count of clicks and switch buttons accordingly.
public boolean showingFirst = true;
if (showingFirst) {
//code here for set needed image on first click
showingFirst = false;
} else {
//code here for set needed image on Second click
showingFirst = true;
}
I set the onClick() function, but when I click the text it works two times that mean I have two dynamic text view. How to resolve it?
My code:
TextView tView[] = new TextView [Array.length];
for(int i =1; i<Array.length; i++)
{
tview[i] = new TextView(this);
tview[i].setId(i);
tview[i].setText(Array[i]);
tview[i].setOnTouchListener(new OnTouchListener()
{
Public boolean onTouch(View v ,MotionEvent event)
{
Toast.makeText(getApplicationcontext,"MapVal",Toast.LengthShort).show();
}
});
}
Problem is in using OnTouchListener. Event onTouch() is calling not one time on every tap action, but minimum two: on touch down and on touch up. Use OnClickListener and setOnClickListener() instead.
if u want to set an onclick listener then use
urtextvw_name.setOnClicklistener()