How to find visible text in text form

I am new to android, I created one text view and set some text in this text view.

Now I want to find only visible text in this text view. How can i do this?

Giru Bhai example:

When I try 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 is the output of LogCat:

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

The following VendorDetailsActivity.java code:

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);
    };

}
+4
source share
1 answer

Use getLineStart()- return the text offset for the beginning of the specified line (0 ... getLineCount ()). If the specified string is equal to the number of lines, returns the length of the text.

getLineEnd() - .

 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);
         }
     });

TextView?

+2

Source: https://habr.com/ru/post/1546410/


All Articles