Not getting text from EditText on ActionBar button click - android

I have used ActionBar in my app. I want to get value from an EditText i.e on other Layout. I have tried to get that layout and then getting the EditText value.
Code to get the layout in which EditText is
public View textInputChecks() {
LayoutInflater mLayoutInflater = getLayoutInflater();
View textQrView = mLayoutInflater.inflate(R.layout.text_qr, null);
return textQrView;
}
Code for ActionBar button click
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO This is really needed to make app icon a toggle of nav drawer.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else {
switch (item.getItemId()) {
case R.id.action_ok:
View textInputView = textInputChecks();
EditText textInput = (EditText) textInputView.findViewById(R.id.qrTextInput);
String text = textInput.getText().toString();
if (text.length()>0) {
Toast.makeText(getApplicationContext(), "Ok",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "No Text added",
Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
return super.onOptionsItemSelected(item);
}
It always shows "No Text Added" Toast message even if you add text

Related

Cannot Select Item in Navigation Drawer

I created a navigation bar in android and I am unable to select an item. I want to display a toast when a specific item is selected but it doesn't enter into the if condition of checking id of the item clicked.
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (abdt.onOptionsItemSelected(item)) {
if (item.getItemId() == R.id.addCat) {
Toast.makeText(
getApplicationContext(),
"Category Name Field is Empty!",
Toast.LENGTH_LONG
).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
In Navigation Drawer to implement item selection, you need to implement the interface of NavigationView.OnNavigationItemSelectedListener and
navigationView.setNavigationItemSelectedListener(this);
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.addCat:
Toast.makeText(getApplicationContext(), "Category Name Field is Empty!", Toast.LENGTH_LONG).show();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}

How to write functionality for back arrow which appears when search is opened in action bar in android?

i have a collapseActionView search in actionbar..A back arrow appears in the place of home button in actionbar when we press on search button...on focusing edittext soft keyboard appears...i have a problem when we click on the back arrow without submitting the search value the soft keyboard doesnot hide itself...how to write functionality for the backarrow which appears in the actionbar when the search view is open?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
inflater.inflate(R.menu.main_activity_bar_l, menu);
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
inflater.inflate(R.menu.main_activity_bar, menu);
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
inflater.inflate(R.menu.main_activity_bar, menu);
} else {
inflater.inflate(R.menu.main_activity_bar, menu);
}
View v = (View) menu.findItem(R.id.my_action).getActionView();
/** Get the edit text from the action view */
txtSearch = (EditText) v.findViewById(R.id.myActionEditText);
/** Setting an action listener */
ImageView Search = (ImageView) v.findViewById(R.id.search);
Search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
search_value = txtSearch.getText().toString();
if (txtSearch.getText().toString().equals("")) {
Toast.makeText(getBaseContext(),
"Please enter search values", Toast.LENGTH_SHORT)
.show();
} else {
Intent intent = new Intent(MainActivity.this,
Display.class);
intent.putExtra("Action_search", txtSearch.getText()
.toString());
startActivity(intent);
txtSearch.setText("");
}
}
});
return super.onCreateOptionsMenu(menu);
}
i wrote functionality for home button but it doesnot work
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
//
//
//
case android.R.id.home:
Toast.makeText(MainActivity.this, "home", Toast.LENGTH_LONG).show();
// count the active fragment
// if(getSupportFragmentManager().getStackBackEntryCount() > 0) {
// // hide soft method as above
// InputMethodManager mImm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
// mImm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
// // do the pop backstack
// getSupportFragmentManager().popBackStack();
// } else {
// // some stuff like finish the activity
}
return super.onOptionsItemSelected(item);
}
Just write this code in onPause:
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}

Popup Menu won't change its selected state

I have this code here, I created a popup menu when the user long-presses the "edit_text" view's area which displays a popup menu with "Red" "Yellow" radio button option which changes the background color of the "text_view", but I'm not sure why when I select the other option, like when red is currently selected, I select yellow and the other way around(I have the red option selected as the default state), the selected state does not change at all, red is still selected no matter how many times I press yellow. Could you guys help me with this please? Thank you very much.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_text = (EditText)findViewById(R.id.edit_text_1);
text_view = (TextView)findViewById(R.id.textView1);
//==========_CREATE A POPUP MENU WHEN LONG-CLICK ON EDITTEXT AREA_==========\\
edit_text.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
final PopupMenu pop_up = new PopupMenu(getContext(), v);
getMenuInflater().inflate(R.menu.main, pop_up.getMenu());
//GROUP'S ID IS "group".
pop_up.getMenu().setGroupCheckable(R.id.group, true, true);
pop_up.show();
pop_up.setOnMenuItemClickListener(listener);
return true;
}
});
}
OnMenuItemClickListener listener = new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case R.id.red:
text_view.setBackgroundColor(Color.RED);
if (!item.isChecked()) {
item.setChecked(true);
}
return true;
case R.id.yellow:
text_view.setBackgroundColor(Color.YELLOW);
if (!item.isChecked()) {
item.setChecked(true);
}
return true;
default:
return false;
}
}
};
protected Context getContext() {
return this;
}
Looks like there's some logical issue with your code. Based on the documentation:
When a checkable item is selected, the system calls your respective
item-selected callback method (such as onOptionsItemSelected()). It is
here that you must set the state of the checkbox, because a checkbox
or radio button does not change its state automatically.
It's easy to observe that yours onLongClick(View v) get called every time and so the menu gets created every long click again (with initial items states). To fix the issue you can store checked / unchecked state (or current color) some there and set item states properly every time in onLongClick(View v). Like the following:
public class MainActivity extends ActionBarActivity {
private static final String TAG = "MainActivity";
private TextView mText;
// For persistent storage SharedPreferences should be used instead of local variable
private int mColor = Color.RED;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edit = (EditText) findViewById(R.id.edit_text_1);
mText = (TextView)findViewById(R.id.textView1);
mText.setBackgroundColor(mColor);
edit.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Log.d(TAG, "onLongClick");
final PopupMenu pop_up = new PopupMenu(MainActivity.this, v);
final Menu menu = pop_up.getMenu();
getMenuInflater().inflate(R.menu.popup_menu, menu);
//GROUP'S ID IS "group".
menu.setGroupCheckable(R.id.group, true, true);
pop_up.show();
switch(mColor) {
case Color.RED:
menu.findItem(R.id.yellow).setChecked(false);
menu.findItem(R.id.red).setChecked(true);
break;
case Color.YELLOW:
menu.findItem(R.id.red).setChecked(false);
menu.findItem(R.id.yellow).setChecked(true);
break;
default:
break;
}
pop_up.setOnMenuItemClickListener(mMenuItemClickListener);
return true;
}
});
}
final OnMenuItemClickListener mMenuItemClickListener = new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case R.id.red:
mText.setBackgroundColor(Color.RED);
mColor = Color.RED;
return true;
case R.id.yellow:
mText.setBackgroundColor(Color.YELLOW);
mColor = Color.YELLOW;
return true;
default:
return false;
}
}
};
Also, I'd suggest to checkout ContextMenu which provides slightly more convenient way for creating long-click context menus. Please note that documentation of ContextMenu from onCreateContextMenu() clearly states that menu in proper state should be populated by the app every time:
Called when a context menu for the view is about to be shown. Unlike
onCreateOptionsMenu(Menu), this will be called every time the context
menu is about to be shown and should be populated for the view (or
item inside the view for AdapterView subclasses, this can be found in
the menuInfo)).

drop down menu on image button on action bar

what i have is an action bar .. and i have only logo in it and an image button and i want when i press on the button a spinner with a three values will appear and each one of them open a new activity , here is my code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.app.ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText("My Own Title");
ImageButton imageButton = (ImageButton) mCustomView
.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// i want the spinner to be add here
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
#Override
public boolean onNavigationItemSelected(int arg0, long arg1) {
// TODO Auto-generated method stub
return false;
}
ublic class MainActivity extends Activity{
...
...
/**
* On selecting action bar icons
* */
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
case R.id.action_location_found:
// location found
LocationFound();
return true;
case R.id.action_refresh:
// refresh
return true;
case R.id.action_help:
// help action
return true;
case R.id.action_check_updates:
// check for updates action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Launching new activity
* */
private void LocationFound() {
Intent i = new Intent(MainActivity.this, LocationFound.class);
startActivity(i);
}
}
Try This code :
For better understanding go through following link
http://www.androidhive.info/2013/11/android-working-with-action-bar/

How can I change some text in an ActionBar item after an onOptionItemSelected event?

I'm using ActionBar in my application, and I want when the user clicks a button, the item in the ActionBar should change the text.
This is my code for onOptionsItemSelected():
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
.show();
finish();
break;
case R.id.lg:
Toast.makeText(getBaseContext(), "ma", Toast.LENGTH_SHORT).show();
break;
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
break;
case R.id.nerlandais:
Toast.makeText(getBaseContext(), "brahim", Toast.LENGTH_SHORT)
.show();
}
return true;
}
What must I do to change the item title from another item?
Example: When I click in French item I want to change nerlandais item title.
If you want to change an MenuItem's title clicking to another item you can do something like this :
private String mMenuItemTitle;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.nerlandais);
item.setText(mMenuItemTitle);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
mMenuItemTitle = "My New Title";
supportInvalidateOptionsMenu();
break;
}
return true;
}
Try item.setTitle("new title")
Or, if from another item then the Menu has findItem

Categories

Resources