Search code examples
androideclipsetogglebuttongraphical-layout-editor

Android Graphical layout doesnt show toggle button


I'm new with android programming and i have a problem. I use Eclipse and i create a Toggle Button but in Graphical layout that doesn't show anything and it shows me this "Exception raised during rendering: -1". What can i do? I post below my code that i have used.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="25dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/etCommands"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Type a Command"
    android:password="true" />

<LinearLayout
    android:weightSum="100"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_weight="20"
        android:id="@+id/bResults"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Try Command" />

    <ToggleButton
        android:layout_weight="80"
        android:paddingBottom="10dp"
        android:checked="true"
        android:id="@+id/tbPassword"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="ToggleButton" />
</LinearLayout>

<TextView
    android:id="@+id/tvResults"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="invalid" />


Solution

  • The recommended way to use "layout_weight" is to set the dimension to "0dp" and set the desired weight, so android will resize it:

    <ToggleButton
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="80"
        android:text="ToggleButton" />
    

    I don't think this is the reason it crashes, so you better post the entire xml

    ----UPDATE----------------------------------------------

    I don't get any rendering error, but as your button width is set to "fill_parent" (you should use "match_parent" instead as "fill_parent" is deprecated), and there are two items in a horizontal linear layout, the second one is not visible.

    Also, use "0dp" when using weights for the intended dynamic dimension.

    Here is your layout fixed. I also swapped weights so your buttons look better (I think it was a bug)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="25dp" >
    
    <EditText
        android:id="@+id/etCommands"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Type a Command"
        android:password="true" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100" >
    
        <Button
            android:id="@+id/bResults"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="80"
            android:text="Try Command" />
    
        <ToggleButton
            android:id="@+id/tbPassword"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20"
            android:checked="true"
            android:paddingBottom="10dp"
            android:text="ToggleButton" />
    </LinearLayout>
    
    <TextView
        android:id="@+id/tvResults"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="invalid" />