How to play next song in Media Player in Android Studio? - java

In my Android App, I want to play set of some songs. Here with the help of my code I can play songs by clicking individually. But I can't make play the next song continuously once the song gets finished using Media Player. And also here I can't make a shuffle song also. Here Looping a same song only possible. But my requirement is to play the next song automatically once the firstly played song gets finished. Here is my code
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="65dp"
android:padding="10dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/listView"/>
</LinearLayout>
</ScrollView>
<include
layout="#layout/app_bar_homepage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_homepage"
app:menu="#menu/activity_homepage_drawer" />
</android.support.v4.widget.DrawerLayout>
Java Code:
package com.musicapp;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.constraint.Group;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class MusicActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ListView listView;
List<String> list;
ListAdapter adapter;
MediaPlayer mediaPlayer;
private int currentSongIndex = 0;
View previousSelectedItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
listView = findViewById(R.id.listView);
list=new ArrayList<>();
Field[] fields = R.raw.class.getFields();
for(int i=0;i<fields.length;i++){
list.add(fields[i].getName());
}
adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
listView.setPadding(0,20,0,0);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(mediaPlayer!=null){
mediaPlayer.release();
}
int resID = getResources().getIdentifier(list.get(i),"raw",getPackageName());
mediaPlayer = MediaPlayer.create(MusicActivity.this,resID);
mediaPlayer.start();
if (previousSelectedItem!=null) {
previousSelectedItem.setBackgroundColor(Color.WHITE);
}
previousSelectedItem=view;
view.setBackgroundResource(R.drawable.bg_with_color);
}
});
}
Could anybody help me?

Related

List not displaying in android studio

Main Activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:titleTextColor="#color/white"
android:background="#color/colorPrimary"
android:id="#+id/ToolbarMain"
tools:targetApi="honeycomb">
</android.support.v7.widget.Toolbar>
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:dividerHeight="10dp"
android:divider="#null"
android:layout_below="#+id/ToolbarMain">
</ListView>
</RelativeLayout>
main activity.java
package com.example.admin.ttabledemo;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ListView listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupIViews();
iniToolbar();
}
private void setupIViews(){
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.ToolbarMain);
listview = (ListView)findViewById(R.id.lvMain);
}
private void iniToolbar(){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("TIMETABLE APP");
}
I am trying to build a time table app. List is not displaying. Just the titlebar.
Although in design listview is visible. please help.
Nothing is showing on the screen. I can't figure out what is wrong.
This is my first attempt to build an app. I am a complete beginner.
Use setAdapter() like this way.
String data[] = new String[]{"Most Popular", "UpComing", "Top Rated"};
ListView listview = (ListView) findViewById(R.id.lvMain);
YourAdapterName cma = new YourAdapterName(this, android.R.layout.simple_list_item_1, data);
listview .setAdapter(cma);
You have to add something to show. Try the following code:
String items[]={"one","two","three","four","five"};
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
listview.setAdapter(arrayAdapter);

fragment remove/add issue searchview

I'm having some issues with a searchView in my Fragments.
So, when I'm pressing the search_icon within the actionbar, the method searchClick() is executed.
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:title="#string/app_name"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:onClick="searchClick"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
/>
</menu>
MainActivity.Java
package formelhelfer5_10.formelhelfer5_10;
import android.app.SearchManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements SearchView.OnCloseListener, SearchView.OnQueryTextListener
{
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent sIntent = getIntent();
if (Intent.ACTION_SEARCH.equals(sIntent.getAction())) {
String query = sIntent.getStringExtra(SearchManager.QUERY);
}
}
public void searchClick(MenuItem item){
Fragment frag = new Results();
Fragment rem = new FaecherFrag();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(rem).add(R.id.FragFaecher,frag).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnCloseListener(this);
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onClose() {
Fragment frag = new Results();
Fragment rem = new FaecherFrag();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(rem).add(R.id.results,frag).commit();
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
TextView t = (TextView) findViewById(R.id.tv);
if (query.isEmpty()){return false;}else
{t.setText(query);}
return false;
}
}
searchClick then removes the current Fragment
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="#+id/main_content"
tools:context="formelhelfer5_10.formelhelfer5_10.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="formelhelfer5_10.formelhelfer5_10.MainActivity"
tools:showIn="#layout/activity_main"
android:background="?android:colorBackground">
<fragment
android:id="#+id/FragFaecher"
android:name="formelhelfer5_10.formelhelfer5_10.FaecherFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
FaecherFrag.Java
package formelhelfer5_10.formelhelfer5_10;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Array;
public class FaecherFrag extends ListFragment {
sql sqlo = new sql();
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] m_fach = sqlo.getFaecher(getContext());
ListAdapter adptFach =
new ArrayAdapter<>(
getActivity(), android.R.layout.simple_list_item_1, m_fach);
setListAdapter(adptFach);
//Array m_fach wird als Liste angezeigt
}
#Override
public void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
transFach(position);
//uebergibt die Position des Fachs an die naechste Methode
}
private void transFach(int position){
String[] m_fach = sqlo.getFaecher(getContext());
String sql_fach = m_fach[position]; //String für die SQL-Abfrage. Zeigt welches Fach ausgewaehlt wurde
String[] m_thema = sqlo.getThemen(getContext(), sql_fach);
Intent toThemen = new Intent(getActivity(),Themen.class);
toThemen.putExtra("thema_key",m_thema);
startActivity(toThemen);
}
}
and adds another Fragment that displays the query in a TextView
fragment_results.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/results"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="?android:colorBackground" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv"
android:text="Text"
android:textAppearance="?android:textAppearanceLarge"
android:textStyle="bold"/>
</RelativeLayout>
Results.Java
package formelhelfer5_10.formelhelfer5_10;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Results extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_results, container, false);
return rootView;
}
}
My first Issue is, that when I submit the query, the second Fragment (fragment_results) "disappears" and the normal ListFragment (activity_main) appears. I don't know how to solve this.
The second Issue is, that when I dismiss/close the Search Widget, I expect the OnCloseListener to remove the second Fragment (fragment_results) and show the ListFragment (activity_main). Although I implemented this in the OnCloseListener, nothing happens. Or am I confusing the OnCloseListener with something else?
I would be glad, if you can help me with this.
For first problem, you are setting onClick in xml for menu item. This is wrong and will not call. Please implement onItemMenuSelected using https://developer.android.com/guide/topics/ui/menus.html#RespondingOptionsMenu

mainlistview1 cannot be resolved or is not a field

I am following a tutorial on making list views, but I cannot find the cause of the error
mainlistview1 cannot be resolved or is not a field.
Here is my current code:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.os.Build;
import android.widget.AdapterView.*;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
String[] riverCities = new String[] {"Greenville", "Vicksburg",
"Memphis", "Natchez", "Cape Girardeau", "Helena"};
ListView listView = (ListView)findViewById(R.id.mainListView1);
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.id.mainListView1, riverCities);
listView.setAdapter(adapter);
}
The layout
<LinearLayout 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:orientation="horizontal" >
<ListView
android:id="#+id/mainListView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ListView>
</LinearLayout>
problem:
android.R.id.mainListView1
The mainListView1 is not located in the android default layout id thus the compiler cannot find it.
solution:
Change it to the default list view item id
ListAdapter adapter = new ArrayAdapter<String>(this ,android.R.layout.simple_list_item_1, riverCities);

android - list cannot be resolved or is not a field

I ve been searching for hours and I can't figure out what I'm doing wrong.
"list cannot be resolved or is not a field"
here is my search.java file:
package com.example.testxxx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Search extends ListActivity {
private TextView mTextView;
private ListView mListView;
private String query;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mTextView = (TextView) findViewById(R.id.text);
mListView = (ListView) findViewById(R.id.list);
}
}
and here is my search.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<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: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=".Search" >
<TextView
android:id="#+id/text"
android:textColor="?android:textColorPrimary"
android:background="#android:drawable/title_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp" />
</RelativeLayout>
I have noticed that if i change
android:id="#android:id/list"
to
android:id="#android:id/list"
then it will find it, but if i do so, i will have another error because I am extending listactivity that need the listview to have the ID set this way (android:id="#android:id/list")
Do you have a solution? Thank you!
Just replace
mListView = (ListView) findViewById(R.id.list);
with
mListView = (ListView) findViewById(android.R.id.list);
or simply you can call
mListView = getListView()
as you are extending ListActivity and your xml layout contains a ListView with id android.R.id.list
when you referencing to internal ID's (ID's may be drawable ID's, String ID's or any resource) then those ID's will not be generated in your R.java. you have to import those ID's from where you referenced.

How to load all user installed apps into gridview and make them clickable in android

I am making an android launcher, and i need to know how i can load all the apps into a gridview. I want system stuff like calculator and all of that to show up as well. How can i do that? I have search for a long time now without any solution. Please help me and thanks very much!
Java code:
package com.mysoftware.mysoftwareos.launcher;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.LinearLayout;
public class AllAppsActivity extends Activity {
LinearLayout allappsLayout;
GridView allappsGridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allapps_screen);
//Import views
allappsLayout = (LinearLayout)findViewById(R.id.allappsLayout);
allappsGridView = (GridView)findViewById(R.id.allappsGridView);
//Setup animation for the main layout
Animation a = AnimationUtils.loadAnimation(this, R.anim.fadeout);
a.reset();
allappsLayout.clearAnimation();
allappsLayout.startAnimation(a);
//Load all apps into the apptray
}
}
My xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/allappsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wallpaper"
android:orientation="vertical" >
<GridView
android:id="#+id/allappsGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" >
</GridView>
</LinearLayout>
ADW.Launcher is an open source android launcher...
browse around: http://code.google.com/p/adw-launcher-android/

Categories

Resources