How to create text boxes dynamically in eclipse android - android

I want to design a simple android application.That prompt user to enter number of numbers you want.Then it should create text boxes dynamically according to user requirements.can any one give me the code.Thanks in advance

The question you are asking will be closed soon because of off-topic but I will give you little tips about adding the TextView OR EditText, If you want to add TextView in number of times then do like this
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0; i<number; i++)
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("TextView "+i);
this.myLayout.addView(tv); //Where mLayout is your Parent Layout
}

Related

how to make android app containing huge text content

I want to make an android app that have a huge text content. imagine that i want to make a dictionary app. so what should i do. what method should i choose. should i make lots of textviews and activities or there is a solution?
I'm newbie in android programming...please help me! I tried by making a lot of textviews and activity manually but it's time-consuming...
Take a look at http://developer.android.com/guide/topics/ui/layout/listview.html you can import words for example from a databases and put them into the listview then dynamically create activities.
If you want TextViews you can always make them by code.
You declare a xml with a scrollView on top and inside a linearlayout.
On the class you do (or something close to that):
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
for(String text: textList){
TextView valueTV = new TextView(this);
valueTV.setText(text);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
((LinearLayout) layout).addView(valueTV);
}
Hope it helps

android - How to set the button params in the Listview

I added the Button has footer view of the List view pro-grammatically. Now i want to set the params of the button and set the button gravity as center vertical. I did in the following way but it's not working.How to implement this?
btnMoreUpcomingExits = new Button(this);
btnMoreUpcomingExits.setText("More Upcoming Exits");
btnMoreUpcomingExits.setBackgroundResource(R.layout.moreupebtn_widget);
btnMoreUpcomingExits.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.WRAP_CONTENT, ListView.LayoutParams.WRAP_CONTENT));
btnMoreUpcomingExits.setTextColor(Color.WHITE);
btnMoreUpcomingExits.setTypeface(null, Typeface.BOLD);
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
btnMoreUpcomingExits.setOnClickListener(btnMoreUpcomingExitsListener);
getListView().addFooterView(btnMoreUpcomingExits);
setListAdapter(new UpcomingExitsResultsListViewAdapter(this));
You currently have
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
I assume this is an oversight and you meant CENTER_VERTICAL? This will vertically align the text inside the button which it should already be doing so your question seems confusing. Are you instead asking how to align the button itself vertically? If so you need to set the gravity on the buttons parent.
As for the params, you are already setting layout params. What else are you trying to do? You should aim to be more specific in your questions so it is easier to identify what you are trying to do.
Try this:
LinearLayout layout=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
btnMoreUpcomingExits = new Button(this);
btnMoreUpcomingExits.setText("More Upcoming Exits");
btnMoreUpcomingExits.setBackgroundResource(R.layout.moreupebtn_widget);
btnMoreUpcomingExits.setLayoutParams(params);
btnMoreUpcomingExits.setTextColor(Color.WHITE);
btnMoreUpcomingExits.setTypeface(null, Typeface.BOLD);
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
layout.addView(btnMoreUpcomingExits);
btnMoreUpcomingExits.setOnClickListener(btnMoreUpcomingExitsListener);
getListView().addFooterView(btnMoreUpcomingExits);
setListAdapter(new UpcomingExitsResultsListViewAdapter(this));
Check this this it might help you.
Check this link
you might use WindowManager.LayoutParams to set attribute of button in listview.

Android: Tablerow multiline textview clipping vertically

I am dynamically adding rows with two columns to a table. One of the columns holds text that will spread across multiple lines. I've added the weight on the layout parameters for the textview so it no longer clipps outside of the screen, but it seems to be restricted to two and a half lines showing in the table, regardless of how long the multiline text is. The table is just defined in xml and everything else is done programmatically.
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams paramsStar = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
paramsStar.setMargins(0,0,40,0);
TableRow.LayoutParams paramsText = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.FILL_PARENT, (float)1.0);
TableRow tr = new TableRow(this);
tr.setLayoutParams(rowParams);
TextView viewStar = new TextView(this);
viewStar.setTextSize((float) 30.0);
viewStar.setText("*");
viewStar.setLayoutParams(paramsStar);
viewStar.setTypeface(Typeface.DEFAULT_BOLD, 2);
TextView viewText = new TextView(this);
viewText.setText("Long multiline text piece"); //<--- Here the long text goes
viewText.setLayoutParams(paramsText);
tr.addView(viewStar);
tr.addView(viewText);
table.addView(tr, rowParams);
How do i fix this vertical clipping?
Screenshot of the problem below:
Here is the link showing the problem
I've found the root of the problem. The first column values textsize made the multiline text clip. If i reduce the textsize, the problem disappears.
I cant explain why though.
Another answer I'll drop in here: Android measures table row height in a very non-intuitive way. If you have 2 TextView(s), one for each column of a TableRow, you need to ensure that whatever top and bottom padding you've set for the first TextView is set identically for the 2nd TextView or you will get strange things like content clipping and weird positioning of the 2nd TextView.

how to check if a TextView exist

I created on loop 5 TextViews, set to them some text and added they to LinearLayout.
After that I need to change text in them. I want to delete all of them and create new one on loop again. But before TextView deleting I must to be sure that its exists. How to do it?
for(int i=0; i<5; i++){
TextView tv = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(params);
tv.setId(i);
tv.setText("some data");
ll.addView(tv);
}
Thank you for help and your time!
If you want to delete all of them then just perform ll.removeAllViews().
You should keep a reference to each of the text views you create if you want to remove them.
If I understand your question correctly, simply create an array of TextViews with 5 elements and run the loop to do what you want with them.
To "delete" them you can call:
tv[i].setVisibility(View.GONE);
To change text and re-add them:
tv[i].setText("New Text");
tv[i].setVisibility(View.VISIBLE);
Better solution is to hold references to your TextViews in a list, so you can interact with them later on after you've created them. Otherwise you won't be able to see the objects.

Can't center TextView dynamically in TableRow

I have dynamically made a TableRow with an ImageButton and a TextView.
I've set a gravity on my TableRow using TableRow.setGravity(Gravity.CENTER_VERTICAL);
Then, on my TextView, I'm using this code
TextView tv = new TextView(this);
tv.setText(functie[beschrijving]);
tv.setWidth(300);
//tv.setHeight(60);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setPadding(15, 0, 10, 0);
tr.addView(tv);
When I do this, I'm getting this result. It's not quite centered, it looks like the text right below the center. Does anyone know how to put the text completely in the center?
Hi
I think you should use the following property of Parent Layout
android:layout_centerVertical="true"
maybe this code help you.
android.widget.TableRow.LayoutParams params = new android.widget.TableRow.LayoutParams();
params.gravity = Gravity.CENTER_VERTICAL;
tr.addView(tv, params);
But I'm not sure.

Categories

Resources