How to identify textview reached the bottom of screen? - android

I am developing book reader application. I have LinearLayout.
<LinearLayout
android:id="#+id/llReader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical" >
</LinearLayout>
I am taking html files from internal storage line by line. I am assigning one text view for one line and putting them into LinearLayout.
private void readFile(LinearLayout ll) throws IOException {
fileName = myDatabase.getBookById(book_id) + ".html";
FileInputStream fis = openFileInput(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = "";
while (null != (line = br.readLine())) {
TextView tv = new TextView(this);
tv.setTextSize(24);
tv.setText(Html.fromHtml(line));
ll.addView(tv);
}
br.close();
}
How to identify that TextViews reached the bottom of the screen?

You can via pixel manipulation:
private void readFile(LinearLayout ll) throws IOException {
fileName = myDatabase.getBookById(book_id) + ".html";
FileInputStream fis = openFileInput(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = "";
int parentHeight = ll.getHeight(); // in pixels
int sumHeigth = 0;
while (null != (line = br.readLine())) {
TextView tv = new TextView(this);
tv.setTextSize(24);
tv.setText(Html.fromHtml(line));
ll.addView(tv);
sumHeigth += tv.getHeight();
if(sumHeigth>parentHeight) {
// if can't fit in LinearLayout
ll.removeView(tv);
// break; // or what you want in this situation
}
}
br.close();
}

Related

how to read text new line from a text file in raw and display it on different text views

I'm trying to creat text views through loop
and each new line in my text file should display on another text view.
BUt getting all the text on a single text view instaed of different lines on different textviews.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
PlayWithRawFiles();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),
"Problems: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}// onCreate
public void PlayWithRawFiles() throws IOException {
int i;
String str="";
StringBuffer buf = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.status);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is!=null) {
while ((str = reader.readLine()) != null) {
buf.append(str + "\n" );
RelativeLayout rl= (RelativeLayout) findViewById(R.id.relativelayout);
RelativeLayout.LayoutParams rlp = (new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
rlp.setMargins(0,5,0,5);
for (i=0; i<100; i++)
{
TextView tv = new Text View(this);
tv.setText(buf.toString());
tv.setTextSize(20);
tv.setLayoutParams(rlp);
rl.addView(b);
}
}
}
is.close();
}
}

Android: Can I use single InputStream for multi method?

I have some problem about using readline(). I have single inputStream it's from main class
private String url = "BoardLayoutSet/01_basic.templete";
private InputStream boardName = getResources().getAssets().open(url);
Board b = new Board(boardName);
And I send this "boardName" is InputStream to "Board" class. In this Board.class has constructor like this code below
public Board(InputStream boardName) throws IOException{
int[] d = LayoutDimensions(boardName); //<----First,use InputStream
....
......
build(null, boardName); //<<--Second,Use InputStream
}
First method is called by this constructor(Above).
First method it use "InputStream" like this(Below)
public int[] LayoutDimensions(InputStream boardName) throws IOException {
BufferedReader reader1 = new BufferedReader(new InputStreamReader(boardName));
L1 = reader1.readLine(); // #Basic
L1 = reader1.readLine(); // %Level 1
L1 = reader1.readLine(); // space line
L1 = reader1.readLine(); // First row of board
for (int i = 0; i < L1.length(); i++) {
if (L1.charAt(i) != ' ') {
x++;
}
}
System.out.println(L1);
System.out.println("Width(x):" + x);
// ////////////////
boardName.reset();
BufferedReader reader2 = new BufferedReader(new InputStreamReader(boardName));
L2 = reader2.readLine(); // #Basic
L2 = reader2.readLine(); // %Level 1
L2 = reader2.readLine(); // space line
L2 = reader2.readLine(); // First row of board
while (L2.length()!=0) {
System.out.println(L2.charAt(0));
y++;
L2 = reader2.readLine();
}
System.out.println("Height(y):" + y);
.....
......
reader1.close();
reader2.close();
reader3.close();
return dimensions;
}
When I use one method only .it can work. Next, I need to call Second method like this
protected void build(Random r1, InputStream boardName) throws IOException {
if (r1==null) {
long seed = new Random().nextLong();
r = new Random(seed);
} else {
r = r1;
}
tTile = new Tile[depth][height][width];
int x;
int y;
int z=-1;
BufferedReader buffer = new BufferedReader(new InputStreamReader(boardName));
String L=null;
buffer.reset();
L = buffer.readLine(); //Basic
.......
......
......
it's error since this line>> L=buffer.readLine();
I tried to swap order : use second method before fist method . the second can use but first method is error.
I think it's problem about Inputstream so I tried to make two Inputstream but it's not better.
It seem you already closed the input stream in the method LayoutDimensions.
This
reader1.close();

I want to show the whole c program on screen in android

I wanted to show the whole c program on screen which should be visible to user.
I used textView but i am getting errors as the code contains special symbols.
for example:
android:text=" #include <stdio.h>
int main()
{
int x, y, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
return 0;
}" />
I also want that the user should be able to scroll the code as the codes may be larger than the example.I am a noob so please suggest me any alternate for the textView to display the code.
Save your C code in a file in assets folder, for example "res/assets/code.c".
Write a function that reads the content of a file to a String:
private String readFileInAssetsDir(String filename) {
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(filename)));
String line;
while((line = br.readLine()) != null)
sb.append(line + "\n");
} catch(Exception e) {
// TODO
}
return sb.toString();
}
And now define a WebView (not a TextView) in your layout (the advantages are that you can show any character, and WebView provides zoom and scroll directly):
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
And finally I would enclose all C code in a <pre></pre> tag and then show it inside the WebView widget:
String plainCode = readFileInAssetsDir("code.c");
String htmlCode = "<pre>" + plainCode + "</pre>";
webView.loadDataWithBaseURL("", htmlCode, "text/html", "utf-8", "");
EDIT: THIS WORKS BUT READS ONLY A SINGLE LINE OF THE TEXT IN THE TXT FILE
String line;
TextView view;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addingprogrammultipletimes);
AssetManager am = getAssets();
try {
InputStream is = am.open("add.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine();
view = (TextView) findViewById(R.id.textView);
view.setText(line);
} catch (Exception e) {
e.printStackTrace();
}
}

How to create the exact number of textview and put the string value into the textviews?

String hallo = "blabla " + "jojo " + "\n" + "doj " + "drasl " +"\n";
InputStream is = new ByteArrayInputStream(hallo.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String reader;
while ((reader = in.readLine()) != null) {
String[] RowData = reader.split(" ");
String eitt = RowData[0];
String tvo = RowData[1];
I have a string with the same format as the hallo string, except I don't know how many lines there are. I'm trying to put the information in a table layout (or what ever is most convenient).
How do I automatically create the exact number of textview windows in the table layout as I need, and automatically input my string value?
By the way, the textview is inside a scrollview layout, if that matters.
Use the following code
String hallo = "blabla " + "jojo " + "\n" + "doj " + "drasl " + "\n";
InputStream is = new ByteArrayInputStream(hallo.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(is));
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
String line;
try {
while ((line = in.readLine()) != null) {
String[] RowData = line.split(" ");
String eitt = RowData[0];
String tvo = RowData[1];
TextView textView = new TextView(this);
textView.setText(line);
linearLayout.addView(textView);
}
} catch (IOException e1) {
e1.printStackTrace();
}
with this xml layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>

How to create the exact number of textview and put them in to tableview?

I have this code
public class MainActivity extends Activity {
TextView textview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String hallo = "blabla " + "jojo " + "lol " + "\n" + "doj " + "drasl " + "\n";
InputStream is = new ByteArrayInputStream(hallo.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(is));
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
String line;
try {
while ((line = in.readLine()) != null) {
String[] RowData = line.split(" ");
String eitt = RowData[0];
String tvo = RowData[1];
TextView textView = new TextView(this);
textView.setText(line);
linearLayout.addView(textView);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
with this xml layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
I want to insert these parts of a string into a table layout, so that when my pieces of text are printed they come out in nice columns. The way this works, it just prints the string line for line instead of putting it into columns. It's also very important to know that I don't know how many lines are going to be in the string, so I can't just make 4 textview boxes because there could be 50 og a hundred lines, so I need to make an i amount of textview boxes in a table view with i being the number of lines
Try by adding setLayoutParams to TextView :
//Your code...
String line;
try {
while ((line = in.readLine()) != null) {
String[] RowData = line.split(" ");
String eitt = RowData[0];
String tvo = RowData[1];
TextView textView = new TextView(this);
textView.setText(line);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT
,LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView);
}
} catch (IOException e1) {
e1.printStackTrace();
}
//Your code...

Categories

Resources