How to change text when data is not available on MPAndroidChart library? - android

I am using MPAndroidChart to draw some charts on my Android application and I would like to change the default message that appears when the data is not available.
I am using a CombinedChart and a BarChart and in none of them I am able to change the text when data is not available.
I know that there are few questions on Stackoverflow related with this theme. For example:
MPAndroidChart - Change message "No chart data available"
MPAndroidChart -Use multiple text instead of "No Chart Data available" depends on the data
but all of them make reference to one or more of these methods:
.setDescription("");
.setNoDataTextDescription("Custom message.");
.setNoDataTextDescription("Custom message");
.setNoDataText("Custom message");
Any of them worked for me.
My snippet of code in which I try to change the text is the following:
combinedChart.setDescription(null);
combinedChart.setNoDataText("No data");
combinedChart.setData(data);
combinedChart.animateXY(2500,2500);
How can I provide a different text message to the user when data is not available?
EDIT: I have added .invalidate method as #SudhakarRaju suggested but it also does not work. My actual code is:
combinedChart.setDescription(null);
combinedChart.setNoDataText("No data");
combinedChart.setNoDataTextDescription("No data");
combinedChart.setNoDataTextDescription("No data");
combinedChart.invalidate();
combinedChart.setData(data);
combinedChart.animateXY(2500,2500);
//I also tried to put combinedChart.invalidate(); here but it also does not work.
Thanks in advance!

The same way above you mentioned but you have to add one extra line. combinedChart.invalidate(); This will work.

This code allows you to style the chart if no data:
mChart.setNoDataText(getResources().getString(R.string.no_data_available));
mChart.setNoDataTextColor(BaseActivity.getAppColor(R.color.black));
// from: https://github.com/PhilJay/MPAndroidChart/issues/89
Paint p = mChart.getPaint(Chart.PAINT_INFO);
if (p != null) {
p.setTextSize(getResources().getInteger(R.integer.no_data_text_size));
}

Remove the combinedChart.setData(data) call.
For some reason, if you send an empty Data object that contains an empty data set, the "no data" text will not be displayed.
I had the same problem and I resolved it by simply not setting the data if it's empty, or using combinedChart.clear() for that matter.

Related

Android RecyclerView Custom Image Displaying

I have a recyclerview which displays data from an SQLite database.
I have a list of icons that I save in an ArrayList and apply color to them inside the ArrayList too.
Example: mMoodIcons.add(new MoodIcons(R.drawable.excited_icon, R.mipmap.background_clouds_excited, ContextCompat.getColor(mContext, R.color.excited), mContext.getString(R.string.excited)));
In the recycerview I have this code:
if(currentLog.getMoodBefore() == 3){
holder.moodIcon.setImageResource(R.drawable.confident_icon);
holder.moodIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.confident));
}
I need to make it dynamic so, for example, each mood has a number 1 = happy, 2 = fine, etc
So when the recycler view is displaying the information from the log it can display the correct mood icon with the color applied.
Could someone help me with the logic to write this code? I'm hitting a wall.
EDIT: The way I have it written above works in the if statement. But that would require 18 if statements. Is there a slicker way of writing this, maybe using an arraylist and a for loop?
I solved this myself. I created an arraylist of the mood icons and colors. Then I used this code:
for (MoodIconsWorkoutLog moodBefore : moodIcons) {
if(moodBefore.getMoodBefore() == currentLog.getMoodBefore() ){
holder.moodIcon.setImageResource(moodBefore.getMoodImage());
holder.moodIcon.setColorFilter(moodBefore.getColour());
}
}
Worked perfectly

Find multiple elements in web view with espresso

I'm testing a hybrid app, where each view has a web view.
In one of these web views I have a list of elements with the same attribute. They have the same xpath locator that is something like:
//h4[contains(#data-role, 'product-name')]
I want to create a list of these elements and iterate through them, count them, get their attributes.
In the documentation, I found two similar methods:
findElement(locator, value)
and
findMultipleElements(locator, value)
Though it's totally unclear to me how to use it. I tried to find examples on it but with no success.
Could someone help me with this?
Here is the solution that I have found.
#kaqqao is right that findMultipleItems call returns Atom<List<ElementReference>> that is not usable with onWebView() because there you have only withElement() that accepts either Atom<ElementReference> or just ElementReference
What you can do though is perform your action that find multiple items and just get results from your Atom. This is how it works internally if you check the source of doEval method inside Web.java for espresso.
val elements = with(AtomAction(findMultipleElements(
Locator.XPATH,
"YOUR_COMPLEX_XPATH"
), null, null)) {
onView(ViewMatchers.isAssignableFrom(WebView::class.java)).perform(this)
this.get()
}
This code will give you List<ElementMatcher>.
Then just run it as
elements.forEach {
onWebView().forceJavascriptEnabled().withElement(it).perform(webClick())
}
Can you try something like that? Since what you should care about is really the ElementReference and you can iterate the lsit returned from findMultipleElements with simple for/foreach statement:
yourList = findMultipleElements(locator, value);
yourList.size(); //this will get you the count of found elements with that locator
for(Atom<ElementReference> item : yourList ){
item.getAttribute...
//and whatever you want
}

setBackgroundColor method, android

Right now I'm building a simple form and I'm designing it so that if the user hasn't entered the necessary info before clicking the submit button the background turns red. And after if they have entered the correct info the form goes back to the way it was before.
// "if empty then set fields to red" checks
if (firstLastName.getText().toString().equals("")) {
firstLastName.setBackgroundColor(Color.RED);
}
else
firstLastName.setBackgroundColor(Color.WHITE);
}
The problem is that white apparently isn't what it was before, because it looks different. Is there a way to reset the form without deleting the info entered by the user?
If I'm not being clear please let me know and Ill try to elaborate.
How about setting and removing a color filter instead of changing the background color:
if (firstLastName.getText().toString().equals("")) {
// 0xFFFF0000 is red
firstLastName.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.DARKEN);}
else {
//Setting to null removes filter
firstLastName.getBackground().setColorFilter(null);
}

Displaying an array of objects, one at a time through a single dialog... instead of several dialogs

In my application I have a list of questions stored in an ArrayList, and I want to display a dialog that shows one question, and then continues to the next one after the question is answered. The way that I'm currently doing it (iterating through a loop) hasn't been working because it just layers all of the dialogs on top of one another all at once which causes a host of other issues. What I'm looking for is a way to still iterate through the questions, but just change the layout of the dialog each time until it has finished each question in the list. Can anyone give me a good pointer for how to get this going?
You can make a function that takes title and message as parameters and shows a dialog.
showDialog(String title, String message){ // Show dialog code here}
Within that dialog's answer button's listener call another function (showQuestion(currentQuestion)) that iterates the arrayList till it is over
int currentQuestion=0;
ArrayList<QuestionObject> questionList;
showQuestion(int i){
if(i<questionList.size()){
showDialog(questionList.get(i).getTitle,questionList.get(i).getMessage);
currentQuestion++;
}else{
//quiz is over
}
}
I assume you mean that you just want to change 1 single layout(created within XML i.e main.xml). In order to do this, make sure that the class your working on is pointing to that layout. From there (assuming your using an Event listener for when the user submits an answer) you can change do as you want by the following:
TextView txt = (TextView) findViewById(R.id.textView); // references the txt XML element
and in your Event listener, if the answer is correct then change(Have i be a global variable thats initially set to 0).
if(i<arrayList.size()){
txt.setText(arrayList.get(++i));
}else{
txt.setText("You Finished");
}
From there, in the else statement, you can change arrayLists and reset i to 0;
If you are trying to use the positive, neutral, and negative buttons; then you may have problems with multiple dialogs. Try defining a customized layout with your own TextViews, ListViews, and Buttons. You can implement listeners and everything else like a regular layout. Then just pass your customized layout to the dialog through AlertDialog.Builder.setView().
PS If you include code examples of what you are currently doing we can provided answers that are less vague.

how to add firstvalue by default null in spinner.?

I get the list data from webservices (Json) in spinner. But before websevices data in spinner i want show null value.(Need first value is null)
please any one help me..
please give me a sample code.
Thank You.
You could add a dummy object representing your null value. But spinners are not designed for this - they should contain some data (or nothing if no data is available).
So in your case I would recommend using a ListView or something similar instead which could be opened in a new activity with startActivityForResult and return an Intent.
Are you doing whole process with single thread?
you can use your listener, first you inflate empty array/list and later you can set your data change..
A null value inside a spinner will give you a really annoying exception. It's better to add as first element of the array some object like "Please, select one" or "Nothing selected". When you create the array you give to the spinner, first add this element and then load the real data.

Categories

Resources