ListView cap to TextView

0

You see, I have to do an exercise in which, I have a textview next to a ListView, which has several names. When you click on the ListView, the text of the TextView will be changed to the element of the chosen ListView. But I find that the listview is placed on top of the screen and covers the textview.

But when I execute it, I find this other:

I will pass the code of the activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<ListView
    android:id="@+id/Lista"
    android:layout_width="278dp"
    android:layout_height="332dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.355"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.573">

</ListView>

<TextView
    android:id="@+id/Texto"
    android:layout_width="203dp"
    android:layout_height="39dp"
    android:layout_gravity="center_vertical"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Nombre"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.471"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.016" />

Edit: Just in case, I will also pass the code that I have in Main_Activity.java, in case there was the fault.

package com.example.pcx.textos;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends ListActivity{
    String nombres[]={"Paco","Rita","Carlos","Mario"};
    ListView lista;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        lista=findViewById(R.id.Lista);
        ArrayAdapter <String> adaptador=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,nombres);
        setListAdapter(adaptador);
        getListView().setSelection(0);
    }

    public void OnItemSelected(ListView p, View v, int position, long id){
        CharSequence nombre=((TextView)v).getText();
        TextView superior=findViewById(R.id.Texto);
        superior.setText(nombre);
    }
}

Aside, I'll upload an attempt in which I group the objects in RelativeLayout, which is the current state.

    
asked by Miguel Alparez 14.01.2018 в 11:46
source

1 answer

2

Actually there are several ways to achieve what you want, could be with RelativeLayout , LinearLayout or any other type of layout, since you ask is a very basic design and with any layout you can achieve. Of course, depending on the design you want to make, it is better to use one than the other.

In this case, I will use a RelativeLayout for the design.

Your problem is mainly that the position of the components ( TextView , ListView ) is linked to the layout, which in this case are the edges of the screen, since the layout occupies the entire screen. And being linked to the layout, the position of these changes according to the size of the screen. To keep the distance of the components always the same, you can link them together and so no matter what screen position they are in, they will always have the same distance.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/Texto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Hola Mundo"
        android:textSize="24sp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"/>

    <ListView
        android:id="@+id/Lista"
        android:layout_width="278dp"
        android:layout_height="332dp"
        android:layout_below="@+id/Texto"
        android:layout_marginTop="25dp"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

In this case the ListView is linked to the TextView through the property android:layout_below , this property places one component under another. To place one component under another, you must specify the id of that component.

The android:layout_centerHorizontal property aligns the component to the horizontal center of the layout.

The other properties used are well known, so I do not think there is a need to explain them. Keep in mind that some properties that you can use in the components are unique to the layout you are using. For example, in a linearLayout you can not use android:layout_below or android:layout_centerHorizontal , since they are properties of RelativeLayout .

To better understand how the design of the Android user interfaces works, I recommend you read this official guide (in Spanish), which talks about layout designs.

Update

Seeing your MainActivity I see that you have several errors so the view of your application is not displayed correctly.

Your activity extends from ListActivity which is a special activity for lists. Well, if you are going to extend that activity you have to know a few things.

This activity implements its own layout with a listView that occupies the whole screen, if you do not overwrite the layout of the activity it will show the layout of ListActivity , which is what you are doing now and why the list is shown occupying the whole screen. To overwrite the activity layout use the setContentView() method.

ListActivity already has a ListView , so it is not necessary to create a new one, you can use the one you already have.

At the ListView of the activity_main you have to assign the id @android:id/list , which is the id used by ListActivity to link the list with the ListView you already have.

The OnItemSelected() method that you use to listen to the events of ListView does not work, for the reason that it is a method that you created yourself and that has no relation to the events of ListView . Even Android Studio (AS) should show you the name of the gray color method, which indicates that you are not using it. The method you should use to listen to the events is onListItemClick() , which is a legacy method of ListActivity .

The TextView you have to link in onCreate() , the views are usually created there. The TextView you have to declare as a global variable so that you can use it in the onCreate , the onListItemClick() and any other place in the class.

AS should mark you as an "incompatible types" syntax error in both the ListView and the TextView , since you are not casting the objects to convert them from type view to type TextView or type ListView . Even, AS must throw you an error when trying to execute the project.

Knowing all the errors and correcting them your code should look like this:

MainActivity

public class MainActivity extends ListActivity {

    TextView superior;
    String nombres[]={"Paco","Rita","Carlos","Mario"};

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

        superior = (TextView) findViewById(R.id.Texto);

        ArrayAdapter <String> adaptador = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, nombres);
        setListAdapter(adaptador);

    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        CharSequence nombre = ((TextView) v).getText();
        superior.setText(nombre);
    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/Texto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Hola Mundo"
        android:textSize="24sp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"/>

    <ListView
        android:id="@android:id/list"
        android:layout_width="278dp"
        android:layout_height="332dp"
        android:layout_below="@+id/Texto"
        android:layout_marginTop="25dp"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

So you can better understand how the activity ListActivity works, I recommend you read the documentation official Android.

    
answered by 14.01.2018 / 15:21
source