How to make a Scrollview with multiple Components?

Asked

Viewed 302 times

0

I am trying to get an Activity of a project of mine to contain a considerable amount of Components.

I tried to do this using a Scrollingactivity that contained more than just the "default" textView (the one that comes when it is created), but I found that it is not possible, at least that I know.

My Activity basically contains this model:

inserir a descrição da imagem aqui

But as you can imagine it doesn’t just contain these two questions.

I also tried a Scrollview, but I must not have done the same the right way or simply the same has a limitation of not being able to "exceed" the layout limit.

I would like to know how to put all this in Activity:

inserir a descrição da imagem aqui

With the image I wanted to express that the content I want to add exceeds the same, to the point that I could not use a Scrollview.

I made a code template (only with the first two Buttons, questions and answers) in case it is useful for a better understanding and/or answer:

Mainactivity:

package genesysgeneration.chinterative;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnSim01, btnSim02, btnNao01, btnNao02;
    private TextView tvPergunta01, tvPergunta02, tvResposta01, tvResposta02;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnSim01=(Button)findViewById(R.id.btnSim01);
        btnSim02=(Button)findViewById(R.id.btnSim02);
        btnNao01=(Button)findViewById(R.id.btnNao01);
        btnNao02=(Button)findViewById(R.id.btnNao02);

        tvPergunta01=(TextView)findViewById(R.id.tvPergunta01);
        tvPergunta02=(TextView)findViewById(R.id.tvPergunta02);
        tvResposta01=(TextView)findViewById(R.id.tvResposta01);
        tvResposta02=(TextView)findViewById(R.id.tvResposta02);

    }

    public void onClick(View v){

        switch (v.getId()){
            
            case R.id.btnSim01:
                
                break;
            
            case R.id.btnNao01:
                
                break;
            
            case R.id.btnSim02:
                
                break;
            
            case R.id.btnNao02:
                
                break;
            
        }
        
    }

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="genesysgeneration.chinterative.MainActivity">

    <TextView
        android:id="@+id/tvPergunta01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pergunta 1" />

    <TextView
        android:id="@+id/tvResposta01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/tvPergunta01"
        android:layout_marginTop="104dp"
        android:text="texto com a resposta baseada na escolha do button " />

    <TextView
        android:id="@+id/tvPergunta02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:text="pergunta 2" />

    <Button
        android:id="@+id/btnSim01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/tvPergunta01"
        android:layout_marginTop="26dp"
        android:text="sim" />

    <Button
        android:id="@+id/btnNao01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/btnSim01"
        android:text="nao" />

    <Button
        android:id="@+id/btnSim02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/btnSim01"
        android:layout_alignRight="@+id/btnSim01"
        android:layout_below="@+id/tvPergunta02"
        android:layout_marginTop="68dp"
        android:text="sim" />

    <Button
        android:id="@+id/btnNao02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnNao01"
        android:layout_alignStart="@+id/btnNao01"
        android:layout_alignTop="@+id/btnSim02"
        android:text="nao" />

    <TextView
        android:id="@+id/tvResposta02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/btnSim02"
        android:layout_marginTop="46dp"
        android:text="texto com a resposta baseada na escolha do button " />

</RelativeLayout>

  • Put your activity_main in your question.

  • you speak of xml?

  • yes, exactly.

1 answer

0


In this question about Layout does not fit on the screen after rotation has an answer that can solve your problem. Basically one should use the ScrollView this way below. See:

<ScrollView 
    ...
    ...
    <RelativeLayout
        ...
        ...

    </RelativeLayout>
</ScrollView>

So your file activity_main.xml should look like this:

<?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">

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

        <TextView
            android:id="@+id/tvPergunta01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pergunta 1" />

        <TextView
            android:id="@+id/tvResposta01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/tvPergunta01"
            android:layout_marginTop="104dp"
            android:text="texto com a resposta baseada na escolha do button " />

        <TextView
            android:id="@+id/tvPergunta02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:text="pergunta 2" />

        <Button
            android:id="@+id/btnSim01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/tvPergunta01"
            android:layout_marginTop="26dp"
            android:text="sim" />

        <Button
            android:id="@+id/btnNao01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/btnSim01"
            android:text="nao" />

        <Button
            android:id="@+id/btnSim02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/btnSim01"
            android:layout_alignRight="@+id/btnSim01"
            android:layout_below="@+id/tvPergunta02"
            android:layout_marginTop="68dp"
            android:text="sim" />

        <Button
            android:id="@+id/btnNao02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/btnNao01"
            android:layout_alignStart="@+id/btnNao01"
            android:layout_alignTop="@+id/btnSim02"
            android:text="nao" />

        <TextView
            android:id="@+id/tvResposta02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/btnSim02"
            android:layout_marginTop="46dp"
            android:text="texto com a resposta baseada na escolha do button " />

    </RelativeLayout>
</ScrollView>
  • now how do I edit? scroll view is "tying" everything, I can’t get the commits out of it

  • @Dummy symphores If you are having difficulty with scrollview to drag components on the screen, I advise first you create all the layout with all the components and lastly you add the scrollview.

  • 1

    @Another piece of advice I give is, if yours layout is very large, it would be interesting, instead of using scrollview, to create a dynamic list to add these questions. Search a little about RecyclerView.

  • the temporary solution I found was to put both the scrollview and the relativelayout with the height 2000dp, so I can edit further below

  • Cool! Solved the problem? Need some more example?

  • I still have to perform more tests. qq thing warning, I will already accept your Resp

  • @Dummies Anything we are here! Abs. Good luck!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.