I am new to android. I have being trying to implement job portal application. Now in trying to invoke webservices and getting responses. But my problem is that when I am calling webservice everything is working fine but user details like username and email, id are not displaying on navigation drawer header. Please help me out.
Here is my piece of code:
public class UserHome extends FragmentActivity {
private static final CharSequence Vanaja = null;
private static final int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String OPERATION_NAME = "loginvalidate";
static boolean errored = false;
private static final CharSequence Home = null;
private DrawerLayout mDrawerLayout;
protected ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
protected CharSequence mDrawerTitle;
// used to store app title
protected CharSequence mTitle;
int childcount;
// slide menu items
protected String[] navMenuTitles;
private TypedArray navMenuIcons;
public String emailvalue,passwordvalue;
String emailid="";
String password="";
SoapObject result;
protected ArrayList<NavDrawerItem> navDrawerItems;
protected NavDrawerListAdapter adapter;
protected FrameLayout frameLayout;
TextView name,emailid1;
public void getLoginValues(String emailid,String password)
{ emailvalue=emailid;
passwordvalue=password;
}
final String s=emailid;
final String s1=password;
/*public void getLoginValues(String editTextUsername,
String editTextPassword) {
emailvalue=editTextUsername;
passwordvalue=editTextPassword;
}
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation1);
name = (TextView)findViewById(R.id.industry11);
emailid1=(TextView)findViewById(R.id.mail);
try
{
final ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#BDBDBD")));
#SuppressWarnings("unused")
AsyncTaskRunner async=(AsyncTaskRunner) new AsyncTaskRunner(this).execute();
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items11);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons22);
mDrawerLayout=(DrawerLayout)findViewById(R.id.drawerlayout);
mDrawerList = (ListView) findViewById(R.id.slidermenu);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.userslider, mDrawerList, false);
mDrawerList.addHeaderView(header, null, false);
mDrawerLayout.closeDrawers();
RelativeLayout update=(RelativeLayout)findViewById(R.id.update);
update.setOnClickListener(new View.OnClickListener() {
#SuppressWarnings("unused")
#Override
public void onClick(View v){
switch (v.getId()) {
case R.id.update:
//what to put here
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_container, new ProfileUpdate());
ft.commit();
bar.setTitle(" Updae Profile");
break;
}
}
}
);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.icd2, //nav menu toggle icon
R.string.drawer_open, // nav drawer open - description for accessibility
R.string.drawer_close // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
bar.setIcon(R.color.transparent);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(1);
}}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
#SuppressWarnings("unused")
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment=null;
String title="null";
switch (position) {
case 1:
fragment =new SearchJobs1();
title="";
/* Intent intent1 = new Intent(getApplicationContext(),HomeFragment.class);
startActivity(intent1);*/
break;
case 2:
fragment =new LatestJobs();
/* Intent intent2 = new Intent(getApplicationContext(), Homepage.class);
startActivity(intent2);*/
break;
case 3:
fragment =new AppliedJobs();
/* Intent intent3 = new Intent(getApplicationContext(), FindPeopleFragment.class);
startActivity(intent3);*/
break;
case 4:
fragment =new ChangePassword();
/*Intent intent4 = new Intent(getApplicationContext(), CommunityFragment.class);
startActivity(intent4);*/
break;
case 5:
fragment =new Unsubscribe();
/* Intent intent5 = new Intent(getApplicationContext(),PagesFragment.class);
startActivity(intent5);*/
break;
case 6:
fragment =new LogOut();
Intent intent6 = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent6);
break;
/*case 6:
fragment =new ContactUs();
Intent intent6 = new Intent(getApplicationContext(),PhotosFragment.class);
startActivity(intent6);
break;
case 7:
fragment =new AboutUs();
Intent intent6 = new Intent(getApplicationContext(),PhotosFragment.class);
startActivity(intent6);
break;*/
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position-1);
setTitle(navMenuTitles[position-1]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
getActionBar().setTitle(" " + mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
public void setActionBarTitle(String string) {
// TODO Auto-generated method stub
getActionBar().setTitle(" " + string);
}
public void setActionBarImage(ImageView view)
{
Log.d("Tittlename", "image is"+view);
getActionBar().setCustomView(view);
}
private class AsyncTaskRunner extends AsyncTask<Void,Void,Void> {
String result1;
UserHome outer;
public AsyncTaskRunner(UserHome outer) {
this.outer = outer;
}
private String TAG;
#Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}
#Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "doInBackground");
WebserviceCall service=new WebserviceCall();
try
{
/*if(isNetworkAvailable())
{*/
SoapObject result;
result=service.invokeLoginWS(emailvalue, passwordvalue, OPERATION_NAME);
/*
String s=outer.emailid;
String s1=outer.password;
=======
/*String s=UserHome.this.emailid;
String s1=UserHome.this.password;*/
/*
result=service.invokeLoginWS(s,s1," ");*/
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi.inflate(R.layout.user_home, null);
String resultvalue=result.getProperty("firstname").toString();
emailid= result.getProperty(0).toString();
password = result.getProperty(1).toString();
name.setText(resultvalue);
resultvalue=result.getProperty("emailid").toString();
emailid1.setText(resultvalue);
/*else
{
Intent intent1 = new Intent(getActivity(),InternetProblem.class);
startActivity(intent1);
}*/
}
catch(Exception e)
{
Handler refresh = new Handler(Looper.getMainLooper());
refresh.post(new Runnable() {
public void run()
{
try {
Intent intent1 = new Intent(getApplicationContext(),InternetProblem.class);
startActivity(intent1);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
/*Handler refresh = new Handler(Looper.getMainLooper());
refresh.post(new Runnable() {
public void run()
{
try {
serviceCall();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}); */
/* int childCount = result.getPropertyCount();
*/
return null;
}
#Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
}
}
}
Related
I find it hard to use Fragment because I still don't have enough knowledge about it, so I tried to create my project using an Activity class first, but I still have to convert it into fragment cause I was using a left nav. I need someone's guide because I can't seem to find anything on the internet
Everything turns out to be fine but when I change this section of the code
new ReportsTab.GetHttpResponse(ReportsTab.this).execute();
It would display an error "Gethttpresponse cannot be applied"
This is my Activity Class
public class Reports extends AppCompatActivity {
ListView ReportsListView;
ProgressBar progressBar;
Button status;
String HttpUrl = "http://................../reports_app2.php";
List<String> IdList = new ArrayList<>();
AlertDialog alertDialog;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reports);
ReportsListView = (ListView)findViewById(R.id.listview1);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
new GetHttpResponse(Reports.this).execute();
//Adding ListView Item click Listener.
ReportsListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(Reports.this,MainActivity.class);
// Sending ListView clicked value using intent.
intent.putExtra("ListViewValue", IdList.get(position).toString());
startActivity(intent);
//Finishing current activity after open next activity.
finish();
}
});
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
NavigationView nvDrawer = (NavigationView) findViewById(R.id.nv);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setupDrawerContent(nvDrawer);
}
private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
public Context context;
String JSonResult;
List<ReportsJava> ReportsList;
public GetHttpResponse(Context context)
{
this.context = context;
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... arg0)
{
// Passing HTTP URL to HttpServicesClass Class.
HttpServicesClass httpServicesClass = new HttpServicesClass(HttpUrl);
try
{
httpServicesClass.ExecutePostRequest();
if(httpServicesClass.getResponseCode() == 200)
{
JSonResult = httpServicesClass.getResponse();
if(JSonResult != null)
{
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(JSonResult);
JSONObject jsonObject;
ReportsJava reports;
ReportsList = new ArrayList<ReportsJava>();
for(int i=0; i<jsonArray.length(); i++)
{
reports = new ReportsJava();
jsonObject = jsonArray.getJSONObject(i);
// Adding Student Id TO IdList Array.
IdList.add(jsonObject.getString("id").toString());
//Adding Student Name.
if("0".equals(jsonObject.getString("status").toString()))
{
reports.LightpostStatus = "BLACKOUT";
}
else if("1".equals(jsonObject.getString("status").toString()))
{
reports.LightpostStatus = "FIXED";
}
else if("2".equals(jsonObject.getString("status").toString()))
{
reports.LightpostStatus = "DEFECTIVE";
}
reports.LightpostName = jsonObject.getString("lightpost_code").toString();
reports.LightpostAddress = jsonObject.getString("lightpost_location").toString();
reports.LightpostTime = jsonObject.getString("lightpost_time").toString();
ReportsList.add(reports);
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
Toast.makeText(context, httpServicesClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
progressBar.setVisibility(View.GONE);
ReportsListView.setVisibility(View.VISIBLE);
ReportsListAdapterClass adapter = new ReportsListAdapterClass(ReportsList, context);
ReportsListView.setAdapter(adapter);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menuLogout:
SharedPrefManager.getInstance(this).logout();
finish();
startActivity(new Intent(this, MainActivity.class));
break;
case R.id.menuSettings:
Toast.makeText(this, "You clicked settings", Toast.LENGTH_LONG).show();
break;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
public void selectItemDrawer(MenuItem menuItem){
Fragment myFragment = null;
Class fragmentClass;
switch (menuItem.getItemId()){
case R.id.viewlights:
fragmentClass= ViewLightpost.class;
break;
case R.id.viewreports:
fragmentClass= ReportsTab.class;
break;
case R.id.viewaccount:
fragmentClass= EditAccount.class;
break;
case R.id.viewabout:
fragmentClass= AboutTab.class;
break;
default:
fragmentClass= ViewLightpost.class;
}
try{
myFragment=(Fragment) fragmentClass.newInstance();
}
catch (Exception e)
{
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flcontent,myFragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawerLayout.closeDrawers();
}
private void setupDrawerContent(NavigationView navigationView){
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
selectItemDrawer(item);
return false;
}
});
}
}
new ReportsTab.GetHttpResponse(ReportsTab.this.getActivity()).execute();
Get the activity from the fragment instance.
I am creating an app with a slider and it was working fine and then I added another view and implemented an OnClickListener() for the newly added Views after that I am getting the following error when I open my app
Process: com.spintum.preexam, PID: 31905
java.lang.NoClassDefFoundError: com.spintum.preexam.CustomList$DrawerItemHolder
at com.spintum.preexam.CustomList.getView(CustomList.java:38)
at android.widget.AbsListView.obtainView(AbsListView.java:2338)
at android.widget.ListView.makeAndAddView(ListView.java:1812)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1645)
at android.widget.AbsListView.onLayout(AbsListView.java:2149)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:931)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2317)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2023)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
The class in which the logcat shows the NoClassDefFound error is as follows
public class CustomList extends ArrayAdapter<DrawerItem> {
Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
public CustomList(Context context, int layoutResourceID,
List<DrawerItem> listItems) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
DrawerItemHolder drawerHolder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view.findViewById(R.id.drawer_item_text);
drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_item_icon);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
dItem.getImgResID()));
drawerHolder.ItemName.setText(dItem.getItemName());
return view;
}
private static class DrawerItemHolder {
TextView ItemName;
ImageView icon;
}
}
The error is shown in drawerHolder = new DrawerItemHolder(); line.
I have not made any changes to this class so I am thinking the problem might be caused by the extra views that I added in my original activity that acts as a fragment container. The activity's code is as follows
public class Home extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
private static int pos;
List<DrawerItem> dataList = new ArrayList<DrawerItem>();
View statistics,test,syllabus,share;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
this.getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.color.background));
this.overridePendingTransition(R.layout.fade_in, R.layout.fade_out);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
statistics=findViewById(R.id.statistics_container);
test=findViewById(R.id.test_container);
syllabus=findViewById(R.id.syllabus_container);
share=findViewById(R.id.share_container);
statistics.setOnClickListener(mBottomDrawerListener);
syllabus.setOnClickListener(mBottomDrawerListener);
test.setOnClickListener(mBottomDrawerListener);
share.setOnClickListener(mBottomDrawerListener);
TextView image=(TextView)findViewById(R.id.statistics_image);
TextView text=(TextView)findViewById(R.id.statistics);
image.setTextColor(Color.parseColor("#03a9f4"));
text.setTextColor(Color.parseColor("#03a9f4"));
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START);
int width = getResources().getDisplayMetrics().widthPixels;
DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerList.getLayoutParams();
//params.width = (width * 75) / 100;
mDrawerList.setLayoutParams(params);
// set up the drawer's list view with items and click listener
/*
* mDrawerList.setAdapter(new ArrayAdapter<String>(this,
* R.layout.list_item, mPlanetTitles));
*/
dataList.add(new DrawerItem("Home", R.drawable.home));
dataList.add(new DrawerItem("Intelligence Questionnaire",
R.drawable.bulb));
dataList.add(new DrawerItem("Change Country", R.drawable.location));
dataList.add(new DrawerItem("LeaderBoard", R.drawable.cup));
dataList.add(new DrawerItem("How to play", R.drawable.help));
dataList.add(new DrawerItem("Exam Tips", R.drawable.tip));
mDrawerList.setAdapter(new CustomList(Home.this, R.layout.list_item,dataList));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */ mDrawerLayout, /* DrawerLayout object */R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
Typeface iconfont=FontManager.getTypeface(getApplicationContext(),FontManager.FONTAWESOME);
FontManager.markAsIconContainer(findViewById(R.id.btm_nav_bar),iconfont);
}
public View.OnClickListener mBottomDrawerListener=new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager=getFragmentManager();
setBlackText(v);
TextView image,text;
switch (v.getId()){
case R.id.statistics_container:
fragmentManager.beginTransaction().replace(R.id.content_frame,new HomePage()).commit();
image=(TextView)v.findViewById(R.id.statistics_image);
text=(TextView)v.findViewById(R.id.statistics);
image.setTextColor(Color.parseColor("#03a9f4"));
text.setTextColor(Color.parseColor("#03a9f4"));
break;
case R.id.syllabus_container:
image=(TextView)v.findViewById(R.id.syllabus_image);
text=(TextView)v.findViewById(R.id.syllabus_btm);
image.setTextColor(Color.parseColor("#03a9f4"));
text.setTextColor(Color.parseColor("#03a9f4"));
break;
case R.id.test_container:
image=(TextView)v.findViewById(R.id.test_image);
text=(TextView)v.findViewById(R.id.Tests);
image.setTextColor(Color.parseColor("#03a9f4"));
text.setTextColor(Color.parseColor("#03a9f4"));
break;
case R.id.share_container:
image=(TextView)v.findViewById(R.id.share_image);
text=(TextView)v.findViewById(R.id.share_btm);
image.setTextColor(Color.parseColor("#03a9f4"));
text.setTextColor(Color.parseColor("#03a9f4"));
break;
}
}
};
public void setBlackText(View v){
TextView img[]=new TextView[4];
TextView t1,t2,t3,t4;
img[0]=(TextView)findViewById(R.id.statistics_image);
t4=(TextView)findViewById(R.id.statistics);
img[1]=(TextView)findViewById(R.id.test_image);
t1=(TextView)findViewById(R.id.Tests);
img[2]=(TextView)findViewById(R.id.syllabus_image);
t2=(TextView)findViewById(R.id.syllabus_btm);
img[3]=(TextView)findViewById(R.id.share_image);
t3=(TextView)findViewById(R.id.share_btm);
for(TextView singleTextView:img){
singleTextView.setTextColor(Color.parseColor("#000000"));
}
t1.setTextColor(Color.parseColor("#000000"));
t2.setTextColor(Color.parseColor("#000000"));
t3.setTextColor(Color.parseColor("#000000"));
t4.setTextColor(Color.parseColor("#000000"));
}
/*
* #Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater
* inflater = getMenuInflater(); inflater.inflate(R.menu.activity_main,
* menu); return super.onCreateOptionsMenu(menu); }
*
* Called whenever we call invalidateOptionsMenu()
*
* #Override public boolean onPrepareOptionsMenu(Menu menu) { // If the nav
* drawer is open, hide action items related to the content view boolean
* drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
* //menu.findItem(R.id.action_websearch).setVisible(!drawerOpen); return
* super.onPrepareOptionsMenu(menu); }
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch (item.getItemId()) {
case 1:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available,
Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
/*
* Fragment fragment = new HomePage(); Bundle args = new Bundle();
* args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
* fragment.setArguments(args);
*/
pos = position;
FragmentManager fragmentManager = getFragmentManager();
switch (pos) {
case 0:
fragmentManager.beginTransaction().replace(R.id.content_frame, new Fragment_Statistics()).commit();
break;
case 1:
break;
case 2:
fragmentManager.beginTransaction().replace(R.id.content_frame, new Country()).commit();
case 3:break;
case 4:break;
case 5:break;
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
The DrawerItem class used in the Home activity is given below
public class DrawerItem {
String ItemName;
int imgResID;
public DrawerItem(String itemName, int imgResID) {
super();
ItemName = itemName;
this.imgResID = imgResID;
}
public String getItemName() {
return ItemName;
}
public void setItemName(String itemName) {
ItemName = itemName;
}
public int getImgResID() {
return imgResID;
}
public void setImgResID(int imgResID) {
this.imgResID = imgResID;
}}
Can someone see where I am going wrong? Thanks in advance..
DrawerItemHolder class should be public, or at least it should not be private.
I'm creating android fragment that can play youtube video by using Youtube API Player. In MainActivity class, I'm getting error that
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment =null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new VideoFragment();
break;
case 2:
fragment = new ThirdFragment();
break;
default:
break;
}
Type mismatch: cannot convert from VideoFragment to Fragment
However, when I switch to new code:
private void displayView(int position) {
// update the main content by replacing fragments
VideoFragment fragment =null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new VideoFragment();
break;
case 2:
fragment = new ThirdFragment();
break;
default:
break;
}
This error occur
Type mismatch: cannot convert from ThirdFragment to VideoFragment
MainActivity full code:
public class MainActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {
final Context context = this;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to arrayonCreateOptionsMenu
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Video
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Event Story");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Develop and maintained by W.I.N Sdn. Bhd");
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
public void onNavigationDrawerItemSelected(int position) {
// TODO Auto-generated method stub
}
private void displayView(int position) {
// update the main content by replacing fragments
VideoFragment fragment =null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new VideoFragment();
break;
case 2:
fragment = new ThirdFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
public void openBrowser(View view){
//Get url from tag
String url = (String)view.getTag();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
//pass the url to intent data
intent.setData(Uri.parse(url));
startActivity(intent);
}}
VideoFragment.java
public class VideoFragment extends YouTubeFailureRecoveryActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview_fragment);
YouTubePlayerFragment youTubePlayerFragment =
(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.VideoView);
youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo("https://www.youtube.com/watch?v=pzWfjOaAbcE");
}
}
#Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.VideoView);
}
}
Fragments and Activities are not the same thing. Activity does not inherit from fragment look at this question.
In the case VideoFragment inherited Fragment, and assuming HomeFragment and ThirdFragment also inherit from Fragment, you first approach should work.
In my android application I am using "navigation drawer"..
I have "BaseActivity.java"
public class BaseActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
protected RelativeLayout _completeLayout, _activityLayout;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer);
// displayView(0);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
}
public void set(String[] navMenuTitles, TypedArray navMenuIcons) {
mTitle = mDrawerTitle = getTitle();
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items
if (navMenuIcons == null) {
for (int i = 0; i < navMenuTitles.length; i++) {
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i]));
}
} else {
for (int i = 0; i < navMenuTitles.length; i++) {
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i],
navMenuIcons.getResourceId(i, -1)));
}
}
getActionBar().setDisplayHomeAsUpEnabled(true);
adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, // nav menu toggle icon
R.string.app_name, // nav drawer open - description for
// accessibility
R.string.app_name // nav drawer close - description for
// accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
// menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
switch (position) {
case 0:
Intent intent = new Intent(this, FirstActivity.class);
startActivity(intent);
finish();// finishes the current activity
break;
case 1:
Intent intent1 = new Intent(this, SecondActivity.class);
startActivity(intent1);
finish();// finishes the current activity
break;
default:
break;
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
and my "FirstActivity.java" is:--
FirstActivity extends BaseActivity {
private String[] navMenuTitles;
private TypedArray navMenuIcons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
set(navMenuTitles, navMenuIcons);
}
}
and the SecondActivity.java is same as "FirstActivity.java"..
But when I run the application, It only showing FirstActivity..and navigation button(drawer) is not working.no any error is also occuring..can anyone tell me where is the problem????
i get the Choreographer error The application may be doing too much work on its main thread.
In my main activity each time i launch the app or get back to the main activity.
From what i understand there's to many work to be done on one core (because i'm not using threads), so i have to find a solution which probably evolves threads and AsyncTask.
I was trying to figure out what exactly cause the cpu put his effort but i couldn't figure it out by myself.
Here's the code of the main activity which includes a navigation drawer:
public class MainActivity extends ActionBarActivity
implements View.OnClickListener {
private Animation animAlpha;
private Animation animRotate;
private MediaPlayer background_music;
static int is_login = 0;
private boolean isOpen = false;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private ViewFlipper flipper;
ImageButton messages,weekly_day_times,shabat_times,events;
private ViewGroup mContainerView1,mContainerView2,mContainerView3,mContainerView4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
animRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
background_music = MediaPlayer.create(this, R.raw.yedid_nefesh);
flipper = (ViewFlipper) findViewById(R.id.viewFlipper);
startFlipper();
background_music.start();
messages = (ImageButton) findViewById(R.id.messages_button);
weekly_day_times = (ImageButton) findViewById(R.id.weekly_times_button);
shabat_times = (ImageButton) findViewById(R.id.sat_times_button);
events = (ImageButton) findViewById(R.id.events_button);
messages.setOnClickListener(this);
weekly_day_times.setOnClickListener(this);
shabat_times.setOnClickListener(this);
events.setOnClickListener(this);
mContainerView1 = (ViewGroup) findViewById(R.id.add_shabat_container);
mContainerView2 = (ViewGroup) findViewById(R.id.add_weekly_container);
mContainerView3 = (ViewGroup) findViewById(R.id.add_messages_container);
mContainerView4 = (ViewGroup) findViewById(R.id.add_events_container);
findViewById(R.id.b_shabat_add).setOnClickListener(this);
findViewById(R.id.b_weekly_add).setOnClickListener(this);
findViewById(R.id.b_messages_add).setOnClickListener(this);
findViewById(R.id.b_events_add).setOnClickListener(this);
IntializeNavDrawer();
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
setButtonsAnimation(messages);
setButtonsAnimation(weekly_day_times);
setButtonsAnimation(shabat_times);
setButtonsAnimation(events);
}
private void setButtonsAnimation(ImageButton button) {
int rand = (int)(Math.random()*700);
Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
hyperspaceJump.setStartOffset(rand);
button.startAnimation(hyperspaceJump);
}
/*
Initialize all list views in navigation drawer.
Handles actions of open or close drawer.
*/
private void IntializeNavDrawer() {
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
null, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
/*
Initialize properties and start the background flipper
*/
private void startFlipper() {
flipper.setFlipInterval(5000);
flipper.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
flipper.startFlipping();
}
#Override
protected void onResume() {
super.onResume();
flipper.startFlipping();
background_music.start();
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onClick(View v) {
Intent i;
switch(v.getId())
{
case R.id.messages_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, Messages.class);
startActivity(i);
return;
}
case R.id.sat_times_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, ShabatPrays.class);
startActivity(i);
return;
}
case R.id.weekly_times_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, WeeklyPrays.class);
startActivity(i);
return;
}
case R.id.events_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, Events.class);
startActivity(i);
return;
}
case R.id.b_shabat_add:
if(isOpen){
removeItem(mContainerView1);
isOpen = false;
}else {
addItem(mContainerView1,R.layout.nav_drawer_l_changes_item_1);
isOpen =true;
}
return;
case R.id.b_weekly_add:
if(isOpen){
removeItem(mContainerView2);
isOpen = false;
}else {
addItem(mContainerView2,R.layout.nav_drawer_l_changes_item_1);
isOpen =true;
}
return;
case R.id.b_messages_add:
if(isOpen){
removeItem(mContainerView3);
isOpen = false;
}else {
addItem(mContainerView3,R.layout.nav_drawer_l_changes_item_2);
isOpen =true;
}
return;
case R.id.b_events_add:
if(isOpen){
removeItem(mContainerView4);
isOpen = false;
}else {
addItem(mContainerView4,R.layout.nav_drawer_l_changes_item_2);
isOpen =true;
}
return;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#SuppressLint("NewApi")
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
#Override
protected void onStop() {
super.onStop();
background_music.pause();
flipper.stopFlipping();
}
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0) {
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.WRAP_CONTENT));
}
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
private void addItem(ViewGroup mContainerView,int layout) {
// Instantiate a new "row" view.
final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
layout, mContainerView, false);
// Because mContainerView has android:animateLayoutChanges set to true,
// adding this view is automatically animated.
mContainerView.addView(newView, 2);
}
public void removeItem(ViewGroup mContainerView){
mContainerView.removeViewAt(2);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
Also here are puctures with and withougt the navigation bar:
I was hoping you will show me what cuase to that error and an example on how should i get over it.
thank you
Here is some information from the Android documentation site about multi-threading.
https://developer.android.com/training/multiple-threads/index.html
http://developer.android.com/guide/components/processes-and-threads.html
http://developer.android.com/guide/faq/commontasks.html#threading
You can look around the web for more tutorials.