My Toast doesn't appear - android

I don't know why but the toast doesn't appear when I run the program. This is my code:
class Number {
int number;
public boolean isSquare() {
double squareRoot = Math.sqrt(number);
if (squareRoot==Math.floor(squareRoot)) {
return true;
} else {
return false;
}
}
public boolean isTriangular() {
int x = 1;
int triangularNumber = 1;
while(triangularNumber<number) {
x++;
triangularNumber = triangularNumber + x;
}
if (triangularNumber == number) {
return true;
} else {
return false;
}
}
}
public void idButton (View view) {
EditText inputNumber = (EditText) findViewById(R.id.inputNumber);
Number myNumber = new Number();
myNumber.number = Integer.parseInt(inputNumber.getText().toString());
String message = "";
if (myNumber.isSquare()){
if (myNumber.isTriangular()){
message = myNumber.number + " your number is triangular and square";
}
}
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
After compiling there's no error in the code, please guide me what should be improved on the code since I'm still beginner. Could you help me? Thanks.

String message = "";
if (myNumber.isSquare()){
if (myNumber.isTriangular()){
message = myNumber.number + " your number is triangular and square";
}
}
In this code, you haven't write any else statement. What if the number is neither square nor triangular? In both of these scenerios you will get an empty message as you have declared message = "";
So write your code as
String message = "";
if (myNumber.isSquare()){
if (myNumber.isTriangular()){
message = message+myNumber.number + " your number is triangular and square";
}
else
{
message = message+"No triangular";
}
}
else
{
message = message+"No square";
}

Related

Is there a way to make WebView identify an item where user selected text?

I have developed an app, sort of an text reader, and need to introduce a new functionality to it (users are asking for it- I've intended to incorporate it all along, just that I never figured out how to do it). I'm generating an html from the text and displaying it in a webview. I want the user to be able to select a section of the text and then open the context menu. What I want to identify is on which section he has longClicked on (each line of the html consists of pre-formatted line/section number, the line text and may be finished with a href link), and which part of it was selected. Once I figure the first part out, the second is just a matter of sorting it out.
I've tried using <span id='someuniquenumber'> for the line text, but webView does not recognize it as an anchor*. I've also, unsuccessfully, tried to enable text selection on links (which IMHO is not a desirable option, but will resort to it, if there are no other options left).
Here's an example of the text selection problem- I want to be able to identify the selection as "line 5, words 4 through 10", or at least that the user has selected the text from the line marked as 5.
Any help would be greatly appreciated- just bear in mind that I can affect both the webView behaviour and the HTML code displayed. Thanks in advance. :)
event handler returns get extra=0 in type='null'
If I use tags, I do not know the way to allow text selection within that link, whereas without it I cannot identify the element user has clicked on (and the selected text may certainly not be unique, and therefore may not be searched within HTML).
EDIT
Here's the code I have regarding webView (onCreate in MainActivity):
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
int temp = url.lastIndexOf("/") + 1;
String link = url.substring(temp);
if (link.charAt(0) == 'L') {
link = link.substring(1);
LinksFragment mLinksFragment = LinksFragment.newInstance(Integer.valueOf(link),textZoom,zoomAll,sans,fragNo);
mLinksFragment.show(fm,"fragment_links");
} else if (link.charAt(0) == 'C') {
// show or add comment
} else {
// follow link
// append link to clipboard
ClipData tmp = myCB.getPrimaryClip();
if (!myCB.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
tmp = ClipData.newPlainText("text","");
myCB.setPrimaryClip(tmp);
}
ClipData.Item tmpI = tmp.getItemAt(0);
String ts = tmpI.getText().toString();
ts += link + " ";
tmp = ClipData.newPlainText("text",ts);
myCB.setPrimaryClip(tmp);
}
return true;
}
});
// detect clicked element
mWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
WebView.HitTestResult hr = ((WebView)view).getHitTestResult();
Toast.makeText(MainActivity.this, "getExtra = "+ hr.getExtra() + "Type= " + hr.getType(),
Toast.LENGTH_LONG).show();
//return true;
return false;
}
});
mWebView.getSettings().setDefaultFontSize(14);
mWebView.setBackgroundColor(0x00000000);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
updateFields(); // applies custom fontface and fontsize to elements incl. mWebView
mWebView.loadDataWithBaseURL("file:///android_asset/",wV,"text/html; charset=utf-8","utf-8",null);
And here's the code for generating html inserted into the webView (a separate function fetching text, adding headings and chapter titles, formatting the text)-the actual text is stored in tmpStr variable:
public String fetchText(Bible mB, boolean append, boolean filter, String wV) {
BibleInfo.Error mE;
String tN;
char tT;
int[] Lines;
String[] Lttrs;
int[] noVerses = new int[3];
String fS1 = "<p class='ps'>";
String Psalm = getResources().getString(R.string.Psalm);
String Chapter = getResources().getString(R.string.Chapter);
boolean estExc = false;
char lttr = 'a'-1;
mE = mB.mBI.mE;
int tmp = 0;
for (int i=0; i<3; i++) {
tmp += mB.mBI.noBooks(i);
noVerses[i] = mB.mBI.getLine(tmp,1,1);
}
// separating link data
int cnt = 0;
Lines = new int[mB.mLink.getLines().length];
Lttrs = new String[mB.mLink.getLines().length];
for (String tS:mB.mLink.getLines()) {
tN = "";
Lttrs[cnt] = "";
for (tmp=0; tmp<tS.length(); tmp++) {
tT = tS.charAt(tmp);
if (tT>='0' && tT<='9') {
tN += tT;
} else {
Lttrs[cnt] += tT;
}
}
Lines[cnt++] = Integer.valueOf(tN);
}
if (!append) {
wV = "";
}
noVerses = mB.mBI.getTriLink(Lines[0],mE);
if ((noVerses[1] == 0 || (noVerses[1] == 1 && noVerses[0] != mB.mBI.getSirach())) && mB.mLink.isLong()) {
if (mB.mBI.getTitles() != null) {
wV += "<h1>" + mB.mBI.getTitles()[noVerses[0]] + "</h1>";
}
}
for (int i=0; i<Lines.length; i++) {
int tX = Lines[i];
int[] temp = mB.mBI.getTriLink(tX, mE);
if (temp[2] == 1 && mB.mLink.isLong() && temp[1] != 0) {
if (temp[0] == mB.mBI.getPsalms()) {
wV += "<h2>" + Psalm + " " + temp[1] + "</h2>";
} else {
wV += "<h2>" + Chapter + " " + temp[1] + "</h2>";
}
}
String tmpStr = mB.getLineText(tX - 1,filter);
if (noVerses[0] == mB.mBI.getPsalms()) {
wV += fS1;
if (Lttrs[i] != "") {
tmpStr = parseVerse(tmpStr,Lttrs[i]);
}
tmpStr = tmpStr.replace(mB.mBI.mSeparator, "<br>");
} else {
wV += "<p>";
if (noVerses[0] == mB.mBI.getEsther()) {
int noBrks = 0;
int lastOccurrence = 0;
if ((temp[1]==1) & (temp[2]==1)) {
estExc = true;
while (lastOccurrence != -1){
lastOccurrence = tmpStr.indexOf(mB.mBI.mSeparator,lastOccurrence);
String tStr = "<br><sup>" + temp[2] + (char)(98+noBrks) + "</sup> ";
if (lastOccurrence != -1) {
if (noBrks == 0) {
tmpStr = "<span id='apoch'>" + tmpStr;
}
tmpStr = tmpStr.replaceFirst("\\|", tStr);
noBrks +=1;
}
}
if (tmpStr.lastIndexOf(mB.mBI.mSeparator) != -1) {
tmpStr += "</span>";
}
// remove character before last </sup>
} else {
estExc = false;
while (lastOccurrence != -1){
lastOccurrence = tmpStr.indexOf(mB.mBI.mSeparator,lastOccurrence);
String tStr = "<br><sup>" + temp[2] + (char)(97+noBrks) + "</sup> ";
if (lastOccurrence != -1) {
if (noBrks == 0) {
tmpStr = "<span id='apoch'>" + tmpStr;
}
tmpStr = tmpStr.replaceFirst("\\|", tStr);
noBrks +=1;
}
}
if (noBrks != 0) {
tmpStr += "</span>";
}
tmpStr = tmpStr.replaceFirst("<span id='apoch'>","");
tmpStr = tmpStr.replaceFirst("<br>","<br><span id='apoch'>");
}
}
}
// add hyperlink for links
if (!TextUtils.isEmpty(mB.getLinks()[tX-1])) {
lttr++;
if (lttr>'z') { lttr = 'a'; }
String ts= "<sup><span id='links'><a href='L" + (tX-1) + "'>" + lttr;
ts += "</a></span></sup>";
tmpStr += ts;
}
if (estExc) {
wV += "<sup>" + temp[2] + "a</sup> " + tmpStr + "</p>";
} else {
wV += "<sup>" + temp[2] + "</sup> " + tmpStr + "</p>";
}
}
wV = fS2 + wV;
return wV;
}
Calls to log events or set user properties fired from within a WebView must be forwarded to native code before they can be sent to Google Analytics for Firebase.
Implement JavaScript handler
The first step in using Google Analytics for Firebase in a WebView is to create JavaScript functions to forward events and user properties to native code. The following example shows how to do this in a way that is compatible with both Android and iOS native code
function logEvent(name, params) {
if (!name) {
return;
}
if (window.AnalyticsWebInterface) {
// Call Android interface
window.AnalyticsWebInterface.logEvent(name, JSON.stringify(params));
} else if (window.webkit
&& window.webkit.messageHandlers
&& window.webkit.messageHandlers.firebase) {
// Call iOS interface
var message = {
command: 'logEvent',
name: name,
parameters: params
};
window.webkit.messageHandlers.firebase.postMessage(message);
} else {
// No Android or iOS interface found
console.log("No native APIs found.");
}
}
function setUserProperty(name, value) {
if (!name || !value) {
return;
}
if (window.AnalyticsWebInterface) {
// Call Android interface
window.AnalyticsWebInterface.setUserProperty(name, value);
} else if (window.webkit
&& window.webkit.messageHandlers
&& window.webkit.messageHandlers.firebase) {
// Call iOS interface
var message = {
command: 'setUserProperty',
name: name,
value: value
};
window.webkit.messageHandlers.firebase.postMessage(message);
} else {
// No Android or iOS interface found
console.log("No native APIs found.");
}
}
Implement native interface
public class AnalyticsWebInterface {
public static final String TAG = "AnalyticsWebInterface";
private FirebaseAnalytics mAnalytics;
public AnalyticsWebInterface(Context context) {
mAnalytics = FirebaseAnalytics.getInstance(context);
}
#JavascriptInterface
public void logEvent(String name, String jsonParams) {
LOGD("logEvent:" + name);
mAnalytics.logEvent(name, bundleFromJson(jsonParams));
}
#JavascriptInterface
public void setUserProperty(String name, String value) {
LOGD("setUserProperty:" + name);
mAnalytics.setUserProperty(name, value);
}
private void LOGD(String message) {
// Only log on debug builds, for privacy
if (BuildConfig.DEBUG) {
Log.d(TAG, message);
}
}
private Bundle bundleFromJson(String json) {
// ...
}
}
Once you have created the native interface, register it with your WebView so that it is visible to Javascript code running in the WebView:
// Only add the JavaScriptInterface on API version JELLY_BEAN_MR1 and above, due to
// security concerns, see link below for more information:
// https://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object,%20java.lang.String)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mWebView.addJavascriptInterface(
new AnalyticsWebInterface(this), AnalyticsWebInterface.TAG);
} else {
Log.w(TAG, "Not adding JavaScriptInterface, API Version: " + Build.VERSION.SDK_INT);
}
Source: Firebase

How do you cloud save each level data. for unity Game Like Candy crush game

I am developing a puzzle game for Android using unity. So I want to save the score, time and stars earned for each level in the google play Cloud. But I am able to save it for one single level if I try to save it for more then 1 level. it overwrites the previous data. So how do I overcome this problem?
public class CloudSave : MonoBehaviour {
[SerializeField]
private Text message;
private int totalCoin = 20;
public int levelNumber;
public int starEarnedPerLevel;
#region Cloud_Save
private string GetSaveString() {
string data = "";
data += PlayerPrefs.GetInt("HighScore").ToString();
data += "|";
data += totalCoin.ToString();
data += "|";
data += levelNumber.ToString();
data += "|";
data += starEarnedPerLevel.ToString();
return data;
}
private void LoadSaveString(string save) {
string[] data = save.Split('|');
PlayerPrefs.SetInt("HighScore", int.Parse(data[0]));
totalCoin = int.Parse(data[1]);
levelNumber = int.Parse(data[2]);
starEarnedPerLevel = int.Parse(data[3]);
Debug.Log("LoadSaveString Function");
}
private bool isSaving = false;
public void OpenSave(bool saving) {
Debug.Log("Open Save");
if (Social.localUser.authenticated) {
isSaving = saving;
((PlayGamesPlatform)Social.Active).SavedGame
.OpenWithAutomaticConflictResolution(
"MyCloudFile",
DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpen);
}
}
private void SavedGameOpen(SavedGameRequestStatus reqStatus, ISavedGameMetadata metadata) {
Debug.Log("SavedGameOpen");
if (reqStatus == SavedGameRequestStatus.Success) {
if (isSaving)// Writting
{
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(GetSaveString());
SavedGameMetadataUpdate update = new SavedGameMetadataUpdate.Builder().WithUpdatedDescription("Saved At :" + System.DateTime.Now.ToString()).Build();
((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(metadata, update, data, SaveUpdate);
}
else // Reading or Loading
{
((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(metadata,SaveRead);
}
}
}
//success Save
private void SaveUpdate(SavedGameRequestStatus reqStatus, ISavedGameMetadata metadata) {
Debug.Log(reqStatus);
if (reqStatus == SavedGameRequestStatus.Success)
{
message.text = ("Data Saved successfully ");
}
else {
message.text = ("Data Saved failed " + reqStatus.ToString());
}
}
//Load
private void SaveRead(SavedGameRequestStatus reqStatus, byte[] data) {
if (reqStatus == SavedGameRequestStatus.Success) {
string savedData = System.Text.ASCIIEncoding.ASCII.GetString(data);
message.text = ("Data read successfully " + savedData);
LoadSaveString(savedData);
}
else
{
message.text = ("Data read Failed!" + reqStatus.ToString());
}
}
#endregion
}
Use JSON to keep the game data , splitting string is hard to maintain.
{
"currentLevel":"3",
"totalCoin":"20",
"levelData":[
{
"levelNum":1,
"starEarn":3,
"highScore":123456
},
{
"levelNum":2,
"starEarn":1,
"highScore":1234
}
]
}
//GameData.cs
[System.Serializable]
public class GameData {
public int currentLevel;
public int totalCoin;
private Dictionary<int, LevelData> levelDataList;
public GameData(){
//
// constructor
//
}
public string GetSaveString(){
string res = JsonUtility.ToJson (this);
res = res.Substring (0, res.Length - 1);
res = res + ",\"levelData\":[";
foreach (LevelData levelData in levelDataList.Values) {
res = res + JsonUtility.ToJson (levelData);
res = res + ",";
}
if(levelDataList.Count > 0)
res = res.Substring (0, res.Length - 1);
res = res + "]}";
return res;
}
}
//LevelData.cs
[System.Serializable]
public class LevelData {
public int levelNum;
public int starEarn;
public int highScore;
public LevelData(){
//
// constructor
//
}
}

Empty EditText return string into name, id

I am trying to get the string of the EditText and if it is " I want to get " also but the string is appearing this in the EditText
android.support.v7.widget.AppCompatEditText{42e33310 VFED..CL .F ....ID40,40-1160,315 #7f0c006f app:id\CalculatorDisplay}
after getting it and giving it to the EditText again
and here is a part of the onClick that works when I press a button and the rest of it is not dealing with this problem the problem is in NoRepeatNumber as I tested that when I get it in TestTV TextView I saw the code above
public void onClick(View view) {
TestTV.setText("enterd ");
String buttonPressed = ((Button) view).getText().toString();
PublicButtonPressed = buttonPressed;
if (DIGITS.contains(buttonPressed)) {
SinOrNumber=2;
TestTV.append("numbering");
// digit was pressed
if (!Started) {
mCalculatorDisplay.setText("");
TestTV.append("\nenterd setText");
} else {
TestTV.append("\nenterd setText eslse");
}
if (Started) {
OperationEnded = true;
}
if (NumberingEnded) {
FullNumber = String.valueOf(Number[Ni]);
Ni++;
NumberingEnded = false;
FullCalculation= FullNumber + " ";
NoRepeatNumber="";
DisplayCalculations();
}
FullNumber = FullNumber + buttonPressed;
Number[Ni] = Integer.parseInt(FullNumber);
TestTV.append("\nNumber[" + Ni + "] =" + + Number[Ni] + "\n" + "buttonPressed =" + buttonPressed + "\nFullNumber =" + FullNumber);
//DisplayCalculations();
if ("".equals(mCalculatorDisplay)) {
TestTV.append("\nentered equal\"\"");
NoRepeatNumber="";
} else {
if (NoRepeatNumber == "") {
NoRepeatNumber = String.valueOf(mCalculatorDisplay);
TestTV.append("\nNoRepeatNumber =" + NoRepeatNumber);
}
}
if(!NumberingEnded){
mCalculatorDisplay.setText(NoRepeatNumber + FullNumber);
}
if (!Started) {
Started = true;
}
if (userIsInTheMiddleOfTypingANumber) {
if (buttonPressed.equals(".") && mCalculatorDisplay.getText().toString().contains(".")) {
// ERROR PREVENTION
// Eliminate entering multiple decimals
} else {
Number[Ni] = Integer.parseInt(FullNumber);
}
}
userIsInTheMiddleOfTypingANumber = true;
} else {}}
Call mCalculatorDisplay.getText().toString() instead of String.valueOf(mCalculatorDisplay).
I guess mCalculatorDisplay is an EditText?
Update: NoRepeatNumber = String.valueOf(mCalculatorDisplay); - this is the problem part

Android strange action "program"

The program draws two digits, and the sign. IF statement checks to see if it has been drawn + / -. If it draws + add, if - is subtraction.
Draw works.
Then the user is given the result of the task. And here is the problem.
If you give a result which is in the "result". Function if something does. If you entered an incorrect answer is displayed Toast: Try Again.
The problem is that sometimes as to give a good result is is displayed Try Again.
How to eliminate this problem? Might different check?
Code:
private String sign;
private int numberOne, numberTwo, result = 0;
private int charsEntered = 0;
private EditText et;
private Button ok;
String[] CHAR = { "+", "-" };
Random intGen = new Random();
CaptchaInterface.OnCorrectListener mCorrectListener;
public void setOnCorrectListener(CaptchaInterface.OnCorrectListener listener) {
mCorrectListener = listener;
}
public EasyMathCaptcha(Context context) {
super(context);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}
public static int randomOne() {
Random generator = new Random();
int x = generator.nextInt(10);
return x;
}
public static int randomTwo() {
Random generator = new Random();
int x = generator.nextInt(10);
return x;
}
public void onCreate(Bundle icicle) {
setContentView(R.layout.all_math_captcha);
sign = (CHAR[Math.abs(intGen.nextInt() % 2)]);
numberOne = randomOne();
numberTwo = randomTwo();
TextView display = (TextView) findViewById(R.id.tvRandomTask);
display.setText(numberOne + " " + sign + " " + numberTwo);
if ((CHAR[Math.abs(intGen.nextInt() % 2)]).equals("+")) {
result = (numberOne + numberTwo);
} else if ((CHAR[Math.abs(intGen.nextInt() % 2)]).equals("-")) {
result = (numberOne - numberTwo);
}
et = (EditText) findViewById(R.id.etTask);
ok = (Button) findViewById(R.id.btAgree);
ok.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
charsEntered = Integer.parseInt(et.getText().toString());
} catch (NumberFormatException nfe) {
Toast.makeText(et.getContext(), "That's not a number!",
Toast.LENGTH_SHORT).show();
}
if (charsEntered == result) {
if (mCorrectListener != null)
mCorrectListener.onCorrect();
dismiss();
} else if (charsEntered != result) {
Toast.makeText(et.getContext(), "Try again!", Toast.LENGTH_SHORT)
.show();
}
}
}
The error is in the following code:
if ((CHAR[Math.abs(intGen.nextInt() % 2)]).equals("+")) {
result = (numberOne + numberTwo);
} else if ((CHAR[Math.abs(intGen.nextInt() % 2)]).equals("-")) {
result = (numberOne - numberTwo);
}
You are using the random number generator which can give you results different than what it gave the first time.
Change it to:
if (sign.equals("+")) {
result = (numberOne + numberTwo);
} else if (sign.equals("-")) {
result = (numberOne - numberTwo);
}

substring check assistance with android

Can someone please help me with some code I have it is supposed to check that the string being passed is of 2 string and 3 ints which works fine, but if the 1 int is a zero it doesn't work
so if it was CM044 it won't work, CM450 will work can someone please help.
public boolean checkModule(String Module) {
if(Module.length() == 5){
boolean hasString = false;
boolean hasInt = false;
String letters = Module.substring(0, 2);
Pattern p = Pattern.compile("^[a-zA-Z]+$");
Matcher m = p.matcher(letters);
if (m.matches()) {
hasString = true;
}
String numbers=Module.substring(2,5);
try {
int num = Integer.parseInt(numbers);
String n = num + "";
if (num >0 && n.length() == 3)
hasInt = true;
} catch (Exception e) {
}
if (hasInt && hasString) {
return true;
}
}else{
return false;
}
return false;
}
Thanks
If you are going to use regular expressions you should definitely stick with them and not vary in and out.
package com.examples;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
new Main();
}
public Main() {
String[] testInputs = new String[] {"CM045", "CM450"};
for(String input : testInputs) {
System.out.println("The module " + input + " is " + (checkModule(input) ? "valid" : "invalid"));
}
}
public boolean checkModule(String Module){
Pattern p = Pattern.compile("[A-Z]{2}[0-9]{3}");
Matcher m = p.matcher(Module.toUpperCase());
return m.matches();
}
}
if the String is "045" the Integer value is 45 and so the length of num can't be 3
Use this
if(num>=0) {
hasInt = true;
}
and it has to work

Categories

Resources