Some activities load more difficult than others with my navigation drawer - android

I have an app that connects my activities with a navigation drawer.The problem is that some activities load more difficult than others(when i press the icon on drawer,instead of opens the activity immediately,it stays for 1-2 seconds and then loads the activity). It might be a dummy question but is it possible if you could give me some advice on how to fix it?Here is one of the activities that load more difficult
public class ImportAPI extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
public TextView fullnameside, emailside;
public static String stravaToken;
public ImageButton btnStrava;
public ImageView tickStrava;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.importapi);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setTitle("");
toolbar.setSubtitle("");
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);
View headerView = navigationView.getHeaderView(0);
fullnameside = (TextView) headerView.findViewById(R.id.fullnameside);
emailside = (TextView) headerView.findViewById(R.id.emailside);
fullnameside.setText(""+GetInfo.fullname);
emailside.setText(""+GetInfo.email);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(3).setChecked(true);
//STRAVA
tickStrava=(ImageView) findViewById(R.id.tickStrava);
btnStrava=(ImageButton) findViewById(R.id.stravaBtn);
connectStrava();
}
public void connectStrava(){
btnStrava.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent getStravaApi= new Intent(ImportAPI.this,StravaSetupApi.class);
startActivity(getStravaApi);
}
});
//GET ACCESS TOKEN FROM STRAVAS AUTHORIZE ACCOUNT
String accessToken = StravaAuthenticateActivity.getStravaAccessToken(this);
stravaToken=accessToken; //make static var so i can use it anywhere i want
//Get athletes activities from GetStravaAthleteActivities.java
new GetStravaAthleteActivities.AthleteActivities();
//check if token is null so i can display the tick and also disable the button press
if(stravaToken!=null)
{
btnStrava.setEnabled(false);
tickStrava.setVisibility(View.VISIBLE);
}
else {
tickStrava.setVisibility(View.INVISIBLE);
}
}

Try checking whether it's the Activity's fault and not the drawer menu's. You could time your onnectStrava() method, which could be causing the delay. Add log messages at the beginning and the end of the method.
public void connectStrava(){
Log.d(“TAG”, “STRAVA Entry point);
…
Log.d(“TAG”, “STRAVA Exit point);
}
then check the timestamps on your IDE Logcat and see how long it takes for the method to run. if it's indeed 1-2 seconds, then you know what causes the delay. If that's the case you could try to start the method on a separate thread or as an AsyncTask, so as to not have the method block your UI

Related

Drawer Layout stays open even after changing application

So i have a drawer layout inside Homepage of my activity it goes like this.
public void settingDrawer() {
if (drawer == null) {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.setScrimColor(Color.TRANSPARENT);
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);
int width = getResources().getDisplayMetrics().widthPixels;
DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) navigationView.getLayoutParams();
params.width = width;
navigationView.setLayoutParams(params);
}
}
it works just fine, but when i change activity and came back (using back button, or home button) the Drawer Layout stays open, i tried to close it when i use startActivity() method but it just doesn't right. i think there is a mistake that i made but i don't know where.
When you are going to Next Activity then first close Drawer like this
drawer.closeDrawer(GravityCompat.START);
so the above code will be close your drawer before navigating to next page and when you back to Activity then drawer is already closed.

Navigation Drawer Refreshed

I am following this answer on SO, which is titled as:
Change NavigationView items when user is logged
The code works fine but the content of NavigationView change when I restart the app. I want the content to be changed after I click Login or LogOut item menus
This is my code in onCreate() method:
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);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if(islogin())
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer1);
} else
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer2);
}
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
and here is the islogin() method:
public boolean islogin(){
// Retrieve data from preference:
prefs = getSharedPreferences("UserLoginData", MODE_PRIVATE);
String username = prefs.getString("username", null);
if (username == null) {
return false;
}
else{
return true;
}
}
Any help would greatly appreciated! Thank You
Note: Though this question seems duplicate of some, but its just the title, contents are entirely different.
Though I didn't get answer, I am posting my solution here.
I did solve it a very simple logic and its working very great.
Step 1:
I first initialize Toolbar toolbar; globally above class.
Step 2:
Then I just create a simple method called: myDrawer() and wrap all my Drawer code inside it. Like this:
public void myDrawer(){
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);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if(islogin())
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer2);
} else
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer);
}
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
}
Now I have hands over the Navigation and I can do anything with it, Like Refreshing and Calling again etc.
Step 3:
I am calling the method in Main Activity i-e: OnCreate
myDrawer();
Step 4:
And I am calling it every time I do logic of Signin and SignOut.
Wow! It's working like a charm.
PS: Just for the info purpose, Here is my onNavigationItemSelected, where I can handle Click Events:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Intent intent;
if (id == R.id.nav_item_item1) {
intent = new Intent(MainActivity.this, SomeClass1.class);
startActivity(intent);
} else if (id == R.id.nav_item_item2) {
intent = new Intent(getApplicationContext(), SomeClass2.class);
startActivity(intent);
} else if (id == R.id.nav_item_logout) {
// my other logic for signout
myDrawer();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Credits:
User: manish jain Answer

Android UI lag from setting image

I'm trying to create a simple app to learn about navigation drawers and setting images. I based this app on the template that Android Studio created for me.
I read that getting the drawable can be a long process so I made an Asynctask for it. Despite this, my UI still lags and I get the message that frames are being skipped due to too much work on the main thread. I figured out that the following line is causing those issues:
pic.setImageDrawable(drawable); //(in onPostExecute)
I did all the processing in the background and this line should just be setting the drawable. Why is this lagging so much?
I read somewhere that setting the dimensions might make a difference. Right now I have both dimensions set to match_parent and the image is centered in the view. I don't want to give it a definite size.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private ImageView pic;
#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.setDrawerListener(toggle);
toggle.syncState();
pic = (ImageView) findViewById(R.id.pic);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//set first item to be selected and set its image
navigationView.getMenu().getItem(0).setChecked(true);
new ImageSetter().execute();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
try {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
} catch (NullPointerException e) {
Log.e("back pressed", e.getStackTrace().toString());
}
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
new ImageSetter(id).execute();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
/**
* class for getting the image drawable in the background and then setting it
*/
private class ImageSetter extends AsyncTask<Integer, Void, Drawable> {
int id = //image1;
public ImageSetter() {
}
public ImageSetter(int id) {
this.id = id;
}
/**
* Get drawable in background
* Precondition: id has already been set
* #param params
* #return drawable of the picture to set
*/
#Override
protected Drawable doInBackground(Integer... params) {
int pic = R.drawable.b1;
switch (id) {
//set pic
}
return getResources().getDrawable(pic);
}
// Once complete, see if ImageView is still around and set drawable
#Override
protected void onPostExecute(Drawable drawable) {
if (pic != null) {
pic.setImageDrawable(drawable);
}
}
}
}
The code looks fine to me. using setImageDrawable() shouldn't be lagging too bad and does need to be run in the UI thread. You have offloaded the task to another thread, which is good.
How big is the source image that you are fetching? Android is notoriously bad at handling large images...
If your source image is too big, I would look at Loading Large Bitmaps Efficiently from the Android developer site. This will give you some tips on handling Bitmaps.

android navigation drawer not showing title bar in kitkat

In my application , i created a navigation drawer using Android studio template.
It works perfectly in Android 5.0 above devices but the title bar does not appear in Kitkat devices (only statius bar can be seen) , I was unable to find a solution,
The below is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().show();
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.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Intent intent = getIntent();
linkNo = intent.getIntExtra(FirstMenuFragment.LINK_NO, 0);
//Make firstItem active
navigationView.getMenu().getItem(linkNo).setChecked(true);
}
Thanks in advance
In KitKat, making the statusbar (semi-)transparent is more complex compared to Lollipop+. So the titlebar is still there, it's just below the status bar ;)
Edit: If you absolutely want to achieve this on KitKat, you could use a SystemBarTintManager (https://github.com/jgilfelt/SystemBarTint/blob/master/library/src/com/readystatesoftware/systembartint/SystemBarTintManager.java).
But I don't recommend that, I've done it myself and it's not so easy to get it right.

How can I make the top logo in a navigation drawer act properly as a button?

I am trying to make the top logo of the nav drawer act as a button that takes me back to the first fragment of the app, but I can't seem to make the button work at all, not even for simple tasks, such as a toast. The app crashes upon opening the app, so I don't even get to try using the button.
This is the error I get: "Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference"
The logo is declared as an ImageButton in XML and this is the java code:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public ImageButton logo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set the initial fragment
MainFragment mainFragment = new MainFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mainFragment);
fragmentTransaction.commit();
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
logo = (ImageButton) findViewById(R.id.main_icon);
logo.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Toast toast = Toast.makeText(getApplicationContext(), "works", Toast.LENGTH_SHORT);
toast.show();
}
});
}
Assuming your ImageButton is in the NavigationView's header, and that the NavigationView has only one header View, you can call findViewById() on that.
logo = (ImageButton) navigationView.getHeaderView(0).findViewById(R.id.main_icon);
Alternatively, since the View is an ImageButton, you could set an onClick attribute on the layout entry, and move your onClick() method to the Activity.
<ImageButton android:id="#+id/main_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_logo"
android:onClick="onClick" />
If you choose this option, make sure to remove the setOnClickListener() call.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
...
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(), "works", Toast.LENGTH_SHORT);
toast.show();
}
}
you have to create view. Use this
View view = navigationView.inflateHeaderView(R.layout.nav_header_main);
logo = (ImageButton) view.findViewById(R.id.main_icon);

Categories

Resources