Press the button to switch the three images in order I made an app that gives different Toast texts.
package com.example.rswitch;
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View;
import android.widget.ImageView; import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// boolean Boolean type that can contain true or false // isShowing Determine if it is displayed on the screen
// Let the computer determine that the images re1 and re2 are displayed on the screen boolean re1IsShowing = true; boolean re2IsShowing = true;
public void fade (View view) {
// Associate Java variables re1, re2, re3 with xml id respectively // Animation will not be executed unless linked ImageView re1 = findViewById(R.id.re1); ImageView re2 = findViewById(R.id.re2); ImageView re3 = findViewById(R.id.re3);
if(re1IsShowing) {
//swichボタン押した後、画像re1は表示されていないと判定させる re1IsShowing = false;
re1.animate().alpha(0).setDuration(1000);
re2.animate().alpha(1).setDuration(1000);
Toast.makeText (this, "Cute?", Toast.LENGTH_LONG) .show ();
} else if(re2IsShowing) {
//swichボタン押した後、画像re2は表示されていないと判定させる re2IsShowing = false;
re2.animate().alpha(0).setDuration(1000);
re3.animate().alpha(1).setDuration(1000);
Toast.makeText (this, "Play ~", Toast.LENGTH_LONG) .show ();
} else {
re1IsShowing = true;
re2IsShowing = true;
re3.animate().alpha(0).setDuration(1000);
re1.animate().alpha(1).setDuration(1000);
Toast.makeText (this, "Do you look good?", Toast.LENGTH_LONG) .show (); }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
However, the text is small, so I want to make it larger. Refer to the article that appeared on the net https://blog.fujiu.jp/2013/11/14-android-toast.html
Try changing text and toast
package com.example.rswitchtoastarrange;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.view.View;
import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// boolean Boolean type that can contain true or false // isShowing Determine if it is displayed on the screen
// Let the computer determine that the images re1 and re2 are displayed on the screen boolean re1IsShowing = true; boolean re2IsShowing = true;
TextView text = new TextView(getApplicationContext());
public void fade (View view) {
// Associate Java variables re1, re2, re3 with xml id respectively // Animation will not be executed unless linked ImageView re1 = findViewById(R.id.re1); ImageView re2 = findViewById(R.id.re2); ImageView re3 = findViewById(R.id.re3);
if(re1IsShowing) {
//swichボタン押した後、画像re1は表示されていないと判定させる re1IsShowing = false;
re1.animate().alpha(0).setDuration(1000);
re2.animate().alpha(1).setDuration(1000);
text.setText ("Cute?"); text.setTextSize(30);
Toast toast = new Toast(getApplicationContext());
toast.setView(text);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
} else if(re2IsShowing) {
//swichボタン押した後、画像re2は表示されていないと判定させる re2IsShowing = false;
re2.animate().alpha(0).setDuration(1000);
re3.animate().alpha(1).setDuration(1000);
Toast.makeText (this, "Play ~", Toast.LENGTH_LONG) .show ();
} else {
re1IsShowing = true;
re2IsShowing = true;
re3.animate().alpha(0).setDuration(1000);
re1.animate().alpha(1).setDuration(1000);
Toast.makeText (this, "Do you look good?", Toast.LENGTH_LONG) .show (); }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
If you run as, the app will drop immediately after starting the app on the emulator even though no error occurs.
Looking at Logcat,
2020-01-24 20:10:35.166 1835-1835/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument 2020-01-24 20:10:35.167 1835-1835/? E/netmgr: WifiForwarder unable to open QEMU pipe: Invalid argument
I looked it up, but I'm not sure.
Copy and paste the reference article as it is
package com.example.toastbig;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = new TextView(getApplicationContext());
//Toastに表示する文字 text.setText ("\ large red text /"); //フォントの種類 text.setTypeface(Typeface.SANS_SERIF); //フォントの大きさ text.setTextSize(30); //フォントの色 text.setTextColor(Color.RED); //文字の背景色(ARGB) text.setBackgroundColor(0x88dcdcdc);
//Toastの表示 Toast toast = new Toast(getApplicationContext()); toast.setView(text); toast.setDuration(Toast.LENGTH_LONG); toast.show(); }
}
Then \ large red letters /
Is displayed properly.
public class MainActivity extends AppCompatActivity Not in
protected void onCreate(Bundle savedInstanceState) { I wondered if I had to put the code inside,
Below TextView protected void onCreate(Bundle savedInstanceState) { In setContentView(R.layout.activity_main);
When I move it to public void fade (View view) To view cannot find symbol and An error has occurred.
package com.example.rswitchtoastarrange;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.view.View;
import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// boolean Boolean type that can contain true or false // isShowing Determine if it is displayed on the screen
// Let the computer determine that the images re1 and re2 are displayed on the screen boolean re1IsShowing = true; boolean re2IsShowing = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = new TextView(getApplicationContext());
public void fade (View view) {
// Associate Java variables re1, re2, re3 with xml id respectively // Animation will not be executed unless linked ImageView re1 = findViewById(R.id.re1); ImageView re2 = findViewById(R.id.re2); ImageView re3 = findViewById(R.id.re3);
if(re1IsShowing) {
//swichボタン押した後、画像re1は表示されていないと判定させる re1IsShowing = false;
re1.animate().alpha(0).setDuration(1000);
re2.animate().alpha(1).setDuration(1000);
text.setText ("Cute?"); text.setTextSize(30);
Toast toast = new Toast(getApplicationContext());
toast.setView(text);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
} else if(re2IsShowing) {
//swichボタン押した後、画像re2は表示されていないと判定させる re2IsShowing = false;
re2.animate().alpha(0).setDuration(1000);
re3.animate().alpha(1).setDuration(1000);
Toast.makeText (this, "Play ~", Toast.LENGTH_LONG) .show ();
} else {
re1IsShowing = true;
re2IsShowing = true;
re3.animate().alpha(0).setDuration(1000);
re1.animate().alpha(1).setDuration(1000);
Toast.makeText (this, "Do you look good?", Toast.LENGTH_LONG) .show (); }
}
}
}
What should I do.
https://akira-watson.com/android/toast-custom.html [Android] Customize Toast
The article seems to be helpful, but I think I should give up because it seems complicated and I can not understand it.
Recommended Posts