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!
Related
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);
I've been looking around how to create UI elements purely by code
How can I do this?
RelativeLayout mainLayout = FindViewById<RelativeLayout>(Resource.Id.MainRelativeLayout);
TextView newText = new TextView(this);
newText.Text = "HEY THERE NEW TEXT MADE VIA CODE";
mainLayout.AddView(newText);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Turns out that you need to place your code after setting the content view
/ Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
RelativeLayout mainLayout = FindViewById<RelativeLayout>(Resource.Id.MainRelativeLayout);
TextView newText = new TextView(this);
newText.Text = "HEY THERE NEW TEXT MADE VIA CODE";
mainLayout.AddView(newText);
oh and you do need to specify the type under FindViewById< TYPE >
So in my application i have a linear layout, to which i'm adding programmatically some CardViews (android L cardview) like this :
//This is my LinearLayout
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
//Here i create my CardView from a prepared xml layout and inflate it to the LinearLayout
View card = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
//Now i change the 'text' value of the Card's text views
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title1");
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description1");
//...
//Now i do the same thing for another card
View card2 = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
TextView cardTitle2 = (TextView) card2.findViewById(R.id.text_card_title);
cardTitle2.setText("Title2");
TextView cardDecription2 = (TextView) card2.findViewById(R.id.text_card_description);
cardDecription2.setText("Description2");
//...
The two cards are displayed properly, but what happens is than the first card displayed has "Title2" and "Description2" written in the textViews, while the second card has the default values defined in the xml.
It seems to me that by calling card.findViewById() or card2.findViewById() i get always the TextView of the first card.
So my question is : how do i manage to differentiate the cards i create programmatically and then correclty access the view within them?
Try this way,hope this will help you to solve your problem.
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
for (int i=1;i<=2;i++){
View card = View.inflate(getApplicationContext(), R.layout.account_card, null);
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title"+i);
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description"+i);
card.setTag(i);
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Toast.makeText(context,pos,Toast.LENGTH_SHORT).show();
}
});
myLayout.addView(card);
}
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");
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.