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?
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.
Related
I'm developing a Whack-a-mole game in Android Studio and I'm having a hard time with AsyncTask (our teacher told us to use it).
Basically the AsyncTasks are not doing anything.
I tried using one asynctask to increase an int and change a textview with the new value each time it increased and it worked, however it doesn't work for what I want it to do.
TextView tv;
ImageView iv1;
ImageView iv2;
ImageView iv3;
ImageView iv4;
boolean salir = false;
int puntuacion = 0;
int vidas = 3;
Hilo hilo;
Hilo hilo2;
Hilo hilo3;
Hilo hilo4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
tv = findViewById(R.id.textView);
iv1 = findViewById(R.id.imageView);
iv2 = findViewById(R.id.imageView2);
iv3 = findViewById(R.id.imageView3);
iv4 = findViewById(R.id.imageView4);
hilo = new Hilo(iv1);
hilo2 = new Hilo(iv2);
hilo3 = new Hilo(iv3);
hilo4 = new Hilo(iv4);
hilo.execute();
hilo2.execute();
hilo3.execute();
hilo4.execute();
}
public void updatePuntuacion() {
tv.setText("Puntuacion: " + puntuacion);
}
public void updateVidas() {
}
private class Hilo extends AsyncTask<Void, Void, Integer> {
boolean estado = false;
ImageView img;
public Hilo (ImageView img) {
this.img = img;
}
#Override
protected Integer doInBackground(Void... voids) {
while(!salir){
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (estado) {
puntuacion++;
updatePuntuacion();
} else {
vidas--;
updateVidas();
}
}
});
if(estado) {
try {
Thread.sleep(Math.round(Math.random()*5000+2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
img.setImageResource(R.drawable.topo);
estado=true;
publishProgress();
} else {
try {
Thread.sleep(Math.round(Math.random()*3000+1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
img.setImageResource(R.drawable.blank);
estado=false;
publishProgress();
}
if(puntuacion == 100) {
salir=true;
}
if(vidas == 0) {
salir = true;
}
}
return puntuacion;
}
}
I expect the imageviews to change it's image every X secs (those are random) but they don't.
I hope you guys can help me, thanks in advance!!!
You need to understand how an Async task works.
You cannot do all the work in "doInBackground" .
You need to remove the onClickListener form "doInBackground" .
Start your async task in the imageview onClick and
After your complete your background processing stuff in "doInBackground" ,
set the image to imageview in "onPostExecute" .
Please go through this https://developer.android.com/reference/android/os/AsyncTask
to understand what an Async Task is first
I have an autogenerated list of edittexts gotten from the user input. What i want to do is shuffle the edittexts when the shuffle button is clicked or get the texts and set them to different edittexts. What i tried doing is to get the texts of each edittext adding it to an arraylist and shuffling it and then recreating the layout with the shuffled list. But that in itself is giving me errors. When the shuffle button is clicked it gives me this error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
Thanks For the help
`
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnForCreate = (Button) findViewById(R.id.btnCreateTxt);
editTextForInputToCreate = (EditText) findViewById(R.id.textForInputToCreate);
listLayout = (LinearLayout) findViewById(R.id.listLayout);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnShuffleTxt = (Button) findViewById(R.id.btnShuffleTxt);
editTextForInputToCreate.animate().translationX(-1000f);
btnForCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bringTextInputBackOnScreen();
}
});
btnDisplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editTextForInputToCreate.getText().toString().length() >= 0) {
try {
listLayout.removeAllViews();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
length = Integer.parseInt(editTextForInputToCreate.getText().toString());
for (i = 0; i < length; i++) {
editTextCollection = new EditText[length];
editText = new EditText(MainActivity.this);
editText.setId(i + 1);
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setHint("Input" + " " + (i + 1));
listLayout.addView(editText);
editTextCollection[i] = editText;
}
}
});
btnShuffleTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listLayout.removeAllViews();
for (EditText gottenEditText:editTextCollection)
{
String gottenTexts = gottenEditText.getText().toString();
list.add(gottenTexts);
}
Collections.shuffle(list);
}
});
}
private void bringTextInputBackOnScreen()
{
editTextForInputToCreate.animate().translationXBy(1000f).setDuration(2000);
}
`
Please make some corrections in your EditTextCollection.
editTextCollection = new EditText[length];// initialization outside the loop
for (i = 0; i < length; i++) {
editText = new EditText(MainActivity.this);
editText.setId(i + 1);
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setHint("Input" + " " + (i + 1));
listLayout.addView(editText);
editTextCollection[i] = editText;
}
btnShuffleTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listLayout.removeAllViews();
for (EditText gottenEditText:editTextCollection)
{
String gottenTexts = gottenEditText.getText().toString();
list.add(gottenTexts);
}
Collections.shuffle(list);
for (EditText gottenEditText:editTextCollection)
{
gottenEditText.setText(list.get(editTextCollection.indexOf(gottenEditText)));
}
}
});
public class Game extends AppCompatActivity {
static int i = 0;
static int p = 0;
String porc0;
TextView tvporc;
Button btn;
TextView tvresult;
String mutqbar;
char generacvac;
char mutq;
int mutqitiv;
int generacvacitiv;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Random random = new Random();
btn = (Button) findViewById(R.id.button4);
tvporc=(TextView) findViewById(R.id.tvPorc);
tvresult=(TextView) findViewById(R.id.result);
generacvac=(char)(random.nextInt(26)+'a');
tvresult.setText(generacvac);
generacvacitiv = (int) generacvac;
et=(EditText) findViewById(R.id.editText2);
tvresult.setText(generacvac);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mutqbar = et.getText().toString();
try {
mutq = mutqbar.charAt(0);
mutqitiv = (int) mutq;
if (mutqitiv<97 || mutqitiv>122){
tvporc.setText("Մուտքագրեք տառ");
}
int abs = Math.abs(mutqitiv-generacvacitiv);
if (abs>0 && abs<5) tvresult.setText("Դու շատ մոտ ես!");
else if (abs>=5 && abs<10) tvresult.setText("Դու մոտ ես~");
else if(abs>=10) tvresult.setText("Դու հեռու ես~");
} catch (Exception e1) {
tvresult.setText("Դաշտը դատարկ է");
}
}
});
}}
I have this code, but when I run it, it returns error java.lang.RuntimeException: Unable to start activity ComponentInfo{com.guessit.guessit/com.guessit.guessit.Game}: android.content.res.Resources$NotFoundException: String resource ID #0x66. and crush on device.Already 1 hour I'm trying to find the problem. please help me
Thanks I already find the solution. I writed tvresult.setText(generacvac); but it cast to int, and I need to write tvresult.setText(generacvac+"")
I have a simple app that updates a TextView from a values generated in a loop when i click on a button.
But when i run the app, it only shows the last number in the TextView. I added a Thread.sleep() to slow down the loop but it did not work, Still the same result. What am i doing wrong here? Thanks in advace
public class MainThread extends ActionBarActivity {
Button btnShow;
TextView txtSee;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_thread);
btnShow = (Button) findViewById(R.id.btnShow);
txtSee = (TextView) findViewById(R.id.txtSee);
btnShow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
for (int i = 0; i <= 5; i++) {
String text1 = "Iteration No " + i + " occured";
txtSee.setText(text1);
Thread.sleep(2000);
}
} catch (InterruptedException e) {
}
}
});
}
....
}
use
txtSee.append(text1);
instead of
txtSee.setText(text1);
your loop is executing too fast to let you observe the changes
Call another function inside loop and use
txtSee.setText(text1); inside that function.
or (int i = 0; i <= 5; i++) {
String text1 = "Iteration No " + i + " occured";
call_function(text1,txtSee);
}
call_function:-
call_function(String text1,TextView txtSee;){
txtSee.setText(text1);
Thread.sleep(2000);
}
i'm new to android programming. I have the following code happening on a button click
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.morse_btn);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loopCode();
}
});
}
which calls this:
public void loopCode()
{
String code = "Hello There";
TextView view = (TextView) findViewById(R.id.code_txt);
String s = "";
for(int i = 0; i < code.length(); i++)
{
s+=code.charAt(i);
view.setText(s);
try {
TimeUnit.SECONDS.sleep(1);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
but when i run it on my phone, the text does not get appended until after the for loop has gone through, i.e i press the button, and after a few seconds, the whole string "Hello There" appears.
How can I make it write the text one character at a time, like a typewriter style.
Thanks
You need to use view.append("") which will append new text to the existing one.
Try this code:
int i = 0; //declare this globally
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i != 10) {
text.append(" " + i);
i++;
handler.postDelayed(this, 1000);
}
}
}, 1000);
}
This code will append a new number to the TextView every one second until it has reached the count 10. You can apply the same logic.
I had provided this solution to a question here -
[EDIT]
Try this:
String code = "Hello There"; //declare variable globally
int i = 0; //declare globally
TextView view; //declare globally
public void loopCode()
{
view = (TextView) findViewById(R.id.code_txt);
//String s = "";
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i != code.length()) {
view.append(" " + code.charAt(i));
i++;
handler.postDelayed(this, 1000);
}
}
}, 1000);
}
}
Don't forget to declare int i = 0 and String code = "Hello There" globally.
Exist 2 different method setText in TextView.
public final void setText (int resid)
public final void setText (CharSequence text)
when you put variable int in setText, the android try find String in classe R variable in same code.
To resolve this you then cast int to string using String.valueOF(...)
see more in;
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#valueOf(int)
try
public void loopCode()
{
String code = "Hello There";
TextView view = (TextView) findViewById(R.id.code_txt);
String s = "";
for(int i = 0; i < code.length(); i++)
{
view.setText(String.valueOf(i));
try {
TimeUnit.SECONDS.sleep(1);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}