I want to add text to my dynamically added EditTexts.
As for now its just adding a layout with two EditText in, its getting added to a LinearLayout on button click.
How do I set the text of these Editexts? Because now I can add as many as I like and every EditText has the same id as the have in the layout file.
My mainactivity.java with the inflation upon button clicklooks like this:
final LinearLayout mView = (LinearLayout) getLayoutInflater().inflate(R.layout.more_rest_main, null);
mView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
restLayout.addView(mView);
And here is my layout for more_rest_main.xml
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/added_rest_from"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:hint="#string/time_from"
android:textSize="15dp"
android:focusable="false"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/added_rest_to"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:hint="#string/time_to"
android:textSize="15dp"
android:focusable="false" />
The thing to understand here is that you can look for a child view in any ViewGroup (LinearLayout is a ViewGroup).
You're probably familiar with retrieving a View in an Activity onCreate, where after calling setContentView, you have access to any View with findViewById.
So after inflating your ViewGroup, you can use the same approach you would use in an Activity onCreate, but you need to look for your ViewGroup's child views:
final LinearLayout mView = (LinearLayout) getLayoutInflater().inflate(R.layout.more_rest_main, null);
EditText txtAddedRestFrom = (EditText)mView.findViewById(R.id.added_rest_from);
restLayout.addView(mView);
Once you have your reference you can invoke any method on your EditText, such as setText.
You can create an EditText dynamically like following:
EditText editText = new EditText(myContext);
editText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//add the same logic as in the XML file
mView.addView(editText);
You can also set an ID dynamically. If you are using API 17 and above, do the following:
editText.setId(View.generateViewId())
Otherwise you can set a positive Integer of your choice as an ID, instead of using the:
View.generateViewId()
With the code above and since these will be added dynamically, you don't need the XML file as the EditTexts will be created dynamically.
If you want to keep the existing XML file, you can just call the
setTag()
to each one of the inflated EditTexts and set a unique value in the setTag method. That way you can distinguish the EditTexts. And since you can do that, you can add whatever text you need to each one of them.
To reply to your comment, on how to set an OnCLickListener:
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// here you can get either the Tag or the ID that
// you've set dynamically. And do whatever you like to do
view.getTag()
view.getId()
}
});
Related
How does one programmatically, via one button click at a time, add multiple TextViews to an existing RelativeLayout without the TextViews overlapping onto one another.
I am trying something like this -
The following code exists inside the onCreate() method:
TextView textViewToSeeFirst = (TextView) findViewById(R.id.textView1);
RelativeLayout rLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TextView newTextView = new TextView(TheActivityYouAreUsingActivity.this);
newTextView.setText("text you want");
rLayout1.addView(newTextView, lparams);
}
The TextViews are being added to the RelativeLayout, but they are all on top of one another, how does one fix this?
The goal is to programmatically, via one button click at a time, add multiple TextViews to an existing RelativeLayout, and without the TextViews overlapping onto one another.
Here is what I finally came to, this works but I am unsure if it is the best way to go (or even a good way).
The following code exists inside the onCreate() method:
// Creates a variable to keep track of the amount of TextViews, the
// first TextView, an array, and then stores the TextView in the Array
int numberOfTextViews = 1;
TextView[] textViewArray = new TextView[20];
TextView textViewToSeeFirst = (TextView) findViewById(R.id.textView1);
textViewArray[numberOfTextViews - 1] = textViewToSeeFirst;
// Also need to reference the RelativeLayout we are putting TextViews in
RelativeLayout rLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lparams.addRule(RelativeLayout.RIGHT_OF, textViewArray[numberOfTextViews - 1].getId());
lparams.addRule(RelativeLayout.ALIGN_TOP, textViewArray[numberOfTextViews - 1].getId());
TextView newTextView = new TextView(TheActivityYouAreUsingActivity.this);
newTextView.setText("text you want");
newTextView.setId(numberOfTextViews);
textViewArray[numberOfTextViews] = newTextView;
rLayout1.addView(textViewArray[numberOfTextViews], lparams);
numberOfTextViews = numberOfTextViews + 1;
}
Some things to keep in mind:
the parameters for RelativeLayout.LayoutParams are important, see developer material on these parameters. WRAP_CONTENT was chosen for my needs because it causes the TextViews to only take up the size of their text, rather than their entire parent... Overlapping was occurring before I changed this.
the id of each new TextView must be set if it is to be referenced later on for new layout parameters
the id must be a positive number, and zero is not positive
the RelativeLayout is holding the TextViews and handling them, the textViewArray is just so the ids of each TextView can be stored and referenced if need be
The corresponding XML has this going for it inside the main parent:
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".2" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/die_one" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name_a_button" />
</RelativeLayout>
Notice that the first RelativeLayout, and the TextView both have an id, this is so the addRule() method in the activity can reference them.
This code allows a user to click a button and add new TextViews to a RelativeLayout without them overlapping.
Why dont you add all your text views in your xml file (as much as you want) before running you app. Just set the visibilities to the textviews which you dont want to show initially to "GONE" and then in button click listener just keep changing the visibility of textview to "View.Visible" .
If you want a new textview to appear each time you press a button then set a counter to a button and each time a counter increments you change the desire textview's visibility to View.Visible. If you understood what i am saying you will be able to make the code on your own.
You can use Linear Layout with orientation as vertical instead of Relative Layout. It will align all the textviews vertically one below the other.
I dont consider adding large number of textviews to the xml file a valid solution, as the number of times the user will click a button is unknown.
ANDROID
I have a layout defined in xml and have a static textview, edittext and checkboxes which are all formatted as below:
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="2"
android:singleLine="true"
android:textSize="14dp"
android:width="180dp"
I add textview, edittext and checkbox dynamically. I need the new added ones to have the same visual display as the ones already present(static ones) on the layout! Could someone guide or point me how to go about it?
You can add the layout by inflating each time you need textview, edittext and chekbox.This way your layout will have same look.Because you are reusing your static layout in xml.
See Layout Inflating for details
You have a layout with textview,edittext and checkbox.Now you will use the layout as a view.Like you said for each row you have to inflate the layout and add it to the row.So for every row you will have the copy of the layout.
TableLayout layout=(TableLayout)findViewById(R.id.tblLayout);
Now you can add the view after inflating the layout
View rowView = inflater.inflate(R.layout.row_layout, null, true);
TableRow tblrow=new TableRow(this);
tblRow.add(rowView);
layout.add(tblRow);
You can create any control Dynamic like this way.
Here i show for Edit Text same way you can do for others.
EditText et = new EditText(this);
et.setText("");
et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Use the following code for dynamic configurations about EditText.
EditText my_edit = (EditText) findViewById(R.id.my_edit);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 2.0f;
my_edit.setLayoutParams(params);
my_edit.setTextSize(20);
my_edit.setWidth(180);
my_edit.setSingleLine(true);
For this you must need LinearLayout as a parent of your EditText.
I hope this helps you somehow.
Thanks.
I need to edit an xml file, the problem is that I have multiple xmls and in all my classes/activities I can't put the xml as the main View.
So I must acces an xml file without setting it as main view on any activity.Is that even possible?
I mean I can make: View j = (View) findViewById(R.layout.mytest);
But how can I edit a button's text for ex in that mytest.xml file?
although you can inflate any layout using LayoutInflator and make any changes for that instance in it , but you can not look for a persistent change in layout . so first setContentView , then change for that view only .
So you're saying you want to dynamically edit a layout xml file??? Example, you have a layout like...
<LinearLayout>
...
<Button
android:id="#+id/myButton"
...
android:text="Some text" />
</LinearLayout>
...and you want to dynamically edit android:text="Some text" at run time???
If so, that isn't the way to do it. Simply leave the android:text attribute out of the layout xml and just set the text of the button at runtime.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContenView(R.layout.mytest);
Button btn = (Button) findViewById(R.id.myButton);
btn.setText("Whatever");
}
Hey guys please help me out I am new to android application development
Here is the scenerio: This is my layout declaring xml file:
<LinearLayout xxx
<Textview aaa>
</TextView>
</LinearLayout>
//The below LinearLayout I need to display when it meets some condition in java class
i.e if(true) then display the following layout else dont. I can check this condition only after user provides some input.
<LinearLayout xxx
<Textview aaa>
To be displayed after the condition is checked
</TextView>
</LinearLayout>
//following layout is displayed with the first one.
<LinearLayout xxx
<Textview aaa>
</TextView>
</LinearLayout>
Any idea how to do it?
Take a few moments to read the android dev guide. It is worth the time: http://developer.android.com/guide/topics/ui/index.html
Basically, you want to use IDs to refer to the xml layout:
android:id="#+id/myxmlid"
and in your java file:
LinearLayout ll = findViewById(R.id.myxmlid);
if (yourCondition)
mainLayout.add(ll);
I'm assuming that you want to add a widgets to the current layout, rather than just change the text of the current TextView.
Also, this assumes that you want to add more than just a new TextView. If you only need that, you don't need to wrap it in a LinearLayout, which is used to add rows or columns of widgets.
You don't replace your entire layout programmatically just to change the text in one TextView. The way this kind of thing is handled in android, is to include a field in your Activity class for your textview, then instantiate it in your onCreate() method with findViewById() after you've called setContentView() to load the layout so that you can access that TextView's fields and methods.
First, you TextView needs an id in the layout xml.
<TextView android:id="#+id/sometext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Then in your Activity...
TextView mTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.sometext);
}
Somewhere else in the program...
public void myMethod(){
mTextView.setText("Text says this now");
}
Hopefully that gets the idea across. Good luck!
Thank Aleadam for suggesting me to read the link. Follwoing was my approach to get the output.
What I did was I assigned my LinearLayout Visibility to "GONE" (android:Visibility="GONE") when declarning the XML, and in the program after the condition is met, changed the visibility to "VISIBLE". (by using layout.setVisibility(View.VISIBLE))
I have a linearlayout how can i access view inside this layout with the help of
programming.
Sure, say if you have a LineraLayout linearLayout, and in its xml you have a TextView like
<LinearLayout [...]>
<TextView android:id="#+id/textView" [...] />
</LinearLayout>
then you can access that TextView by
final TextView txt = (TextView)linearLayout.findViewById(R.id.textView);
Here you have your LinearLayout defined in an xml resource file.
You must assign an id attribute to your TextView for that to be accessible from your code directly. For this purpose stands there the android:id="#+id/textView".
In the xml file you will need to give the view an id..
android:id="#+id/someRandomID"
Then within your main java file you add this:
LinearLayout layout = (LinearLayout)findViewById(R.id.someRandomID);
(([TYPE]) findViewById(R.id.[NAME]))
For example, setting the text on a button:
((Button) findViewById(R.id.my_button)).setText("New text");
Assuming you have a linearlayout with id "linear1" and within that layout you have a ImageView with id "image1", you can do the following in your onCreate method in your activity class:
public void onCreate(Bundle bundle)
{
setContentView(R.layout.linear1);
ImageView image = (ImageView) findViewById(R.id.image1);
}
This is a very simple example, assuming that you are setting linear1 as the main layout of your activity.