Adding features to Clipboard ActionBar - android

In my app, I want to be able to start my own function when user copy a text to the clipboard.
I'm overriding function onActionModeFinished and as I want to be sure user tapped on Copy I'm getting selected navigation index. Unfortunately if always return -1. Is there something I'm doing wrong here ?
#Override
public void onActionModeFinished(ActionMode mode) {
super.onActionModeFinished(mode);
if (_actionBar != null) {
int index = _actionBar.getSelectedNavigationIndex();
// here index is always -1
}
}

ActionBar can contain tabs and list selector on the left side. So, getSelectedNavigationIndex is used to get the position in this navigation items not in the menu items located in the right side.

Related

Search view shows no record found after entering 3 characters not before

When I search something it updates recycler view accordingly and onQueryTextChange method I am showing layout No items found if it doesn't matches, it is showing No items found when I enter 3rd character and if first two character doesn't match then it shows blank screen.
I want it to show No items found even if first two characters doesn't match.
#Override
public boolean onQueryTextChange(String query) {
newsListAdapter.getFilter().filter(query);
if (newsListAdapter.getItemCount() < 1) {
listRecyclerView.setVisibility(View.GONE);
noRecord.setVisibility(View.VISIBLE);
} else {
listRecyclerView.setVisibility(View.VISIBLE);
noRecord.setVisibility(View.GONE);
}
return false;
}
if your are using search view check this link Android search list while typing,
if you use autocompletetextview you can set the throushouldvalue
For AutoCompleteTextView use this:
AutoCompleteTextView searchAutoCompleteTextView = (AutoCompleteTextView) mSearchView.findViewById(getResources().getIdentifier("search_src_text", "id", getPackageName()));
searchAutoCompleteTextView.setThreshold(1);
The threshold value defines, how much characters you need to type in, until the first proposal can be shown (listeners get called).

MPAndroidChart grouped barchart onclick

I've successfully used grouped barchart. I've got two bars per result (one red and one blue bar) but I haven't been able to retrieve X value when I click the blue one, only get the X value when I click the red bar.
LINK TO IMAGE
When I click either bar, the setOnChartValueSelectedListener method is called, but only when I click the red bar I get the XAxis value with this code:
barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, Highlight h) {
if (null != barChart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), barChart.getXAxis()) && !barChart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), barChart.getXAxis()).isEmpty()) {
ContentManager.getInstance().setRegion(barChart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), barChart.getXAxis()));
}
}
#Override
public void onNothingSelected() {
}
});
how can I get the value when I click the blue bar?
barChart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), barChart.getXAxis())
that line returns the value clicking the red bar
FIXED.
Just use Math.floor to use a rounded value on a bar click.

Android bar chart: undo selection

There's some way to undo a selection on Mikephil charting? I have an application which opens an activity when I selects one value in a bar chart. That works fine, however, when I returns to activity that contains the chart, the selection remains. So, when I selects again, the selection is cleared and the activity does not open. What I want is ever I select a value on bar chart the function "onValueSelected" be executed. How can I do that?
That is the code fragment which calls assyncronously an activity when a value is selected.
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
if(e.getVal() == 0);
else {
GetClientesCadastradosDiaAsync task = new GetClientesCadastradosDiaAsync();
task.execute();
}
}
#Override
public void onNothingSelected() {
// do nothing
}
});
At the end of your onValueSelected() method, call:
chart.highlightValues(null);
Now, this will only remove the highlighting. If you select that same bar again, onNothingSelected() will be called.
Therefore, in onNothingSelected(), again call onValueSelected(). You will have to pass the parameters here, but looks like you will only need the Entry parameter, and for the other 2 you can pass null.

RecyclerView keeps repeating cached (?) subview and not looking to onBindViewHolder

I have RecyclerView where every list item has an ImageButton, thee image of which I define in the adapter's onBindViewHolder():
int myVote = getMyVote();
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
}
So ratingButton is a star in the right bottom corner of the list item layout. Its shape is filled with gray color (and accordingly, a log record is pushed) if the condition (myVote != 0) is satisfied.
The problem is that when I scroll the list down I can watch other stars became filled even though I can see the only one record in the log window (for the correct list item). Moreover, this list items with incorrectly changed buttons repeat every 5 rows, and that's what's confusing me. If I changemListView.setItemViewCacheSize(0); the repeat period changes to 3, so we can assume it somehow connected with the RecyclerView's caching and recycling mechanism.
Please, help me to work the problem out. Thanks!
Don't forget to implement
public long getItemId(int position) {}
otherwise, you'll see repeated items in RecyclerView.
Try to change your code to:
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
} else {
holder.ratingButton.setImageResource(int another resource);
}
}
You may also have to write else part of main condition with some another resource like:
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
} else {
holder.ratingButton.setImageResource(int another_resource);
}
It is worked for me.

How to get the "up" button used on the Toolbar?

This is a short question:
I'm trying to force the action bar (used by a Toolbar) to use LTR alignment. I've succeeded making the layout itself use LTR, but not the "up" button (as I've done here, before Toolbar was introduced) .
It seems this view doesn't have an ID, and I think using getChildAt() is too risky.
Can anyone help?
The answer
Here's one way I've found to solve this, based on this answer .
I made it so that it is guarranteed to find only the "up" button, and whatever it does, it will revert back to the previous state it was before.
Here's the code:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
public boolean onCreateOptionsMenu(final Menu menu)
{
// <= do the normal stuff of action bar menu preparetions
if(VERSION.SDK_INT>=VERSION_CODES.JELLY_BEAN_MR1&&getResources().getConfiguration().getLayoutDirection()==View.LAYOUT_DIRECTION_RTL)
{
final ArrayList<View> outViews=new ArrayList<>();
final CharSequence previousDesc=_toolbar.getNavigationContentDescription();
for(int id=0;;++id)
{
final String uniqueContentDescription=Integer.toString(id);
_toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if(!outViews.isEmpty())
continue;
_toolbar.setNavigationContentDescription(uniqueContentDescription);
_toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if (outViews.isEmpty())
if (BuildConfig.DEBUG)
throw new RuntimeException(
"You should call this function only when the toolbar already has views");
else
break;
outViews.get(0).setRotation(180f);
break;
}
_toolbar.setNavigationContentDescription(previousDesc);
}
//
return super.onCreateOptionsMenu(menu);
}
It seems this view doesn't have an ID
You're right, the navigation view is created programmatically and never sets an id. But you can still find it by using View.findViewsWithText.
View.findViewsWithText comes with two flags:
View.FIND_VIEWS_WITH_TEXT
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION
The navigation view's default content description is "Navigate up" or the resource id is abc_action_bar_up_description for AppCompat and action_bar_up_description for the framework's, but you can easily apply your own using Toolbar.setNavigationContentDescription.
Here's an example implementation:
final Toolbar toolbar = ...;
toolbar.setNavigationContentDescription("up");
setActionBar(toolbar);
final ArrayList<View> outViews = Lists.newArrayList();
toolbar.findViewsWithText(outViews, "up", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
outViews.get(0).setRotation(180f);
Results

Categories

Resources