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);
Related
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?
I'm new on Android Studio and I'm working on a project.
I'm facing this error
I've declared listview but don't know why it's not working.
CODE:
package com.example.animation;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class view_rooms extends AppCompatActivity {
ListView listView;
String mTitle[]={"Room1","Room2","Room3","Room4","Room5"};
String mDescription[]={"Access Room1","Access Room2","Access Room3","Access Room4","Access Room5"};
int images[]={R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_rooms);
listView=findViewById(R.id.ListView);
}
findViewById() searches the view by id into the layout file. In this case into the layout: R.layout.activity_view_rooms. You need to define into this layout one ListView widget and assign one id to search with findViewById().
For example into the layout activity_view_rooms you need to include the view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
And later you can get the instance of view with finViewById() in this way:
package com.example.animation;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class view_rooms extends AppCompatActivity {
ListView listView;
String mTitle[]={"Room1","Room2","Room3","Room4","Room5"};
String mDescription[]={"Access Room1","Access Room2","Access Room3","Access Room4","Access Room5"};
int images[]={R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_rooms);
listView=findViewById(R.id.list_view_id);
}
You need to add this line android:id="#+id/ListView" in your activity_view_rooms.xml
So the code will looks like this:
<ListView
android:id="#+id/ListView"
android:layout_width="match_parent"
android:layout_height="match_parent">
I am using a custom adapter for my ListView which I already don't understand as much as I would like to. I can find information online on how to add a row to ListView but it doesn't integrate nicely into my code, I'm guessing because I'm using a custom adapter. I have my setOnClickListener() method for my button in my Main Activity and have been experimenting there but just cant figure it out I'm also not sure if my method is in the right place?
this is the mainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import static java.util.logging.Logger.global;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] Chores = {""};
ListAdapter MyAdapter = new CustomAdapter(this, Chores);
ListView listViewObject = (ListView)findViewById(R.id.customListView_ID);
listViewObject.setAdapter(MyAdapter);
listViewObject.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String ChoreString = String.valueOf(parent.getItemAtPosition(position));
}
}
);
// this is where I am trying to have button click add another row to my listView
final Button button = (Button) findViewById(R.id.button_ID);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//this is where I am stuck
}
});
}
}
here is my CustomAdapter class
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
class CustomAdapter extends ArrayAdapter{
public CustomAdapter(Context context, String[] choreText) {
super(context, R.layout.custon_listview_row, choreText);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater myInflater = LayoutInflater.from(getContext());
View customView = myInflater.inflate(R.layout.custon_listview_row, parent, false);
String singleListItem = (String) getItem(position);
EditText choreText = (EditText) customView.findViewById(R.id.editText_ID);
ImageButton imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID);
choreText.setText(singleListItem, TextView.BufferType.EDITABLE);
imageButton.setImageResource(R.drawable.clock);
return customView;
}
}
activity_main.xml
<?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:id="#+id/activity_main"
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="com.example.emilythacker.chorelist.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/customListView_ID"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp" />
<Button
android:text="Add Chore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/button_ID" />
</RelativeLayout>
and custom_listview_row.xml
<?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"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="60dp">
<ImageButton
android:layout_width="60dp"
android:scaleType="fitCenter"
app:srcCompat="#drawable/clock"
android:id="#+id/imageButton_ID"
android:layout_height="60dp"
android:background="#null"
android:layout_alignParentRight="true"
android:padding="5dp"
android:layout_weight="1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/editText_ID"
android:layout_alignParentLeft="true"
android:alpha = ".5"
android:hint="Enter chore"
android:maxLines="1"
android:inputType="text"
/>
</RelativeLayout>
First make your list into an ArrayList instead of String array.
private ArrayList<String> arrayList;
In the buttons onClick
arrayList.add("New Item");
((ArrayAdapter)MyAdapter).notifyDataSetChanged();
public void onClick(View v) {
String stringToAdd = ... //
MyAdapter.add(stringToAdd);
}
You will need to add final to the adapter declaration for this to work:
final ListAdapter MyAdapter = new CustomAdapter(this, Chores);
EDIT
To add more than one row at a time:
public void onClick(View v) {
String[] rowsToAdd = ... //
MyAdapter.addAll(rowsToAdd);
}
i have the Problem that my App doesn't even start,it just crashes.
I would be very thankful for help. :)
I already tried to use listView = (ListView) findViewById(R.id.drawerList);
But then there are no Elements in the drawer.
MainActivity.java:
package com.CE.DE;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private DrawerLayout drawerLayout;
private ListView listView;
private String[] planets;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);
planets=getResources().getStringArray(R.array.planets);
listView.setAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,planets));
}
}
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="#+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left">
</ListView>
</android.support.v4.widget.DrawerLayout>
This issue is that you have not initialized your ListView
Solution
create a new ListView object -
#Override
protected void onCreate(Bundle savedInstanceState){
....
listView = (ListView)findViewById(R.id.drawerList);
listView.setAdapter(...);
}
}
Try This Code
package com.CE.DE;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private DrawerLayout drawerLayout;
private ListView listView;
private String[] planets;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);
planets=getResources().getStringArray(R.array.planets);
listView = (ListView)findViewById(R.id.drawerList);
listView.setAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,planets));
}
}
I've set up a toolbar in one of my application's activities, but it crashes everytime I try to go to said activity. Maybe I'm missing something? Can someone help me out with this? Haven't used toolbar before so still a little confused.
Here's the code of activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ProjectCreateScreen extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout1);
Toolbar toolbar = (Toolbar) findViewById(R.id.AwesomeBar);
setSupportActionBar(toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
toolbar.inflateMenu(R.menu.menu_main);
final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);
Button btn = (Button) findViewById(R.id.addBtn);
final ArrayList<String> listItems=new ArrayList<String>();
final ListAdapter addAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.listFrame, listItems);
final ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(addAdapter);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
noProject.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
listItems.add("New Project");
((ArrayAdapter) addAdapter).notifyDataSetChanged();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent switchToEdit = new Intent(ProjectCreateScreen.this,
teamCreateScreen.class);
startActivity(switchToEdit);
}
});
}
And the xml file the activity uses:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rl">
<android.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="60dp"
android:minHeight="60dp"
android:id="#+id/AwesomeBar"
android:background="#android:color/white">
</android.widget.Toolbar>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/addBtn"
android:layout_below="#+id/AwesomeBar"
android:background="#drawable/add_greyslate"/>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/noProjectsNotice"
android:id="#+id/NOPROJECT"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="16sp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/addBtn"
android:id="#+id/lv"
android:visibility="invisible">
</ListView>
</RelativeLayout>
In your XML file replace:
<android.widget.Toolbar
with:
<android.support.v7.widget.Toolbar
Because in your code you are referring to support version.