1
I am implementing a visual settings section of a project and one of the settings should allow the user to change the fontFamily of all texts, there is also the option to leave all texts uppercase or lowercase. I know it’s possible to capitalize a textView using textAllCaps, but I haven’t found anything about fontFamily, nor about applying these settings to the entire project. Is there any way to solve this problem? Grateful from now on.
The project still has nothing ready regarding the change of all textViews, because I do not know how to do this. But I’ll leave the Activity code settings.
Configuration XML Activity:
<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 of Activity settings:
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;
}
}
}
Have you tried youTextView.setTypeface(Typeface.create("sans-Serif-light", Typeface.NORMAL)); ? An alternative to changing all fonts of the project’s textview, is to create a subclass that extends from Textview, use a sharedpreference to retrieve the configuration the user placed, and then return it in their custom textview. Add your code, which I try to help better.
– Murillo Comino
Thanks for the tips buddy, I’ll try to implement them. About persistence of user settings, I’m using JSON to save/recover, what do you think?
– Emerson Ruan Dantas Silva
I created an Alphatextview class that extends from Textview, but I don’t know how I can make all the Alphatextview I use change by changing some of the settings I implemented on the settings screen. I would have to somehow have a list of all the Alphatextviews used?
– Emerson Ruan Dantas Silva
I will make one here, using sharedpreference ai you modify to use with the Json you receive. I will soon command.
– Murillo Comino
Okay, I went up the example on github if you want to take a look. Anything just ask
– Murillo Comino