Add a TextView to SlidingDrawer Dynamically - android

I want to add a TextView dynamically to the SlidingDrawer. Below is my XML code:
<SlidingDrawer
android:id="#+id/sdDrawerBottom"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:content="#+id/sdBottomContent"
android:handle="#+id/sdBottomHandle"
android:orientation="vertical" >
<ImageView
android:id="#+id/sdBottomHandle"
android:layout_width="50dp"
android:layout_height="20dp"
android:background="#drawable/shape_rounded_corners_blue_top"
android:scaleType="center"
android:src="#drawable/icon_arrow_up_white" />
<LinearLayout
android:id="#+id/sdBottomContent"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/llBottomNavigation"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:scrollbars="horizontal" >
</LinearLayout>
</LinearLayout>
</SlidingDrawer>
And this is my java code:
LinearLayout llBottomNavigation = (LinearLayout) mButtonsView
.findViewById(R.id.llBottomNavigation);
for (int i = 0; i < navigationList.size(); i++) {
TextView tvTab = new TextView(this);
tvTab.setText(Content.getTitle(navigationList.get(i)));
tvTab.setId(i);
tvTab.setTextColor(getResources().getColor(android.R.color.black));
tvTab.setTextSize(12);
if (Hardware.getScreenSize(this) > 4) {
tvTab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
} else {
tvTab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
} tvTab.setSingleLine(true);
tvTab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
llBottomNavigation.addView(tvTab);
llBottomNavigation.invalidate();
}
It's not adding any TextViews to the SlidingDrawer. Is there any mistake in my code?

#unflagged.destination , while addView you shoud pass layoutparams with view... try this...
SlidingDrawer slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
LinearLayout llBottomNavigation = (LinearLayout) slidingDrawer.findViewById(R.id.llBottomNavigation);
for (int i = 0; i < 10; i++) {
System.out.println("in loop, " + i);
TextView tvTab = new TextView(this);
tvTab.setText("item "+i);
tvTab.setId(i);
tvTab.setTextColor(getResources().getColor(android.R.color.black));
tvTab.setTextSize(12);
/* if (Hardware.getScreenSize(this) > 4) {
tvTab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
} else {
tvTab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
} tvTab.setSingleLine(true);*/
tvTab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
LinearLayout.LayoutParams paramsFortvTab = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llBottomNavigation.addView(tvTab, paramsFortvTab);
//llBottomNavigation.invalidate();
}
if any query, plz ask...

Related

how to show fraction with denomainator, horizantal line and numerator on run time in android

Is there any why to show the fraction neat and clean in android
denominator, horizontal line and numerator
For example
4/5+x + 3
would be
4
_____ + 3
5 + x
Actually i want to show the algebraic equation on run time.
please tell me the answer i am working on this since four or five days but no success is achieved.
try this:
MainActivity Code:
public class MainActivity extends Activity {
EditText etInput;
LinearLayout resultLayout;
Button btnResult;
ViewTreeObserver mVto;
int widthOfLine;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput = (EditText) findViewById(R.id.etInput);
btnResult = (Button) findViewById(R.id.btnShowRes);
btnResult.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String temp = etInput.getText().toString().trim();
if(temp.contains("/")){
System.out.println("inside if");
String[] numArr = temp.split("/");
Log.d("dj", "num1: & num2: "+numArr[0]+" "+numArr[1]);
showResults(numArr[0], numArr[1]);
}
else Toast.makeText(getBaseContext(), "Pattern invalid", Toast.LENGTH_SHORT).show();
}
});
resultLayout = (LinearLayout) findViewById(R.id.showResultLay);
}
protected void showResults(String numerator, String denominator) {
final LinearLayout linChild = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
linChild.setGravity(Gravity.CENTER);
linChild.setLayoutParams(childParams);
linChild.setOrientation(LinearLayout.VERTICAL);
TextView tvNumerator = new TextView(getBaseContext());
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tvNumerator.setLayoutParams(tvParams);
linChild.addView(tvNumerator);
tvNumerator.setText(numerator);
tvNumerator.setTextColor(Color.BLACK);
tvNumerator.setTextSize(25);
final View dividerView = new View(getBaseContext());
dividerView.setBackgroundColor(Color.RED);
linChild.addView(dividerView);
final TextView tvDenominator = new TextView(getBaseContext());
LinearLayout.LayoutParams tv1Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tvDenominator.setLayoutParams(tv1Params);
linChild.addView(tvDenominator);
tvDenominator.setTextColor(Color.BLACK);
tvDenominator.setText(denominator);
tvDenominator.setTextSize(25);
mVto = linChild.getViewTreeObserver();
mVto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
widthOfLine = tvDenominator.getWidth();
LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 5);
viewParams.width = widthOfLine;
dividerView.setLayoutParams(viewParams);
}
});
resultLayout.addView(linChild);
}
}
layout file:
<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: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="dj.numeratordenominator.main.MainActivity" >
<EditText
android:id="#+id/etInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="textNoSuggestions" >
<requestFocus />
</EditText>
<LinearLayout
android:id="#+id/showResultLay"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btnShowRes"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" >
</LinearLayout>
<Button
android:id="#+id/btnShowRes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etInput"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>

set content in scrollview ? Android

I have an Xml that add LinearLayout and RelativeLayout in ScrollView by programmatically.When i add Text with OnclickButton for first time show me message but for 2nd time get me crash :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollID"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="1" >
<EditText
android:id="#+id/txtInpuConversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="Text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnSend"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Click" />
</LinearLayout>
</LinearLayout>
My code :
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
LinearLayout rilative;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
ScrollView sclView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
sclView = (ScrollView) findViewById(R.id.scrollID);
Button btn = (Button) findViewById(R.id.btnSend);
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUser.setText(Medtconversation);
relativeLayout = new RelativeLayout(context);
firstLinearLayout= new LinearLayout(context);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(txtviewUser,
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
firstLinearLayout.addView(relativeLayout);
Crash here now======>sclView.addView(firstLinearLayout, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
edtconversation.setText("");
}
});
}
}
I need that when i click on Button and send message for 2nd time create a new RelativeLayout in LinearLayout for show.(In scrollView)
Error :
AndroidRuntime
MainActivity$1.onClick(MainActivity.java:54)
mmmmm. I recommend that you using from table and you can add programmatically your TextView and etc. simply :
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
TextView txtviewUserT;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
TableRow tr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
Button btn = (Button) findViewById(R.id.btnSend);
final TableLayout tl = (TableLayout) findViewById(R.id.tbl);
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUserT = new TextView(MainActivity.this);
txtviewUserT.setText("OK");
txtviewUser.setText(Medtconversation);
tr = new TableRow(context);
tr.addView(txtviewUser);
tr.addView(txtviewUserT);
tl.addView(tr);
edtconversation.setText("");
}
});
}
}
Your XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/xxx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<TableLayout
android:id="#+id/tbl"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="1" >
<EditText
android:id="#+id/txtInpuConversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="Text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnSend"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Click" />
</LinearLayout>
</LinearLayout>
First you need to add the LinearLayout to ScrollView
Then add the relative layout to the LinearLayout
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
LinearLayout rilative;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
ScrollView sclView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
Button btn = (Button) findViewById(R.id.btnSend);
sclView = (ScrollView) findViewById(R.id.scrollID);
firstLinearLayout= new LinearLayout(this);
relativeLayout = new RelativeLayout(this);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUser.setText(Medtconversation);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(txtviewUser,
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
sclView.addView(firstLinearLayout);
firstLinearLayout.addView(relativeLayout);
sclView.addView(firstLinearLayout, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
edtconversation.setText("");
}
});
}
}
You are creating a new LinearLayout and RelativeLayout only once (inside onCreate), however you add them multiple times. Instead create new instances from inside of the onClick method:
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
LinearLayout firstLinearLayout= new LinearLayout(context);
RelativeLayout relativeLayout = new RelativeLayout(context);
...
firstLinearLayout.addView(relativeLayout);
...
}
});

android layout listview with edit text

In my project need to make layout listview with edit text
please suggest me how i can make this kind of layout
Thanks In Advance
Any Help Is Appreciated
Use following code.
<?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:layout_weight="1"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/mylinear"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="fill_parent"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/group4ff"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/vazgecff"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:onClick="cagir"
android:text="vazgec" />
<Button
android:id="#+id/kaydetff"
android:layout_width="0dp"
android:layout_weight=".70"
android:layout_height="wrap_content"
android:onClick="cagir"
android:text="kaydet" />
</LinearLayout>
</LinearLayout>
Activity.java
public class Form3 extends Activity {
LinearLayout[] llx ;
TextView[] tx ;
EditText[] ex ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form3);
DBHandler db = new DBHandler(this);
ArrayList<Siparis> listem = db.getSiparis();
db.close();
LinearLayout ll = (LinearLayout) findViewById(R.id.mylinear);
llx = new LinearLayout[listem.size()];
tx = new TextView[listem.size()];
ex = new EditText[listem.size()];
for (int i = 0; i < listem.size(); i++) {
llx[i] = new LinearLayout(this);
tx[i] = new TextView(this);
ex[i] =new EditText(this);
tx[i].setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,0.8f));
ex[i].setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,0.2f));
tx[i].setText(listem.get(i).getMalzeme_adi());
ex[i].setInputType(InputType.TYPE_CLASS_NUMBER);
llx[i].setId(i);
llx[i].setClickable(true);
final int j = i;
llx[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
msg(tx[j].getText().toString());
}
});
llx[i].addView(tx[i]);
llx[i].addView(ex[i]);
ll.addView(llx[i]);
}
}
private void msg(String x){
Toast.makeText(this, x, Toast.LENGTH_LONG).show();
}
public void cagir(View view){
switch (view.getId()) {
case R.id.kaydetff:
for (int i = 0; i < ex.length; i++) {
if(!ex[i].getText().toString().equals("")){
Log.e(tx[i].getText().toString(),ex[i].getText().toString());
}
}
break;
default:
break;
}
}
}

Unable to dynamically set image in an ImageView inside a TableRow

I have this LinearLayout and inside it, there is a button and a TableLayout with no rows till now, i'll add them dynamically.
<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="vertical"
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/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_select" />
<TableLayout
android:id="#+id/tablay"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
On click of the button I want the camera to be open and the image clicked to be set on an ImageView inside a new tablerow.
Now here is the java snippet.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
tableLayout = (TableLayout) findViewById(R.id.tablay);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
bitmap = (Bitmap) data.getExtras().get("data");
tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
imageView.setImageBitmap(bitmap);
tableRow.addView(imageView);
tableLayout.addView(tableRow);
}
Everything goes fine but the image doesn't show up on the screen.
Edit:
tableRow = new TableRow(this);
imageView = new ImageView(this);
tableRow.addView(imageView,new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Set Params like this, because table row accept params of type TableRow only
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
In the main.xml file,
<TableRow
android:id="#+id/infoRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="Loading Content"
android:id="#+id/loadingMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button
android:id="#+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Add New" />
</TableRow>
for eg: say testItem.java
public class testItem extends Activity
{
private List<String> results = new ArrayList<String>(); // Please add values to this string list.
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.main);
int count=results.size();
TableLayout tl=(TableLayout)findViewById(R.id.mainLayout);
for(int i=0;i<count;i++)
{
final String str=results.get(i);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(this);
textview.setText(str);
tr.addView(textview);
Button buttn = new Button(this);
buttn.setText("DELETE");
buttn.setTextColor(Color.BLUE);
tr.addView(buttn);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}
Remove imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
Change tableRow.addView(imageView);
to
tableRow.addView(imageView,100,150); (new width and height)

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