I was wondering how to give a TextField an 'id' in java. So I am pretty much wanting to know how to give the TextField android:id="#id/NAMEHERE" but do it in java. Here is the code so far:
TextView taskname = new TextView(this);
taskname.setText("Task " + tasknumber);
tasklayout.addView(taskname);
What do I need to add to give it an id?
If your question is to how to set id into activity, then understand purpose of id.
Id is specially required when you want to fetch view reference into activity class, where view has been declared into xml file. But in Activity, if you are creating any view, by
TextView taskname = new TextView(this);
here, you already have view reference, but if you still want to set id, then you can use method
taskname.setId(10002);
Try this
static final int TEXT_ID = 80001;
TextView taskname = new TextView(this);
taskname.setId(TEXT_ID);
and later in your code you can refer to this textview by
TextView t = (TextView)findViewById(TEXT_ID);
or
TextView t = taskname;
Related
I'm trying to get a text from a TextView so I tried to use getText() method but didn't work. I'm using fragments only, so there's a way I can get my text from the TextView?
Ps. I tried with getEditText() but I get NULL.
you are probably calling TextView textView = (TextView) findViewById(R.id.yourInFragmentTextView); on your activity layout?
You need to get the reference to the actual view in the fragment.
First you need a reference to the inflated fragment and than call
TextView textView = (TextView) myFragment.findViewById(R.id.yourInFragmentTextView);
String text = textView.getText().toString();
String str = textview.getText().toString(); should work
try clearing cache and restart
I've made a TableRow class and added my own methods to it. In the TableRow is a EditText where i want to retreive values from. I thought about using the getChildAt() for finding the right EditText in the right TableRow. But because i made my own TableRow class i can't use the getChildAt() wich is in the original TableRow class or parents of it.
So how can i get the EditText values from the tablerows? Create my own getChildAt method?(if so, how?) or is there a different way?
Example-
EditText editEmail, editPass;
String name = "", pass = "";
editEmail=(EditText)findViewById(R.id.editEmail);
editPass=(EditText)findViewById(R.id.editPass);
name = editEmail.getText().toString();
pass = editPass.getText().toString();
you can try this. the values now in string variable and you can use these variables wherever you want.
Whenever you create dynamic view you can assign an id to that view as
final int ID=1001;
EditText et=new EditText(this);
et.setId(ID);
and get the view using
findviewbyid(ID) ;
If you are using xml
android:id="#+id/ed1"
In Activity
EditText et=(EditText)findViewById(R.id.ed1);
I was wondering how to refer to an View if I create it programmatically.
I have to create new passenger Views with "Add Passenger" and respectively "Remove Passenger" buttons to my app. The promts are kept LinearLayouts called "#+id/passenger" which have two EditTexts called "#+id/passenger_name" and "#+id/passenger_weight". Those are then kept in a yet another parent LinearLayout called passenger_layout that can hold all the passenger LinearLayouts in a bunch
Adding new passengers is easy, but I have no idea how to refer to the newly created elements. I guess they get a identifier of some sort automatically? I'd prefer them to be "passenger_name%" and "passenger_weight%", where % is an index _passengerCount.
addPassenger.Click += delegate {
//Add to index
++passengerCount;
//Prep new passenger layout
var newLayout = new LinearLayout(Activity);
//Set LayoutParameters from the existing passenger LayoutParameters
newLayout.LayoutParameters = newPassenger.LayoutParameters;
//Prep the new EditTexts
var name = new EditText(Activity);
var weight = new EditText(Activity);
//Set the EditTexts' LayoutParameters from existing LayoutParameters
name.LayoutParameters = new ViewGroup.LayoutParams(passengerName.LayoutParameters);
weight.LayoutParameters = new ViewGroup.LayoutParams(passengerWeight.LayoutParameters);
//These cleary don't work :<
// name.Id = Resources.GetIdentifier( "passenger_name" + passengerCount, "id", Activity.PackageName);
// weight.Id = "passenger_weight" + passengerCount;
//Add EditTexts to the new passenger layout and then and then add the new passenger to the parent LinearLayout
newLayout.AddView(name);
newLayout.AddView(weight);
passengerLayout.AddView(newLayout);
Log.Debug(GetType().FullName, "Add clicked");
};
That is my click delegate to create a new passenger, but again even if I create them like this I don't know how I can find them later if I have to for example remove them or get the name or weight data.
How do I refer to programmatically created UI elements?
When you create a view, try giving it some ID, and holding that ID as a static reference somewhere.
Then, you could simply call the containing view's findViewById(MY_VIEWS_ID) and get the view.
Of course, alternatively, you could always hold a reference to the view you created somewhere in your code when you create it. If you're afraid of memory leaks, you could use WeakReference.
Hope this helps.
You could maintain a static variable within the Activity that will contain the UI element after it gets initialized.
Something like:
// Definition
private static TextView textViewToSave = null;
textViewToSave = // create the TextView programatically.
// Do stuff with the saved TextView
textViewToSave.setText("O Hai world!");
I have a list view with a custom adapter that I am attaching and It needs to have a header with some modifiable information in it. I have created an xml layout for the header, and when its applied everything displayes correctly. But when I try to reference the elements in the header view and set there contents, I receive a Resource not found exception. The exception is specifically thrown at "name.setText(task.getName());". I presume it would be thrown at each of the 3 elements I reference If it go that far.
What is the correct way to do this? Are the resources im referencing not in scope? How can I modify them correctly?
The onCreate of my activity containing the list looks like what is below:
super.onCreate(savedInstanceState);
setContentView(R.layout.taskview);
Object sTask = getIntent().getSerializableExtra("task");
TaskNode task = (TaskNode)sTask;
View header = getLayoutInflater().inflate(R.layout.header, null);
TextView name = (TextView) header.findViewById(R.id.hname);
TextView description = (TextView) header.findViewById(R.id.hdescription);
TextView completion = (TextView) header.findViewById(R.id.hcompletion);
name.setText(task.getName());
description.setText(task.getDescription());
completion.setText(task.completion());
taskList = (ListView) findViewById(R.id.taskList);
taskList.addHeaderView(header);
TaskViewListItem adapter = new TaskViewListItem(this, getApplicationContext(), task);
taskList.setAdapter(adapter);
From the exception you get one from getName(), getDescription() and completation() is returning a numeric value. When you call setText with an int value android will lookup inside R in order to find the String with the id you provide as parameter to setText. If a String with that id does not exist the android.content.res.Resources$NotFoundException exception will be thrown. You can get the String representation of a number with:
String value = String.valueOf(numericValue);
I have multiple TextViews . When I click on the TextView I would like to grab the associated object. I know there are ListAdapters for list views and other collection type views. Is there a way to dynamically associate a custom object with a TextView?
Yes you can attach. Here are apis from the View since TextView is extended from View you can use them.
setTag(Object tag)
setTag(int key, Object tag)
How to use:
class YourCustomData {
public int data;
}
// in your onCreate
TextView tv = (TextView)findViewById(R.id.your_text_view);
tv.setTag(new YourCustomData());
-
// say somewhere you have a handler/listener for text view
// Note: you have to write the code to get the textview
YourCustomData ycd = (YourCustomData)tv.getTag();
Android Developers Reference:
setTag(java.lang.Object)
getTag()
You can also use a HashMap<View, Object>
http://developer.android.com/reference/java/util/HashMap.html