I am trying to learn android development in Eclipse and I am stuck. I created a button with using this.
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="bla"
android:layout_gravity="center"
android:textSize="15dp"
android:id="#+id/bla"
/>
And my listener is the following.
button1=(Button) findViewById(R.id.bla);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
What I want is the following. When I click my button I want 3 different textfields to be seen. After I enter some values to that textfields, I want to get back to the initial screen which contains button1. How can I do that? Also with which method I can store the values that are written to this textfields?
Edit:All answers were helpful and I upvoted them but I accepted Diego's answer because of the clarity
If you want the EditText appear after you click a Button you can include all of them in the same XML layout file, set the EditText to INVISIBLE (android:visibility="invisible") and change it to VISIBLE in the OnClick listener.
To create EditText call this on OnButton Click
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout2);
EditText tv;
List<EditText> allEds = new ArrayList<EditText>();
for (int i = 0; i < 5; i++) {
tv = new EditText(this);
tv.setText("Dynamic TextView" + i);
tv.setId(i + 5);
ll.addView(tv);
}
And get inserted values from EditText check this
String[] strings = new String[allEds.size()];
for (int i = 0; i < allEds.size(); i++) {
strings[i] = allEds.get(i).getText().toString();
}
Initially create 3 edit text's in your xml file and place them as invisible like android:visibility="invisible" for each and every edit text,
now in our java file when button is clicked visible them. like buttonid.setVisibility(View.VISIBLE);
Related
Suppose I have a Table layout and i have a button called 'add people' which dynamically adds a row with two EdiTexts in it each time I click it. now there is another button which is supposed to save the first editText values in database and Add(sum) all the values of second editText .in HTML is very straight forward I can simply add class attribute to both inputs and loop through each class name. but I have no idea how i am supposed to do it in android.
What about saving the references in two lists? Then you can simply iterate over the lists to either store the values in a db or to sum them.
original answer check
public void init(){
TableLayout tablelayout = (TableLayout) findViewById(R.id.displayLinear);
for (int i = 0; i <2; i++) {
//Dynamic Button
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}
}
I'm programatically creating a series of radio buttons in a radiogroup:
for (Soldier soldier:clickedSquad.getMembers()) {
Integer I=0;
soldier.setId(I);
RadioButton radiobutton=new RadioButton(getContext());
radiobutton.setText(soldier.toString());
radiobutton.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
radiobutton.setId(soldier.getId());
I++;
soldierRgrp.addView(radiobutton);
}
It creates the radiobuttons as I intend, but when I click several they all stay clicked like a checkbox, and I need only one to be clicked at a time like radiobuttons usually do.
Any idea why this is happening?
The radiogroup is in the XML and looks as follows:
<RadioGroup
android:layout_margin="10dp"
android:id="#+id/reg_rgrp_soldiers"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioGroup>
RadioButtons behave that way only if they same id. In your case they do! You are initialising your variable I=0 each time. Hence, each soldier is given the same id. Soldiers don't particularly like that! Change you code to:
int i=0;
for (Soldier soldier:clickedSquad.getMembers()) {
soldier.setId(i++);
RadioButton radiobutton=new RadioButton(getContext());
radiobutton.setText(soldier.toString());
radiobutton.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
radiobutton.setId(soldier.getId());
soldierRgrp.addView(radiobutton);
}
I am creating many button from JSON using java code
Relativelayout rl = (Relativelayout) findById(R.id.layout_productos);
...
JSONArray jsonArray = new JSONArray(tmp.getString("productos"));
Button bt[] = new Button[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i ++){
final float scale = getResources().getDisplayMetrics().density;
int padding_40dp = (int) (40 * scale + 0.5f);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, padding_40dp);
bt[i] = new Button(DetalleServicioActivity.this);
bt[i].setText(jsonArray.getJSONObject(i).getString("nombre"));
bt[i].setTag(jsonArray.getJSONObject(i).getString("id_producto"));
bt[i].setTextColor(Color.parseColor("#FFFFFF"));
bt[i].setBackgroundColor(Color.parseColor("#D8D8D8"));
bt[i].setLayoutParams(params);
bt[i].setEnabled(false);
bt[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
rl.addView(bt[i]);
}
inside Relativelayout
<RelativeLayout
android:id="#+id/layout_productos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
</RelativeLayout>
The problem is when is run the app, the buttons are created one upon another one.
But I need that they are located one next to the other and when the screen of a line jump approaches the edge to continue
How can I do that??
You need to add rule in order to have them aligned next to eachother. Rules are up to you.
params.addRule(RelativeLayout.RIGHT_OF, previousViewId);
I have an Activity with two layouts, both implemented in R.layout.main. The first one is a Relative Layout with the app's main screen, and the other is a Table Layout, holding a kind of Preferences Screen. Normally, the first one is set to visible, and the second one to gone. By clicking a button I make the Relative Layout gone, and the Table Layout visible.
And here starts my problem, I wanted to set a OnClickListener to that Table Layout (which is actually an array of buttons).
I tried something like:
final TableLayout table = (TableLayout)findViewById(R.id.tab);
table.setOnClickListener(new OnClickListener(){
public void onClick(View arg){
Button clickedButton = (Button)arg;
String t = (String) clickedButton.getTag();
Toast toast = Toast.makeText(getApplicationContext(),t,Toast.LENGTH_SHORT);
toast.show();
}
});
Obviously, it doesn't work.
I'm quite new to Android programming, and I've been looking for a suitable solution for the whole day without any results.
It couldn't work because you are first trying to cast a TableLayout to a button...
if your TableLayout is only containing buttons you could do something like:
TableLayout yourRootLayout = findView....
int count = yourRootLayout.getChildCount();
for(int i = 0; i < count; i++){
View v = yourRootLayout.getChildAt(i);
if(v instanceof TableRow){
TableRow row = (TableRow)v;
int rowCount = row.getChildCount();
for (int r = 0; r < rowCount; r++){
View v2 = row.getChildAt(r);
if (v2 instanceof Button){
Button b = (Button)v2;
b.setOnClickListener(this);
}
}
}
}
and let your activity implement OnClickListener. Just copy your Existing onClick into Activity itself...
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.