Change the text of textview based on its ID - android

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

Related

TextView auto create new line when end of the line and edittex not align with textview

I have 4 TextView: " what is your name?", "in some circumstances" ,you might in some circumstances,you might", "________" and " to help user understand why your app needs a permission.For example " and a Edittext.
Now I want create a layout and when I user layout add view in my class ( not file.xml ) it can show same :
I used custom layout same here : Custom RowLayout and add view in code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main22);
int permissionCheck =
rowLayout = (RowLayout) findViewById(R.id.row);
TextView tv1 = new TextView(this);
tv1.setText("what is your name?");
rowLayout.addView(tv1);
EditText ed1 = new EditText(this);
ed1.setWidth(50);
rowLayout.addView(ed1);
TextView tv2 = new TextView(this);
tv2.setText(" in some circumstances ,you might in some circumstances,you might");
rowLayout.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText("_____");
rowLayout.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setText("to help user understand why your app needs a permission.For example");
rowLayout.addView(tv4);
<convert.htv.ex.convertdata.RowLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"></convert.htv.ex.convertdata.RowLayout>
and result
You can see two problems in here:
1. Edittext not in line with text view.
2. when string text length it create a new line. I want a part off textview will show in old line and when end of the line it will show the rest of textview in newline ( sample picture 1).
How I can do it? please help me.
1)Answer for first problem,
add this line of code
tv1.setGravity(Gravity.CENTER)
2) Change this
ed1.setWidth(50);
to
ed1.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

How can i access a Text view and print

I need to access a text view which is present inside Linear layout and print the text using gettext()?
Layout looks like:
Linearlayout
Linearlayout
Imageview
Relativelayout
Textview
The layouts around your TextView wouldn't matter unless you are playing with their Visibility.
The Activity in which you are accessing your TextView needs its call to:
setContentView(R.layout.yourLayout);
use textView= (TextView) findViewById(R.id.textViewId);
to map your textView and then can
textView.getText()
http://developer.android.com/guide/topics/resources/layout-resource.html
http://www.intertech.com/Blog/android-layout-and-id-attribute/
Create a linearLayoutObject for first layout and then create another object for TextView.
UiObject lLayout = new UiObject(new UiSelector().className(LinearLayout.class.getName()).index(1));
UiObject tView = lLayout.getChild(new UiSelector().className(TextView.class.getName()).index(1));
String tDetails = tView.getText();
Now you can use tDetails for comparison or other purpose.

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.

Updating a textview in different layout xml from the main activity

I've created a new .xml file in my layout folder called log.xml. It only contains one TextView.
Is it possible to set the text on the textview located in the log.xml from my main activity? Or can it only be set when in an activity which uses the log.xml as view? Hope you get what i mean here, otherwise ill elaborate.
Thanks
If you don't set the xml you are talking about on "setContentView()" you can always get it with layout inflater. You'll have to add the tv to the current layout using addView() though.
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.log, null); //log.xml is your file.
TextView tv = (TextView)vi.findViewById(R.id.tv); //get a reference to the textview on the log.xml file.
Unless log.xml is included in the current, visible layout findViewById() will return null.
Since you want to set the TextView's text when you load it in a new activity, you can pass the new String in the Intent used to start your Activity.
In the appropriate onClick() from your First Activity:
Intent intent = new Intent(this, Second.class);
intent.putExtra("myTextViewString", textString);
startActivity(intent);
In your Second Activity's onCreate():
setContentView(R.layout.log);
TextView textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if(extras != null) {
String newText = extras.getString("myTextViewString");
if(newText != null) {
textView.setText(newText);
}
}
Below solution worked for me -
Get View object of layout XML file (ex. toast_loading_data) -
View layout = inflater.inflate(R.layout.toast_loading_data,
(ViewGroup) findViewById(R.id.toast_layout_root));
Get the TextView element from this View (ex. TextView id - toast_text)-
TextView tvToast = (TextView) layout.findViewById(R.id.toast_text);
Set text of the TextView -
tvToast.setText("Loading data for " + strDate + " ...");
Below is snippet for customized Toast message from Main Activity -
View layout = inflater.inflate(R.layout.toast_loading_data,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView tvToast = (TextView) layout.findViewById(R.id.toast_text);
tvToast.setText("Loading data for " + strDate + " ...");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0); //Set toast gravity to bottom
toast.setDuration(Toast.LENGTH_LONG); //Set toast duration
toast.setView(layout); //Set the custom layout to Toast
Hope this helps
I think I understand what you are trying to say. If you do this:
TextView tv = (TextView) findViewById(R.id.textView2);
tv.setText(output);
where textView2 is the id of the text view that you want to set the text of, you can set it to any string value using the setText() function. Hope this helps!

text not changing in a TextView while doing Layout Inflater

in my app when the activity get loaded, i am doing the following
setContentView(R.layout.main2);
layoutToAdd = (LinearLayout) findViewById(R.id.linearLayout1);
for(i=0; i<num;i++)
{
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View view = inflater.inflate(R.layout.camera, null);
layoutToAdd.addView(view);
}
The value of num differs for each time.
In my LayoutInflater layout i have a text view, edit text and a button.
Now they are shown according to the number of times mentioned in num, now for each time i want the text and button name to be changed. How to set the text for TextView and Button.
Just set them after you inflate the layout
View view = inflater.inflate(R.layout.camera, null);
TextView tv = view.findViewById(R.id.textviewid); //id defined in camera.xml
Button b = view.findViewById(R.id.buttonid); //id defined in camera.xml
tv.setText();
b.setText();
layoutToAdd.addView(view);
What I suggest it,you should create an xml file containing just a LinearLayout.
Then programaticall,you should create TextViews,EditTexts and Buttons and add it to LinearLayout.
You can set different properties of those components like below:
Here is example of setting TextView's properties.For rest of the components,you can set the same.
TextView tv=new TextView(context);
tv.setBackgroundResource(R.drawable.textview_bg);
tv.setPadding(20, 5, 40, 5);
tv.setText("set your text");
tv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.RED);
tv.setClickable(true);
tv.setId(id);//where id is an integer which should be unique for each TextView
layout.addView(tv);
Also you need to create and add all these three component inside the for loop,providing unique ids depending on i you use to iterate the loop!And you can have a String array for textviews and buttons to set their names in for loop,which would be easier for you for passing the string you like to set to them in loop.
You must store the reference of the view inflated and store in a List then when you want to modify simply list.get(2).findViewById(R.id.textbox_id).
Hope it help.

Categories

Resources