This is not a problem, but more of an efficiency question. I have multiple TextViews (2 of them) in my XML Layout of my Android App. My question is that can I select multiple TextViews, findViewById multiple TextViews on a single line?
Is this valid for my question?
TextView title, darkThemeTitle = findViewById(R.id.title); findViewById(R.id.darkThemeTitle);
When you use TextView title, darkThemeTitle = findViewById(R.id.title); findViewById(R.id.darkThemeTitle); in your code .
This line TextView title, darkThemeTitle = (TextView) findViewById(R.id.title); will show that Variable 'title' might not have been initialized .So title never initialized in the code .
And findViewById(R.id.tab_layout); will return View in your code .And it never return darkThemeTitle in your code .
And you can do like this .
TextView title = (TextView) findViewById(R.id.title); TextView darkThemeTitle = (TextView) findViewById(R.id.darkThemeTitle);
Another way
TextView title = null, darkThemeTitle = null;
TextView[] textViews = {title, darkThemeTitle};
Integer[] ids = {R.id.title, R.id.darkThemeTitle};
for (int i = 0; i < textViews.length; i++) {
textViews[i] = (TextView) findViewById(ids[i]);
}
The only recommendation is to use template ids to find views:
TextView[] themedViews = new int[NUMBER_OF_THEMES];
for (int k = 0; k < NUMBER_OF_THEMES; k++)
themedViews[k] = findViewById(context.getResources().getIdentifier("some_prefix" + String.valueOf(k), "id", packageName));
This will find all views for current activity.
Or you can use parent.findViewById to find subviews of a specified view.
I discourage you from doing this because it's more difficult for other programmers to read and doesn't save you much time typing. Use:
TextView title = findViewById(R.id.title), darkThemeTitle = findViewById(R.id.darkThemeTitle);
Have you tried using ButterKnife? This library help you with Dependency Injection so you don't need to worry about the findViewById. Just call the #BindView(view_id) and the type and name of the variable that you want to bind.
#BindView(R.id.title)
TextView title;
#BindView(R.id.darkThemeTitle)
TextView darkThemeTitle;
Remember that you need to add the dependency in your build.gradle file
compile 'com.jakewharton:butterknife:8.8.1'
And call the bind method in the onCreate of your activity
ButterKnife.bind(this);
Related
How to create a TextView with a long text so that the text in this TextView is automatically reviewed and displayed from end to end like horizontal slides?
In fact, I want textview to scroll automatically and scroll again from the beginning after the text is finished.
The image is the output of my code, but I do not want this.
enter image description here
make ellipsize=marquee and singleLine=true within textView
I found the answer!
You must add these features to TextView :
android:ellipsize = "marquee"
android:fadingEdge = "horizontal"
android:marqueeRepeatLimit = "marquee_forever"
android:scrollHorizontally = "true"
android:singleLine = "true"
then:
TextView txt = findViewById(R.id.text);
txt.setSelected(true);
When i display one textview it works, but when i display the second, the first textview disappears. Please help.
Here is my code
Intent intent = getIntent();
String[] data = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
TextView name = new TextView(this);
name.setTextSize(25);
name.setText(data[0]+"\n");
name.setText("\n"+data[1]);
// Show text view
setContentView(name);
/* AlertDialog dialog = new AlertDialog.Builder(DisplayMessageActivity.this).create();
dialog.setTitle(name);
dialog.setMessage(message);
*/
// Show the Up button in the action bar.
setupActionBar();
Its overwrite the value on TextView.
So Use:
name.setText(data[0]+"\n"+data[1]);
Instead Of:
name.setText(data[0]+"\n");
name.setText("\n"+data[1]);
U can also use :
String dataStr="";
for(int i=0;i<data.length();i++)
{
dataStr=dataStr+"\n"+data[i];
}
name.setText(dataStr);
you must set text in one step
name.setText(data[0]+"\n"+data[1])
You are not appending text to your textview. You are actually replacing the text in textview. Before you do name.setText(); just add name.getText().toString();
For example: name.setText(name.getText().toString()+" My New data ");
Though it would be a good practice if you can use a StringBuilder for this. But above code should do the trick.
If you need to display whole array to textview,Just convert the array to string and set as text
name .setText(Arrays.toString(array));
This question already has answers here:
How to get string from ListView?
(3 answers)
Closed 9 years ago.
I'm trying to get the string from my listView item that I click.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
I have tried accessing it using:
TextView text = (TextView) arg1.findViewById(R.id.text1);
The id of the textView of android.R.layout.simple_list_item_1 is android:id="#android:id/text1" but according to Eclipse text1 cannot be resolved or is not a field
I'm sure I'm making a mistake accessing the default android textView in the simple list item, but I'm not sure how to access it. Any ideas?
ArrayAdapter has getItem(int position), you can rely on it to retrieve the string your a looking for. How did you get arg1?
This ended up working as the solution for me. Thanks for giving me the though user2433059
TextView textView = (TextView) arg1.findViewById(android.R.id.text1);
String text = textView.getText().toString();
text1 cannot be resolved because it is an Android resource and you won't be able to access it through your R variable.
You will fix your issue using:
TextView text = (TextView) arg1.findViewById(android.R.id.text1);
String itemText = text.getText().toString();
I'm attempting to create a few radio buttons and add them a RadioGroup dynamically. When I use the LayoutInflater method of pulling in the xml and adding it to the current view, everything works fine. The correct radio buttons show up.
However when I try to cast the View that LayoutInflater.inflate returned to a RadioButton (so I can setText), I get a force close with a java.lang.ClassCastException.
for (int i = 0; i < options.length(); i++) {
JSONObject option = options.getJSONObject(i);
View option_view = vi.inflate(R.layout.poll_option, radio_group, true);
option_view.setId(i);
RadioButton rb = (RadioButton) option_view.findViewById(i);
rb.setText(option.getString("response"));
}
poll_option.xml:
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:text="RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The problem is you're not getting the views you think you're getting. LayoutInflater.inflate() called with a supplied root view means the view returned to you is THAT root view (not the inflated view). The method in which you are calling it inflates a new RadioButton and attaches it to the Group, but the return value (option_view) is the group itself, not the individual item. Since you need to play with the view before attaching it to the group, I'd recommend code like this (which works):
//I added these for posterity, I'm sure however you get these references is fine
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RadioGroup radio_group = new RadioGroup(this);
//Get the button, rename it, then add it to the group.
for(int i = 0; i < options.length(); i++) {
JSONObject option = options.getJSONObject(i);
RadioButton option_view = (RadioButton)vi.inflate(R.layout.poll_option, null);
option_view.setText(option.getString("response"));
radio_group.addView(button);
}
Editorial Note:
Just my $0.02, for such a simple layout, running this inflation process over and over in a loop may be a bit too much (inflation is expensive). You could easily create the same RadioButton in code, and add it with your LayoutParams, like:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
for (int i = 0; i < options.length(); i++) {
RadioButton option_view = new RadioButton(this);
option_view.setText(option.getString("response"));
radio_group.addView(option_view, params);
}
This code I didn't test, but it should be pretty close :p
Hope that Helps!
You probably want to use findViewById and locate the radio button in the inflated view. Something like:
RadioButton rb = (RadioButton)option_view.findViewById(R.id.yourButtonId);
See http://developer.android.com/reference/android/view/View.html#findViewById(int)
you want to radiobutton.setId(INT)
and then later get it by findViewById() to get the button.
The setID(Int) should be used when you dynamically create the button. You can now access it later with findViewById.
Basically, I have a LinearLayout that holds a random amount of horizontal LinearLayouts, and in each of the horizontal LinearLayouts there's a TextView and an EditText. I want to be able to get the value of each EditText children of the master LinearLayout.
Sorry if it's confusing, I'm no good at explaining things!
Could I just set the same ID for each of the EditTexts then use findViewById, or would that only return the first instance of an EditText?
Thanks,
Alex.
LinearLayout ll = //Your Layout this can be any Linear or Relative layout
//in which you added your spinners at runtime ;
int count = ll.getChildCount();
for(int i =0;i<count;i++)
{
View v = ll.getChildAt(i);
if(v instanceof Spinner)
{
// you got the spinner
EditText s = (EditText) v;
Log.i("Item selected",s.getText().toString());
}
}
findViewById returns only the first view with the given id. You're going to have to traverse the view hierarchy yourself, at least until you get down to each horizontal linear layout. You'll find the methods ViewGroup.getChildCount() and ViewGroup.getChildAt(int) useful for this.
You would need to call findViewById on each of the LinearLayouts. If you do this, you can set the same ID for each EditText.