Changing Font for specific texts in the App by User Settings - java

I'm working on a Japanese Vocabulary App in Android Studio in Java.
I want to make it possible for the user to select a Font that best fits there preferences.
Currently, I have a Font added to res and defined a font family using the font file. All japanese text (Textbox, Buttons etc.) in the App uses this font family.
Is there a way to change the font family itself or change the font family tag-value pair in xml for every element using this font family while the app is running.
This is my current font family definition
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font app:fontStyle="normal" app:fontWeight="400" app:font="#font/kosugimaru_regular"/>
<font app:fontStyle="italic" app:fontWeight="400" app:font="#font/kosugimaru_regular" />
</font-family>
And this is how i use this font family in xml:
<TextView
android:id="#+id/TV_KanjiChar"
android:fontFamily="#font/kanji_default"
android:text="学"
android:textSize="60sp"
...
/>
if there is no way of changing that binding while running the app is there another elegant way of solving this problem? I Would like to avoid using update functions in every activity to change the font family dynamically onCreate.

I'm going ahead and answer my question.
A quite elegant solution is to create a subclass of Textview and Button and add a UpdateFont function to the constructors.
public class KanjiTextView extends AppCompatTextView {
public KanjiTextView(Context context) {
super(context);
updateFont();
}
public KanjiTextView(Context context, AttributeSet attrs) {
super(context, attrs);
updateFont();
}
public KanjiTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
updateFont();
}
public void updateFont()
{
int font_res = get_user_selected_font();
Typeface typeface = getFont(getContext(), font_res.get_Res());
setTypeface(typeface);
}
}
_____________________________________________________________________________
<com.[package].KanjiTextView
android:id="#+id/TV_Kanjitext"
android:text="学校"
android:textAlignment="center"
android:textSize="30sp"
...
/>

Related

Android - Make whole search view clickable with query hint

So i've been working on this inventory management system in Android Studio. In the fragment for Product Taking i have a search view and i want to make this search views whole body to be clickable. Part of this problem is solved here: Android - Make whole search bar clickable. But i want the search view to have a visible query hint. So basically i want it to be a button with search icon and a text. I want that because it is supposed to open a dialog where the user actually going to search for products. Not from this search view. When i add the setIconified(true) whole body is clickable but query hint is not visible. Like this:
When i add setIconified(false) query hint is visible but only search icon is clickable. Like this:
You can intercept all touches before SearchView consume them. I've created a simple class that intercept all touch events.
Kotlin:
class TouchInterceptorLayout #JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
// You need override this method.
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
return true
}
}
Java:
public class TouchInterceptorLayout extends FrameLayout {
public TouchInterceptorLayout(Context context) {
super(context);
}
public TouchInterceptorLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchInterceptorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
}
See how xml looks:
<com.example.testci.temp.TouchInterceptorLayout
android:id="#+id/interceptorLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<SearchView
android:id="#+id/searchView"
android:queryHint="#string/app_name"
android:iconifiedByDefault="false"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</com.example.testci.temp.TouchInterceptorLayout>
Now you need just set OnClickListener to interceptorLayout.
Full code with my experiment you can find here.

Toggling the color of a button's drawable and text in java

I need a Button or TextView that changes the color of its CompoundDrawable (Top) and Text when it is clicked to show whether or not is on.
It needs to be either a Button or TextView
There can only be 1 drawable resource per button
The Text color needs to change
DrawableTop color need to change, NOT BE REPLACED WITH A DIFFERENT DRAWABLE
The background of the view needs to remain transparent
The state of the button needs to be saved
I tried different things, but I think my best shot is to create a new Java file which extends Button or TextView. I was thinking something like this:
public class CustomButton extends android.support.v7.widget.AppCompatButton {
int colorOn;
int colorOff;
boolean isOn = false;
public CustomButton(Context context) {
super(context);
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void onButtonClicked(){
if(isOn){
setCompoundDrawablesColors(colorOn);
setTextColor(colorOn);
} else {
setCompoundDrawablesColors(colorOff);
setTextColor(colorOff);
}
isOn = !isOn;
}
}
My questions are:
Where do I insert onButtonClicked? onTouchEvent? onPressed? onFocused? Implement an OnClickListener?
How do I change the color of the Drawable so it updates UI immediately? I know changing the color is done with setCompoundDrawablesTint(color, mode). Does that update the UI asap? I haven't been able to get it to work.
How do I do this without any XML code?
In fact, you can customize the view to draw their own, you can dispatchTouchEvent (MotionEvent event) to listen to the key state call OnClickListener, this will be easier to achieve.

ImageButton resizes when I change background

I am making my first android app in Android Studio. I have an ImageButton that I want to change on click. For that I made a new class called Field that extends ImageButton class. so, I use that instead of ImageButton. And in the class field I redefined a constructor so that I added that it sets OnClickListener when it creates Field object.
Here is Java code I used for that: `
public Field(Context context) {
super(context);
init();
}
public Field(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Field(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setImageResource(R.drawable.open);
}
});
}`
Than, when I create Field object in my activity, OnClickListener is set.
And, it works. However, the new image called open is bigger that my previous image called pic01, but both images have the same size 185x185.
Here is my activity xml:
<asd.clicker.Field
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/asd.clicker.Field1"
android:background="#drawable/pic01"
android:layout_above="#+id/asd.clicker.Field4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="25dp" />
I noticed that, even when I just change pic01 to open in my xml, the open image is bigger than pic01, both are .png. How can I get image of the same size when I click?
Change the two lines
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to
android:layout_width="48dp"
android:layout_height="48dp"
or some other size you wan't your view to have. wrap_content will change the view's size based on the displayed drawable.

Showing CustomViews properly in Graphical Layout of android xml

I have made a very simple Custom TextView. Everything works fine in android device. However on graphical layout of eclipse All I can see is the class name written instead of TextView original Text. How Can I test it in eclipse graphical layout?
Below is my code
public class MyTextView extends TextView
{
public MyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public MyTextView(Context context)
{
super(context);
init();
}
private void init()
{
//if (!isInEditMode())
{
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "HelveticaLTStd-Bold.otf");
setTypeface(tf);
this.setTextColor(Color.parseColor("#FFD200"));
this.setShadowLayer(1, 1, 1, Color.BLACK);
}
}
i had the same problem with custom fonts on custom textViews.
the graphical editor is quite buggy and lacks many features that work fine on real devices.
this is one example of such a thing.
in order to fix it , just don't load fonts when isInEditMode() returns true, and ignore how the text looks like.
in fact, maybe the shadow feature also doesn't work well, so you might want to add it too.

Uniform EditText size

I'm trying to get all edit text views to be the same size without using a hard definition like px or dp, but rather to use a wrap_content and get them all to be the same size. This is what I have, but it doesn't work:
enterAge = (EditText) findViewById(R.id.enterAge);
enterAge.addTextChangedListener(new TextWatcher1());
enterWeight = (EditText) findViewById(R.id.enterWeight);
enterWeight.addTextChangedListener(new TextWatcher2());
enterWeight.setWidth(enterAge.getWidth());
enterHeight = (EditText) findViewById(R.id.enterHeight);
enterHeight.addTextChangedListener(new TextWatcher3());
enterHeight.setWidth(enterAge.getWidth());
enterMealFrequency = (EditText) findViewById(R.id.enterMealFrequency);
enterMealFrequency.addTextChangedListener(new TextWatcher4());
enterMealFrequency.setWidth(enterAge.getWidth());
Any suggestions on why this isn't working the way I want it to?
wrap_content is fundamentally the opposite of what you are saying -- it specifies to make the view only as large as it needs to be, regardless of the parent size.
You need to write your UI as a view hierarchy with layouts describing how to position the views. It is very simple to create a vertical row of text views that are all given them same width by putting them in a LinearLayout.
It depends entirely on when you are executing this code. The values for the width and height of a View will not return valid data in Java code until layout is complete (this happens sometime later than when the layout is inflated from XML). If you are doing this in onCreate() or even onResume(), this is too early and getWidth() will likely return zero.
You need to work with the layout system to determine when you will have valid view sizes to work with.
Hope that Helps!
I have solved similar problem introducing my own class which extends EditText, e.g.
public class SimpleEditText extends EditText
{
private static float textSize=14.0;
public SimpleEditText(Context context)
{
super(context);
this.setTextSize(textSize);
}
public SimpleEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
this.setTextSize(textSize);
}
public SimpleEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
this.setTextSize(textSize);
}
}
In resource you can directly use reference to SimpleEditText instead of EditText (for sure one have to keep in mind that all size related XML attributes will be ignored...)

Categories