EditText with cross(x) button at end of it - android

Is there any way to add the x-graphics in the android Editbox like the of iPhone
So that by clicking on that x graphic it can clear all the values in the Editbox
Is there any way to listen weather i touch a specific part of an edit text
Thank you

Yes there is a way to achieve this.
Define a RelativeLayout like this one.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<ImageButton android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:layout_alignRight="#id/edittext"/>
</RelativeLayout>
Now what happens. The ImageButton gets drawn on top of the EditText. We set its right edge to be equal to the right edge of the EditTexts in order to get it appear on the right side.
Now you have to assign your ImageButton an OnCLickListener with if overridden method to just set the EditTexts text to a empty string like that.
EditText editText = null;
ImageButton clear = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clear);
editText = (EditText) findViewById(R.id.edittext);
clear = (ImageButton) findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
editText.setText("");
}
});
}
Now here we simply tell our ImageViews OnClickListener to reset our EditTexts text upon a click. Simple as that. ;)
Of course my example uses not very aesthetic images but you can fine tune the images yourself. The principle works.

You can download it, it works like iPhone
https://github.com/GhOsTTT/editTextXbutton

Related

How to make EditText's parent clickable?

Suppose I have this pink box:
It consists of LinearLayout with its children: TextView as field name and an EditText. EditText is intentionally disabled. What I want is that, user can click wherever user wants on that pink box. By the way, please just ignore any UI/UX things that you found weird.
I've tried, but user can't tap the area that EditText occupies. User have to tap in TextView or blank area on pink box so that the apps got the 'click'. But if user taps on EditText's area, nothing will happen.
I've tried playing with some things in xml's properties, such as set LinearLayout's clickable to true, and all children or just EditText's properties of clickable, focusable, and focusableInTouchMode to false, all to no avail. EditText area still cannot be clicked.
Any idea? Can't it be reached just through xml? Should it be done programmatically just to turn bypass EditText's click?
You can simply add onTouch Listener instead of click Listener.
You need to request the parent layout (LinearLayout, whatever) and loop through the views if you dont want to bind them all. If you use databinding its easier. Anyway, here is a solution (a small piece of code is required!).
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="out of focus"/>
<LinearLayout
android:id="#+id/linearTest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test for clicking"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="another clicking test"
/>
<EditText
android:id="#+id/editTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="focus edittext when clicking linearlayout or other elements inside"
/>
</LinearLayout>
</LinearLayout>
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear_focus_test);
final EditText editText = (EditText) findViewById(R.id.editTest);
LinearLayout linearTest = (LinearLayout) findViewById(R.id.linearTest);
for (int i = 0; i < linearTest.getChildCount(); i++)
View v = linearTest.getChildAt(i);
v.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
editText.requestFocus();
}
});
}
}
If you dont like boilerplate you may also use lambda (use 1.8 features)
for (int i = 0; i < linearTest.getChildCount(); i++)
linearTest.getChildAt(i).setOnClickListener(v1 -> editText.requestFocus());
If you use at least API 24 you can even make it shorter:
IntStream.range(0, linearTest.getChildCount()).forEach(i -> linearTest.getChildAt(i).setOnClickListener(v1 -> editText.requestFocus()));

How to fill an activity with either checkbox or radiogroup?

I'm trying to make an app that would ask the user questions and the user would select answer(s) and click the button to submit the answer(s). The answers can be in form of checkboxes or radiogroup to make it fun. However, I've got trouble figuring out how it will be possible to fill an activity with one of them based on how the answer is designed.
To pose the question, I can use a TextView; all good there. But how would I change the second view to checkboxes or radiogroup based on the type of answer I'm expecting? As a beginner, I've learnt how to make use of different views, but haven't come across such a case.
Any suggestions would be greatly appreciated.
You can use ViewSwitcher
layout xml
<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ViewSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/button_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chk_android"
android:checked="true" />
<RadioButton
android:id="#+id/button_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_male"
android:checked="true" />
</ViewSwitcher>
Activity class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.ViewSwitcher);
Button Next = (Button) findViewById(R.id.button_checkbox);
Button Previous = (Button) findViewById(R.id.button_radio);
To navigate through the views use showNext() or showPrevious() methods
switcher.showNext();
switcher.showPrevious();

Android apply animation to button background

I have a button which currently has an animation assigned to it on click:
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
v.startAnimation(animRotate);
}
});
Here is an example of the button in XML:
<Button
android:id="#+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="#string/btnText"
android:textColor="#ffffff"
android:textSize="18dp"
android:padding="12dp"
android:drawableTop="#drawable/btnIcon"
android:layout_weight="1" />
The animation works spot on, however on the button itself there is a text value and a drawable value, and I would like to be able to target the drawable and animate that only. I have searched all over the net and everything appears to be targeting objects by their id without any background/drawable selection.
Any feedback is welcome, even if its just to inform me that it can't be done.
Many thanks.
You may want to create a FrameLayout with the TextView and the Button and animate only the Button.

View.layout() works until next UI update

My launching activity consists of Linear layout with two buttons. Both buttons have listeners: the first one (b) moves itself upon click: 30px to the left and 30px back upon the next click.
The second one (b2) changes its text upon click. Here is the code:
public class TestActivity extends Activity {
public final String TAG="TestActivity";
boolean toTop=true;
boolean setInitialText=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b=(Button)findViewById(R.id.button);
Button b2=(Button)findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int modifier;
if(toTop) modifier=-30;
else modifier=30;
v.layout(v.getLeft()+modifier,v.getTop(),v.getRight()+modifier,v.getBottom());
toTop=!toTop;
}
});
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String currentText;
if(setInitialText)currentText="Press to change text";
else currentText="Press to change back";
((Button)v).setText(currentText);
setInitialText=!setInitialText;
}
});
}
}
XML layout-file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to begin animation" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to change text" />
My problem: when b is moved to the left and I press b2, b moves to its initial position. Why? I don't want it to move back and didn't specify it anywhere.
It looks like View.layout looses its effect. Why it happens? I tested that in other situations and it seems that any UI update makes all invoked View.layout methods loose their effect.
In my main project there is a ListView which is populated with images from background - all view moves loose effect when new image appears. Besides, if I add EditText and try to enter something (as user), view moves loose their effect as well. Can anyone explain to me what's going on and why do views move back?
Looks like parent layout repositions it's siblings after you set a new text for the button2, because as describe in xml, button2 wraps it's content by width and height.
As you changed button's content, it requests it's parent layout to get a new position for it. In this case parent layout will recalculate position values for all of it's siblings. That's why button1 also comes back to it's previous position.
Keep in mind, that you also set the gravity value for parent layout as center, that means that when layout will position it's siblings it will position them in it's center.
Try to experiment with some ohter layout classes like FrameLayout, which has absolute manner of positioning it's siblings and RelativeLayout which and also try your case getting rid of layout's gravity.
Here says that this issue may be solved, using view.setLayoutParams() instead of view.layout()

LinearLayout's click listener is never called

Trying to get an onclick listener working on a linearlayout but its never called :(. Have enabled clickable and focsuable (both modes) and still cant get the click listener to respond. Platform details: Android 3.0.. Any help?? Code below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_items_button"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center_horizontal"
android:paddingTop="#dimen/gen_margin_xsmall"
android:paddingBottom="#dimen/gen_margin_xsmall"
android:background="#drawable/rule_bg_menu_button"
android:clickable="true"
android:focusableInTouchMode="true"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/menu_items"
android:tag="image"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="text"
android:text="#string/menu_items_icon_txt"
style="#style/textDisplay.mediumLarge"
/>
</LinearLayout>
and in the code to add the event listener
_itemsButton = (LinearLayout) menu.findViewById(R.id.menu_items_button);
final Intent itemsIntent = new Intent(this, ItemsActivity.class);
_itemsButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(itemsIntent); //Never called!
}
}
);
The reason I'm doing this and not using an Image button instead is because the background of the "button" is state based (gradient changes) but also the image and in order to combine to the two on click / on focus, I used a linearlayout which has an ImageView in itself.. any suggestions on why the clickListener is not working on the linearLayout?
thx
Did the click go to the ImageView instead of the LinearLayout? Try clicking in the pad area (if any) or try putting the click listenner on the ImageView1.
(adding my response as a new answer so that I can use the PRE tag.)
The easy way is to set the same click listener on the image view and the text view.
View.OnClickListener activityLauncher = new View.OnClickListener() {... }
layout.setOnClickListener(activityLauncher);
imageView.setOnClickListener(activityLauncher);
textView.imageView.setOnClickListener(activityLauncher);
The width of your LinearLayout is set to "0dip", you should see nothing on the screen.
If the width is changed to "FULL_PARENT", that works. Please check your code carefully again.

Categories

Resources