Create infinite number of EditText on Clicking the button in custom dialog - android

I have to create a edit text one by one below inside custom dialog while clicking the button.
So far I have tried and created one edit text on clicking the button in custom dialog.
MainActivity.java:
Below I am shown the code what I had tried so far:
Edited:
import android.support.v7.app.ActionBarActivity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Button button;
private LinearLayout ll;
EditText et;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.addBtn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainActivity.this);
//setting custom layout to dialog
dialog.setContentView(R.layout.custom_dialog_layout);
dialog.setTitle("Add List");
//adding button click event
Button createEditText = (Button) dialog.findViewById(R.id.button);
createEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// dialog.dismiss();
et= new EditText(MainActivity.this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
dialog.addContentView(et,params);
((LinearLayout) dialog.findViewById(R.id.container)).addView(et);
}
});
dialog.show();
}
});
}
}
custom_dialog_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Next" />
</LinearLayout>
</RelativeLayout>
I don't know how to create infinite number of edittext one by one below.Anyone can help me with this.Thank you.

The way I've done it before is to have some containing layout (linearlayout, usually) and add my views to that container. Rough code:
LinearLayout container = (LinearLayout) findViewById(R.id.container);
and in your onclick listener
EditText edit = new EditText(MainActivity.this);
container.addView(edit);
Edit
Okay, after managing to absolutely miss the point of your question in my initial post, I went and checked out what was going on. What's happening (I think) is that it IS actually adding more edittexts, but they're all on top of each other so it looks like that it's not. So here's what you have to do, which plays into what I wrote above:
In your custom dialog view, add a linearlayout container
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/container"></LinearLayout>
and in your onclick, you add your edit texts to the linearlayout, NOT the dialog view
et= new EditText(TestActivity.this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
((LinearLayout) dialog.findViewById(R.id.container)).addView(et);
and that should work.

Related

Android inflating views from other layouts

I'm relatively new to android development and I'm trying to find a way to inflate a view repeatedly each time when a button is pressed, in a different location, so every inflated view has its own position:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class teamCreateScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_locate_layout);
}
public void createTeam(View view) {
final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam);
View teamBox = View.inflate(this, R.layout.team_box, rlTeam);
final TextView teamBoxView = (TextView) findViewById(R.id.team_task_box);
teamBoxView.setX(0);
teamBoxView.setY(230);
}
}
The XML code of the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rlTeam">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/teamAddBtn"
android:text="+"
android:textSize="30sp"
android:onClick="createTeam"/>
</RelativeLayout>
XML code of the view that's being inflated:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="192dp"
android:layout_height="120dp"
android:id="#+id/team_task_box"
android:text="New Team" />
</RelativeLayout>
I want to use the same view to inflate multiple boxes with different coordinates in the layout. Every time I press the button to inflate the view again it inflates the box in the same coordinates so they overlap. I need to make the second box to appear to the first one's right, the third below 1st and so on, much like a grid of boxes.
Try this code and tell me whether it works. Remove the inflating of layout
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class teamCreateScreen extends Activity {
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_locate_layout);
}
public void createTeam(View view) {
final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(getApplicationContext());
if(tv.getId()>0) {
relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
}
tv.setId(i);
r1Team.addView(tv, relativeParams);
i++;
}
}
Declare int i=0; as a global variable and increment it in the createTeam() method.

I can display dynamic content in android activity but need to display it in pop up

MainActivity.java
Displays generated no of views from the loop.
My Code displays items generated based on the loop in an activity. I need it to be displayed in a pop up window.
package com.androidexample.dynamicallycreateelements;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamically_create_view_element);
final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
// create the layout params that will be used to define how your
// button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//Create four
for(int j=0;j<4;j++)
{
// Create LinearLayout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
// Create TextView
TextView product = new TextView(this);
product.setText(" Product"+j+" ");
ll.addView(product);
// Create TextView
TextView price = new TextView(this);
price.setText(" $"+j+" ");
ll.addView(price);
// Create Button
final Button btn = new Button(this);
// Give button an ID
btn.setId(j+1);
btn.setText("Add To Cart");
// set the layoutParams on the button
btn.setLayoutParams(params);
final int index = j;
// Set click listener for button
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i("TAG", "index :" + index);
Toast.makeText(getApplicationContext(),
"Clicked Button Index :" + index,
Toast.LENGTH_LONG).show();
}
});
//Add button to LinearLayout
ll.addView(btn);
//Add button to LinearLayout defined in XML
lm.addView(ll);
}
}
}
dynamically_create_view_element.xml
layout 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"
tools:context=".FirstScreen"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#898989"
android:textStyle="bold"
android:textSize="16sp"
android:text="Shopping Cart" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearMain"
>
</LinearLayout>
</LinearLayout>
Screenshot of output:
What you are looking for is a Dialog. Android has some good documentation and examples here: http://developer.android.com/guide/topics/ui/dialogs.html

Create edittext view dynamically like Google Keep

I'm trying to create dynamically generated EditText views like the Google Keep app so when the user starts to enter text in one edit text view, a new blank one is generated below it.
I have a RecyclerView set up with an adapter that will contain the edit text views.
What's the best way to approach this?
The best way to do this is to have a TextView and below the textView have a list view.
Everytime the user enters something on the TextView and presses save, add the new entry to the arrayList which you are using to populate the ListView. Then refresh the ListView. That should do the trick.
In your MainActivity.java:
package com.junglesofts.strsend;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
public int i = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout ll = (TableLayout) findViewById(R.id.tblMain);
View mTableRow = null;
mTableRow = (TableRow) View.inflate(getApplicationContext(), R.layout.mrowrayout, null);
EditText txtNew = (EditText)mTableRow.findViewById(R.id.txtNew);
txtNew.setId(i);
txtNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
++i;
TableLayout ll = (TableLayout) findViewById(R.id.tblMain);
View mTableRow = null;
mTableRow = (TableRow) View.inflate(getApplicationContext(), R.layout.mrowrayout, null);
EditText txtNew = (EditText)mTableRow.findViewById(R.id.txtNew);
txtNew.setId(i);
txtNew.setOnClickListener(this);
mTableRow.setTag(i);
ll.addView(mTableRow);
}
});
mTableRow.setTag(i);
ll.addView(mTableRow);
}
}
In your activity_main.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tblMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.junglesofts.strsend.MainActivity">
</TableLayout>
And in layout folder, create a new layout and rename it to mrowrayout.xml and in it:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutRow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText android:id="#+id/txtNew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your text!"/>
</TableRow>
I tested it and it works for me.

Multiple TextViews Added in Loop onClick Unexpected Behavior

I have two RelativeLayouts that I am programmatically adding TextViews to from an ArrayList with the intent that, based on the TextView clicked, I can index back into the original ArrayList. For my trivial code example, I'm arbitrarily splitting the items into the two RelativeLayouts.
The code works as expected when the first TextView is clicked. The dialog shows up displaying the correct word and index. However, after closing the dialog, if a different TextView is clicked, the word and index (testWord and testID in my code example) are not updated and only the first TextView's information is displayed. It seems that onClick is only being called on the first click.
Here is an example Java class (I apologize for any formatting errors, it's my first time posting here):
package com.test.test;
import java.util.ArrayList;
import java.util.Scanner;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class AndTestActivity extends Activity {
private final int DIALOG_CASE_ITEM_SELECT=0,CURR_ID=128,P_ID=256,P_HR=512,C_HR=1024;
private final String TEST="This is a test. Only a test.";
private int numPast,testID;
private String testWord;
private ArrayList<String> test;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Scanner s=new Scanner(TEST);
test=new ArrayList<String>();
while(s.hasNext()){
test.add(s.next());
}
int currID=CURR_ID,pID=P_ID,pHrID=P_HR,cHrID=C_HR,tempLen=3;
numPast=0;
RelativeLayout rlP=(RelativeLayout)findViewById(R.id.caseItemListPastLayout);
RelativeLayout rlC=(RelativeLayout)findViewById(R.id.caseItemListCurrLayout);
for(int x=0;x<test.size();x++){
if(x>tempLen){
addToRL(test.get(x),true,rlC,currID,cHrID);
currID++;
cHrID++;
}else{
numPast++;
addToRL(test.get(x),false,rlP,pID,pHrID);
pID++;
pHrID++;
}
}
}
private void addToRL(String sb, boolean current,RelativeLayout rl,int id,int hrID){
TextView citv=new TextView(this);
citv.setText((CharSequence)sb);
citv.setMovementMethod(new ScrollingMovementMethod());
citv.setTextSize(15);
citv.setTextColor(Color.BLACK);
citv.setGravity(Gravity.CENTER_HORIZONTAL);
if(current){
citv.setBackgroundColor(Color.RED);
}else{
citv.setBackgroundColor(Color.WHITE);
}
citv.setPadding(20, 10, 20, 10);
citv.setWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 360, getResources().getDisplayMetrics()));
citv.setId(id);
RelativeLayout.LayoutParams cilp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if(current&&id!=CURR_ID)cilp.addRule( RelativeLayout.BELOW, hrID-1);
else{
if(id!=P_ID)cilp.addRule( RelativeLayout.BELOW, hrID-1);
}
citv.setLayoutParams(cilp);
citv.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TextView tv0=(TextView)v;
int tvID=tv0.getId()-P_ID;
if(tvID<0){
tvID=(tv0.getId()-CURR_ID)+numPast;
}
testID=tvID;
testWord=test.get(testID);
showDialog(DIALOG_CASE_ITEM_SELECT);
}
});
rl.addView(citv);
View hr=new View(this);
hr.setBackgroundColor(getResources().getColor(R.color.background));
RelativeLayout.LayoutParams hrlp=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 5);
hrlp.addRule(RelativeLayout.BELOW,id);
hr.setLayoutParams(hrlp);
hr.setId(hrID);
rl.addView(hr);
}
protected Dialog onCreateDialog(int id) {
Dialog dialog;
AlertDialog.Builder builder;
switch(id){
case DIALOG_CASE_ITEM_SELECT:
builder = new AlertDialog.Builder(this);
builder.setMessage("word: "+testWord+" index: "+testID)
.setCancelable(true)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
}
Here is the associated XML for the layout (nothing special, but I thought I'd include it so there's a fully working example):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical"
android:background="#color/background"
android:screenOrientation="portrait">
<ScrollView
android:id="#+id/caseItemCurrScroll"
android:fillViewport="true"
android:padding="10dp"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout android:id="#+id/caseItemListCurrLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RelativeLayout>
</ScrollView>
<View
android:id="#+id/caseItemScrollSpacer"
android:layout_width="fill_parent"
android:layout_below="#id/caseItemCurrScroll"
android:layout_height="5dp"
android:background="#color/background"/>
<ScrollView
android:id="#+id/caseItemPastScroll"
android:fillViewport="true"
android:padding="10dp"
android:layout_below="#id/caseItemScrollSpacer"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout android:id="#+id/caseItemListPastLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
That should be a fully "working" example, with the addition of
<color name="background">#3E3E3E</color>
in the strings.xml
onCreateDialog() is only called once, while onPrepareDialog() is called each time the Dialog opens.
Override onPrepareDialog() and call ((AlertDialog) dialog).setMessage() here.

Button and EditTextbox and layout issue

This is my code. So when button is clicked, I have two edit text boxes that show up everytime, Button is clicked. They are coming up and down, instead I want them to be side by side.
Also I wanted to know that can I change the linear layout to absolute, since I want the button position to be at the middle of the page. Also, I want when I click the button edit text boxes should go to the top not at the bottom.
Hope so, I made this questions clear.
java code :
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PlusbuttonActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.button);
root = (LinearLayout) findViewById(R.id.linearLayout);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText t = new EditText(PlusbuttonActivity.this);
EditText a = new EditText(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
a.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addViewToRoot(t);
addViewToRoot(a);
}
});
}
private void addViewToRoot(View v){
root.addView(v);
}
}
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout">
<Button android:layout_width="wrap_content" android:text="+" android:id="#+id/button" android:layout_height="wrap_content"></Button>
</LinearLayout>
To make them side by side change linearlayout orientation="horizontal" but to me it sounds like you'd rather play around with RelativeLayout. Look it up before you get stuck and try to play with it http://developer.android.com/resources/tutorials/views/hello-relativelayout.html.

Categories

Resources