Display text programmatically for TextView - android

I am looking to set a text programmatically, but for some reason, when I am doing so, no text appears on the screen. I have attached my code below.
LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View lay = inflater.inflate(R.layout.fs, null);
lsc = (TextView) lay.findViewById(R.id.fst);
lsc.setText("XYZ performed on this day.");
Any ideas? "fs" is the xml file which has a textView component, whose id is "fst".

Related

Change textsize of textview with button

I want to implement a feature that allows user to change the textSize of a textView in another view inside the app,
So I have a button with its "onClick" property set to:
Class mainActivity
public void increaseFont(View view)
{
MainViewPager.changeTextViewTextSize(mTextSize);
}
Class MainViewPager
static public void changeTextViewTextSize(int aTextSize)
{
View detailView = (View) LayoutInflater.from(mContext).inflate(R.layout.details, null);
TextView description = (TextView) detailView.findViewById(R.id.story_description);
description.setTextSize(aTextSize);
}
QUESTIONS is the textSize can't be changed when clicking the button. So how to?
The text size can changed at run time of course. You issue is related to the method changeTextViewTextSize. Using the inflater you are creating a new instance of R.layout.details, and through it, you are looking for the TextView you want to change the text size. But that layout is not at screen. It is not what you are seeing.

How to change the text attribute of widget during run-time, android?

Hi I'm trying to create the layout dynamically based on my data model. But I have some problem doing that. The xml file I have is here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#f9f9ff">
<TextView
android:layout_width="97dp"
android:layout_height="33dp"
android:id="#+id/textView" android:layout_alignParentLeft="true" android:layout_marginLeft="15dp"
android:layout_alignParentTop="true" android:layout_marginTop="19dp"/>
</RelativeLayout>
This is named as dynamic.xml. And I'm trying to inflate this layout at run-time. For that I'm doing:
ViewGroup parent = (ViewGroup) findViewById(R.id.vertical_container);
//getting the TextView and EditText view.
LayoutInflater inflater = this.getLayoutInflater();
View currentView = inflater.inflate(R.layout.dynamic,null);
TextView textView = (TextView) currentView.findViewById(R.id.textView);
//trying to change the attributes. but #fails!
textView.setText("Setting the text from code");
textView.setId(3);
//setting to the view
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.dynamic, null);
parent.addView(view);
but this doesn't seems to be working! The line :
textView.setText("Setting the text from code");
doesn't work. I can see only the empty text in my emulator but not the text Setting the text from code. Not sure where I'm making the mistake. The changes that I made in my code are not seen in my emulator.
I can see only the dynamic xml layout rendered with its attributes specified!
Not sure where I'm making the mistake. Is it possible to achieve what I'm doing here? Thanks in advance.
I think you are setting wrong view for getting result .No need of twice inflation of layout
//getting the TextView and EditText view.
LayoutInflater inflater = this.getLayoutInflater();
View currentView = inflater.inflate(R.layout.dynamic,null);
TextView textView = (TextView) currentView.findViewById(R.id.textView);
//trying to change the attributes. but #fails!
textView.setText("Setting the text from code");
textView.setId(3);
//Now you had customize your Textview .Now just add this customize view to your view
//setting to the view
parent.addView(currentView);
You are currently adding the
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.dynamic, null);
to
parent.addView(view);
But you use textView.setText("Setting the text from code");
from this
View currentView = inflater.inflate(R.layout.dynamic,null);
You are not adding the currentView to the parent
Try this parent.addView(currentView);

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.

Android: 2 toasts same time

Ok, I have 1 custom toast (xml layout) and it works great:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout));
ImageView image = (ImageView) layout.findViewById(R.id.logo);
image.setImageResource(R.drawable.logo);
title = (TextView) layout.findViewById(R.id.title);
txt = (TextView) layout.findViewById(R.id.text);
toast = new Toast(appContext);
toast.setGravity(Gravity.FILL_HORIZONTAL|Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
But when I try to make 2nd one same way I get error "Source not found" which doesn't really tell me anything about what's wrong.
LayoutInflater infl = getLayoutInflater();
View lay = infl.inflate(R.layout.toast_arrows, (ViewGroup) findViewById(R.id.toast_lay));
toastarrows = new Toast(appContext);
toastarrows.setGravity(Gravity.FILL_HORIZONTAL|Gravity.CENTER, 0, 0);
toastarrows.setDuration(Toast.LENGTH_SHORT);
toastarrows.setView(lay);
toastarrows.show();
I'd like those 2 toasts to appear almost same time in different places of the screen.
Anyone can tell me please what's wrong with this code?
you are sure that you can show 2 Toast at the same time?
i'm not sure about this, i tried it but i can display only one Toast.
you have tried to show only the second one?
It seems that if you do really create two toasts at the sametime they will still be shown one after another in the same place. So I think your stuggling's vain.

Categories

Resources