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.