How do i open an edit text popup on actionbar menu click - java

I am trying to display a popup edit text when "action_guess" is clicked. i am not sure on how to go about this. here is my code:
public MyMenuItemClickListener() {
}
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_guess:
Toast.makeText(mContext, "guess code", Toast.LENGTH_SHORT).show();
return true;
default:
}
return false;
}

If you want an EditText in a Dialog, you can follow this example.
https://www.mkyong.com/android/android-prompt-user-input-dialog-example/
If you want it appear in the ActionBar, you can overlay the EditText inside the ActionBar layout and show/hide with setVisibility().

Related

How can I use action item as a button in android programming?

I have a menu upside my page and it has an action item. like this:
action item
I set app:showAsAction="always" for it.
I have invisible view (in UI) in my page too.
I want to if I click on it, my invisible view appears. so I set a boolean parameter for that.
My boolean set true but my object doesn't appear at all. How can I fix it?
here is my code:
public static boolean myBoolean=false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this if doesn't check after change parameter
if(myBoolean)
findViewById(R.id.checkBox).setVisibility(View.VISIBLE);
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_setting: {
myBoolean=true;
return true;
}}}
What I am getting from this is you want to show a view based on the click of some menu item in action bar right?. if this is true then do like this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_setting:
if(myBoolean){
findViewById(R.id.checkBox).setVisibility(View.GONE);
myBoolean = false;
}else{
findViewById(R.id.checkBox).setVisibility(View.VISIBLE);
myBoolean = true;
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you can't decide the visibility earlier in your code since click may happen sometime after onCreate() only.

How to hide default hamburger icon of Navigation view android

I need to hide the hamburger icon
This is my toolbar
I need to hide the default hamburger icon of navigation bar and load it from another button click.The navigation bar need to appear on the attachment icon click in my toobar and need to disappear when i click outside(anywhere).Can this be done ?
if you are using ActionBarDrawerToggle then you can add a line:
toggle.setDrawerIndicatorEnabled(false);
and opening and closing drawer you can write in your click event:
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
You can hide the hamburger icon by doing this:
toolbar.setNavigationIcon(null); // to hide Navigation icon
toolbar.setDisplayHomeAsUpEnabled(false); // to hide back button
If you have added the attachment icon manually (As an imageView inside a Toolbar) :
boolean isDrawerOpen = false;
imageViewAttachment..setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!isDrawerOpen) {
mDrawerLayout.openDrawer(Gravity.LEFT);
isDrawerOpen = true;
}
else {
drawerLayout.closeDrawer(Gravity.END);
isDrawerOpen = false;
}
}
});
Or, if you've added as a Menu item :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.attachment:
if(!isDrawerOpen) {
mDrawerLayout.openDrawer(Gravity.LEFT);
isDrawerOpen = true;
}
else {
drawerLayout.closeDrawer(Gravity.END);
isDrawerOpen = false;
}
return true;
}
return super.onOptionsItemSelected(item);
}

How to check if drop down menu of ActionBar is active

I'm using the ActionBar extending from the AppCompatActivity. How can I check, if the dropdown menu of the ActionBar is opened at the moment.
I've tried it in this method. But it does not fire if I open the drop down menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
Log.i("ActionBar", "ActionBar dropdown is open at this moment");
switch (item.getItemId()) {
case R.id.preferences:
intent.setClass(StartupActivity.this, PreferencesActivity.class);
startActivityForResult(intent, 0);
return true;
case R.id.info:
intent.setClass(StartupActivity.this, InformationActivity.class);
startActivityForResult(intent, 0);
return true;
case R.id.contact:
intent.setClass(StartupActivity.this, ContactActivity.class);
startActivityForResult(intent, 0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It only fires, if I click on an item of the drop down menu. But I want to check, if the user clicks on the three-dot menu.
Try the following method:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
// menu is open
return super.onMenuOpened(featureId, menu);
}
I've tried this method, it works:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// menu open
return super.onPrepareOptionsMenu(menu);
}
Even when initializing the menu, and not only when you click, but better than nothing.

How to remove text formatting when pasting from clipboard on Android

Right now I am working on an Android application that requires allowing the user to cut copy and paste onto an editText fields. But when I copy a formatted string from other places (i.e. a string that is underlined) and paste it on to the editText field, it shows it as a formatted version. How do I remove this?
I have tried to add a textwatcher by adding addTextChangedListener, and in the after text change I just do edittext.setText(s.toString()+"") but this creates an infinite loop. :(
Please help! Thanks in advance.
Edit----
I have made some progress by setting setCustomSelectionActionModeCallback
editDestination_.setCustomSelectionActionModeCallback(new Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {return true;}
#Override
public void onDestroyActionMode(ActionMode mode) {}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.removeItem(android.R.id.paste);
menu.removeItem(android.R.id.selectAll);
menu.add(0, CUSTOM_PASTE, 0, "Paste").setIcon(R.drawable.paste_ic);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case CUSTOM_PASTE:
edittext.setText(readFromClipboard(aContext_));
// Finish and close the ActionMode
mode.finish();
return true;
default:
break;
}
return false;
}
});
This is working pretty well until I realize there are two types of cut/copy/and paste on my phone. One is when the edit text is empty and I long click on the field. This bring up a popup menu. The other one is when there is text in the field, and when I long click, this bring up a cut/copy/and paste bar below my action bar. My code from above is only affecting the bar-below-action bar one. :(
PROBLEM :
The thing you are doing is creating an infinite loop because everytime you call setText(), again afterTextChanged() gets called (because you are changing the text inside the EditText.
SOLUTION :
Suppose the EditText is editTextToClearStyle
EditText editTextToClearStyle = (EditText)findViewById(R.id.youredittextname);
editTextToClearStyle.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
editTextToClearStyle.setTextAppearance(getApplicationContext(),R.style.normalText);
}
I have solved it.
I set a onLongClickListener to catch it before the popup-cut/copy/paste menu shows up.
CharSequence actions[] = new CharSequence[] {"paste"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setItems(actions, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
edittext.setText(readFromClipboard(aContext_));
}
});
builder.show();
Then if there is a string already present in the EditText, I return false to allow normal android system below-action-bar-cut/copy/paste function to work.

how to override action bar back button in android?

I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#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();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...

Categories