Search code examples
androidbuttontextviewsettext

TextView.settext() not working , Button.settext() works well


I have been working on a class that uses Runtime.exec and gets its output , however it wasn't very stable ... Now I used this class to get the output of the command "ls /sdcard/" (knowing that I have the right permissions in my Manifest) ... (I will write codes below) My problem is that the Text output from the command can be shown as Toast , and it can be set as a text for a button , but not a text for a TextView ! when I run my TextView.settext() , the TextView shows no text at all! What's weirder is that if I use settext() like TextView.settext("Hey there , I am reset"); it works and shows that text ! Here are the codes :

MainActivity.class/Button.setOnClickListener:

B1.setOnClickListener(new View.OnClickListener() {          
    public void onClick(View arg0) {
        String sdcard = File.separator+Environment.getExternalStorageDirectory().toString()+File.separator; //unused
        String[] arr = {"ls" , "/sdcard/..ROMS/"};//unused - was for experimentation
        String r = shell.sendSingleCommand("ls /sdcard/"); //"r" should now hold the output (stdin and stderr) of the command - shell is defined as Shells shell = new Shells();
        B3.setText(r); //Text of the button changes to the String r
        text.setText(r); //Text is blank , no text is shown at all , not even what it originally was - "text" is my TextView ... Here's my problem
        Show(r); // a method that shows a Toast for a given String - shows the String r normally in a normal Toast ...

    }
});

Declarations of most variables in MainActivity:

        final Button B1 = (Button) findViewById(R.id.button1);
        final Button B2 = (Button) findViewById(R.id.button2);
        final Button B3 = (Button) findViewById(R.id.button3);
        final SharedPreferences pref = getSharedPreferences("seaskyways.testingproject",0);     
        final TextView text = (TextView) findViewById(R.id.testing_text_view);
        final Shells shell = Shells.Shells(); //which returns new Shells()

Layout/activity_main.xml:

<ScrollView xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="37dp"
            android:text="Button 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="52dp"
            android:text="Button 2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button1"
            android:layout_centerHorizontal="true"
            android:text="Button 3" />

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/button3" >

            <TextView
                android:id="@+id/testing_text_view"
                android:layout_width="222dp"
                android:layout_height="236dp"
                android:gravity="center|top"
                android:padding="10sp"
                android:text="Hey there !"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/abs__primary_text_holo_dark" />

        </ScrollView>
</RelativeLayout>
</ScrollView>

Show() :

public void Show(String a){
    Toast.makeText(getApplicationContext(),a , Toast.LENGTH_SHORT).show();
}

Shells.class/sendSingleCommand():

public String sendSingleCommand(String singlecmd) { 
    String a = "";
    final BufferedReader in ;
    final BufferedReader er;

    final String r;

        try {
            p = Runtime.getRuntime().exec(singlecmd);           
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    er = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    try{
        String n;

        while((n = er.readLine())!=null){
            a = a + n + "\n";
        }

        while((n=in.readLine())!=null){
            a = a +"\n"+ n;
            while((n = er.readLine())!=null){
                a = a +"\n"+ n;
            }
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    p.destroy();
    try {
        in.close();
        er.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    final StringBuilder build = new StringBuilder();
    build.append(a);
    if(a.startsWith("null")){
        build.substring(4);
    }
    a= build.toString();
    return a;
}

AndroidManifest.xml (incase needed):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="seaskyways.testingproject"
    android:versionCode="1"
    android:versionName="beta" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
    <uses-permission 
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        />

    <application
        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >
        <activity
            android:name="seaskyways.testingproject.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Holo" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="seaskyways.testingproject.MenuActivity"
            android:label="@string/title_activity_menu" >
        </activity>
        <activity
            android:name="seaskyways.testingproject.Splash"
            android:label="@string/title_activity_splash" android:theme="@style/Theme.Sherlock.Dialog" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="seaskyways.testingproject.HorizontalScrollView"
            android:label="@string/title_activity_splash" >
            <intent-filter>
                <action android:name="android.intent.action.HSV" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

if you need to know anything in the comments please !


Solution

  • ScrollView can host only one direct child

    Try to use this layout: Change to the names that you use and try it out.

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="138dp" >
    
                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />
    
                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="104dp"
                    android:text="Button" />
    
                <Button
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginRight="43dp"
                    android:text="Button" />
    
                <ScrollView
                    android:id="@+id/scrollView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="50dp"
                    android:layout_marginTop="50dp" >
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
    
                        <TextView
                            android:id="@+id/textView1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="TextView" />
    
                    </LinearLayout>
                </ScrollView>
    
            </RelativeLayout>
    
        </LinearLayout>
    
    
    </ScrollView>