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
Related
Is it possible to find the added TextView to update it with other options.
I am adding by this code new TextView with a Button. Now, I want also set a onClickListerner for the dynamically added TextViews. For this I have to find them with findViewById method. But they aren't createt yet.
Can I make this in a other way and if yes, How?
Button hinzufügenButton = (Button) findViewById(R.id.hinzufügen_Button);
hinzufügenButton.setOnClickListener(new Button.OnClickListener() {
public void onClick (View view){
EditText tischName = (EditText) findViewById(R.id.tisch_name_EditText);
TextView tisch = new TextView(getApplicationContext());
tisch.setText(tischName.getText());
tisch.setAllCaps(true);
tisch.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tisch.setBackgroundColor(Color.parseColor("#9FA8DA"));
tisch.setTextSize(25);
tisch.setId(id);
id++;
LinearLayout tischeAnzeigen = (LinearLayout) findViewById(R.id.tische_LinearLayout);
tischeAnzeigen.addView(tisch);
}
});
Just add the onClickListener as you create your dynamic views
Example:
...
tisch.setId(id);
tisch.setOnClickListener(yourListener);
...
You can use setId to add an id. But you probably don't need to. If you're creating a view dynamically, you have a reference to it when you create it. Just set it on that reference. No need to find it.
I'm developing an android app, but I don't have experience with android and I need some help.
I'm going to simplify my code, here is my problem:
I have declared: TextView test = (TextView) findViewById(R.id.test);
After, i have a setOnClickListener:
test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here i declare: TextView test = (TextView) findViewById(R.id.test2);
When i click test for first time it corresponds to findViewById(R.id.test) like it should, but when I click for second time it still corresponds findViewById(R.id.test).
Any idea why?
Thanks in advance! :)
It's because inside the click listener you're defining a new TextView. It's not the same test variable.
When you do TextView test = (TextView) findViewById(R.id.test2); inside the click listener, a new TextView test variable is defined which is not the same as the one you had defined previously.
If you just want to reassign a value, you should use test = (TextView) findViewById(R.id.test2); inside the onClickListener();
I am usin the CardsUI library taken from here: http://www.androidviews.net/2012/12/cardsui/
and trying to change the textview size for all the cards by pressing a button.
At the moment i haven't used any buttons, just wanted to check if my approach is correct by setting a fixed textsize.
I have created:
MainActivity.java
MyCard androidViewsCard = new MyCard("Title","Description");
androidViewsCard.setSize(20.0f);
MyCard.java
public void setSize(float f) {
View view = LayoutInflater.from(MainActivity.getAppContext()).inflate(R.layout.card_layout, null);
TextView tv = (TextView) view.findViewById(R.id.description);
tv.setTextSize(f);
}
The text does not change size though. Am i missing something?
Just add a unit with it.
You need to use the function setTextSize(unit, size) with unit SP like this,
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, f);//tv is textView
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.
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.