How to ban system soft keyboard? - android

I code to ban system soft keyboard.It can achieve the effct,but it disappear after one flash.I don't want this one flash.This is my code.
public class MainActivity extends Activity {
private EditText mEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditText = (EditText) findViewById(R.id.test_et);
mEditText.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
hideSoftInputMode((EditText)v);
}
});
}
private void hideSoftInputMode(EditText editText) {
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editText.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}

In your manifest file, you can set the windowSoftInputMode to stateAlwaysHidden:
<activity
...other attributes
android:windowSoftInputMode="stateAlwaysHidden">
</activity>

There is another way - don't lay focus on your EditText. Set it on the View at the background (the Layout) or something. This can be done by using:
setFocusable(true);
requestFocus();
This should do the trick for you without working with the Manifest etc.

Related

Android view isDirty inside onClick

I want to check if the text in some EditText is changed, after user clicks some Button. But View#isDirty seems not to return the correct state of the EditText if called inside onClick. For instance, I wrote something like this:
public class MainActivity extends Activity {
EditText editText;
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.f);
editText = (EditText) findViewById(R.id.e);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println((editText.isDirty() ? "is dirty" : "is clean"));
}
});
}
}
before i make any change to the editText, it outputs is clean, as expected. But the same is clean is printed even after I write something in editText.
When will isDirty be called? And is it the correct way to do this at all?
Update:
I also want to check if some Switch and Spinner values are changed. Is isDirty() the correct way to do this?
By the time you click your button edittext is no longer dirty - text is already updated and view redrawn. Maybe if you change your onclick handler you will understand better what is going on.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText = (EditText) findViewById(R.id.e);
System.out.println((editText.isDirty() ? "is dirty" : "is clean"));
}
});
isDirty will return true only as long as view has not been redrawn. This happens quite quickly and basically you do not have (and dont need) any control over this.
I think you need to use some other methods to achieve what you want.
I would suggest to use:
https://stackoverflow.com/a/9459848/5684335
The comment from Okas is a good explanation why.

FindViewById returning null EditText

I have common problem with findViewById() function, it returns null:
<EditText
android:id="#+id/nameText"
/>
<Button
android:text="Save"
android:onClick="buttonClick1"
/>
</TableRow
android:onClick="buttonClick1"
/>
Activity1.java:
public class Activity1 extends ActionBarActivity {
public void buttonClick1(View view) {
setContentView(view);
EditText nameText = (EditText) view.findViewById(R.id.nameText);
EditText lastNameText = (EditText) view.findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) view.findViewById(R.id.indexNumberText);
Log.d(">>>> ", nameText.getText().toString());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
}
}
}
In buttonClick1() findViewById() returns null. Please explain why?
Remove setContentView(view); from buttonClick1 method
and initialise all your textview in this manner by removing view.
EditText nameText = (EditText) findViewById(R.id.nameText);
EditText lastNameText = (EditText) findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) findViewById(R.id.indexNumberText);
Also initialize the controls in the xml properly by giving height and width to controls
Multiple problems unless you're not posting all your code.
1) In your XML you close a TableRow tag, but I don't see you opening it. Might just be lazy copy-pasting.
2) In your onCreate you seem to be missing a listener for your button. There is nothing to indicate that you have a button anywhere. You need to find the view of your button as follows:
Button button = (Button) findViewById(R.id.nameOfButton);
Then you need to set a listener for it like this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here goes whatever should happen when you click the button.
}
});
3) As for your ButtonClick1 method, delete it. It looks like you were trying to create a listener, but it is pretty far from what it should look like.
Try to replace your code by this one :
public class Activity1 extends ActionBarActivity {
#
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
EditText nameText = (EditText) findViewById(R.id.nameText);
EditText lastNameText = (EditText) findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) findViewById(R.id.indexNumberText);
}
public void buttonClick1(View view) {
//Your stuff
Log.d(">>>> ", nameText.getText().toString());
}
}
Since now would be the normal code that have you tried, if you want to set a click on a button you'll have to declare it as :
Button button1 = (Button) findViewById(R.id.button1);
Then you can do the :
public void buttonClick1(View v) {
// Your stuff
}
EDIT
Make sure that you have on your XML all of your EditText and all of your Button for example : or , also make sure that you have an android:id="#+id/yourID in all of your stuff..., by the way instead of using an onClick method in your XML for your Button, you could use this to use an OnClickListener
The point 1 to 3 should go inside of onCreate the point 4 should go outside of onCreate.
1.- Implements View.OnClickListener in your Activity1
public class Activity1 extends ActionBarActivity implements View.OnClickListener {
2.- Declare it
Button button1 = (Button) findViewById(R.id.button1);
3.- Do the setOnClickListener like this :
button1.setOnClickListener(this);
4.- Create an OnClick method :
#Override
public void onClick(View view) {
if (View.equals(button1))
//Your stuff
}

Restoring KeyListener of EditText in android after setting it to null loses Input Type options

I need to make EditText read-only after taking input from user once. I am setting its KeyListener to null after saving its KeyListener object in a variable using getKeyListener(). But the InputType information (eg: Email, URI etc) is lost after I restore the KeyListener object back using setKeyListener().
public class MainActivity extends Activity {
EditText et;
KeyListener kl;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et1);
et.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
kl = et.getKeyListener();
et.setKeyListener(null);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ST","changed");
et.setKeyListener(kl);
}
});
}
}
Here I have set InputType to InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS but on restoring the KeyListener, Soft Keyboard is of type TPYE_CLASS_TEXT and not Email.
How can I get the previous InputType state of EditText after setting its KeyListener to null?
EDIT
I added a log after et.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS) to get values of InputType and I get the following :
D/ST(16190): et.getInputType() : 33, et.getKeyListener().getInputType() : 1
And after restoring the KeyListener I get the follwing result:
D/ST(16190): et.getInputType() : 1 et.getKeyListener().getInputType() : 1
Shouldn't they be same?
On which value (et.getInputType() or et.getKeyListener().getInputType()) does the type of soft keyboard dispatched depend upon?
more appropriate:
et.setEnabled(false);
otherwise
et.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_EMAIL_AD‌​DRESS);
after you reset the keylistener
fathersNameET.setKeyListener(new BaseKeyListener() {
#Override
public int getInputType() {
return 1;
}
});
fathersNameET is your editText instance name.
Thanks
The following should be able to stop a user typing into it after it is called
et.setFocusable(false);

How to let the soft keyboard output from the Edittext which gets focus

I make a soft keyboard in application. There are two edittext, click can get focus, but the soft keyboard can just output in the second edittext. Is there any way to l let the soft keyboard output from the Edittext which gets focus?
Remove android:focusable attribute, android:foucsableInTouchMode attribute and tag in the XML file, Do it now, then
public class YOURMAINActivity extends Activity implements OnClickListener {
private EditText editTxt1;
private EditText editTxt2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_id);
editTxt1 = (EditText)findViewById(R.id.edit_text_one_id);
editTxt2 = (EditText)findViewById(R.id.edit_text_two_id);
editTxt1.setOnClickListener(this);
editTxt2.setOnClickListener(this);
#Override
public void onClick(View view) {
switch(v.getId()) {
case R.id.R.id.edit_text_one_id :
editTxt1.requestFocus();
editTxt1.setFocusable(true);
editTxt2.setFocusable(false);
break;
case R.id.R.id.edit_text_two_id :
editTxt2.requestFocus();
editTxt2.setFocusable(true);
editTxt1.setFocusable(false);
break;
}
// Add Code for Showing Keyboard Here
}
}

Square button android

I want to make a button square with the width the same as the heigth.
I try bt.setWidth(bt.getHeight()) but it doesn't work.
If I hardcode the width (bt.setWidth(90)) it works but I don't know the height so I can hardcoded it.
Here is some code. When I click on a button, it opens a dialog and this dialog must contain the square button.
public class MyClass extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
}
public void doClick(View view) {
final Dialog dialog = new Dialog(view.getContext());
dialog.setContentView(R.layout.dialogLayout);
dialog.setTitle("Title");
Button bt = (Button) dialog.findViewById(R.id.myButton);
bt.setWidth(bt.getHeight());
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
dialog.show();
}
}
How could I do it?
The direct answer is that until the dialog is displayed, it has not measured it's layout and the button height will be zero. You could extend the Dialog class and override the onMeasure() method or attach a global layout listener to the layout and set the button size in onLayoutComplete().
However, your approach might be wrong. Why can't you do this in the dialog's layout XML?

Categories

Resources