Create certain number of EditTexts based on User Input in Android - android

How would I display a certain number of EditTexts on an Android layout based on user input? For example, I am creating a simple GPA Calculator app, and I need the multiple EditTexts based on however many classes the user is taking. I want to make the range from 1 to 6 classes. Would the easiest way be to create 6 EditText fields and only display however many the user needs when he or she specifies, or is there a better way to do this?
Thank you!

You can create the EditText programatically.
btnClick.setOnClickListener(new OnClickListener(){
//loop based on classes needed
EditText myEditText = new EditText(context); // Pass it an Activity or Context
myEditText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
myLayout.addView(myEditText);
});
Check this out.

// Try this way,hope this will help you...
**activity_main.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edtNoCreate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter no EditText wan create"
android:inputType="number"/>
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<LinearLayout
android:id="#+id/lnrDynamicEditTextHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
**MainActivity.java**
public class MainActivity extends Activity{
private LinearLayout lnrDynamicEditTextHolder;
private EditText edtNoCreate;
private Button btnCreate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lnrDynamicEditTextHolder = (LinearLayout) findViewById(R.id.lnrDynamicEditTextHolder);
edtNoCreate = (EditText) findViewById(R.id.edtNoCreate);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(edtNoCreate.getText().toString().length()>0) {
try {
lnrDynamicEditTextHolder.removeAllViews();
} catch (Throwable e) {
e.printStackTrace();
}
int length = Integer.parseInt(edtNoCreate.getText().toString());
for (int i=0;i<length;i++){
EditText editText = new EditText(MainActivity.this);
editText.setId(i+1);
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setHint("EditText "+(i+1));
lnrDynamicEditTextHolder.addView(editText);
}
}
}
});
}
}

for(int i=0;i<3;++i)
{ LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
EditText edttext= new EditText(this);
edttext.setId(i);
edttext.setLayoutParams(params);
layout.addView(edttext);}
}

Related

Adding views programatically to a relative layout within a scrollview

Please bear with me as I am new to the use of Views and Layouts.
I am trying to create an application in which the user can enter a text field, press a button and have a new text field appear and they can continue adding text fields in this way.
My solution was to have the top level be a scrollview and then have a relative view as a child within that(this way I can then programatically insert more editviews in my code with the OnClick() listener.
I have seen and read a couple of other posts pertaining to relative views but it seems there is still something that I am missing. I have tried
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_create_accounts"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nic.mybudget.CreateAccountsActivity">
<RelativeLayout
android:id="#+id/activity_create_accounts_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/activity_name"
android:inputType="textAutoComplete"
android:layout_alignParentTop="true"
android:id="#+id/activity_createAccounts_relativeLayout_activityName"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/activity_createAccounts_relativeLayout_activityName"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_addButton" />
<Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/activity_create_accounts_relativeLayout_activityName_addButton"
android:layout_centerHorizontal="true"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_saveButton" />
</RelativeLayout>
And here is the Code where I try to add new editviews.
public class CreateAccountsActivity extends Activity {
static private final String TAG = "MAIN-Activity";
int numAccounts = 0;
int lastAccountID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_accounts);
final RelativeLayout Relative = (RelativeLayout) findViewById(R.id.activity_create_accounts_relativeLayout);
final TextView oldAccount = (TextView) findViewById(R.id.activity_createAccounts_relativeLayout_activityName);
final TextView newAccount = new TextView(this);
final Button addNewAccountButton = (Button) findViewById(R.id.activity_create_accounts_relativeLayout_activityName_addButton);
addNewAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "addNewAccountOnClick");
numAccounts = numAccounts+1;
int newAccountID = oldAccount.getId() + numAccounts;
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newAccount.setLayoutParams(rlp);
newAccount.setHint("Hint" );
newAccount.setId(newAccountID);
Relative.addView(newAccount);
RelativeLayout.LayoutParams blp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
blp.addRule(RelativeLayout.BELOW, newAccountID-1);
addNewAccountButton.setLayoutParams(blp);
}
});
}
}
As you can see what I am trying (and failing) to do is add the new edit view at the top of the page and simply push everything else down the page. What am I getting wrong here with the relative layout?
Any help is appreciated.
First thing View with id activity_createAccounts_relativeLayout_activityName is EditText and you are casting it with TextView so that is wrong cast it to EditText.
And to your actual problem:
You are using same EditText instance with variable newAccount and adding it again in relative layout if you want to add one more EditText in relative layout you have to initialise EditText inside onclicklistener.
Just add one line newAccount= new EditText(context)in your onclicklistener code before line numAccounts = numAccounts+1;
Happy Coding !

android How to add button over other button?

I'm trying to add button on other button exactly on same position with same width,and height.
I thought i can do that like these codes,
public class MainActivity extends AppCompatActivity {
Button b1;
LinearLayout root;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = (LinearLayout)findViewById(R.id.activity_main);
b1 = (Button)findViewById(R.id.b1);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ViewGroup.LayoutParams size = new ViewGroup.LayoutParams(b1.getWidth(),b1.getHeight());
int[] locations = new int[2];
b1.getLocationInWindow(locations);
Button a = new Button(this);
a.setLayoutParams(size);
a.setText("new Button");
a.setX(locations[0]);
a.setY(locations[1]);
root.addView(a);
}
}
and XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="semo.msoft.myapplication.MainActivity">
<Button
android:id="#+id/b1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"/>
</LinearLayout>
but, result was like this,
resultImage
I thought it should be correct but it seems be wrong, new button had little bit smaller width and height. Additionally , the position of new button was totally wrong.. I don't know why,, someone who know about this, please help
The following code is working perfectly fine on my machine. I basically just changed the way you are setting LayoutParams:
int[] locations = new int[2];
b1.getLocationInWindow(locations);
Button a = new Button(this);
a.setLayoutParams(b1.getLayoutParams());
a.setX(locations[0]);
a.setY(locations[1]);
a.setText("new Button");
frameLayout.addView(a);
PS: You need to change the root to <FrameLayout>. LinearLayout just isn't made to handle overlapping views.

Android: Adding buttons at runtime

I'm trying to add some buttons in my TableRow, at runtime. My TableRow has this structure:
<TableRow
android:layout_marginTop="100px"
android:gravity="bottom"
android:paddingTop="50px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/riga1">
<Button
android:textSize="32px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonOK2"
android:text="YES"
android:onClick="setResult"/>
</TableRow>
And my java code is:
TableRow tr = (TableRow) findViewById(R.id.riga1);
Button b = new Button(this);
b.setText("button 1");
b.setId(1);
b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tr.addView(b);
Button b1 = new Button(this);
b1.setText("button 2");
b1.setId(2);
b1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tr.addView(b1);
But this seems not to work. In my activity I can see only the button defined in the xml file.
Another problem is that if I set in the related TableLayout the parameter:
android:stretchColumns="*"
I must insert (in xml file) at least one button in the TableRow of the aforementioned TableLayout, otherwise I obtain a "division by zero error" near streatchColumns What can I do, in order to let the tableRow empty?
I think that your problem is that you are using the wrong LayoutParams object for your TextViews try this:
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
and then set this layoutParams object:
b.setLayoutParams(layoutParams);
Instead of table layout you should use relative layout it's work for me try it
In Your XML File Like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myMainRelativeLayout"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="47dp"
android:text="Add Button" />
And your java code like bellow
public class MainActivity extends Activity implements
android.view.View.OnClickListener {
Button btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btnAdd) {
RelativeLayout rl = (RelativeLayout)findViewById(R.id.myMainRelativeLayout);
Button btn = new Button(this);
btn.setId(1234);
btn.setText("Add Button Runtime");
btn.setOnClickListener(this);
rl.addView(btn);
}
if(v.getId() == 1234)
{
Toast.makeText(getApplicationContext(), "This is Your New Button", Toast.LENGTH_LONG).show();
}
}
}
try this one it's really work
first you need to find the button using this statement
btn=(Button)findViewById(R.id.Button1);
than you can set the visibility using this:
btn.setVisibility(View.INVISIBLE);
Then you can set the Visibility using the statement:
btn.setVisibility(View.VISIBLE);

Programmatically adding EditText box

I have the following text with me.
"I drink tea and coffee". Now the requirement is to change this text to "I drink EditText AND EditText"....Here the EditText is a edit box where in the user can enter answers once it is clicked. I need to make this change pro grammatically.
Any suggestions on this, as to how this can be achieved????
You could use the following code in button's click event handler:
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
You can ctreate activity structure in your layout xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="#+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="#+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
And set listners to your buttons
private String[] answers = { "tea", "coffee", "juice", "compote" };
...
Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
firstAnswer.setOnClickListener(new OnClickListener() {
private int position = 0;
#Override
public void onClick(View view) {
((Button) view).setText(answers[position]);
position++;
if (position == answers.length)
position = 0;
}
});
By using getText()
Example
Button mButton;
EditText mEdit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}
after that
mEdit.setText("Tea");
EditText et=(EditText)findViewByID(R.id.yourId);
Button bt=(Button)findViewById(R.id.yourBtId);
bt.setOnClickListener(
new View.setOnClickListener()
{
public void onClick(View v)
{
et.setText("You Drink EditText");
}
});
Put These code in onCreate() method
Use ViewSwitcher. This widget displays one of two views it contains:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ViewSwitcher>
And then, when button is pressed, switch it and load text from EditText to TextView or vice versa:
editText.setText(textView.getText());
viewswitcher.showNext();
or
textView.setText(editText.getText());
viewswitcher.showPrevious();
Refer to this for details.
You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively.
On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.
Just Use this : yourTextField.setText("..."+yourEditText.getText().toString());

How to create radio button without knowing exactly count?

How to create radio button in radioGroup in file layout without knowing exactly count
Dose anyone has any ideas?
I request you to say thing clearly,but as i got ,you want many number of radio Button,
Take a loop and create dynamic RadioButton add these button to Radio Group
May be this is of any help;
public class RadDemoActivity extends Activity {
private RadioGroup radGroup;
private Button addButton;
private RadioButton radButton;
private static int no;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
no=0;
doInflateItems();
setAddButton();
}
private void doInflateItems() {
radGroup = (RadioGroup) findViewById(R.id.radGroup);
addButton = (Button) findViewById(R.id.addRad);
}
private void setAddButton() {
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
radButton = new RadioButton(getApplicationContext());
radButton.setText("Option " + no++);
radGroup.addView(radButton);
}
});
}
}
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/addRad" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add" />
<ScrollView android:id="#+id/vScroll" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioGroup android:id="#+id/radGroup"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
This code is adding radio buttons from a button click, you can do the same from a loop if you want to add multiple buttons at once. Hope this helps.

Categories

Resources