4

I am new on android, I have created one textview and set the some text on that text view.

Now I want to find only the visible text on that text view. how can i do this?

Trying Giru Bhai's example:

When trying this code, I get a null pointer exception.

int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
String displayed = textView.getText().toString().substring(start, end);

Here's the LogCat output:

06-28 18:53:04.142: E/AndroidRuntime(28521): Process: com.example.simple, PID: 28521
06-28 18:53:04.142: E/AndroidRuntime(28521): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simple/com.example.simple.VendorDetailsActivity}: java.lang.NullPointerException
06-28 18:53:04.142: E/AndroidRuntime(28521):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at android.app.ActivityThread.access$800(ActivityThread.java:139)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at android.os.Looper.loop(Looper.java:136)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at android.app.ActivityThread.main(ActivityThread.java:5102)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at java.lang.reflect.Method.invoke(Method.java:515)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at dalvik.system.NativeStart.main(Native Method)
06-28 18:53:04.142: E/AndroidRuntime(28521): Caused by: java.lang.NullPointerException
06-28 18:53:04.142: E/AndroidRuntime(28521):     at com.example.simple.VendorDetailsActivity.onStart(VendorDetailsActivity.java:94)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1194)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at android.app.Activity.performStart(Activity.java:5258)
06-28 18:53:04.142: E/AndroidRuntime(28521):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2182)
06-28 18:53:04.142: E/AndroidRuntime(28521):     ... 11 more

Following code of VendorDetailsActivity.java:

import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class VendorDetailsActivity extends Activity {
    private ArrayList<Vendor> arrayVendor;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_vendor_details);
        Bundle bundle = getIntent().getExtras();
        String json = bundle.getString("json_data");
        String vName = bundle.getString("vendorName");

        final Button backButton = (Button) findViewById(R.id.vendordetailsbackbutton);
        final View parent = (View) backButton.getParent();
        parent.post(new Runnable() {

            public void run() {
                final Rect r = new Rect();
                backButton.getHitRect(r);
                r.top -= 20;
                r.bottom += 20;
                r.right += 70;
                parent.setTouchDelegate(new TouchDelegate(r, backButton));
            }
        });
        VendorActivity vendor = new VendorActivity();
        arrayVendor = vendor.getMessage(json);
        String[] vendorName = new String[arrayVendor.size()];
        String[] vendorBooth = new String[arrayVendor.size()];
        String[] vendorBio = new String[arrayVendor.size()];
        String[] vendorPhoto = new String[arrayVendor.size()];
        backButton.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        for (int i = 0; i < arrayVendor.size(); i++) {

            vendorName[i] = arrayVendor.get(i).vName;
            vBio[i] = arrayVendor.get(i).vBio;
            vendorPhoto[i] = arrayVendor.get(i).vLogo;
            vendorBooth[i] = arrayVendor.get(i).vBooth;
            if (vName.equals(vendorName[i]) || vName.equals(vendorBooth[i])) {
                new DownloadImage((ImageView) findViewById(R.id.vendor_photo))
                        .execute(vendorPhoto[i]);


                TextView vendorBoothhash = (TextView) findViewById(R.id.booth_hash);
                vendorBoothhash.setText(vendorBooth[i]);
                TextView vendorbio = (TextView) findViewById(R.id.vendor_bio);
                vendorbio.setText(vendorBio[i]);


                TextView vendorname = (TextView) findViewById(R.id.vendor_name);
                vendorname.setText(vendorName[i]);

            }
        }
    }

    @Override
    protected void onStart() {
        TextView textView = (TextView) findViewById(R.id.vendor_bio);
        int start = textView.getLayout().getLineStart(0);
        int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
        String displayed = textView.getText().toString().substring(start, end);
        TextView vendorbio1 = (TextView) findViewById(R.id.vendor_bio1);
        vendorbio1.setText(displayed);
        System.out.println(displayed);
    };

}
PTech
  • 163
  • 1
  • 11

1 Answers1

3

Use getLineStart() - Return the text offset of the beginning of the specified line ( 0…getLineCount()). If the specified line is equal to the line count, returns the length of the text.

And getLineEnd() - Return the text offset after the last character on the specified line. to get the visible text in textview as

 ViewTreeObserver vto = txtCommName.getViewTreeObserver();
             vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                 @Override
                 public void onGlobalLayout() {
            int start = textView.getLayout().getLineStart(0);
            int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
            String displayed = textView.getText().toString().substring(start, end);
            TextView vendorbio1 = (TextView) findViewById(R.id.vendor_bio1);
            vendorbio1.setText(displayed);
         }
     });

This post may help you also Is there a way of retrieving a TextView's visible line count or range?

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74