Skip to main content
2 de 2
Adição dos códigos da acitivity de configurações

Como mudar atributos de todas as TextViews do projeto em tempo de execução?

Estou implementando uma seção de configurações visuais de um projeto e umas das configurações deve permitir que o usuário possa mudar a fontFamily de todo os textos, também há a opção de deixar todos os textos maiúsculos ou minúsculos. Eu sei que é possível deixar um textView maiúsculo usando o textAllCaps, mas não encontrei nada sobre a fontFamily, tampouco sobre como aplicar essas configurações em todo o projeto. Existe alguma forma de resolver esse problema? Grato desde já.

O projeto ainda não tem nada pronto em relação a mudança de todas as textViews, pois não sei como devo fazer isso. Mas vou deixar os código da Activity de configurações.

XML da Activity de configurações:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/btn_back"
            android:layout_width="75dp"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:background="@drawable/purple_right_rounded_backgroud"
            android:onClick="backToMainScreen"
            android:padding="4dp"
            app:layout_constraintBottom_toTopOf="@+id/guideline8"
            app:layout_constraintStart_toStartOf="@+id/guideline18"
            app:srcCompat="@android:drawable/ic_menu_revert" />

        <TextView
            android:id="@+id/textView19"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginRight="70dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="CONFIGURAÇÕES"
            android:textAllCaps="true"
            android:textSize="24sp"
            android:textStyle="bold"
            app:fontFamily="casual" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/yellow_mustard"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView30"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background_maincolor_dark"
            android:gravity="center"
            android:text="Tipo de letra"
            android:textAllCaps="true"
            android:textColor="@android:color/white"
            android:textSize="20dp"
            android:textStyle="bold"
            app:fontFamily="casual" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="4dp">

            <RadioGroup
                android:id="@+id/rgLetterType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:background="@drawable/white_rounded_backgroud">

                <RadioButton
                    android:id="@+id/rb_casual"
                    android:layout_width="110dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="CASUAL" />

                <RadioButton
                    android:id="@+id/rb_cursiva"
                    android:layout_width="110dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="CURSIVA" />

                <RadioButton
                    android:id="@+id/rb_bastao"
                    android:layout_width="110dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="BASTÃO" />

                <RadioButton
                    android:id="@+id/rb_imprensa"
                    android:layout_width="110dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="IMPRENSA" />

            </RadioGroup>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView32"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Visualização:"
                    android:textAllCaps="true"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/textView31"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="4dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
                    android:textAllCaps="true"
                    android:textSize="20sp"
                    app:fontFamily="casual" />

            </LinearLayout>

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/yellow_mustard"
        android:orientation="vertical">

        <TextView
            android:id="@+id/otherConfigs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background_maincolor_dark"
            android:gravity="center"
            android:text="Outros ajustes"
            android:textAllCaps="true"
            android:textColor="@android:color/white"
            android:textSize="20dp"
            android:textStyle="bold"
            app:fontFamily="casual" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="4dp">

            <TextView
                android:id="@+id/textView21"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="As letras devem ser:"
                android:textAllCaps="true"
                android:textSize="18sp" />

            <RadioGroup
                android:id="@+id/rgLetterCase"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:background="@drawable/white_rounded_backgroud"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/rb_uppercase"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="MAIÚSCULA" />

                <RadioButton
                    android:id="@+id/rb_lowercase"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="MINÚSCULA" />

            </RadioGroup>

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Controller da Activity de configurações:

package com.example.anaplb.appalpha.activities;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup;

import com.example.anaplb.appalpha.R; import com.example.anaplb.appalpha.config.AppConfig;

public class ConfigActivity extends AppCompatActivity { private AppConfig configurator; private RadioGroup rgLetterType; private RadioButton rbCasual; private RadioButton rbCursiva; private RadioButton rbBastao; private RadioButton rbImprensa;

private RadioGroup rgLetterCase;
private RadioButton rbUpper;
private RadioButton rbLower;

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

    this.rgLetterType = findViewById(R.id.rgLetterType);
    this.rgLetterCase = findViewById(R.id.rgLetterCase);
    this.rbCasual = findViewById(R.id.rb_casual);
    this.rbCursiva = findViewById(R.id.rb_cursiva);
    this.rbBastao = findViewById(R.id.rb_bastao);
    this.rbImprensa = findViewById(R.id.rb_imprensa);
    this.rbUpper = findViewById(R.id.rb_uppercase);
    this.rbLower = findViewById(R.id.rb_lowercase);

    this.configurator = AppConfig.getInstance(getApplicationContext());


}

@Override
protected void onResume() {
    super.onResume();
    loadConfigsInView();
}

public void backToMainScreen(View view){
    finish();
}

@Override
protected void onPause() {
    super.onPause();
    pushChanges();
    this.configurator.saveAllChange(getApplicationContext());
}

private void pushChanges(){
    String rgSelectedLetterType =((RadioButton)findViewById(this.rgLetterType.getCheckedRadioButtonId())).getText().toString();
    String rgSelectedLetterCase =((RadioButton)findViewById(this.rgLetterCase.getCheckedRadioButtonId())).getText().toString();
    this.configurator.setCurrentLetterType(rgSelectedLetterType);
    this.configurator.setCurrentLetterCase(rgSelectedLetterCase);
}

private void loadConfigsInView(){
    Log.i("Json-Config","Entrou em LoadConfigs");
    Log.i("Json-Config","CurrentLetterType: " + this.configurator.getCurrentLetterType());
    Log.i("Json-Config","CurrentLetterCase: " + this.configurator.getCurrentLetterCase());
    switch(this.configurator.getCurrentLetterType()){
        case(AppConfig.CASUAL):
            rgLetterType.check(rbCasual.getId());
            Log.i("Json-Config","CASUAL");
        break;

        case(AppConfig.CURSIVA):
            rgLetterType.check(rbCursiva.getId());

            Log.i("Json-Config","CURSIVA");
        break;

        case(AppConfig.BASTAO):
            rgLetterType.check(rbBastao.getId());

            Log.i("Json-Config","BASTAO");
        break;

        case(AppConfig.IMPRENSA):
            rgLetterType.check(rbImprensa.getId());

            Log.i("Json-Config","IMPRENSA");
        break;

    }

    switch(this.configurator.getCurrentLetterCase()){
        case(AppConfig.UPPER):
            rgLetterCase.check(rbUpper.getId());
            Log.i("Json-Config","UPPER");
        break;

        case(AppConfig.LOWER):
            rgLetterCase.check(rbLower.getId());
            Log.i("Json-Config","LOWER");
        break;

    }
}

}