How to write algebra formulas in android xml - android

Here is an example
https://play.google.com/store/apps/details?id=com.entertainment.algebra_calculator_1
I just wanted to know how the formulas are written in xml like 1/x, superscripts, subscripts.

This should work for super script
TextView mTextView = (TextView) findViewById(R.id.textView);
mTextView.setText(Html.fromHtml("10<sup>3</sup>"));
For sub script
TextView mTextView = (TextView) findViewById(R.id.textView);
mTextView.setText(Html.fromHtml("A<sub>2</sub>"));

Related

new object issue

I'm new to Android Studio.
I made a TextView and a Button in Android Studio.
when I click button it supposed to trigger this method:
public void click (View view)
{
TextView tex = (TextView) findViewById(R.id.text_view);
tex.setText("Hello");
}
The code works in this way .
but when I made the method like this:
public void click (View view)
{
TextView tex = new TextView(this);
findViewById(R.id.text_view);
tex.setText("Hello");
}
the code doesn't do what it supposed to do. I mean nothing happen to the TextView text.
Can anyone explain to me what's the difference? and why this happens or what's wrong about the second case?
TextView tex = new TextView(this);
That code creates new textview instance but this textview has no connection with your view.
But TextView tex = (TextView) findViewById(R.id.text_view); finds the first descendant view with the given ID and assigns it to your local variable so you have connection.
More info https://developer.android.com/reference/android/view/View#findViewById(int)
public void click (View view)
{
TextView tex = new TextView(this);
findViewById(R.id.text_view);
tex.setText("Hello");
}
In this section, tex is a textView object but it does not have any field on the view.
TextView tex= findViewById(R.id.textView)
this line assigns a view to tex object.
The reason is quite obvious.
it seems to me that you are fairly new to programming and doesn't understand java very well, and doesn't have any programming experience of some sort. You see, Semi colons are used to terminate a line of code. So, thats one of the things you missed. Second, this:
TextView tex = (TextView) findViewById(R.id.text_view);
tex.setText("Hello");
gets a reference from a view on the first line and sets the text on the second line. While this:
TextView tex = new TextView(this);
findViewById(R.id.text_view);
tex.setText("Hello");
Creates a new view on the first line. Gets a reference of the view on the second whilst not using its reference. And sets the text on the third line to the new view that was created on the first line.
Suggestion
OOP is not a really hard subject, but, if you really want to code, better know a little more about simple programming before diving in to OOP.
https://developer.android.com/reference/android/view/View.html#findViewById(int)
findviewbyid returns a View.
TextView tex = new TextView(this);
findViewById(R.id.text_view);
when you do this, you created a new TextView but you never actualy did anything with the View returned by findViewById since you ignored it.
There are two good ways to this as listed below
setOnClickListner
Using android view binding library (Maybe in combination with ViewModel and difficult, research nice and slow ! :p)
using android:onClick="methodName()" attribute, (I personally don't prefer this, AFAIK doesn't work in fragment)
Here is the Easiest one!
Put this code into onCreate after setContentView is called
Button btn = findViewById(R.id.your_btn_id);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//handle triggner here i.e set hello to your textView
}
});
Happy coding!
You need add tex into your layout to see it:
((ViewGroup) findViewById(R.id.some_container_id)).addView(tex)
When you call
findViewById(R.id.text_view);
and ignore returning value, this function have no effect

Change the text of textview based on its ID

I am dynamically adding a textView into a layout and giving it ID's. Based on some conditions I want to change the text of the TextView based on its ID assigned.
Something like this: personOne.setText("abcd"); where personOne.id = buttonID;
personOne = new TextView(this);
int buttonID = 2000 + personCount;
personOne.setTextAppearance(this, R.style.button_text);
personOne.setTextColor(getResources().getColor(R.color.red));
personOne.setTextSize(15);
personOne.setText(personAdded);
personOne.setId(buttonID);
Help / Suggestions much appreciated.
Thank you
Use something like this:
TextView tv = (TextView) findViewById(yourId);
tv.setText("abcd");
Assuming the TextView is a child of the Activity's content view.
You can use the buttonID to find and manipulate TextView dynamically as follows:
TextView dynamicTextView = (TextView) findViewById(buttonID);
dynamicTextView.setText("The text you want to set");

TextView's show the wrong text

my problem is something I've never seen before...
I'm trying to make an android app, and I've stuck at one small bug, and this one is very tricky.
I have 20 TextViews, 10 medium size and 10 regular size, the little ones.
And somehow two of these textviews, one medium and the other is a little one, change it's text one for another. The first should show a text and the second a number. Somehow they are inverse, and I keep looking at the code, and I can't figure out why.
Some of you are thinking that is a simple thing to solve, that I've just misplace the codes, but I'll tell, I've checked over and over again, and it seens right.
public void showWritings(){
TextView tv1stSpentReportM = (TextView) findViewById(R.id.tv1stSpentReportM);
tv1stSpentReportM.setText(vetorGastos[9].getTipoGasto());
TextView tv2stSpentReportM = (TextView) findViewById(R.id.tv2stSpentReportM);
tv2stSpentReportM.setText(vetorGastos[8].getTipoGasto());
TextView tv3stSpentReportM = (TextView) findViewById(R.id.tv3stSpentReportM);
tv3stSpentReportM.setText(vetorGastos[7].getTipoGasto());
TextView tv4stSpentReportM = (TextView) findViewById(R.id.tv4stSpentReportM);
tv4stSpentReportM.setText(vetorGastos[6].getTipoGasto());
TextView tv5stSpentReportM = (TextView) findViewById(R.id.tv5stSpentReportM);
tv5stSpentReportM.setText(vetorGastos[5].getTipoGasto());
TextView tv6stSpentReportM = (TextView) findViewById(R.id.tv6stSpentReportM);
tv6stSpentReportM.setText(vetorGastos[4].getTipoGasto());
TextView tv7stSpentReportM = (TextView) findViewById(R.id.tv7stSpentReportM);
tv7stSpentReportM.setText(vetorGastos[3].getTipoGasto());
TextView tv8stSpentReportM = (TextView) findViewById(R.id.tv8stSpentReportM);
tv8stSpentReportM.setText(vetorGastos[2].getTipoGasto());
TextView tv9stSpentReportM = (TextView) findViewById(R.id.tv9stSpentReportM);
tv9stSpentReportM.setText(vetorGastos[1].getTipoGasto());
TextView tv10stSpentReportM = (TextView) findViewById(R.id.tv10stSpentReportM);
tv10stSpentReportM.setText(vetorGastos[0].getTipoGasto());
TextView tv1stSpentReportV = (TextView) findViewById(R.id.tv1stSpentReportV);
tv1stSpentReportV.setText(String.valueOf(vetorGastos[9].getValorGasto()));
TextView tv2stSpentReportV = (TextView) findViewById(R.id.tv2stSpentReportV);
tv2stSpentReportV.setText(String.valueOf(vetorGastos[8].getValorGasto()));
TextView tv3stSpentReportV = (TextView) findViewById(R.id.tv3stSpentReportV);
tv3stSpentReportV.setText(String.valueOf(vetorGastos[7].getValorGasto()));
TextView tv4stSpentReportV = (TextView) findViewById(R.id.tv4stSpentReportV);
tv4stSpentReportV.setText(String.valueOf(vetorGastos[6].getValorGasto()));
TextView tv5stSpentReportV = (TextView) findViewById(R.id.tv5stSpentReportV);
tv5stSpentReportV.setText(String.valueOf(vetorGastos[5].getValorGasto()));
TextView tv6stSpentReportV = (TextView) findViewById(R.id.tv6stSpentReportV);
tv6stSpentReportV.setText(String.valueOf(vetorGastos[4].getValorGasto()));
TextView tv7stSpentReportV = (TextView) findViewById(R.id.tv7stSpentReportV);
tv7stSpentReportV.setText(String.valueOf(vetorGastos[3].getValorGasto()));
TextView tv8stSpentReportV = (TextView) findViewById(R.id.tv8stSpentReportV);
tv8stSpentReportV.setText(String.valueOf(vetorGastos[2].getValorGasto()));
TextView tv9stSpentReportV = (TextView) findViewById(R.id.tv9stSpentReportV);
tv9stSpentReportV.setText(String.valueOf(vetorGastos[1].getValorGasto()));
TextView tv10stSpentReportV = (TextView) findViewById(R.id.tv10stSpentReportV);
tv10stSpentReportV.setText(String.valueOf(vetorGastos[0].getValorGasto()));
}
}
the Medium TextViews has a M in their names, and the little ones has a V.
In the variable and in the id.
The tv2stSpentReportM is showing the text of tv2stSpentReportV and tv2stSpentReportV the text of tv2stSpentReportV.
To make things more clear, here is the code of my "Gastos.class", This one is what I have in the array vetorGastos[].
public class Gastos {
private String tipoGasto;
private double valorGasto;
public String getTipoGasto() {
return tipoGasto;
}
public void setTipoGasto(String tipoGasto) {
this.tipoGasto = tipoGasto;
}
public double getValorGasto() {
return valorGasto;
}
public void setValorGasto(double valorGasto) {
this.valorGasto = valorGasto;
}
}
As I've told you before, and no one believe me, here as printscreen showing you exacly what I've told. The id's are correct. So what now? And disbelievers, thank you for removing reputation points just cause you don't believe. Expand the Image so you could see that the id's are correct.
I already solve this one now, just rewrite ONE of the id's, and it get right. So if it was just a misplaced id, i should rewrite two id's. I don't know why. but it solve the problem.
I've already solve this one now.
I don't know why, but just rewrite ONE of the id's solved the problem.
If it were a misplaced id, I should change two ID's not rewrite one, the same writing.

Android, change fonts on a final text view

I want to change the font on a text view
But the tv is modified at the pression of a button, and i define "final" the tv, and the app crashed...
what i can do it?
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/HeroQuest.ttf");
final TextView tv = (TextView) findViewById(R.id.textView1);
tv.setTypeface(tf);
This code work if tv is not final
try
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/HeroQuest.ttf");
TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setTypeface(tf);
final TextView tv = tv1;
....
Define your TextView as field
i.e
private TextView tv;
if u need more clarification on crash.paste the code in your OnClicklistener

Multiple fonts for multiple languages

I have recently come across a situation while developing an app where i have to display different languages in text view . currently I am displaying few using fonts/typeface like this :
Typeface tf = Typeface.createFromAsset(this.getAssets(),
"DroidHindi.ttf");
TextView textView1 = (TextView) findViewById(R.id.textView2);
textView1.setTypeface(tf);
textView1.setText("कचजड, कचजड");
Typeface tf1 = Typeface.createFromAsset(this.getAssets(),
"asunaskh.ttf");
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setTypeface(tf1);
textView.setText("یہ انگریزی نہیں");
Typeface tf2 = Typeface.createFromAsset(this.getAssets(),
"Banglafont.ttf");
TextView textView2 = (TextView) findViewById(R.id.textView3);
textView2.setTypeface(tf2);// এই ইংরেজি নয়
textView2.setText("এই ইংরেজি নয়");
its fine my question is that i have to support for some 20 different languages then things will become very tedious when i apply this in different activities . Any alternative way to achieve.
You do is to create a class and a function with access public that will return you TextView Object with the Font that you want to suppose.
TextView public SetLanguage(TextView tv,string type)
{
TextView newtv = tv;
Typeface tf;
switch(type)
{
case "urdu":
tf = Typeface.createFromAsset(this.getAssets(),"urdu.ttf");
break;
case "hindi":
tf = Typeface.createFromAsset(this.getAssets(),"hindi.ttf");
break;
// up so on
}
newtv.setTypeface(tf);
return newtv;
}
// and call it any where..
TextView textView1 = (TextView) findViewById(R.id.textView2);
textView1 = classobj.SetLanguage(textView1,"urdu");
//assign string of text to it
Initialize your typefaces when the app starts up, and make a method which takes any view and sets the typeface based on the language.
Just like we do with other resource files, we can also define a font directory for any locale or country code we want to. The downside to this approach is that both the fonts have to be named the same although they are different but otherwise a clean solution.

Categories

Resources