how to make android app containing huge text content - android

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

Related

Centering multiple textViews programmatically on Android

I have an Open GL ES 2.0 app and am displaying an Android TextView over the top of my GLSurfaceView.
I have the actual textviews displaying OK but now I need to try to centre them.
This is what I have so far:
text1 = new TextView(this);
text1.setText("This is some sample text");
text1.setTextColor(Color.WHITE);
text1.setTextSize(textSize);
text2= new TextView(this);
text2.setText("And this is some more sample text");
text2.setTextColor(Color.WHITE);
text2.setTextSize(textSize);
RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
textLayout = new RelativeLayout(this);
textLayout.setLayoutParams(textParams);
textLayout.addView(text1);
textLayout.addView(text2);
mainLayout.addView(textLayout);
However, when I run this, only text1 is centered. Text 2 isn't, and starts at the left side of the first (correctly centered) textView. Try as I might, I can't seem to get both of them centered. The following graphic describes what I mean:
(Please note re vertical placement - in my actual results, both TextViews are at the same vertical/'y' position and therefore overlapping - obviously this is expected as I haven't changed the vertical position of the 2nd TextView, but to make things clearer to illustrate, I've moved the 2nd one down manually.......)
The idea I was going with was creating a 'textLayout' to which I could add all of my textViews, then just add that textLayout to my main layout. Thus facilitating the addition and removal of all of the textViews with a single line of code.
Please note that I am not using, and do not wish to use, XML for this - I would like to know how to do this programmatically.
What am I missing?
Edit Here is how I am creating my Main Layout to which I am adding the textView.....
layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
When you are adding "RelativeLayout.CENTER_HORIZONTAL" as a rule to your layout params, what you are actually doing is telling textLayout that it should be centered horizontally in your mainLayout (see http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_centerHorizontal). In code you should actually do text1.setGravity(Gravity.CENTER) and text2.setGravity(Gravity.CENTER).

Add Buttons Into layouts one by one on serval Lines

I want to develop the Messaging application like native Application. The Problem is when i add buttons after adding two or three buttons depending on name size will become like this
My Code is as below
RelativeLayout tr = (RelativeLayout) findViewById(R.id.contacts_div);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button b = new Button(MainActivity.this);
b.setText(name);
b.setId(id);
tr.addView(b,params);
if(prev_id!=0)
{
params.addRule(RelativeLayout.RIGHT_OF,prev_id);
}
I want if the enough space is not available the it will add on Next Line.
i try many posts in stack overflow but fail to solve this problem
Thanks in advance
You should use a gridview for this.
try setting your button position through yout code once the text is set then you can see if the total width of your buttons is too big. Then you'll know when to place a button on the next line.
And as #Snicolas said use a GridView, the placement will be easier.
I hope this will help you.
You should use FlowLayout. It's not standart android component, so you should find implementation you like. E.g. this one is nice.

Android : How to adjust Textview

Hello i am trying to display the content i receive in an activity using TextView but it seems that TextView is overlapping a button that i have put in activity's UI.My goal is to put TextView and the button side by side. I have put the TextView in the UI dynamically like this:
String display = extras.getString("EXTRA_MESSAGE");
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setWidth(20);
textView.setHeight(20);
textView.setText(display);
setContentView(textView);
I know i miss something but i cant find what it is,so can you please suggest a way how to fix that?
Thanks a lot in advance!
When you call setContentView(textView); it changes your layout to just the textView so it isn't overlapping your Button but your Button isn't shown anymore. You need to add it to your layout and put it where you want it.
You can do this by getting a reference to your root View in your xml and calling addView(textVie) on that root View and use addRule() to position your TextView where you want. However, if it isn't necessary to add your TextView dynamically then it is much easier to declare it in your xml.
If you do want to add it dynamically still, then this SO answer, and many more, covers it.

Dynamiclly add a textview using existing textview layout parameters

I am trying to add a TextView from the activity when a button has been pressed. I have found how to add a new textview from the activity, however instead of coding the required layout parameters is it possible to copy an existing textviews parameters (in the xml layout) to the new textview?
I have tried:
TextView tv1 = new TextView(this);
TextView tv2 = (TextView) findViewById(R.id.basetext);;
// its this line below which doesn't work
tv1.setLayoutParams(tv2.getLayoutParams());
But it doesn't copy any of the layout parameters...
Any ideas?
You can do One thing.
If you are trying to set just the Value of the Same TextValue every time and if the Layout properties of that TextView is same at all the time then follow below steps:
First Create the One Layout that only contain he TextView layout only with your appropriate properties. (Let name it as layout_textView.xml)
Now, do add that layout_textView.xml dynamically to your main view as per your requirement.
How it will Solve your issue.
If any query then let me know.
Did you call requestLayout() after adding the 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.

Categories

Resources