1. Download our Official Android App: Forums for Android!

Apps Making my first Android app (and got stuck) :)

Discussion in 'Android Development' started by silviu_stelian, Apr 13, 2011.

  1. silviu_stelian

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Apr 13, 2011

    Apr 13, 2011
    2
    0
    5
    Hello everybody :)

    I just started developing apps for Android in Eclipse and I came upon an interesting challenge. I started working on an application that:
    When launched, shows an input field to enter a sentence and a standard button
    When the button is pressed, the application shows the same sentence (pretty easy so far right? :D ) with the following change:

    The first and last letter of each word is kept the same while all other letters (if any) are randomly mixed.

    And here is where I got stuck as I have no idea where to begin doing it.
    It's basically the "Making an Android App for NOOBS!" thread but with a twist :)
    Thanks and happy coding :cool:
     

    Advertisement

  2. RazzleFnDazzle

    RazzleFnDazzle Well-Known Member
    Rank:
    None
    Points:
    58
    Posts:
    123
    Joined:
    Sep 30, 2010

    Sep 30, 2010
    123
    15
    58
    So you are able to get the application to start up with the text field and the button that display information? That is essentially it. The only other thing I think you would have to do is set an onClickListener for the button. The listener should grab the text from the EditText. Next you will look at the length of the String. You then take the first character using text.charAt(0) and add it to a string that we'll call "random". for length - 2 times you will generate a random character

    Code (Text):
    1.  
    2. char c = (char) ('a' + ((int) (Math.random() * 26)))
    3.  
    You now add c to "random". Now you add text.charAt(text.length() -1) to "random". You now have your random String that is the same length with the first and last letter the same as the string entered in the EditText
     
  3. silviu_stelian

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Apr 13, 2011

    Apr 13, 2011
    2
    0
    5

    Thanks for the reply! Very interesting idea;)
    I've got the on click listener, but now I'm stuck with the strings:mad::

    Code (Text):
    1. import java.util.List;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.Button;
    7. import android.widget.EditText;
    8. import android.widget.TextView;
    9. import java.util.*;
    10.  
    11.  
    12.  
    13. public class main extends Activity {
    14.         /** Called when the activity is first created. */
    15.    
    16.     TextView TextOut;
    17.     EditText getinput;
    18.    
    19.    
    20.    
    21.    
    22.     @Override
    23.    
    24.     protected void onCreate(Bundle savedInstanceState) {
    25.         super.onCreate(savedInstanceState);
    26.         setContentView(R.layout.main);
    27.        
    28.      
    29.         TextOut = (TextView) findViewById(R.id.textView);
    30.         getinput = (EditText) findViewById (R.id.EditText);
    31.         Button Show = (Button) findViewById (R.id.button1);
    32.         Show.setOnClickListener (new View.OnClickListener() {
    33.                        
    34.             public void onClick(View v) {
    35.                 // TODO Auto-generated method stub
    36.                 TextOut.setText(getinput.getText());
    37.                
    38.                
    39.             }
    40.         });
    41.        
    42.     }
    43.      
    44.            
    45.    
    46.        
    47.        
    48.     }    
    49.        
    50. }
    51.  
     
  4. JiMMaR

    JiMMaR Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    363
    Joined:
    Jun 12, 2010

    Jun 12, 2010
    363
    57
    53
    you'd probably need to do something like this
    Code (Text):
    1.  
    2. import java.util.List;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.widget.Button;
    8. import android.widget.EditText;
    9. import android.widget.TextView;
    10. import java.util.*;
    11.  
    12. public class main extends Activity {
    13.     /** Called when the activity is first created. */
    14.  
    15.     TextView TextOut;
    16.     EditText getinput;
    17.     String sentance;
    18.  
    19.     @Override
    20.     protected void onCreate(Bundle savedInstanceState) {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.main);
    23.  
    24.         TextOut = (TextView) findViewById(R.id.textView);
    25.         getinput = (EditText) findViewById(R.id.EditText);
    26.         Button Show = (Button) findViewById(R.id.button1);
    27.         Show.setOnClickListener(new View.OnClickListener() {
    28.  
    29.             public void onClick(View v) {
    30.                 TextOut.setText(getinput.getText());
    31.                 sentance = getinput.getText().toString();
    32.                 sentance = modify(sentance);
    33.                 TextOut.setText(sentance);
    34.  
    35.             }
    36.         });
    37.  
    38.     }
    39.  
    40.     public String modify(String x) {
    41.         // do your algorithm here
    42.         return x;
    43.     }
    44.  
    45. }
    46.  
    47.  
    you just need to implement your algorithm inside the modify() function .. without that it's gonna print whatever you type without modification

    I haven't tested the code above .. but it should work
     

Share This Page

Loading...