0
0

More than 3 years have passed since last update.

Toast 文字大きく表示させたい Android Studio  メモ

Posted at

ボタン押すと3枚の画像が順に切り替わり
それぞれ違うToastテキストが出るアプリを作った。

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 ブーリアン  trueかfalseが入る型
//isShowing  画面に表示されているか判定

//画像re1,re2 は画面に表示されているとコンピューターに判定させる
boolean re1IsShowing = true;
boolean re2IsShowing = true;



public void fade (View view) {

    //Javaの変数re1, re2, re3をxmlの id とそれぞれ紐づけ  
    // 紐付けしないとアニメーションが実行されない
    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, "かわいい?", 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, "遊んで~", Toast.LENGTH_LONG).show();


    } else {

        re1IsShowing = true;
        re2IsShowing = true;


        re3.animate().alpha(0).setDuration(1000);
        re1.animate().alpha(1).setDuration(1000);

        Toast.makeText(this, "似合う?", Toast.LENGTH_LONG).show();
    }



}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);





}

}

しかし、テキストが小さいので大きくしたい。
ネットで出てきた記事を参考に
https://blog.fujiu.jp/2013/11/14-android-toast.html

textや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 ブーリアン  trueかfalseが入る型
//isShowing  画面に表示されているか判定

//画像re1,re2 は画面に表示されているとコンピューターに判定させる
boolean re1IsShowing = true;
boolean re2IsShowing = true;



TextView text = new TextView(getApplicationContext());



public void fade (View view) {

    //Javaの変数re1, re2, re3をxmlの id とそれぞれ紐づけ  
    // 紐付けしないとアニメーションが実行されない
    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("かわいい?");
        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, "遊んで~", Toast.LENGTH_LONG).show();


    } else {

        re1IsShowing = true;
        re2IsShowing = true;


        re3.animate().alpha(0).setDuration(1000);
        re1.animate().alpha(1).setDuration(1000);

        Toast.makeText(this, "似合う?", Toast.LENGTH_LONG).show();
    }



}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);





}

}

としてrunすると、エラー起きないもエミュレーターでアプリ起動してすぐにアプリ落ちてしまう。

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

調べたけどよくわからない。

先ほどの参考記事をそのままコピペして

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("\赤い大きな文字/");
    //フォントの種類
    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();
  }

}

とすると \赤い大きな文字/

とちゃんと表示される。

public class MainActivity extends AppCompatActivity
の中ではなく、

protected void onCreate(Bundle savedInstanceState) {
の中にコードを置かないとダメなのかと思い、

TextView以下を
protected void onCreate(Bundle savedInstanceState) {
のなかの
setContentView(R.layout.activity_main);

のしたに移してみると、
public void fade (View view)
でview に
cannot find symbolと
エラー発生。

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 ブーリアン  trueかfalseが入る型
//isShowing  画面に表示されているか判定

//画像re1,re2 は画面に表示されているとコンピューターに判定させる
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) {

        //Javaの変数re1, re2, re3をxmlの id とそれぞれ紐づけ  
        // 紐付けしないとアニメーションが実行されない
        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("かわいい?");
            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, "遊んで~", Toast.LENGTH_LONG).show();


        } else {

            re1IsShowing = true;
            re2IsShowing = true;


            re3.animate().alpha(0).setDuration(1000);
            re1.animate().alpha(1).setDuration(1000);

            Toast.makeText(this, "似合う?", Toast.LENGTH_LONG).show();
        }



    }




}

}

どうすればいいのかな。

https://akira-watson.com/android/toast-custom.html
[Android] Toast をカスタマイズする

という記事は参考になりそうだが、複雑そうで理解できないので諦めようかな・・・。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0