Why does my App crash? Drawer - java

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));
}
}

Related

Facing problem in findView (Android Studio)

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">

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);

Displaying imageslider within a fragment

I have created an ImageSlider as a new activity seperate to my main project in order to see if it worked. Well guess what, it does! The problem is it is activity based, and when I try and implement it into my other project which has fragments I get so many errors within my MainActivity. Could someone help me please? I believe the issue might be related to my main project also having a navigation drawer & a fragment within the MainActivity main page of the app.
MainActivity test project code which works:
package com.example.user1.imageslidertest;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
LinearLayout sliderDotspanel;
private int dotscount;
private ImageView[] dots;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.ViewPager);
sliderDotspanel = (LinearLayout) findViewById(R.id.SliderDots);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
viewPager.setAdapter(viewPagerAdapter);
dotscount = viewPagerAdapter.getCount();
dots = new ImageView[dotscount];
for(int i = 0; i < dotscount; i++){
dots[i] = new ImageView(this);
dots[i].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.nonactive_dot));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
sliderDotspanel.addView(dots[i], params);
}
dots[0].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.active_dot));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
for(int i = 0; i< dotscount; i++){
dots[i].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.nonactive_dot));
}
dots[position].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.active_dot));
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
ViewPagerAdapter code:
package com.example.user1.imageslidertest;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ViewPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Integer [] images = {R.drawable.a45large, R.drawable.a45largeintior, R.drawable.a45largerear};
public ViewPagerAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.custom_layout, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(images[position]);
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
activity_main.xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.user1.imageslidertest.MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/ViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="#+id/SliderDots"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ViewPager"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:paddingTop="300dp">
</LinearLayout>
</android.support.constraint.ConstraintLayout>
custom_layout.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="292dp"
app:srcCompat="#drawable/a45large" />
</LinearLayout>
My MainActivity from my main project with fragments and navigation drawer (where the issues are when I put the test MainActivity code in):
package com.example.user1.mainproject;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// Code to display Home Fragment on App Launch Page
HomeFragment homeFragment = new HomeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(
R.id.relativelayout_for_fragment,
homeFragment,
homeFragment.getTag()
).commit();
}
This is the biggest issue I've faced within my project so would really apreciate some help, thanks!

Android toolbar doesn't display icons and titles

I am using toolbar to make a customized action bar. I have a class called BaseActivity which I inherit in all other activity. It merges the toolbar to actionbar. But the thing is I ma not getting the toolbar icons and titles visible.
Here's my BaseActivity:
package com.meroanswer.classes;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.meroanswer.R;
public abstract class BaseActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(getResources().getColor(R.color.primary_dark));
}
}
}
protected abstract int getLayoutResource();
protected void setActionBarIcon(int iconRes) {
try{
toolbar.setNavigationIcon(iconRes);
}
catch (NullPointerException e){
Log.d("Roshan", "Exception");
}
}
}
Here's my derived page code
package com.meroanswer;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.meroanswer.DrawerAdapter;
import com.meroanswer.MainActivity;
import com.meroanswer.R;
import com.meroanswer.SlidingTabLayout;
import com.meroanswer.ViewPagerAdapter;
import com.meroanswer.classes.BaseActivity;
import com.meroanswer.classes.Helper;
import com.meroanswer.connection.AsyncPut;
import com.meroanswer.connection.Connection;
import com.meroanswer.database.model.Stream;
import com.meroanswer.database.model.User;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SettingsPage extends BaseActivity {
public WebView webview;
Helper helper;
Context context;
private String temp_str;
private DrawerLayout drawer;
Toolbar toolbar;
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager mLayoutManager;
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout Drawer;
String TITLES[] = {
"Coupons",
"Upgrade to Premium",
"Rate us",
"Share App",
"Settings",
"About",
"Log out"
};
int ICONS[] = {
R.drawable.coupon,
R.drawable.premium,
R.drawable.star,
R.drawable.share,
R.drawable.settings,
R.drawable.about,
R.drawable.logout
};
String NAME = "A B";
String EMAIL = "a#b.com";
int PROFILE = R.drawable.profile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_page);
getSupportActionBar().setIcon(R.drawable.ic_ab_drawer);
getSupportActionBar().setTitle("Settings");
helper = new Helper(context);
webview = (WebView) findViewById(R.id.dashboard_webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setUseWideViewPort(false);
settings.setLoadsImagesAutomatically(true);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
int android_version = android.os.Build.VERSION.SDK_INT;
if (android_version >= 11) {
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
webview.addJavascriptInterface(new SettingsProvider(this, webview),
"Android");
webview.loadUrl("file:///android_asset/ma_html/setup.html");
drawer = (DrawerLayout) findViewById(R.id.drawer);
//drawer.setDrawerShadow(R.drawable.drawer_shadow, Gravity.START);
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View
mRecyclerView.setHasFixedSize(true); // Letting the system know that the list objects are of fixed size
mAdapter = new DrawerAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE); // Creating the Adapter of MyAdapter class(which we are going to see in a bit)
// And passing the titles,icons,header view name, header view email,
// and header view profile picture
mRecyclerView.setAdapter(mAdapter); // Setting the adapter to RecyclerView
mLayoutManager = new LinearLayoutManager(this); // Creating a layout Manager
mRecyclerView.setLayoutManager(mLayoutManager); // Setting the layout Manager
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings_page, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawer.openDrawer(Gravity.START);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected int getLayoutResource() {
return R.layout.activity_settings_page;
}
}
}
and here's my layout resource file for the activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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:background="#color/white"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"/>
<WebView
android:id="#+id/dashboard_webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</WebView>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
<!--<ListView
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/windowBackgroundColor"/>-->
</android.support.v4.widget.DrawerLayout>
menu file
<menu 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"
tools:context="com.meroanswer.SettingsPage">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
Do the following and check whether you can see changes:
Change the menu.xml as shown below:
<menu 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"
tools:context="com.meroanswer.SettingsPage">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="always" /> // This line has been changed from never to always
</menu>
In your onOptionsItemSelected method add the following part:
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(this, "Settings button has been selected", Toast.Length_Long).show();
return true;
}
After these run the application and click on the settings text ion tool bar you will see a toast message.
Hope it helps.

android toolbar not working properly

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.

Categories

Resources