How to dynamically add edit text boxes when button is clicked? - android

How would I implement a button such that a new TextBox is added dynamically on each click?

If you only, and always want to add two Edit Text widgets to your activity when the button is pressed you can do something like this (pseudo code). This assumes that you never want to have more than two edit text components beside your button.
<LinearLayout orientation="horizontal">
<Button >
<EditText id="#+id/et1" visibiltiy="gone" />
<EditText id="#+id/ed2" visibiltiy="gone" />
</LinearLayout>
in your button's onClick listener you can change the components visibility to visible by calling
findViewbyId(R.id.et1).setVisibility(Visible)
findViewbyId(R.id.et2).setVisibility(Visible)

This is my earlier post.
You need to use EditText instead of TextView.
Hope this helps for you.

You should have something like this:
Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText t = new EditText(myContext);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
root.addView(t);
}
});
root: is the root layout where you want to add the EditText.
myContext: could be the activity, etc, etc.
Hope this helps!!

Related

How to Implement EditText in ListView like a Tree view in Android

I am working with android development and want to implement edittext in listview such like that in picture. now want to add this kind of implementation. is there any way to do exactly this thing or any library which is doing this work.any kind of help will be appreciated and thanks an advance.
It's pretty simple. I recommend you not to use listview to implement that. Just use LinearLayout with vertical orientation, and add each field when the "Add More" button item clicked.
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parent_layout); //make sure you have set vertical orientation attribute on your xml
TextView addMoreText = new TextView(context);
addMoreText.setText("Add More");
addMoreText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText editTextItem = new EditText(context);
parentLayout.addView(editTextItem, 0);
}
});
parentLayout.addView(addMoreText);
Note that the code above is just global picture of how to solve your problem. For implementation with close or add button on the right side, you may create custom view that holds EditText, ImageView based your own.

How to add multiple TextViews programmatically to a RelativeLayout

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.

RadioButton with more than text

I'm trying to create a RadioGroup with RadioButtons that have more than text only.
The image below will make things easier to understand.
I want the RadioButton to be selected when the user clicks somewhere in the layout around it.
I've tried do do somthing lik this:
<RadioGroup>
<RelativeLayout>
<RadioButton>
<...other stuff>
</RelativeLayout>
</RadioGroup>
But that didn't work.
Does somebody know how to easily get the requested layout and behaviour? Thanks
EDIT:
I forgot to mention that I don't want to use a list. I want all items to be visible because there are maximum three. Also, the container is a ScrollView
As Der Golem said you can use ListView and set it to be in a single choice mode: list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
But if you need only few layouts you can register Click listener to your layouts and call onClick events on RadioButtons:
bigLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
radioButton.performClick();
}
});
Hope it will help

How can I change the name of buttons on click of a button?

In my calculator app, I want an Inverse button, which when clicked changes the text of other buttons. Like sin to sin inverse etc. Is this possible?
You can just change the text of a button in that button onclick event.
Say For example
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(btn.getText().toString().trim().equals("sin")){
btn.setText("sin inverse");
}else if(btn.getText().toString().trim().equals("sin inverse")){
btn.setText("sin");
}
}
});
I think this will help you
While the other answers are completely right showing you to how rename the buttons, I suggest another solution with a cleaner design: Have different buttons for "sin" and "sin inverse", and just make them visible/invisible when clicking the "Inverse" button. That way you can write clean click handlers and don't have to use a lot of "if (isInverseMode()...)".
To make that work correctly, you just declare some additional buttons for the inverse operations in your XML layout file and set them to android:visibility="gone".
If you then set one the visible buttons to invisible and the next insivible button besides it to visible in the code, then the effect for the user looks like you exchanged one button by the other (so he only notices the text of the button changing).
It's possible.
Just re-set the button text in your onClick method for the button.
buttonId.setText("Your button text");
From what you're saying though it sounds like you want to change the buttons function as well as the text... in which case you need to put an if statement in your onClick method to handle the two button states.
Setting the text on a button would help anytime.
This code may help you..
final Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setText(new StringBuilder(btn.getText().toString().trim()).reverse());
}
});

Robotium: how to use clickOnButton(<name>) for custom buttons

I am new to Android robotium. I am having custom widgets(MyButton, MyTextView, MyCheckBox etc..,) which got inherited from native android widgets. How can i add a click event for my custom controls in a robotium script?
I tried using Solo.clickOnButton("Test Button") where the "Test Button" is an instance of MyButton, but i am not getting click event for the Button. Any suggestions would be really helpful.
Thanks,
-Ron..
I suposse you create the MyButton using extend Button etc etc
Well to asign click action you should use the normal form. For expample
main.xml:
<com.Ron.MyButton
android:id="#+id/custom_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
In your code you can acces to that button
Button myButton = (Button)findViewById(R.id.custom_button);
And then asign onClick action like you do with other normal button:
myButton.setOnclickListener(new onclickListener ....
Other method to asing onClickAction to all views is use int the xml :
<com.Ron.MyButton
android:id="#+id/custom_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="nameOfMethod"/>
And then in your code:
public void nameOfMethod (View v){
//code when click the view
}
(This works with all view, linearlayout, imageviews, bustom button .... all )

Categories

Resources