I don't want the user to insert anything in the middle of the EditText box. I only want to allow the user to click on the EditText box to bring out the keyboard, and insert at the end of the EditText box.
Is there a way to disable the ability for the user to change the cursor position when clicking on the EditText box?
I don't mind if the answer if done in XML or Java.
Thank you :)
Edit: if I have to use setSelection, is there a way to detect when the EditText box is clicked? so that I can set the cursor back to the end when the user click on the EditText.
Use setSelection to set the cursor position. But that won't lock the insertion point. You can tell if someone changes the selection if you create a custom view and subclass EditText to override setSelectionChanged to force a different change to your liking. You may also want to override onTextChanged to verify that any change that happens does not break your rules.
You can apply this trick, This worked for me.
Just disable the cursor from edittext and on its click listener add your code that will keep the cursor on right most.
Here is code that i tried.
<EditText
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="12"
android:background="#color/white"
android:padding="#dimen/activity_margin_10"
/>
Java Code
EditText mEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mEditText = (EditText)findViewById(R.id.edit); // Initialzation of Edittext
mEditText.setSelection(mEditText.getText().length()); // After initialization keep cursor on right side
mEditText.setCursorVisible(false); // Disable the cursor.
/*
Add Click listener and on put your code that will keep cursor on right side
*/
mEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mEditText.setSelection(mEditText.getText().length());
}
});
}
Hope it will help you.
To insert at the end of the EditText box.
you just need to set gravity to the right
android:gravity="right"
Is there a way to disable the ability for the user to change the
cursor position by clicking on the EditText box?
You can use either the xml attribute android:cursorVisible="false" or the java function setCursorVisible(false).
Related
I implemented the interface View.OnclickListener and Override the method onClick.
Problem is, all other buttons and view response on click, but a particular button doesn't work.
Java code for that button is:
if(v.getId() == R.id.btnResetPass){
Log.i("test", "In ResetButton");
resetPassword();
}
and xml file code for this button is:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnResetPass"
android:text="Reset"/>
Nothing happens when I click on this button. But all other Views and Buttons in this onClick method
work properly accept this one. Why isn't it working then?
Have you set the Listener
button.setOnClickListener(this);
I would like the hint to display after a default value I put in the EditText.
For example, the contents of the EditText must look like:
192.168.1.1 (Hint: First IP address) - all inside the EditText, and when the user clicks it, it defaults to show only 192.168.1.1.
The EditText takes a string as input. I want the hint to just be there, but not count as the input string.
Please help.
Thanks
Regards
Ok, I got your point. So you want a floating label when an edit text receive a focus. You just have to wrap your edit text inside TextInputLayout.
Here is my updated sample:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_id"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edit_txt_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/str_reource_id"
android:inputType="depends_on_your_use_case"/>
</android.support.design.widget.TextInputLayout>
Define ip_hint and ip you want in resources then in your onCreate:
editText.setText(R.string.ip_hint);
editTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(final View view, final boolean hasFocus) {
if (hasFocus)
editTxt.setHint(R.string.ip);
else if(editText.getText().equals(context.getResource(R.string.ip))
editText.setText(R.string.ip_hint);
}
});
There is no way to achieve this task. Use hint as input string and append with input string.
Late to the show, but you can also add a formatted string in your hint in onCreate,
<EditText
android:id="#+id/foo"
android:hint="#string/hint_foo"
...
where hint_foo is defined in string resource,
<string name="hint_foo">Foo: %1$s</string>
and then initialise with getString() in activity,
TextView hinter = (TextView) findViewById(R.id.foo);
hinter.setHint(getString(R.string.hint_foo, ip_string));
I have several checkboxes in my Activity_Main.XML similar to as follows
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Video"
android:id="#+id/Video"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/VideoCheck"
android:onClick="onCheckboxClicked"/>
Now on a different activity I want the state of this checkbox to be displayed, for ease of use I have set up the code so it will change the text or a text label. Code in my MenuActivity.java is as follows
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId())
{
case R.id.VideoCheck:
if (checked) {
TextView MyTextLabel = (TextView)findViewById(R.id.Test1);
MyTextLabel.setText("Video");
}
else
{
TextView MyTextLabel = (TextView)findViewById(R.id.Test1);
MyTextLabel.setText("No Video");
}
break;
}
}
Worth saying the text label on the XML display is called MyTextLabel. I think the problem is because the Checkboxes are set up to call "onCheckboxClicked" but that checkbox clicked part is not in the Activity native to that set up.
Essentially the first page(activity_main.xml, MainActivity.Java) of my app has the checkboxes, then a button takes the user to the second page (activity_menu.xml, MenuActivity.java) has the Label set to change depending on the state of the checkboxes on the first page.
I appreciate this is explained horrendously but please ask any question you may have.
You need to send the state of the checkbox in the Intent you use to start the second activity. See Build an Intent for details. Then in the second activity, you need to extract the checkbox state from the received intent. See Receive an Intent.
Note that I am assuming that you already know how to create an Intent to start an Activity. If not, you should read Starting Another Activity.
So, my friend got me to mess with android programming in android studio today. I started making a beginner application and have had some good results. But right now I am trying to get a button to change the value of a textview.
Here is the code for the button and the textview
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one"
android:id="#+id/button3"
android:layout_alignBottom="#+id/button2"
android:layout_alignRight="#+id/textView"
android:layout_toRightOf="#+id/button"
android:onClick="addone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/counter"
android:textSize="40dp"
android:layout_alignTop="#+id/button3"
android:layout_centerHorizontal="true"/>
and here is the code that is attempting to change the value of the text view.
public void addone(){
numtest+=1;
TextView t = (TextView) findViewById(R.id.counter);
t.setText(numtest);
}
THe program compiles and opens, but it crashes whenever i press the button. I believe I have narrowed down the culprit to this line, but im not sure why it isnt working
TextView t = (TextView) findViewById(R.id.counter);
Any ideas?
An overloaded method is your problem. It is subtle, a mistake even a seasoned programmer could make. I've made it myself before.
You are calling TextView.setText(Integer). This attempts to load a string having a resource id of numtest, which does not exist.
Try this instead:
public void addone(View v){
numtest+=1;
TextView t = (TextView) findViewById(R.id.counter);
t.setText(numtest+"");
}
This will call TextView.setText(CharSequence) instead, which is what you're really trying to do.
Also, as Osmium USA demonstrated, your method signature is incorrect. The button pressed callback method must accept a view, and must be public.
When you give a layout element a function to execute, it looks for that name accepting a View (so you know what was clicked if you assign multiple elements the same click behavior in the XML).
So addone() must be addone(View v)
From the Button Docs
In order for this to work, the method must be public and accept a View as its only parameter
William Morrison also has a good point. You should make that number a String either by what he did or use the Integer Class:
t.setText(Integer(numtest).toString());
or
t.setText(numtest+"");
this should work:
public void addone(View view){
switch(view.getId())
{
case R.id.button3:
numtest+=1;
TextView t = (TextView) findViewById(R.id.counter);
t.setText(Integer.toString(numtest));
break;
}
}
I created an Activity displays an ImageView on the screen. I want get haptic feedback when the image is clicked.
In the main layout main.xml I added the next ImageView tag:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:src="#drawable/dog"
android:onClick="doBark"
android:hapticFeedbackEnabled="true"/>
Then, in the Activity code I add this method:
public void doBark(View v) {
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
Log.d("BarkingDog", "is hapticFeedbackEnabled: " + v.isHapticFeedbackEnabled());
}
When I click on the image I can see that doBark() is called and the output of the Logcat says "is hapticFeedbackEnabled: true", but I can't feel anything. I've also tried with the other two HapticFeedback constants, and no luck.
I know that HapticFeedback is enabled because each time I press the menu button, the device vibrates.
Any ideas? Suggestions?
PS: I don't want to use the Vibrator object. By using it, I can make the device vibrate, but I don't think it's the right way to do it.
Take a look at this: http://groups.google.com/group/android-developers/browse_thread/thread/de588e3d15cb9055?pli=1
Do note that it is old though, but the last time I had to use haptic feedback, I followed what Dianne had to say here