Right now i'm doing an educational app.I stucked with some problem.
My app first page has list of java programs name like below
swap two numbers
find biggest number
reverse number
When user press a particular item- it has to go to detail screen and has to display particular program (i mean user press on swap of two no's need to navigate to detail page to display respective program)->right now its going detail view controller
For example I have program like this
/********print star pattern ************/
import java.util.Scanner;
public class Diamond
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter N : ");
int n=sc.nextInt();
System.out.print("Enter Symbol : ");
char c = sc.next().charAt(0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i*2-1;j++)
{
System.out.print(c);
}
System.out.println();
}
for(int i=n-1;i>0;i--)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i*2-1;j++)
{
System.out.print(c);
}
System.out.println();
}
}
}
How can I print above program in detail in view controller ?
Now am using textview, Is it okay to continue with it or Is any other component that i could make use of ?because some programs have more width and height.
or else any other mechanism is available to achieve this?
I researched a lot but didn't find solution.
Can someone suggest me how to go with this? where to store the program? How to display it?
Thanks.
Related
I'm currently working on a school project in Android Studio and so far I've written a code which generates random equations.
Now I display two equations on the screen and the user then has to decide wether the second equation is bigger or smaller than the first one. If the second is bigger, the user presses the button 'bigger', if the second one is smaller, the user presses the button with 'smaller'.
Now I'm trying to write the code that if the user pressed correct, it generates a new equation, if he was wrong, the process stops.
I was thinking of if statements like so:
final ArrayList<String> arrayListCheck = new ArrayList<String>();
if(doubleAnswer1 > doubleAnswer2){
arrayListCheck.add("smaller");
} else {
arrayListCheck.add("bigger");
}
final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger)){
arrayListCheck.add("bigger");
} else {
arrayListCheck.add("smaller");
}
}
};
buttonBigger.setOnClickListener(listener);
buttonSmaller.setOnClickListener(listener);
In the arraylist arrayListCheck it will store either 'bigger' or 'smaller'. Now I want to check if the elements in the arraylist are both the same (either both 'bigger' or 'smaller'), if so a new equation will be generated. If the elements in the arraylist are different (not both the same), the process will be stopped.
I don't know if that really works, so it would be nice if someone could help me with this.
If there is anything unclear in my question, feel free to ask and I will try to clarify the problem :)
Thank you already in advance for your help!
I wouldn't use an ArrayList for that.
I would do the following:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger) && doubleAnswer1 < doubleAnswer2) {
Log.v("TAG", "you are right");
} else if(v.equals(buttonSmaller) && doubleAnswer1 > doubleAnswer2) {
Log.v("TAG", "you are right");
} else {
Log.v("TAG", "you are wrong");
}
}
};
Arrays are not really neccessary for this kind of simple comparison.
I am creating a quiz game which is working fine,but the problem am encountering is that whenever the user selects the correct answer, its shows it as wrong answer,meanwhile it's the correct answer, it's turns out the correct answer is not at the correct location.
Here is my code so far, any ideas how i can fix this.
public class RiddleGuessActivity extends AppCompatActivity {
TextView riddleQuestionTextView;
Button btn1, btn2, btn3, btn4, btnContinue, btnOk;
ArrayList<String> riddles = new ArrayList<>();
ArrayList<String> answers = new ArrayList<>();
int locationOfCorrectAnswers = 0,incorrectAnswers = 0;
ArrayList<String> options = new ArrayList<>();
LinearLayout gameOverLayout;
LinearLayout buttonLayout;
RelativeLayout resultLayout;
TextView timerTextView;
TextView answerTextView;
TextView levelTextView, scoreTextView;
TextView gameOverTextView;
int levelCounter = 1, scoreCounter, riddleCounter = 0;
public void chosenRiddle (View view) {
if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswers))) {
//Toast.makeText(RiddleGuessActivity.this, "Correct", Toast.LENGTH_SHORT).show();
levelCounter++;
scoreCounter+=60;
riddleCounter++;
levelTextView.setText(String.valueOf(levelCounter));
scoreTextView.setText(String.valueOf(scoreCounter));
result();
}else {
Toast.makeText(RiddleGuessActivity.this, "Wrong, It was " +
answers.get(riddleCounter), Toast.LENGTH_SHORT).show();
gameOver();
}
}
public void generateRiddles () {
riddles.add("I kiss my mother before i die. What am i ?");
riddles.add("It is greater than God and more evil than the devil. The poor have it, the rich need it and if you eat it you'll die. What am i ?");
riddles.add("It walks on four legs in the morning, two legs at noon and three legs in the evening. What am i ?");
riddles.add("I am the beginning of the end, and the end of time and space. I am essential to creation and i surround everyplace. What am i ?");
riddles.add("What always runs but never walks, often murmurs, never talks, has a bed but never sleeps, has a mouth but never eats. What am i ?");
riddles.add("At night they come without being fetched, By day they are lost without being stolen. What am i ?");
riddles.add("The one who makes it, sells it. The one who buys it, never uses it. The one that uses it never knows that he's using it. What am i ?");
riddles.add("The more you have of it, the less you see. What am i ?");
riddles.add("I am always hungry, i must be fed, the finger i touch will soon turn red. What am i ?");
riddles.add("If you break me, i do not stop working, if you touch me, i may snared, if you lose me, nothing will matter. What am i ?");
answers.add("Matches");
answers.add("Nothing");
answers.add("Man");
answers.add("Letter E");
answers.add("River");
answers.add("The Stars");
answers.add("A Coffin");
answers.add("Darkness");
answers.add("Fire");
answers.add("Heart");
answers.add("The Moon");
answers.add("Food");
answers.add("Love");
answers.add("Mouth");
answers.add("Money");
Random random = new Random();
riddleQuestionTextView.setText(riddles.get(riddleCounter));
locationOfCorrectAnswers = random.nextInt(4);
options.clear();
for (int i = 0; i < 4; i++) {
if (i == locationOfCorrectAnswers &&
!options.contains(answers.get(riddleCounter))) {
options.add(answers.get(riddleCounter));
}else {
incorrectAnswers = random.nextInt(answers.size());
while (incorrectAnswers == riddleCounter) {
incorrectAnswers = random.nextInt(answers.size());
}
if(!options.contains(answers.get(incorrectAnswers)))
options.add(answers.get(incorrectAnswers));
else --i;
}
}
btn1.setText(options.get(0));
btn2.setText(options.get(1));
btn3.setText(options.get(2));
btn4.setText(options.get(3));
}
Are you missing an onCreate method??
Anyways, I suggest you add the correctAnswer followed by the incorrect options. Basically, forget random numbers and while loops.
Then Collections.shuffle() the list.
Then scan the list for the location of the correct answer.
Log.d seems to not be displaying anything in my terminal. Is this related to using a static method?
public static void setPosition(int pos){
DetailActivity.pos = pos;
DetailActivity.counter++;
Log.d("counter", "" + counter);
if(DetailActivity.counter == 1){
//ad
if (DetailActivity.mInterstitialAd.isLoaded()) {
DetailActivity.mInterstitialAd.show();
counter = 0;
}
}
}
Log is going to print to LogCat. In Android Studio, go to the Android Monitor tab to see this output.
You may add filters for your tag or message in the search box, for example entering "counter" should show your message.
Let's say I have a similar scenario:
I'm using an accessibility service in order to my TTS engine talk when this dialog appears, but the only thing I was able to detect were the selectable views (those pointed by the arrow).
Is there any way to detect the title and (more importantly) whole text inside the dialog?
Yes. I think it is likely that you're grabbing these items off of accessibility events, which focus on a single node. What you want to do instead is look at the entire view hierarchy. You can do this one of two ways. First thing to note is that Accessibility Nodes are a tree. Just like the view heirarchy is a tree. In fact, this tree matches the view hierarchy, almost 1 to 1. Developers can force an element to not be included in the view hierarchy, though this isn't done often in practice. Even if they do you can get this information regardless. Let's assume we want this information. First thing we want to do is make sure it's included.
protected void onServiceConnected() {
super.onServiceConnected();
AccessibilityServiceInfo tempInfo = getServiceInfo();
tempInfo.flags |= AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
setServiceInfo(tempInfo);
}
It's highly likely you can skip this step, but just in case!
After this it's quite simple. First, so that you can see where this information is, let's write a cute little logging function.
public static void logNodeHeirarchy(AccessibilityNodeInfo nodeInfo, int depth) {
if (nodeInfo == null) return;
String logString = "";
for (int i = 0; i < depth; ++i) {
logString += " ";
}
logString += "Text: " + nodeInfo.getText() + " " + " Content-Description: " + nodeInfo.getContentDescription();
Log.v(LOG_TAG, logString);
for (int i = 0; i < nodeInfo.getChildCount(); ++i) {
logNodeHeirarchy(nodeInfo.getChild(i), depth + 1);
}
}
This function should log the entire tree of an accessibility node. Add it as a static function to your accessibility service. Now we just need to call it on the root node. You can easily change the properties that get logged. I find text, content description, and view id to be the most useful.
#Override
public void onAccessibilityEvent(AccessibilityEvent e) {
switch (e.getEventType()) {
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {
logNodeHeirarchy(getRootInActiveWindow(), 0);
}
}
}
This should allow you to see where the information is. All you have to do is figure out how to parse it. Note: You can also crawl up from a leaf node, using getParent().
I am currently developing a game in AndEngine and I have set up my collision detection with the enemies by checking each car against every individaul index of the enemy array as for some reason, a for loop does not work. This is extremely inconvinient as not only does it make increasing and decreasing enemies a chore, but it looks awful! It looks something like this:
if (rManager.getInstance().iceArray[0].getIceSprite().collidesWith(rManager.getInstance().carArray[r].getCarSprite()))
{
rManager.getInstance().carArray[r].setCarSpeed(1f);
} else if (rManager.getInstance().iceArray[1].getIceSprite().collidesWith(rManager.getInstance().carArray[r].getCarSprite())) {
rManager.getInstance().carArray[r].setCarSpeed(1f);
} else if (rManager.getInstance().iceBergArray[0].getIceBergSprite().collidesWith(rManager.getInstance().carArray[r].getCarSprite())) {
rManager.getInstance().carArray[r].setCarSpeed(0f);
} else {
rManager.getInstance().carArray[r].setCarSpeed(0.5f);
}
The for loop I tried was like this with [r] being every car of the car array, but it doesn't seem to do anything.
for (int h = 0; h < rManager.getInstance().snowArray.length; h++)
{
if (rManager.getInstance().snowArray[h].getSnowSprite().collidesWith(rManager.getInstance().carArray[r].getCarSprite())) {
String temp = rManager.getInstance().carArray[r].toString();
Log.e("SNOW", "SNOWWWWW!" + rManager.getInstance().snowArray[h].toString());
rManager.getInstance().carArray[r].setCarSpeed(0.2f);
}
}
Thanks!!
You should use factory pattern or strategy pattern to remove out the if else.
Please see the link below to take reference.
http://www.cumps.be/nl/blog/read/design-patterns-strategy-pattern
or Strategy Pattern with no 'switch' statements?
EDIT :
Base on your code, the startegy pattern should implement follow pesudo code below. I have made some of assumption base on your code already posted above.
You will create a dictionary contains logics and appropriate action as below:
Dictionary<Func<RManager, int, bool>, Action<Car>>
ruleUpdates =
new Dictionary<Func<RManager, int, bool>, Action<Car>>();
Each Func<Ice, Car, bool> you should implement like this
private bool ZeroElementEqualIcePrite(RManager rManager, int r)
{
return rManager.getInstance().iceArray[0].collidesWith(rManager.getInstance().carArray[r].getCarSprite());
}
and Action should implement as below:
public static void DoUpdateOneLevel(Car car)
{
car.setCarSpeed(1f);
}
From there we can implement similar with all of logics following your code with the same way.
You can initialize the dictionary following below
ruleUpdates.Add(FirstElementEqualIcePrite, DoUpdateOneLevel);
ruleUpdates.Add(SecondElementEqualIcePrite, DoUpdateOneLevel);
ruleUpdates.Add(FirstElementEqualIceBergPrite, DoUpdateZeroSpeed);
ruleUpdates.Add(SecondElementEqualIceBergPrite, DoUpdateZeroSpeed);
After that you just loop go through each key of executionList as below.
foreach (KeyValuePair<Func<RManager, int, bool>, Action<Car>>
ruleUpdate in ruleUpdates)
{
if (ruleUpdate.Key.Invoke(rManager, r))
{
ruleUpdate.Value.Invoke(rManager.getInstance().carArray[r]);
break;
}
}
Hope this help. Following the strategy your code will clean and easy to changed does not care much about if / then / else logic as well. You can easy extent it in future.