Search code examples
androidonclickandroid-linearlayoutandroid-relativelayoutandroid-scrollview

set content in scrollview ? Android


I have an Xml that add LinearLayout and RelativeLayout in ScrollView by programmatically.When i add Text with OnclickButton for first time show me message but for 2nd time get me crash :

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

    <ScrollView
        android:id="@+id/scrollID"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >


    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="true"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:weightSum="1" >

        <EditText
            android:id="@+id/txtInpuConversation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:hint="Text" >

            <requestFocus />
        </EditText>
        <Button
            android:id="@+id/btnSend"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Click" />

    </LinearLayout>

</LinearLayout> 

My code :

   public class MainActivity extends Activity {
    String Medtconversation;
    EditText edtconversation;
    TextView txtviewUser;
    LinearLayout rilative;
    RelativeLayout relativeLayout;
    LinearLayout firstLinearLayout;
    ScrollView sclView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
        sclView = (ScrollView) findViewById(R.id.scrollID);
        Button btn = (Button) findViewById(R.id.btnSend);



         final Context context = this;

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Medtconversation = edtconversation.getText().toString();
                txtviewUser = new TextView(MainActivity.this);
                txtviewUser.setText(Medtconversation);
                relativeLayout = new RelativeLayout(context);
                firstLinearLayout= new LinearLayout(context);
                LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                relativeLayout.setLayoutParams(LLParamsT);
                relativeLayout.addView(txtviewUser,
                        new LayoutParams(LayoutParams.WRAP_CONTENT,
                               LayoutParams.WRAP_CONTENT));

                firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
                LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                firstLinearLayout.setLayoutParams(LLParams);
                firstLinearLayout.addView(relativeLayout);

Crash here now======>sclView.addView(firstLinearLayout, new
                          LayoutParams(LayoutParams.WRAP_CONTENT,
                                     LayoutParams.WRAP_CONTENT));
                edtconversation.setText("");
            }
        });
    }
}

I need that when i click on Button and send message for 2nd time create a new RelativeLayout in LinearLayout for show.(In scrollView)

enter image description here

Error :

    AndroidRuntime
MainActivity$1.onClick(MainActivity.java:54)

enter image description here


Solution

  • mmmmm. I recommend that you using from table and you can add programmatically your TextView and etc. simply :

    public class MainActivity extends Activity {
        String Medtconversation;
        EditText edtconversation;
        TextView txtviewUser;
        TextView txtviewUserT;
        RelativeLayout relativeLayout;
        LinearLayout firstLinearLayout;
        TableRow tr;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
            Button btn = (Button) findViewById(R.id.btnSend);
            final TableLayout tl = (TableLayout) findViewById(R.id.tbl);
            final Context context = this;
    
            btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    Medtconversation = edtconversation.getText().toString();
                    txtviewUser = new TextView(MainActivity.this);
                    txtviewUserT = new TextView(MainActivity.this);
                    txtviewUserT.setText("OK");
                    txtviewUser.setText(Medtconversation);
                     tr = new TableRow(context);
                     tr.addView(txtviewUser);
                     tr.addView(txtviewUserT);
                     tl.addView(tr);
                    edtconversation.setText("");
                }
            });
        }
    } 
    

    Your XML :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/xxx"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
    <ScrollView
        android:id="@+id/scrollID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" >
    
        <TableLayout
            android:id="@+id/tbl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
    
        </TableLayout>
    
    </ScrollView>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="true"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:weightSum="1" >
    
            <EditText
                android:id="@+id/txtInpuConversation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:hint="Text" >
    
                <requestFocus />
            </EditText>
            <Button
                android:id="@+id/btnSend"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="Click" />
    
        </LinearLayout>
    
    </LinearLayout>