5
I created a ListView
that features the songs of my sdcard, but as you can see in the image below it is very long regardless of the amount of items, I want its height to be smaller to display more names, so that the list is not too long, and also is very ugly that way.
Screen xml code:
<RelativeLayout xmlns: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 buttons, next, ect, seekBar...):
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 (Presents 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" />
Could you enter the code of your list items? The XML you made to use in getView() of the Adapter of this listview.
– Gustavo Bitencourt
@Gustavobitencourt ready posted all the codes.
– Itallo Freire
Sorry, your code is confused. Try using android:layout_width="wrap_content" in Listview.
– Gustavo Bitencourt
More do you know any method to decrease listview? Do not need to understand the code just want an example of how to do this.
– Itallo Freire
unfortunately does not work only by changing the
width
, I tried not to.– Itallo Freire