Hiding focusability on start - android

In my sign in activity I added a method that set a border for the edit text if it is focusable so that the user can know that he is entering data in this field and the border will be highlighted...but when the activity start, the method directly is applied on the first username edit text knowing that I'm hiding the keyboard...I want this method to work when the keyboard appears not before.Thank you of helping me...This is my code
Java
public class FoodSafetyLogIn extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_safety_log_in);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.t1_layout);
//Hide Keyboard
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
EditText username = (EditText) findViewById(R.id.input_username);
focus(username);
EditText password = (EditText) findViewById(R.id.input_password);
focus(password);
password.setTypeface(Typeface.DEFAULT);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_back) {
Intent i = new Intent(FoodSafetyLogIn.this, MainActivity.class);
startActivity(i);
overridePendingTransition(R.animator.slide_in_right,R.animator.slide_in_right);
return true;
}
return super.onOptionsItemSelected(item);
}
public void OnClick(View view){
}
public void focus(View view){
view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
v.setBackgroundResource(R.drawable.focus_border_style);
else
v.setBackgroundResource(R.color.colorPrimary);
}
});
}
}

This is an example:
<LinearLayout
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
your edit text... />

Add this tag in your EditView
android:focusableInTouchMode="true"

Related

Menu ActionView stops expanding after setting expand listener

I want to set custom search layout in Toolbar Menu. My problem is when I setting expand listener to the search item - the action view is not expanding anymore.
I tryed to call
item.expandActionView();
MenuItemCompat.expandActionView(item);
in onOptionsItemSelected, but it doesn`t work.
My Code:
menu_projects.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:actionLayout="#layout/search_layout"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/search"
app:showAsAction="always|collapseActionView"/>
</menu>
search_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/closeSearchImageView"
android:layout_toStartOf="#+id/closeSearchImageView"
android:background="#null"
android:hint="#string/search"
android:imeOptions="flagNoExtractUi"
android:inputType="textCapSentences"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:singleLine="true"
android:textColor="#android:color/white"
android:textColorHint="#7dffffff"
android:textCursorDrawable="#drawable/cursor"/>
<ImageView
android:id="#+id/closeSearchImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="3dp"
android:src="#drawable/abc_ic_clear_mtrl_alpha"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_below="#+id/searchEditText"
android:background="#7dffffff"/>
</RelativeLayout>
inside fragment
#Override
public void onPrepareOptionsMenu(Menu menu) {
searchViewExpandCollapse = new SearchViewExpandCollapse(menu);//fragment field
super.onPrepareOptionsMenu(menu);
}
private class SearchViewExpandCollapse implements MenuItemCompat.OnActionExpandListener
{
private Menu menu;
public SearchViewExpandCollapse(Menu menu) {
this.menu = menu;
MenuItem searchItem = menu.findItem(R.id.action_search);
MenuItemCompat.setOnActionExpandListener(searchItem, this);//action view not expanding with this
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
}
Any ideas?
I found the solution. First of all I set ActionView to MenuItem programmaticaly after setting listener. And after that I changed return value on listener methods from false to true. Maybe will be helpfull for someone.
Full code:
#Override
public void onPrepareOptionsMenu(Menu menu) {
searchController = new SearchViewExpandCollapse(menu);
if (!query.isEmpty()) {
searchController.expandActionView();
searchController.setQuery(query);
}
super.onPrepareOptionsMenu(menu);
}
private class SearchViewExpandCollapse implements MenuItemCompat.OnActionExpandListener, OnClickListener
{
private final EditText editText;
private final MenuItem searchItem;
private Menu menu;
private View searchView;
public SearchViewExpandCollapse(Menu menu) {
this.menu = menu;
searchItem = menu.findItem(R.id.action_search);
MenuItemCompat.setOnActionExpandListener(searchItem, this);
searchItem.setActionView(R.layout.search_layout);
searchView = searchItem.getActionView();
editText = (EditText) searchView.findViewById(R.id.searchEditText);
editText.addTextChangedListener(textWatcher);
searchView.findViewById(R.id.closeSearchImageView).setOnClickListener(this);
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
menu.setGroupVisible(R.id.main_group, false);
editText.requestFocus();
editText.post(new Runnable()
{
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
menu.setGroupVisible(R.id.main_group, true);
editText.clearFocus();
InputMethodManager inputManager = (InputMethodManager) getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
return true;
}
private TextWatcher textWatcher = new TextWatcher()
{
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
query = s.toString();
searchAdapter.getFilter().filter(query);
}
};
public void expandActionView() {
searchItem.expandActionView();
}
public void setQuery(String query) {
editText.removeTextChangedListener(textWatcher);
editText.setText(query);
editText.setSelection(query.length());
editText.addTextChangedListener(textWatcher);
}
public void onClick(View v) {
if (editText.getText().length() != 0) {
editText.setText("");
} else {
searchItem.collapseActionView();
}
}
}
In case anyone else finds this later on having the same problem (like me) the issue is here:
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
by returning false you override the original action of the menu item. This should instead be:
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
//what you want to do
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//what you want to do
return true;
}

Error while pressing Close Button in Dialog android

I have created my dialog when I press one of the menu buttons:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_first__window, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id)
{
case R.id.action_settings:
About_us aboutus = new About_us(this);
aboutus.show();
return true;
case R.id.close: System.exit(0); return true;
}
return super.onOptionsItemSelected(item);
}
My Dialog layout
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Close"
android:onClick="ext_btn"/>
My Dialog Class
public class mydialog extends Dialog
{
public About_us(Context context) {
super(context);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.about_us);
}
public void ext_btn(View view) {
About_us.this.dismiss();
}
}
I Have tried a lot of codes to make the close button dismiss the dialog.
My app always crashes.
Where is the mistake ?
Try this one.
Button btn_ext = (Button) findViewById(R.id.btn_ext);
btn_ext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});

How to read from a spinner upon the click of a button android studio

so I have a working spinner that shows four options. Any one of these options needs to be chosen by the user and then they need to click a button at the bottom of the screen that will then alter one of four boolean variables, depending on which option was chosen.
Here's my code so far
public class CreateAlarm extends Activity { //declare variables
private Spinner difficultySpinner;
private Button createAlarm;
public static boolean easy = false; //set to true when selected by spinner.
public static boolean medium = false;
public static boolean hard = false;
public static boolean extreme = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_alarm);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_create_alarm, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void addListenerOnButton() {
difficultySpinner = (Spinner) findViewById(R.id.difficultySpinner);
createAlarm = (Button) findViewById(R.id.createAlarm);
createAlarm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
Could someone help me in telling me how I'd go about getting the value to be taken when the button is pressed? i'm not sure how to read from a spinner and everything I've looked at is just adding to my confusion.
thanks!
Add below code in your createAlarm 'onclick()' method:
String item=difficultySpinner.getSelectedItem().toString();

How can I click an Android Action Bar item under program control?

PeterH posted the following code:
//initiate the button
button.performClick();
button.setPressed(true);
button.invalidate();
// delay completion till animation completes
button.postDelayed(new Runnable() { //delay button
public void run() {
button.setPressed(false);
button.invalidate();
//any other associated action
}
}, 800); // .8secs delay time
Can the same type of operation be performed for Action Bar items?
Well, you can save MenuItem as a field and then call onOptionsItemSelected(savedMenuItem). But as ActionBar items are MenuItems and not Buttons (of course, if your action bar isn't customized with view like http://www.vogella.com/articles/AndroidActionBar/article.html#actionbar_dynamic). But if your ActionBar is customized with view and that view has a Button, that Button's behaviour can be customized as considered in your code snippet.
Example:
public class MainActivity extends Activity {
MenuItem item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("mytest");
actionBar.setTitle("TESTESTEST");
TextView tView = (TextView) findViewById(R.id.textView1);
tView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(item);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
this.item = item;
Toast.makeText(this, "settings", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
}

EditText in Actionbar only gets focus when SHOW_AS_ACTION_NEVER

I have a collapsed search EditView menu within my Actionbar (ActionbarSherlock). When setShowAsAction method is set to SHOW_AS_ACTION_NEVER, the keyboard displays and the user can start typing, as expected.
However when I set the value to SHOW_AS_ACTION_ALWAYS, the user has to touch the EditText before they start typing as it doesn't appear to have focus!
Is there something wrong with my code or a way to work around this problem?
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
final MenuItem search = menu.add(0, MENU_SEARCH, MENU_SEARCH, getString(R.string.search));
search.setIcon(R.drawable.ic_action_search);
search.setActionView(R.layout.action_search);
search.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_SEARCH:
mSearch = (EditText) item.getActionView();
mSearch.addTextChangedListener(mFilterTextWatcher);
mSearch.requestFocus();
mSearch.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}, 200);
break;
}
return true;
}
action_search.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="#string/search" />
The solution was to nest the fragment within the Activity.
public class ProductListActivity extends SherlockFragmentActivity {
…
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
ProductListFragment list = new ProductListFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class ProductListFragment extends SherlockListFragment {
…
}
}

Categories

Resources