How to make a Listview with components next to the text on each line?

Asked

Viewed 1,101 times

5

Several current Androids systems have menus like the one in the example below:

acesso desligado

Notice that anywhere I touch my finger in the blue area under "Access my location" it turns on or off the switch and enables or disables the block in "Location Sources", and also makes the blue effect on the clicked block.

encostando na tela para ligar o acesso

Without knowing very well the options that Android offers me I tried to do something similar, either would be the basis of what I really intend to do. I made the following code:

Activity.xml

<ListView
    android:id="@+id/lstSegundaTela"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

Minhaactivity.java

lstSegundaTela = (ListView) findViewById(R.id.lstSegundaTela);
List<Map<String, String>> dados = new ArrayList<>();
String[] valores = new String[] {
        "Acesso a minha localização",
        "Satélite de GPS",
        "Local de rede móvel e Wi-Fi"};
String[] descricoes = new String[] {
        "Permitir que os aplicativos que solicitaram sua permissão " +
        "usem seus dados de localização",
        "Permitir que os aplicativos usem o GPS do telefone para " +
        "determinar sua posição",
        "Permitir que os aplicativos usem o serviço de localização " +
        "do Google para determinar seu local mais rapidamente. Dados " +
        "de localização anônimos serão coletados e enviados ao Google"};
for(int i=0; i<valores.length; i++) {
    Map<String, String> linha = new HashMap<>();
    dados.add(linha);
    linha.put("Titulo", valores[i]);
    linha.put("Subtitulo", descricoes[i]);
}
SimpleAdapter adapter = new SimpleAdapter(
        this,
        dados,
        android.R.layout.simple_list_item_2,
        new String[] {"Titulo", "Subtitulo"},
        new int[] {android.R.id.text1, android.R.id.text2});
lstSegundaTela.setAdapter(adapter); 

The result was as follows:

resultado do meu código

My doubts are:

  • How to place Switch and Checkbox components in the Listview line as an example above?

  • How was the "Location Sources" block created? Or it would be another Listview with a title?

  • You can check here: http://www.vogella.com/tutorials/AndroidListView/article.html

  • It was there that I saw to get as far as I got, after that I stopped. There is an example with Checkbox in Listview, but not according to the example I showed above.

  • This is simple, just create an item in xml with a TextView and a Checkbox, and then use this item in ListView.

  • 1

    @Math, these components are called Preferences (a look at http://developer.android.com/guide/topics/ui/settings.html), the titles are headers of Preferencecategory. If that’s not what you want to see in this tutorial by Cyril Mottier: http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/

  • @Jorgeb. You mean I’d set up a template to use for each line? Hence I could for example make two Textview (one with large font and one with small font) and put together a component. There is no more something ready to do that?

  • @Wakim will look yes. Thank you.

  • Look at my answer. If you want another Textview for the title just add also in XML.

Show 2 more comments

1 answer

6


This is simple, just create an item in XML with a TextView and a Checkbox, and then use this item in ListView:

inserir a descrição da imagem aqui

Itemlist layout:

<LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

       <CheckBox
            android:id="@+item/radioButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:checked="false"
            android:gravity="center_vertical|center_horizontal" />

        <TextView
            android:id="@+item/descObs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical|left"
            android:textColor="@color/White"
            android:textSize="20dp" 
            android:text="check"
            android:textIsSelectable="true"/>

</LinearLayout>

NOTE: Notice that the TextView is also selectable:

android:textIsSelectable="true"

In Your Adapter just call the layout like any other:

public class TeuAdaptador extends ArrayAdapter<TeuObjeto> {

    private LayoutInflater mInflater;

    public TeuAdaptador(
            Context contexto,
            List<TeuObjeto> listObj) {

        super(contexto, R.layout.teu_item_xml, listObj);
        mInflater = LayoutInflater.from(contexto);

    }

    //os teus métodos, getView, etc
    ...
}


PS:

By the way I advise you to have the string’s all in res/values-pt/strings.xml as Resource:

<resources>

    <string name="a_minha_string">Esta é a minha string</string>

and then use context.getString(R.string.a_minha_string) to get the same.

  • How to create XML ok. But what would be the type of adapter to put in Listview? In other words, how to use this XML as a Listview item?

  • Look at my issue, I didn’t quite understand if that’s what you wanted.

  • Also look at the ER I just put in. It’ll make your life easier.

  • Okay, you got the preferences that @Wakim listed, but I prefer to do everything by hand, to win practice and to be able to do whatever you want, without worrying about restrictions.

  • Ah, Preferences are mostly used as Settings, as in the image, and is also done manually without Adapter. At first it works like this, although you have a problem if you need to capture clicks individually and not just the entire line.

  • 1

    As my question was "How to make a Listview with components next to the text on each line?" your answer is correct, it works :) but for my current case I think it will be easier to use Preferences, rs.. Still thanks for the reply, helped me.

Show 1 more comment

Browser other questions tagged

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