Take a look at this example:
public class TestEditSoftKbdActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.editText1).setFocusable(false);
findViewById(R.id.editText1).setClickable(false);
findViewById(R.id.editText1).setEnabled(false);
findViewById(R.id.editText1).setFocusable(true);
findViewById(R.id.editText1).setClickable(true);
findViewById(R.id.editText1).setEnabled(true);
findViewById(R.id.editText1).invalidate();
findViewById(R.id.editText1).requestLayout();
}
}
After this sequence of calls the edit text view would no longer pop up its soft input method upon being touched :(
Could someone explain what is going wrong here?
If you want to close soft keyboard for your text view follow this link. Here is a solution for you. But you need to define your own TextView to do that. He suggests using;
public class NoImeEditText extends EditText {
public EditTextEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onCheckIsTextEditor() {
return false;
}
}
Hope it works.
Related
I have created a custom dialog that is used from a preference screen. Everything works fine except one thing: I want to switch the visibility of the Cancel button based on the status of an internal check.
Normally you have onPrepareDialog and onCreateDialog and you can do this in onCreateDialog. But here we have onPrepareDialogBuilder... so where is onCreateDialogBuilder? Where can I do something like
builder.setNegativeButton(null, null);
after onPrepareDialogBuilder? I cannot do it IN onPrepareDialogBuilder since I need the Cancel button in case the internal check fails.
Can you please help me to get into the right direction?
public UnlockPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.dialog_enter_registration);
}
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder);
builder.setTitle(R.string.label_enter_registration);
}
// would need something like
#Override
protected void onCreateDialogBuilder(AlertDialog.Builder builder) {
super.onCreateDialogBuilder(builder);
if (internalCheckOk())
builder.setNegativeButton(null, null);
else
builder.setNegativeButton(..., ...);
}
I have an activity (MyActivity1) which starts a view class (MyView1) using setContentView();
In MyView1, after some drawings with canvas, I want to be able to return to MyActivity1. I tried MyView1.this.setVisibility(View.INVISIBLE) or View.GONE but these just make the screen blank.
I also tried ((Activity)getContext()).finish(); but it creates a runtime error.
I should say that the back button works fine and pressing it closes MyView1 and brings the activity back. However, I want to be able to do that programmatically inside the view. for example I want when user touches a specific part of the screen in the view class, it closes itself and brings back the parent activity. How should I implement this?
It seems a very simple task! but I could not find the answer after searching through the similar questions.
Here is the way MyActivity1 starts the view:
public class MyActivity1 extends Activity {
MyView1 View1;
public void Start_Button(View view)
{
Context ctx = getApplicationContext();
View1= new MyView1(ctx, null);
setContentView(View1);
}
}
and the part in MyView1 where I want to write something to close itself and bring MyActivity1 back:
public class MyView1 extends View {
static Context mycontext;
public MyView1(Context context, AttributeSet attrs) {
super(context, attrs);
mycontext=context;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.setVisibility(View.INVISIBLE);
((Activity) mycontext).setContentView(R.layout.activity_myactivity1);
// This gives runtime error
}
}
public public MyView1(Activity activity, AttributeSet attrs) {
super(context, attrs);
myActivity=activity;
}
public void Start_Button(View view)
{
View1= new MyView1(this, null);
setContentView(View1);
}
Just call myActivity.setContentView(R.layout.original_layout_for_MyActivity1);
I've got with my own layout.
I would like to receive the text from the editText after positive button on the layout is clicked. But in onPreferenceChange, I always get only the default value.
It seems that I need to bind my own EditText to the preferences somehow, but I don't know how and where to do this.
Can anybody help me?
To answer my own question:
First of all, in PreferenceScreen, you need to state:
<full.path.to.your.OwnLayoutClass
android:name="whatevever"
android:dialogLayout="#layout/your_own_layout" />
your_own_layout can be anything you'd like, linearlayout with of buttons, editTexts, according to your wishes.
Essential is the class representing your own preference Dialog. Here is a simple example of how to do it:
public class YourOwnLayoutClass extends DialogPreference {
private LinearLayout mView;
public YourOwnLayoutClass(Context context, AttributeSet attrs) {
super(context, attrs);
setPersistent(false);
setDialogLayoutResource(R.layout.your_own_layout);
}
#Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
mView = (LinearLayout) view;
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
// get value from editFields, do whatever you want here :)
// you can acces them through mView variable very easily
}
}
}
Important references:
I need to have a custom dialog in Preferences
Concise way of writing new DialogPreference classes?
Android: launch a custom Preference from a PreferenceActivity
How do I show some default suggestions for AutoCompleteTextView before the user type anything? I cannot find a way to do this even with creating a custom class that extends AutoCompleteTextView.
I want to show suggestions for common input values to save the user from typing.
Any suggestions?
You should subclass AutoCompleteTextView and override enoughToFilter() to return true all the time. After that you can call performFiltering("",0) (it's a protected function, so you can export this call via a public function in your class).
Something like that:
public class ContactsAutoCompleteTextView extends AutoCompleteTextView {
public ContactsAutoCompleteTextView(Context context) {
super(context);
}
public ContactsAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ContactsAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean enoughToFilter() {
return true;
}
public void temp() {
performFiltering("",0);
}
}
Itay Kahana's answer is indeed correct. The only thing I would add is that instead of creating a temp() function, to override the onFocusChanged function. Personally I used the following:
#Override
protected void onFocusChanged (boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
performFiltering("", 0);
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
If you dont need it to be dynamic I would go by having a string array in the resources, and then just load the array when the AutoCompleteTextView is about to be viewed. Like:
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
Which can be found on http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
Another way which I have done a couple of times which allows it to learn from the user is o use a database connection with IE a simple cursor. When you create the db you could insert some default values.
HereĀ“s an example with using simple cursor adapter: http://androidcommunity.com/forums/f4/how-to-use-autocompletetextview-with-simplecursoradapter-15875/
Edit 1:
One idea to show the list before the user starts type is to have a simple listview below the EditText. Not sure if you could call the autocompletetextview to show the suggestions, should be possible somehow. Perhaps you need to create your own autocompletetextiew class.
I have a View that I would like a TextView to pop up over when an event occurs. I have done this before (a long time ago) but can't remember how I did it...
My code is for the View element. Upon adding the TextView, I would like it to show over the top of the View.
public class test extends View {
public test(Context context) {
super(context);
setBackgroundColor(Color.RED);
}
TextView tv;
public void adText(TextView tv){
this.tv =tv;
tv.setVisibility(tv.VISIBLE);
}
}
http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/
Solves it. Someone delete that wierd comment please...