ListView too long

5

I created a ListView that shows the songs of my sdcard, but as you can see in the image below it is very long regardless of the quantity of items, I want the height of it to be smaller to display more names, so that the list not too long, and also very ugly this way.

Screenxmlcode:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/wallpaper"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Musica" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btnVoltar"
    android:layout_toRightOf="@+id/btnVoltar"
    android:text="Voltar"
    android:textColor="#ffffffff"
    android:textSize="20sp"
    android:textStyle="bold"
    android:typeface="serif" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/logo" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_toLeftOf="@+id/imageView1"
    android:text="Takion"
    android:textColor="#32CD32"
    android:textSize="20sp"
    android:textStyle="bold"
    android:typeface="serif" />

<ListView
    android:id="@+id/lvPlaylist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true" >

</ListView>

<ImageButton
    android:id="@+id/btnVoltar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/lvPlaylist"
    android:background="#00000000"
    android:src="@drawable/back" />

Player class (screen with play, next, ect, seekBar ...) buttons:

public class Player extends Activity implements View.OnClickListener{

SeekBar music=null;   
AudioManager mgr=null;

static MediaPlayer mp;
ArrayList<File> mySongs;
int position;
Uri u;

Thread updateSeekBar;

ImageButton btnVoltar, btnBackward, btnPrevious, btnNext, btnForward;

private ImageButton btnPlayPause;
private boolean isbtnPlayer = false;
SeekBar progressMusic;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_player);

    mgr=(AudioManager)getSystemService(Context.AUDIO_SERVICE);

    music=(SeekBar)findViewById(R.id.sbMusic);

    initBar(music, AudioManager.STREAM_MUSIC);

    btnVoltar = (ImageButton) findViewById(R.id.btnVoltar);
    btnBackward = (ImageButton) findViewById(R.id.btnBackward);
    btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
    btnPlayPause = (ImageButton) findViewById(R.id.btnPlayPause);
    btnNext = (ImageButton) findViewById(R.id.btnNext);
    btnForward = (ImageButton) findViewById(R.id.btnForward);       

    btnBackward.setOnClickListener(this);
    btnPrevious.setOnClickListener(this);
    btnPlayPause.setOnClickListener(this);
    btnNext.setOnClickListener(this);
    btnForward.setOnClickListener(this);

    progressMusic = (SeekBar) findViewById(R.id.progressMusic);
    updateSeekBar = new Thread(){
        @Override
        public void run() {
            int totalDuration = mp.getDuration();
            int currentPosition = 0;
            while (currentPosition < totalDuration){
                try {
                    sleep(500);
                    currentPosition = mp.getCurrentPosition();
                    progressMusic.setProgress(currentPosition);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    };

    if(mp!=null){
        mp.stop();
        mp.release();

    }

    Intent i = getIntent();
    Bundle b = i.getExtras();
    mySongs = (ArrayList) b.getParcelableArrayList("songlist");
    position = b.getInt("pos", 0);

    u = Uri.parse(mySongs.get(position).toString());
    mp = MediaPlayer.create(getApplicationContext(), u);
    mp.start();

    progressMusic.setMax(mp.getDuration());

    updateSeekBar.start();

    progressMusic.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            mp.seekTo(seekBar.getProgress());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }
    });

    btnVoltar.setOnClickListener(new View.OnClickListener() {       

        @Override
        public void onClick(View v) {

            Intent home = new Intent(Player.this, Musica.class);
            Player.this.startActivity(home);
            Player.this.finish();

        }
    });
}

 private void initBar(SeekBar bar, final int stream) {
        bar.setMax(mgr.getStreamMaxVolume(stream));
        bar.setProgress(mgr.getStreamVolume(stream));

        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          public void onProgressChanged(SeekBar bar, int progress,
                                        boolean fromUser) {
            mgr.setStreamVolume(stream, progress,
                                AudioManager.FLAG_PLAY_SOUND);
          }

          public void onStartTrackingTouch(SeekBar bar) {

          }

          public void onStopTrackingTouch(SeekBar bar) {

          }
        });
      }

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id){
        case R.id.btnPlayPause:
            if(mp.isPlaying()){                 
                isbtnPlayer = true;
                btnPlayPause.setImageResource(R.drawable.player);
                mp.pause();
            }else{
                isbtnPlayer = true;
                btnPlayPause.setImageResource(R.drawable.pause);
                mp.start();
            }break;

        case R.id.btnForward:
            mp.seekTo(mp.getCurrentPosition()+5000);
            break;

        case R.id.btnBackward:
            mp.seekTo(mp.getCurrentPosition()-5000);
            break;

        case R.id.btnNext:
            mp.stop();
            mp.release();
            position = (position+1) %mySongs.size();
            u = Uri.parse(mySongs.get(position).toString());
            mp = MediaPlayer.create(getApplicationContext(), u);
            mp.start();
            progressMusic.setMax(mp.getDuration());
            break;

        case R.id.btnPrevious:
            mp.stop();
            mp.release();
            position = (position-1 < 0)? mySongs.size()-1: position-1;
            u = Uri.parse(mySongs.get(position).toString());
            mp = MediaPlayer.create(getApplicationContext(), u);
            mp.start();
            progressMusic.setMax(mp.getDuration());
            break;              
    }

}

Music Class (Shows the listView with the music):

public class Musica extends Activity {

ListView lv;

String[] items;

ImageButton btnVoltar;

ImageButton btnBack;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.musica);        

    btnVoltar = (ImageButton) findViewById(R.id.btnVoltar);
    btnVoltar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent home = new Intent(Musica.this, Home.class);
            Musica.this.startActivity(home);
            Musica.this.finish();

        }
    });

    lv = (ListView) findViewById(R.id.lvPlaylist);

    final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());

    items = new String[ mySongs.size() ];

    for(int i = 0; i<mySongs.size(); i++){

        //toast(mySongs.get(i).getName().toString());
        items[i] = mySongs.get(i).getName().toString().replace(".mp3", "").replace(".wav", "");

    }

    ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), R.layout.player, R.id.tvPlayer, items);
    lv.setAdapter(adp);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            startActivity(new Intent(getApplicationContext(), Player.class).putExtra("pos", position).putExtra("songlist", mySongs));
        }

    });

}

public ArrayList<File> findSongs(File root){
    ArrayList<File> al = new ArrayList<File>();
    File[] files = root.listFiles();
    for(File singleFile : files){
        if(singleFile.isDirectory() && !singleFile.isHidden()){
            al.addAll(findSongs(singleFile));               
        }else{
            if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")){
                al.add(singleFile);
            }               
        }           
    }       
    return al;
}   

public void toast(String text){
    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();

}

RelativeLayout:

<TextView
    android:id="@+id/tvPlayer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffffffff"
    android:textStyle="bold"
    android:typeface="sans" />

    
asked by anonymous 13.05.2015 / 15:53

6 answers

1

Look for the use of Custom Lists on Android using Adapters. Try to work with the implementation this way because it allows you to define the layout of each line that will be presented.

There is a lot of material on the internet showing how to implement it. I suggest you do a search for a better understanding.

Another tip I can offer you is the implementation of RecyclerView in place of the traditional ListView. It is an "evolution" with improved performance for your application.

Here is an example showing how the ListView application is: Customizing a ListView on Android

The content is of the Alura staff who is an online teaching platform that I really enjoyed and recommend their material: Alura Site

In this link there is an example to implement the RecyclerView and was written by Rafael de Araújo (very well written by sign): Lists with RecyclerView

Here's my contribution to your implementation. I think by following them and looking at the other peer responses you will get a good implementation with excellent performance in your App.

    
04.08.2017 / 13:39
0

The factor is not just shrinking the listView, but putting it the size you want. We know that match_parent, causes it to extend the size of the parent component, and wrap_content, causes the component to become the size that satisfies the data contained in it. So by analyzing your code, I'd try to tinker with two three things:

  • Switch match_parent with android: layout_width="wrap_content" on ListView component.

  • Analyze the operation of the section: getWindow (). setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

  • Parsing the parent element of ListView, which in this case is the RelativeLayout.
  • I hope I have helped!

    Good luck!

        
    13.05.2015 / 20:07
    0

    You can create an xml layout for the ListView row.

    row_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="10dp" >
    
        <TextView
            android:id="@+id/txtName"
            android:layout_width="match_parent"
            android:layout_height="46dp"/>
    
    </LinearLayout>
    

    And create your ArrayAdapter extending from ArrayAdapter

    public class myArrayAdapter extends ArrayAdapter<SUA_CLASSE>{
    
        Context context;
    
    
        public myArrayAdapter (Context ctx) {
            super(ctx, R.layout.row_layout);
            this.context = ctx;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            View rowView = inflater.inflate(R.layout.row_layout, parent, false);
    
            SUA_CLASSE p = this.getItem(position);
    
            txtName = TextView rowView.findViewById(R.id.txtName);
            txtName.setText(SUA_CLASSE.toString());
    
            return rowView;
        }
    
    
        @Override
        public void add(SUA_CLASSE object) {
            super.add(object);
        }
    
        @Override
        public SUA_CLASSE getItem(int position) {
            // TODO Auto-generated method stub
            return super.getItem(position);
        }
    

    }

        
    14.09.2015 / 15:27
    0

    Problem can be here

    android:layout_height="wrap_content" of listview

    This code snippet can cause your getView from the adapter to be called infinitely, and also the problem that happens with you, the ListView layout should have height set to match_parent if you do not want to hide the entire screen with match_parent , consider using weighsum and layout_weight to work with "percentages". Remembering that this must be within a LinearLayout.

    example:

    <LinearLayout
    android:wheigthsum=1 <!-- as somas dos layout_weight deve ser 1 == 100% -->
     ...>
        <LinearLayout
            android:layout_height=0dp
            android:layout_weight=0.3 <!-- 30% -->
            ...>
            <!-- cabeçalho, alguns textviews ou outros componentes aqui -->
        </LinearLayout>
    
        <LinearLayout
            android:layout_height=0dp
            android:wheigthsum=0.5 <!-- 50% -->
            ...>
            <!-- implementa seu listview aqui -->
            <ListView
                android:id="@+id/lvPlaylist"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>
    
        <LinearLayout
            android:layout_height=0dp
            android:layout_weight=0.2 <!-- 20% -->
            ...>
            <!-- rodapé, alguns textviews ou outros componentes aqui -->
        </LinearLayout>
    </LinearLayout>
    
        
    06.08.2016 / 04:32
    0

    You should use margins or some component to limit the size. If you want to leave only the list on the screen, but not filling everything, add margins, if you want to fill this space with another component, use belowof or relativeof the relative layout. Remember, if your application supports different screens, implement other layouts and using rightof or leftof instead of belowof.

        
    09.05.2017 / 19:47
    0
    The problem is probably in your "player" layout file, you can not fully understand its explanation but I believe that your file has a RelativeLayout with a TextView, you should put the "layout_height" property of the two as "wrap_content" this will solve the problem.

        
    04.08.2018 / 12:42