DiegoF
DiegoF

Reputation: 84

Spell word with images in Android

Well my question is rather simple

In my layout I have a EditText and two ImageView

I type in any word EditText in two ImageView that I have to be to display images according to the letter, send it as the word of EditText call my problem arises when you want to call one letter for example:

if I enter the word "home" should go in the word and show the two ImageView image according to the letter then would show C (image C) then A (Picture A)

the problem is that I can make the process of finding one letter, I've tried with a for but only recognizes the last letter, I also tried to make a kind of delay (delay) but did not work

part of my code:

public class deletreo extends Activity {

    protected TextView tv;
    protected EditText etxt;
    protected ImageView img,img2;

    final Handler handle = new Handler();

    protected void mth(){
        Thread t = new Thread(){
            public void run(){

                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                handle.post(proceso);
            }
        };
        t.start();
    }

    final Runnable proceso = new Runnable(){
        public void run(){
            letra();
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        tv = new TextView(this);
        setContentView(R.layout.deletreo);


        etxt = (EditText)findViewById(R.id.text);
        Button btn = (Button)findViewById(R.id.btn7);

        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                mth();

            }   
        });



    }//fin bundle





     private void letra()  {

        String t = etxt.getText().toString();

            char[] array = t.toCharArray();
            int p = array.length;

                for(int j=0; j<p;j++){

                    if(array[j] == 'a' || array[j] == 'A'){
                        img = (ImageView)findViewById(R.id.img);
                        img.setImageResource(R.drawable.aa);
                        img2 = (ImageView)findViewById(R.id.img2);
                        img2.setImageResource(R.drawable.image_1);
                        onStop();
                    }

                        if(array[j] == 'b' || array[j] == 'B'){
                            img = (ImageView)findViewById(R.id.img);
                            img.setImageResource(R.drawable.bb);
                            img2 = (ImageView)findViewById(R.id.img2);
                            img2.setImageResource(R.drawable.image_2);

                        }

any idea how to solve this problem?

Upvotes: 1

Views: 190

Answers (1)

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

the problem is that I can make the process of finding one letter, I've tried with a for but only recognizes the last letter, I also tried to make a kind of delay (delay) but did not work

Of course, it behaves that way. Because you are putting delay for 1 second in starting and after that you are calling method letra() which sets image resource in both imageviews in a loop. So you can see only images associated with last letter.

Try it this way instead:

public class deletreo extends Activity {

    protected TextView tv;
    protected EditText etxt;
    protected ImageView img,img2;

    final Handler handle = new Handler();

    protected void mth(){
        Thread t = new Thread(){
            public void run(){

                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }

            }
        };
        t.start();
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        tv = new TextView(this);
        setContentView(R.layout.deletreo);


        etxt = (EditText)findViewById(R.id.text);
        Button btn = (Button)findViewById(R.id.btn7);

        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                letra();

            }   
        });



    }//fin bundle

     private void letra()  {

        String t = etxt.getText().toString();

            char[] array = t.toCharArray();
            int p = array.length;

            for(int j=0; j<p;j++){

            if(array[j] == 'a' || array[j] == 'A'){
                img = (ImageView)findViewById(R.id.img);
                img.setImageResource(R.drawable.aa);
                img2 = (ImageView)findViewById(R.id.img2);
                img2.setImageResource(R.drawable.image_1);
                onStop();
                    }

            if(array[j] == 'b' || array[j] == 'B'){
                img = (ImageView)findViewById(R.id.img);
                img.setImageResource(R.drawable.bb);
                img2 = (ImageView)findViewById(R.id.img2);
                img2.setImageResource(R.drawable.image_2);

            }
            mth();
        }
    }
}

Call delay function mth() after each round of loop. Hope it works.

Upvotes: 1

Related Questions