add layout programmatically in android button click - android

I have a XML layout file. This is the source
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/rot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff0000" >
<ImageView
android:id="#+id/btn_categorry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
I wrote some code which can add new layout programmatically and I also added(created) image view in layout programmatically. I want to wrote code witch would can add new layout programmatically. For more information. I want to create new layout each image view click witch I created programmatically.
This is a my source
public class MainActivity extends Activity {
RelativeLayout myImage, mainlayout;
private ImageView img;
RelativeLayout staticlayout;
RelativeLayout.LayoutParams parms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (RelativeLayout) findViewById(R.id.rot);
mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
img = (ImageView) findViewById(R.id.btn_categorry);
parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
staticlayout = new RelativeLayout(getApplicationContext());
staticlayout.setBackgroundColor(Color.parseColor("#000000"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
//
//
add_btn.setLayoutParams(parms2);
add_btn.setOnClickListener(new listener());
staticlayout.setId(10);
staticlayout.addView(add_btn);
parms.addRule(RelativeLayout.BELOW, R.id.rot);
staticlayout.setLayoutParams(parms);
mainlayout.addView(staticlayout);
// parms.addRule(RelativeLayout.BELOW, R.id.rot);
}
});
}
class listener implements OnClickListener {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams costomparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout relativelayout = new RelativeLayout(
getApplicationContext());
relativelayout.setBackgroundColor(Color.parseColor("#AB3232"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams imagelayoutparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imagelayoutparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imagelayoutparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setOnClickListener(new listener());
//
//
add_btn.setLayoutParams(imagelayoutparam);
relativelayout.addView(add_btn);
costomparam.addRule(RelativeLayout.BELOW, staticlayout.getId());
relativelayout.setLayoutParams(costomparam);
mainlayout.addView(relativelayout);
}
}
}
My code can create new layout and also can create new layout in image view click. How can I write code to can create new layout automatically each time when I click image view?
If anyone knows solution please help me.

Related

Unable to add RelativeLayout programmatically

I am unable to add a RelativeLayout programmatically to existing xml layout. only xml layout is displaying. Sharing my code
fragment_home.xml:
<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"
tools:context="com.timetable.act.MainActivity$PlaceholderFragment"
android:id="#+id/homeLayout"
android:background="#color/white">
<ImageButton
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="380dp"
android:layout_marginLeft="240dp"
android:background="#drawable/circle"
android:id="#+id/addEvent"
android:src="#drawable/ic_mode_edit_white_24dp"
android:layout_gravity="right|bottom"
android:contentDescription="#string/hello" />
</RelativeLayout>
PlaceholderFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = null;
switch (Integer.valueOf(getArguments().get(ARG_SECTION_NUMBER).toString())) {
case 1:
rootView = inflater.inflate(R.layout.fragment_home, container, false);
displyAllEvent(rootView);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_setting, container, false);
break;
default:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
}
return rootView;
}
private void displyAllEvent(View mainView){
Drawable drawableShadow = ContextCompat.getDrawable(getActivity(),R.drawable.tile_shadow);
RelativeLayout tileLayout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tileLayout.setBackground(drawableShadow);
tileLayout.setPadding(R.dimen.tilePadding,R.dimen.tilePadding,R.dimen.tilePadding,R.dimen.tilePadding);
tileLayout.setLayoutParams(rlp);
tileLayout.setId(999);
RelativeLayout.LayoutParams titleLayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView titleView = new TextView(getActivity());
titleView.setText("Testing title with test");
titleView.setId(111);
titleView.setTextColor(R.color.white);
titleView.setLayoutParams(titleLayout);
tileLayout.addView(titleView);
RelativeLayout.LayoutParams textLeftLayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textLeftLayout.addRule(RelativeLayout.BELOW, titleView.getId());
TextView startHView = new TextView(getActivity());
startHView.setText("left Text");
startHView.setId(222);
startHView.setTextColor(R.color.white);
startHView.setLayoutParams(textLeftLayout);
tileLayout.addView(startHView);
RelativeLayout.LayoutParams textRightLayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textRightLayout.addRule(RelativeLayout.BELOW, titleView.getId());
textRightLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textLeftLayout.addRule(RelativeLayout.ALIGN_RIGHT, startHView.getId());
TextView startMView = new TextView(getActivity());
startMView.setText("Right Text");
startMView.setId(333);
startMView.setTextColor(R.color.white);
startMView.setLayoutParams(textRightLayout);
tileLayout.addView(startMView);
RelativeLayout parentView = (RelativeLayout)mainView.findViewById(R.id.homeLayout);
parentView.addView(tileLayout);
}
output:
Instead of creating a relative layout during runtime, you could simply make a pre-existing one, with all its children, visible... E.g:
if (myCondition) {
myLayout = (RelativeLayout) myContext.findViewById(R.id.myLayoutID);
myLayout.setVisibilty(View.visible);
myLayoutTextView = (TextView) myContext.findViewById(R.id.myTextView);
myLayoutTextView.setVisibility(View.VISIBLE);
}
In case you also have a button in the layout that you want to become click-able when the layout is active, you can do:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (myCondition) {
//Do Stuff Here
}
});

add layout programmatically in android button listener

i have some layout file and i try to add new relativelayout with programmatically
this is a my xml layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/rot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff0000" >
<ImageView
android:id="#+id/btn_categorry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
and this is java code
public class MainActivity extends Activity {
RelativeLayout myImage, mainlayout;
private ImageView img;
RelativeLayout staticlayout;
RelativeLayout.LayoutParams parms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (RelativeLayout) findViewById(R.id.rot);
mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
img = (ImageView) findViewById(R.id.btn_categorry);
parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
staticlayout = new RelativeLayout(getApplicationContext());
staticlayout.setBackgroundColor(Color.parseColor("#000000"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setLayoutParams(parms2);
add_btn.setOnClickListener(new listener());
staticlayout.setId(10);
staticlayout.addView(add_btn);
parms.addRule(RelativeLayout.BELOW, R.id.rot);
staticlayout.setLayoutParams(parms);
mainlayout.addView(staticlayout);
}
});
}
class listener implements OnClickListener {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams costomparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout relativelayout = new RelativeLayout(
getApplicationContext());
relativelayout.setBackgroundColor(Color.parseColor("#AB3232"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams imagelayoutparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imagelayoutparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imagelayoutparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setOnClickListener(new listener());
add_btn.setLayoutParams(imagelayoutparam);
relativelayout.addView(add_btn);
costomparam.addRule(RelativeLayout.BELOW, staticlayout.getId());
relativelayout.setLayoutParams(costomparam);
mainlayout.addView(relativelayout);
}
}
}
i try to explain my problem. i can add new layout and imageview inside new layout(which i created
programmatically) and also i can add new layout in button click with a button i also created programmatically.
and now i want to write code to create new layout each time.i want recursion function.how i can solve my problem ?
if anyone knows solution problem please help me
Check if this solution fulfil your needs:
Activity code:
package com.example.abc;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private RelativeLayout myImage, mainlayout;
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (RelativeLayout) findViewById(R.id.rot);
mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
img = (ImageView) findViewById(R.id.btn_categorry);
img.setOnClickListener(new RecursionOnClickListener());
}
private int layoutId = 1;
class RecursionOnClickListener implements OnClickListener {
#Override
public void onClick(View v) {
RelativeLayout staticlayout = new RelativeLayout(getApplicationContext());
staticlayout.setId(layoutId++);
if(layoutId % 2 == 0) {
staticlayout.setBackgroundColor(Color.parseColor("#000000"));
}
else {
staticlayout.setBackgroundColor(Color.parseColor("#FF0000"));
}
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setLayoutParams(parms2);
add_btn.setOnClickListener(new RecursionOnClickListener());
staticlayout.addView(add_btn);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms.addRule(RelativeLayout.BELOW, mainlayout.getChildAt(mainlayout.getChildCount()-1).getId());
staticlayout.setLayoutParams(parms);
mainlayout.addView(staticlayout);
}
}
}
Layout XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00" >
<RelativeLayout
android:id="#+id/rot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff0000" >
<ImageView
android:id="#+id/btn_categorry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>

Programatically adding views without xml element in android

Description::
On click of button I want to add a textview dynamically on run-time
I don't want to use a textview widget(from xml)
I want to generate textviews programatically
Is it possible ?
If so how ?
MainActivity.java
public class MainActivity extends Activity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
activity_main.xml
<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/Container" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBox1"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:text="Button" />
</RelativeLayout>
Just
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout layout = (LinearLayout) findViewById(R.id.parent_layout);
TextView textView = new TextView(MainActivity.this);
layout .addView(textView);
}
});
You can create a textView using the context of your activity.
example:
RelativeLayout relativeLayout = (RelativeLayout )findViewById(R.id.Container);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
TextView tv = new TextView(MainActivity.this);
relativeLayout.addView(tv,lp);
}
});
Simple example, including layout params (width, height, etc):
// instantiate the new view - int his example, a textview
TextView label=new TextView(context);
label.setText(labelText);
// new LayoutParams, specify the height as WRAP_CONTENT
// width is 0 in this example as we are using the layout weight (2)
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT,2
);
// margins
lp.rightMargin=5;
lp.leftMargin=5;
// apply the layout params to this view
label.setLayoutParams(lp);
// finally, add the view to the container view
content.addView(label);
Something like this:
// Create the TextView
TextView textView = new TextView(this);
// Create some parameters
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
// Get your relative layout by its id
RelativeLayout relativeLayout = (RelativeLayout )findViewById(R.id.Container);
// Add the newly created TextView to the layout
relativeLayout.addView(textView, p);
TextView in a LinearLayout:
LinearLayout layout = new LinearLayout(this);
TextView textview = new TextView(this);
textview.setText("Hellow, I'm a TextView!");
layout.addView(textview);
this.setContentView(layout);

android LinearLayout.LayoutParams add new view

i need add a new button and textedit to my LinearLayout.. if I create a new param, the buttons are created over each other..
my mainactivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyTouchEventView tv = new MyTouchEventView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
addContentView(tv.btnSave, tv.params2);
}
and MyTouchEventView:
public class MyTouchEventView extends View {
public Button btnReset;
public Button btnSave;
public LinearLayout.LayoutParams params;
public LinearLayout.LayoutParams params2;
public MyTouchEventView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Vymazat platno");
btnSave = new Button(context);
btnSave.setText("Ulozit");
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
btnReset.setLayoutParams(params);
params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(40, 20, 40, 30);
btnSave.setLayoutParams(params2);
http://i.stack.imgur.com/kvY1T.png
Why don't you create a simple layout for your activity?
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/my_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="myText" />
<Button
android:id="#+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="myText" />
</LinearLayout>
Then in the onCreate method:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
Also see http://developer.android.com/guide/topics/ui/layout/linear.html

How to horizontally align some programmatically added views?

I have designed a layout as in the image below:
After entering text in the EditText, when I press the Add+ Button the TextView and Button will be added as shown in the image below:
I want to show the Button on the right side of the TextView. How should I do this?
Another question, how should I remove corresponding View when user clicks a button? The code:
public class ExampleActivity extends Activity {
private LinearLayout mLayout;
private EditText mEditText;
private Button mButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mEditText = (EditText) findViewById(R.id.editText);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText()
.toString()));
mLayout.addView(createNewButton());
}
});
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New text: " + text);
return textView;
}
private Button createNewButton() {
final LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(lparams);
button.setText(" - ");
return button;
}
}
The TextViews and Buttons are stacked because you probably use a LinearLayout with the orientation vertical. You could wrap your TextView + Button into a LinearLayout and then add this LinearLayout to your own layout or you could use a TableLayout like below(I've added some ids so you can delete the rows you want):
public class SomeActivity extends Activity {
private EditText mInput;
private TableLayout mTable;
private static int sCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addButton = (Button) findViewById(R.id.add);
mInput = (EditText) findViewById(R.id.editText1);
mTable = (TableLayout) findViewById(R.id.table1);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mTable.addView(addRow(mInput.getText().toString()));
}
});
}
private TableRow addRow(String s) {
TableRow tr = new TableRow(this);
tr.setId(1000 + sCount);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams tlparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
tr.addView(textView);
TableRow.LayoutParams blparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(blparams);
button.setText(" - ");
button.setId(2000 + sCount);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mTable.removeView(findViewById(v.getId() - 1000));
}
});
tr.addView(button);
sCount++;
return tr;
}
}
where the main layout file is:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TableLayout
android:id="#+id/table1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</LinearLayout>
</ScrollView>
If for some reason, you don't like the TableLayout use a LinearLayout to wrap you TextView and Button with the layout file above(and of course remove the TableLayout):
//...
ll = (LinearLayout) findViewById(R.id.parent);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//where ll is the LinearLayout with the id parent
ll.addView(addRow(mInput.getText().toString()));
}
});
}
private LinearLayout addRow(String s) {
LinearLayout tr = new LinearLayout(this);
tr.setId(1000 + sCount);
tr.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams tlparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
tr.addView(textView);
LinearLayout.LayoutParams blparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(blparams);
button.setText(" - ");
button.setId(2000 + sCount);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
ll.removeView(findViewById(v.getId() - 1000));
}
});
tr.addView(button);
sCount++;
return tr;
}
LinearLayout having the property Orientation to align control either Vertically/Horizontally
so just set Orientation of the same
http://developer.android.com/reference/android/widget/LinearLayout.html
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mLayout.setOrientation(LinearLayout.HORIZONTAL);
Updated
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="schemas.android.com/apk/res/android";
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText"
android:layout_width="293dp"
android:layout_height="wrap_content" >
<requestFocus /> </EditText>
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add+" />
</LinearLayout>

Categories

Resources