TextView's show the wrong text - android

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.

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

how change changed textview android

I passed the data from the first actvity (like int VNIMANI and int OBRATNOST). This data set my TextView after I came on second activity. now I need a button. And when I click on the button I need increment +1. But the code doesn't work.
TextView tvPaklic = (TextView) findViewById(R.id.paklic);
String paklic = String.valueOf( + 10+ (VNIMANI+OBRATNOST));
tvPaklic.setText(paklic);
}
int newPaklic = 0;
public void plusPaklic (View v){
newPaklic = newPaklic + 1;
displayPaklicDve (newPaklic);
}
private void displayPaklicDve(int newPaklic) {
TextView tvpaklicdve = (TextView) findViewById(R.id.paklic);
String paklicdve = String.valueOf(newPaklic);
tvpaklicdve.setText(paklicdve);
}
I would really like to suggest you refer this one Read this, will solve your problem
You were used 2 Textview objects with referring one id check that.
TextView tvPaklic = (TextView) findViewById(R.id.paklic);
TextView tvpaklicdve = (TextView) findViewById(R.id.paklic);

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");

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.

Android Programmatically Stroke

I want to draw a black stroke on my text in Android.
I have seen this example:
How do you draw text with a border on a MapView in Android?
Where the solution overrides onDraw() to create the stroke.
The problem is, I'm still relatively starting out in Android, and I have no idea how to transition to using that solution.
In my onCreate I set the text typeface (it's custom):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeatures();
// Set content view and component listeners
setContentView(R.layout.meme_maker);
setListeners();
context = this;
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Impact.ttf");
TextView mmt = (TextView) findViewById(R.id.meme_maker_title);
TextView ttc = (TextView) findViewById(R.id.top_text_canvas);
TextView tbc = (TextView) findViewById(R.id.bottom_text_canvas);
ttc.setTypeface(tf);
tbc.setTypeface(tf);
mmt.setTypeface(tf);
}
And I have an onClickListener where I change the text content of the TextView, based on the user writing the text he/she wants in a TextEntry and clicking a button afterwards.
ImageView ii = (ImageView) findViewById(R.id.insert_image);
ii.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText tt = (EditText) findViewById(R.id.top_text_text);
EditText bt = (EditText) findViewById(R.id.bottom_text_text);
TextView ttc = (TextView) findViewById(R.id.top_text_canvas);
TextView btc = (TextView) findViewById(R.id.bottom_text_canvas);
ttc.setText(tt.getText().toString().toUpperCase());
btc.setText(bt.getText().toString().toUpperCase());
}
});
It's pretty straightforward so far. My question is: how to insert the stroke of the text? Where? Do I need to create a Canvas and Paint objects?
The simplest way to get a shadow for text rendered in a TextView is to set up a style as described in this answer. That requires very little work and sounds like it will work fine in your situation.
Using the technique you link to involves extending an existing View class, overriding onDraw(), and using Canvas.drawText() on the canvas passed to onDraw() to render the text yourself. That can be exactly what you need in some situations, but sounds like overkill for your current situation. If you want to look into it further, the Android dev guide on the subject is a good read.

Categories

Resources