Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How to turn Text View display into List View display?? I was given the question below... and I have completed the code. Instead of displaying
How to turn Text View display into List View display??
I was given the question below... and I have completed the code. Instead of displaying it as a list view, I did it as text view. How can I display it as list view (am not too sure about it)?
Question: You are required to implement an app that reads a text file and displays the text on the screen line by line using listview. You can copy and paste a text file of your choice into the folder raw/yourtextfile.txt.
//content_main.xml
xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:scrollbars="vertical" android:id="@+id/text1" /> RelativeLayout>
//MainActivity.java
package com.user.qs3; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.text.method.ScrollingMovementMethod; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.ListView; import android.widget.TextView; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); textView = (TextView) findViewById(R.id.text1); textView.setMovementMethod(new ScrollingMovementMethod()); String data = ""; StringBuffer sbuffer = new StringBuffer(); InputStream Is = this.getResources().openRawResource(R.raw.sample); BufferedReader reader = new BufferedReader(new InputStreamReader(Is)); if (Is != null) { try{ while ((data = reader.readLine()) != null) { sbuffer.append(data); } textView.setText(sbuffer); Is.close(); } catch (Exception e) { e.printStackTrace(); } } FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started