When i open this activity, drawer is already opened by default. When i try to close the drawer, it doesn't. In short drawer is malfunctioning. I have attached both XML code & JAVA code.
This is XML File Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_home">
<include
android:id="#+id/action_bar"
layout="#layout/actionbar"
/>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/nav_view"
app:headerLayout="#layout/navigation"
app:menu="#menu/drawer_menu"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginRight="20dp"
android:layout_marginBottom="210dp"
android:src="#drawable/add_to_cart"
android:elevation="6dp"
android:id="#+id/fab_place_order"
app:pressedTranslationZ="12dp"
app:backgroundTint="#color/fabOrder"
android:visibility="invisible"
/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginRight="20dp"
android:layout_marginBottom="150dp"
android:src="#drawable/ic_account_balance_wallet_black_24dp"
android:elevation="6dp"
android:id="#+id/fab_balance"
app:pressedTranslationZ="12dp"
app:backgroundTint="#color/fabBalance"
android:visibility="invisible"
/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginRight="20dp"
android:layout_marginBottom="90dp"
android:src="#drawable/ic_exit_to_app_black_24dp"
android:elevation="6dp"
android:id="#+id/fab_logout"
app:pressedTranslationZ="12dp"
app:backgroundTint="#color/fabExit"
android:visibility="invisible"
/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:src="#drawable/menu"
android:elevation="6dp"
android:id="#+id/fab_menu"
app:pressedTranslationZ="12dp"
android:backgroundTint="#color/fabMain"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="230dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="70dp"
android:text="Place Order"
android:textColor="#003200"
android:textStyle="bold"
android:visibility="invisible" />
<TextView
android:id="#+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/label1"
android:layout_alignLeft="#+id/label1"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/label1"
android:layout_marginBottom="170dp"
android:text="Check Balance"
android:textColor="#000032"
android:textStyle="bold"
android:visibility="invisible" />
<TextView
android:id="#+id/label3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/label1"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/label1"
android:layout_marginBottom="110dp"
android:text="Logout"
android:textColor="#320000"
android:textStyle="bold"
android:visibility="invisible" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
This is JAVA File Code:
public class HomeActivity extends AppCompatActivity {
ConnectionDetector connectionDetector = new ConnectionDetector(this);
DatabaseHelper helper = new DatabaseHelper(this);
Handler handler = new Handler();
FloatingActionButton fab_menu,fab_balance,fab_logout,fab_order;
Animation fabOpen,fabClose,fabClockwise,fabAnticlockwise;
boolean isOpen = false, status = false;
ProgressBar progressBar;
private boolean isUserClickedBackButton = false;
private DrawerLayout drawerLayout;
TextView t1,t2,t3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
helper.startFirebaseDB();
Toolbar toolbar = findViewById(R.id.action_bar);
toolbar.setTitleTextColor(getResources().getColor(R.color.white));
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
toggle.syncState();
progressBar = findViewById(R.id.progressBar);
fab_menu = findViewById(R.id.fab_menu);
fab_menu.setEnabled(false);
fab_order = findViewById(R.id.fab_place_order);
fab_balance = findViewById(R.id.fab_balance);
fab_logout = findViewById(R.id.fab_logout);
t1 = findViewById(R.id.label1);
t2 = findViewById(R.id.label2);
t3 = findViewById(R.id.label3);
fabOpen= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_open);
fabClose= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_close);
fabClockwise= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_clockwise);
fabAnticlockwise= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_anticlockwise);
handler.postDelayed(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.GONE);
fab_menu.setEnabled(true);
}
},2000);
fab_menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isOpen)
{
fab_order.startAnimation(fabClose);
fab_balance.startAnimation(fabClose);
fab_logout.startAnimation(fabClose);
fab_menu.startAnimation(fabAnticlockwise);
fab_balance.setClickable(false);
fab_logout.setClickable(false);
handler.postDelayed(new Runnable() {
#Override
public void run() {
t1.setVisibility(View.INVISIBLE);
t2.setVisibility(View.INVISIBLE);
t3.setVisibility(View.INVISIBLE);
}
},200);
isOpen = false;
}
else
{
fab_order.startAnimation(fabOpen);
fab_balance.startAnimation(fabOpen);
fab_logout.startAnimation(fabOpen);
fab_menu.startAnimation(fabClockwise);
fab_balance.setClickable(true);
fab_logout.setClickable(true);
isOpen = true;
handler.postDelayed(new Runnable() {
#Override
public void run() {
t1.setVisibility(View.VISIBLE);
t2.setVisibility(View.VISIBLE);
t3.setVisibility(View.VISIBLE);
}
},200);
}
}
});
fab_order.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
status = helper.checkUserStatus(helper.getUser());
if(!connectionDetector.isConnected()) {
Toast.makeText(HomeActivity.this, "Check your internet connection !!!", Toast.LENGTH_LONG).show();
return;
}
else if(!status) {
Toast t1 = Toast.makeText(HomeActivity.this, "Access Denied !!!", Toast.LENGTH_LONG);
Toast t2 = Toast.makeText(HomeActivity.this, "Please contact: Ayaz Handloom Store", Toast.LENGTH_LONG);
t1.setGravity(Gravity.CENTER,0,0);
t1.show();
t2.setGravity(Gravity.CENTER,0,0);
t2.show();
return;
}
else {
Intent newOrder = new Intent(HomeActivity.this, NewOrderActivity.class);
startActivity(newOrder);
finish();
}
}
});
fab_balance.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent balance = new Intent(HomeActivity.this,BalanceActivity.class);
startActivity(balance);
finish();
}
});
fab_logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent logout = new Intent(HomeActivity.this,MainActivity.class);
helper.logout();
Toast.makeText(HomeActivity.this, "Logout success !!!", Toast.LENGTH_LONG).show();
startActivity(logout);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.edit_profile:
break;
case R.id.change_password:
break;
case R.id.settings:
break;
case R.id.logout:
DialogInterface.OnClickListener dialogBox = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
Intent logout = new Intent(HomeActivity.this,MainActivity.class);
helper.logout();
Toast.makeText(HomeActivity.this, "Logout success !!!", Toast.LENGTH_LONG).show();
startActivity(logout);
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
Toast cancel = Toast.makeText(HomeActivity.this, "Request cancelled !!!", Toast.LENGTH_SHORT);
cancel.setGravity(Gravity.CENTER,0,0);
cancel.show();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setMessage("Are you sure to logout?").setPositiveButton("Yes", dialogBox).setNegativeButton("No", dialogBox).show();
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START))
drawerLayout.closeDrawer(GravityCompat.START);
else {
if (!isUserClickedBackButton) {
Toast.makeText(this, "Press back key again for Main Page !!!", Toast.LENGTH_LONG).show();
isUserClickedBackButton = true;
handler.postDelayed(new Runnable() {
#Override
public void run() {
isUserClickedBackButton = false;
}
}, 3000);
} else {
Intent back = new Intent(HomeActivity.this, MainActivity.class);
startActivity(back);
finish();
}
}
}
}
If i delete XML code which is inside DrawerLayout, then it works fine.
you forgot to add drawerLayout.addDrawerListener(toggle); before toggle.syncState();
Related
I am trying to add forward and back button on custom toolbar in android studio I have tried adding it as a menu as well as separate buttons but every time when I run the app the buttons are not appearing. Code is working fine I am not able to detect what am i missing here. Here is the code :
XML
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/toolbar01"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.4"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/menu_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:src="#drawable/menu"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3">
<Button
android:id="#+id/back"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/ic_baseline_arrow_back_ios_24">
</Button>
<Button
android:id="#+id/forward"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/ic_baseline_arrow_forward_ios_24">
</Button>
<Button
android:layout_marginLeft="50dp"
android:id="#+id/refresh"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/ic_baseline_refresh_24"
android:layout_marginStart="40dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ProgressBar
android:id="#+id/pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:progress="50"
android:progressTint="#color/colorPrimary"
android:indeterminate="true"
android:indeterminateDuration="2000"
android:maxHeight="3dp"
android:minHeight="3dp"
tools:targetApi="lollipop" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
>
<WebView
android:id="#+id/webView"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
</WebView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
JAVA:
public class Webview extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private WebView webview;
NavigationView navigationView;
DrawerLayout drawer;
ImageView menuicon, forward, back, refresh;
static final float END_SCALE = 0.7f;
LinearLayout contentView;
ProgressBar progressBar;
SwipeRefreshLayout swipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
navigationDrawer();
Loadweb();
}
private void toolbuttons() {
forward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onForwardPressed();
}
});
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webview.reload();
}
});
}
private void Loadweb() {
webview.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
});
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
}
});
webview.loadUrl("https://www.nationalsavings.com.pk/index.php");
progressBar.setMax(100);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
webview.reload();
}
}, 3000);
}
});
swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private void navigationDrawer() {
//navigation view
navigationView.bringToFront();
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.nav_home);
menuicon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (drawer.isDrawerVisible(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);
else drawer.openDrawer(GravityCompat.START);
}
});
animateNavigationDrawer();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.toolbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
onBackPressed();
break;
case R.id.forward:
onForwardPressed();
break;
case R.id.refresh:
webview.reload();
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerVisible(GravityCompat.START) || webview.canGoBack()) {
drawer.closeDrawer(GravityCompat.START);
webview.goBack();
} else {
super.onBackPressed();
}
}
public void onForwardPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
Toast.makeText(this, "Can't go back", Toast.LENGTH_SHORT).show();
}
}
private void animateNavigationDrawer() {
drawer.setScrimColor(getResources().getColor(R.color.colorPrimary));
//Add any color or remove it to use the default one!
//To make it transparent use Color.Transparent in side setScrimColor();
//drawerLayout.setScrimColor(Color.TRANSPARENT);
drawer.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Scale the View based on current slide offset
final float diffScaledOffset = slideOffset * (1 - END_SCALE);
final float offsetScale = 1 - diffScaledOffset;
contentView.setScaleX(offsetScale);
contentView.setScaleY(offsetScale);
// Translate the View, accounting for the scaled width
final float xOffset = drawerView.getWidth() * slideOffset;
final float xOffsetDiff = contentView.getWidth() * diffScaledOffset / 2;
final float xTranslation = xOffset - xOffsetDiff;
contentView.setTranslationX(xTranslation);
}
});
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
webview.loadUrl("https://www.nationalsavings.com.pk/index.php");
} else if (id == R.id.nav_privacy_policy) {
webview.loadUrl("https://www.nationalsavings.com.pk/privacy-policy.php");
} else if (id == R.id.nav_share) {
Intent shareintent = new Intent();
shareintent.setAction(Intent.ACTION_SEND);
shareintent.putExtra(Intent.EXTRA_TEXT, "https://www.nationalsavings.com.pk/index.php");
shareintent.setType("text/plain");
startActivity(Intent.createChooser(shareintent, "Share via"));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
I am trying to display a pop up window when i click on a menu overflow item. I have written some code also for that. But it outputs nothing.
Here is some code:
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.settings_id:
displayPopupWindow();
return true;
case R.id.about_us_id:
return true;
case R.id.logout_id:
startActivity(new Intent(HomePage.this,MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void displayPopupWindow() {
PopupWindow popup = new PopupWindow(this);
View layout = getLayoutInflater().inflate(R.layout.popup, null);
popup.setContentView(layout);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
My Xml file Code is below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#color/colorPrimary">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="165dp"
android:id="#+id/textView"
tools:text="This is a Pop Up Window!"
android:textSize="36sp"
android:textStyle="normal|bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:textAlignment="center"
android:fontFamily="sans-serif" />
</RelativeLayout>
Where is the error? Please help me.
Here is your solution,
MainActivity.java
public class MainActivity extends Activity {
private PopupWindow mPopupWindow;
private Button btnClosePopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
break;
case R.id.gamepad:
Toast.makeText(getBaseContext(), "You selected Gamepad", Toast.LENGTH_SHORT).show();
break;
case R.id.camera:
Toast.makeText(getBaseContext(), "You selected Camera", Toast.LENGTH_SHORT).show();
initiatePopupWindow();
break;
case R.id.video:
Toast.makeText(getBaseContext(), "You selected Video", Toast.LENGTH_SHORT).show();
break;
case R.id.email:
Toast.makeText(getBaseContext(), "You selected EMail", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private void initiatePopupWindow() {
try {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,
(ViewGroup) findViewById(R.id.popup_element));
mPopupWindow = new PopupWindow(layout, 300, 370, true);
mPopupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mPopupWindow.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
popup.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp">
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="Hello!" />
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
In my app I have to use navigation drawer with all activities. So I have created a base activity called DrawerActivity. I wrote the code for navigation drawer in DrawerActivity and then extended the UserDashBoardActivity from DrawerActivity.
The problem is that DrawerActivity properties aren't executed in UserDashBoardActivity. I can't interact with DrawerActivity in UserDashBoardActivity. Here that drawer menu is in ActionBar in all the activities.
This is my DrawerActivity
public class DrawerActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_list_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerItems = getResources().getStringArray(R.array.navigation_drawer_items_array);
//mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(
this, R.layout.drawer_list_items, mDrawerItems));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout, R.drawable.ic_menu,
R.string.drawer_open, R.string.drawer_close) {
public void onDrawerOpened(View view) {
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
for (int index = 0; index < menu.size(); index++) {
MenuItem menuItem = menu.getItem(index);
if (menuItem != null) {
// hide the menu items if the drawer is open
menuItem.setVisible(!drawerOpen);
}
}
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
switch (position) {
case 0: {
Intent intent = new Intent(DrawerActivity.this, UserDashBoardActivity.class);
startActivity(intent);
break;
}
case 1: {
Intent intent = new Intent(DrawerActivity.this, AdmissionActivity.class);
startActivity(intent);
break;
}
default:
break;
}
mDrawerLayout.closeDrawer(mDrawerList);
}
}
This is my UseDashBoardActivity
public class UserDashBoardActivity extends DrawerActivity {
private Context context;
private ImageButton searchBtn;
private ImageButton favBtn;
private ImageButton profileBtn;
private ImageButton reminderBtn;
private ImageButton logoutBtn;
private ImageButton notificationBtn;
private ImageView seatchIcon;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
#Override
protected void onStart() {
super.onStart();
AppActivityStatus.setActivityStarted();
AppActivityStatus.setActivityContext(context);
}
#Override
protected void onPause() {
super.onPause();
AppActivityStatus.setActivityStoped();
}
#Override
protected void onResume() {
super.onResume();
AppActivityStatus.setActivityStarted();
}
#Override
protected void onStop() {
super.onStop();
AppActivityStatus.setActivityStoped();
}
#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_user_dash_boad, menu);
return true;
}
// delete the selected event from event list added here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
return true;
case R.id.action_favourite:
return true;
case R.id.action_navigation:
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setCustomView(R.layout.action_bar_layout);
setContentView(R.layout.user_dash_board);
context = getApplicationContext();
searchBtn = (ImageButton) findViewById(R.id.search_btn);
favBtn = (ImageButton) findViewById(R.id.fav_btn);
profileBtn = (ImageButton) findViewById(R.id.profile_btn);
reminderBtn = (ImageButton) findViewById(R.id.reminder_btn);
notificationBtn = (ImageButton) findViewById(R.id.notification_btn);
logoutBtn = (ImageButton) findViewById((R.id.logout_btn));
final EditText Search = (EditText)findViewById(R.id.emailAddress);
mDrawerList = (ListView)findViewById(R.id.drawer_layout);
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent regAct = new Intent(getApplicationContext(), SearchActivity.class);
// Clears History of Activity
regAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(regAct);
}
});
favBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
Intent tabAct = new Intent(getApplicationContext(),TabHostActivity.class);
tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(tabAct);
}
});
profileBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Intent tabAct = new Intent(getApplicationContext(),AboutCollegeActivity.class);
tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(tabAct);
}
});
}
}
This is my actionbar xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.after2.svirtzone.after2_gradle.UserDashBoardActivity">
<item
android:id="#+id/action_notify"
android:icon="#drawable/mail_icon"
appmunu:showAsAction="always"
android:title="Notification" />
<item
android:id="#+id/action_favourite"
android:icon="#drawable/favourite_icon"
appmunu:showAsAction="always"
android:title="Favourite" />
<item
//this is the menu button for navigation drawer
android:id ="#+id/action_navigation"
android:icon="#drawable/ic_menu"
appmunu:showAsAction = "always"
android:title="navigation"
android:layout_gravity="left"/>
</menu>
This is the xml layout for navigationdrawer listview
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/activity_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" ></FrameLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
This layout is UserDashboard layout
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/appblue"
android:orientation="vertical">
<EditText
android:id="#+id/emailAddress"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#drawable/edit_text_style"
android:gravity="center|start"
android:hint="#string/edittext_hint"
android:inputType="textEmailAddress"
android:maxLength="40"
android:textSize="18sp"
android:visibility="gone" />
<ImageView
android:id="#+id/search_icon_btn"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginLeft="580dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-90dp"
android:padding="5dp"
android:src="#drawable/search_icon" />
<ImageButton
android:id="#+id/search_btn"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="210dp"
android:layout_marginTop="30dp"
android:background="#drawable/search_blue"
android:gravity="center" />
<TextView
android:id="#+id/searchCollege"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
android:text="#string/search_college"
android:textColor="#color/green"
android:textSize="30sp" />
<ImageButton
android:id="#+id/fav_btn"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="200dp"
android:layout_marginRight="5dp"
android:layout_marginTop="-305dp"
android:background="#drawable/fav_blue"
android:gravity="center" />
<TextView
android:id="#+id/myFavourites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="500dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="#string/my_favourites"
android:textColor="#color/green"
android:textSize="30sp" />
</LinearLayout>
The UserDashboard activity is executed as it is but I need the property of DrawerActivity to be executed along with this activity. How to do it?
Let the parent Activity add the required ViewGroup as child of the FrameLayout. To achieve this, use the following method for DrawerActivity:
protected void addToContentView(int layoutResID)
{
// as part of onCreate() we had already
// setContentView(R.layout.activity_drawer);
if (layoutResID >= 0)
{
Toast.makeText(this, "activity content found", Toast.LENGTH_LONG).show();
FrameLayout fl = (FrameLayout)findViewById(R.id.activity_frame);
View.inflate(this, layoutResID, fl);
}
else
{
Toast.makeText(this, "activity content missing", Toast.LENGTH_LONG).show();
}
}
Then, in the onCreate() method of the child Activity:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// call this instead of setContentView()
addToContentView(R.layout.activity_user_dashboard);
// put here your code as before...
}
Drawer's layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<!--you need to add everything to this frameLayout then only you will be able to access Navigation DrawerLayout-->
<FrameLayout
android:id="#+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
<ListView
android:id="#+id/navigation_list"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:fitsSystemWindows="true"
android:background="#color/white"
/>
</android.support.v4.widget.DrawerLayout>
The Activity with Drawer:(Only important code is shown)
public abstract class BaseDrawerLayoutActivity extends AppCompatActivity implements OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_drawer_layout_activity);
//rest of the things
//NOTE: loadNavMenu is abstract so that you can implement it in other activities as per you need, otherwise remove this and load it here itself
loadNavMenu();
}
protected abstract void loadNavMenu();
// call this method whenever you enter new activity and load the fragment here.
public void showFragment(Fragment object, String title) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment, object);
transaction.commit();
}}
Those activities which need the drawer menu can inherit above class. Not inflate all the layout's inside FrameLayout only.
public class MainActivity extends BaseDrawerLayoutActivity {
public static final String TAG = OLMSActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//the method below will call the BaseDrawerLayoutActivity method and change the fragment for you to see.
showFragment(new MyProfileFragment(), "My Profile");
}
#Override
protected void loadNavMenu() {
NavItems.clear();
//load your menu items
}
private void loadNavFavourites(){
//todo
}}
MyProfileFragment is just a class with fragment and this one call inflate a layout in its OnCreateView method.
In my app i am extending the drawerActivity which is having the properties of navigation drawer. The problem is that menu icon appears at the action bar. When i click the action bar nothing happends. Here i have posted the ode for your reference
drawerActivity.class
public class DrawerActivity extends ActionBarActivity {
protected DrawerLayout mDrawerLayout = null;
protected ListView mDrawerList = null;
protected String[] mDrawerItems;
protected ActionBarDrawerToggle mDrawerToggle = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_list_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerItems = getResources().getStringArray(R.array.navigation_drawer_items_array);
//mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(
this, R.layout.drawer_list_items, mDrawerItems));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout, R.drawable.ic_menu,
R.string.drawer_open, R.string.drawer_close) {
public void onDrawerOpened(View view) {
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
for (int index = 0; index < menu.size(); index++) {
MenuItem menuItem = menu.getItem(index);
if (menuItem != null) {
// hide the menu items if the drawer is open
menuItem.setVisible(!drawerOpen);
}
}
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
switch (position) {
case 0: {
Intent intent = new Intent(DrawerActivity.this, UserDashBoardActivity.class);
startActivity(intent);
break;
}
case 1: {
Intent intent = new Intent(DrawerActivity.this, AdmissionActivity.class);
startActivity(intent);
break;
}
default:
break;
}
mDrawerLayout.closeDrawer(mDrawerList);
}
}
This is userDashBoardActivity
public class UserDashBoardActivity extends DrawerActivity {
private Context context;
private ImageButton searchBtn;
private ImageButton favBtn;
private ImageButton profileBtn;
private ImageButton reminderBtn;
private ImageButton logoutBtn;
private ImageButton notificationBtn;
private ImageView seatchIcon;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
#Override
protected void onStart() {
super.onStart();
AppActivityStatus.setActivityStarted();
AppActivityStatus.setActivityContext(context);
}
#Override
protected void onPause() {
super.onPause();
AppActivityStatus.setActivityStoped();
}
#Override
protected void onResume() {
super.onResume();
AppActivityStatus.setActivityStarted();
}
#Override
protected void onStop() {
super.onStop();
AppActivityStatus.setActivityStoped();
}
#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_user_dash_boad, menu);
return true;
}
// delete the selected event from event list added here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
return true;
case R.id.action_favourite:
return true;
case R.id.action_navigation:
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_dash_board);
context = getApplicationContext();
searchBtn = (ImageButton) findViewById(R.id.search_btn);
favBtn = (ImageButton) findViewById(R.id.fav_btn);
profileBtn = (ImageButton) findViewById(R.id.profile_btn);
reminderBtn = (ImageButton) findViewById(R.id.reminder_btn);
notificationBtn = (ImageButton) findViewById(R.id.notification_btn);
logoutBtn = (ImageButton) findViewById((R.id.logout_btn));
final EditText Search = (EditText)findViewById(R.id.emailAddress);
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent regAct = new Intent(getApplicationContext(), SearchActivity.class);
// Clears History of Activity
regAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(regAct);
}
});
favBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
Intent tabAct = new Intent(getApplicationContext(),TabHostActivity.class);
tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(tabAct);
}
});
profileBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Intent tabAct = new Intent(getApplicationContext(),AboutCollegeActivity.class);
tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(tabAct);
}
});
}
}
This Activity is extends from DrawerActivity
This is my Drawer.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/activity_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" ></FrameLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
this is UserDashBoard.xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/appblue"
android:orientation="vertical">
<EditText
android:id="#+id/emailAddress"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#drawable/edit_text_style"
android:gravity="center|start"
android:hint="#string/edittext_hint"
android:inputType="textEmailAddress"
android:maxLength="40"
android:textSize="18sp"
android:visibility="gone" />
<ImageView
android:id="#+id/search_icon_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="350dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-60dp"
android:padding="5dp"
android:src="#drawable/search_icon" />
<ImageButton
android:id="#+id/search_btn"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="100dp"
android:layout_marginTop="15dp"
android:background="#drawable/search_blue"
android:gravity="center" />
<TextView
android:id="#+id/searchCollege"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="100dp"
android:layout_marginTop="10dp"
android:text="#string/search_college"
android:textColor="#color/green"
android:textSize="18sp" />
<ImageButton
android:id="#+id/fav_btn"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="100dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-150dp"
android:background="#drawable/fav_blue"
android:gravity="center" />
<TextView
android:id="#+id/myFavourites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="650px"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="#string/my_favourites"
android:textColor="#color/green"
android:textSize="18sp" />
<ImageButton
android:id="#+id/profile_btn"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="100dp"
android:layout_marginTop="10dp"
android:background="#drawable/profile_blue"
android:gravity="center" />
</LinearLayout>
The Adapter that you are setting is wrong you are using an ArrayAdapter but you should be actually using an ActionBarDrawerToggle like this
private void initDrawer() {
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
if you need furthur assistance refer this link http://developer.android.com/training/implementing-navigation/nav-drawer.html
You need to add listview in navigation drawer like below.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/activity_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start" >
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
In your drawer activity (DrawerActivity.class)
public class DrawerActivity extends ActionBarActivity
Replace the actionbaractivity with appcompactability. I think this should solve your problem.
I'm using minSDK = 8 and targetSDK = 17
At activity start, my 3 screens (3 different Views) are set to invisible in the XML layout. In onResume, I call setScreen(1), which starts an animation that sets View 'wait' to visible. However, at line 34 where 'wait' is set to visible, nothing happens. If I remove the comment // from line 43 where 'main' is set to invisible, 'wait' becomes visible.
Why does 'wait' not appear when line 43 is commented out? Why does it take setting the already invisible 'main' View to invisible for 'wait' to appear?
public class TestActivity extends Activity {
static final String TAG = "test";
public Context context;
Activity myActivity;
UserFunctions userFunctions;
Button but_begin;
int status;
View main;
View wait;
View noconnect;
Animation fadeIntoWait;
Animation fadeIntoConnect;
Animation fadeIntoMain;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);
context = getApplicationContext();
myActivity = this;
main = findViewById(R.id.screen_main);
wait = findViewById(R.id.screen_wait);
noconnect = findViewById(R.id.screen_connect);
fadeIntoWait = AnimationUtils.loadAnimation(this, R.anim.fade_in);
fadeIntoConnect = AnimationUtils.loadAnimation(this, R.anim.fade_in);
fadeIntoMain = AnimationUtils.loadAnimation(this, R.anim.fade_in);
fadeIntoWait.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
wait.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//main.setVisibility(View.INVISIBLE);
//noconnect.setVisibility(View.INVISIBLE);
}
});
fadeIntoConnect.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
noconnect.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
wait.setVisibility(View.INVISIBLE);
}
});
fadeIntoMain.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
main.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
wait.setVisibility(View.INVISIBLE);
}
});
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onResume() {
super.onResume();
setScreen(1);
}
public void setScreen(int screen) {
switch (screen) {
case 1: // into wait
wait.startAnimation(fadeIntoWait);
break;
case 2: // to noconnect
noconnect.startAnimation(fadeIntoConnect);
break;
case 3: // to main
main.startAnimation(fadeIntoMain);
break;
}
}
public void gotoActivity(Class<?> s) {
Intent g = new Intent(getApplicationContext(), s);
// Close all views before launching Dashboard
g.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(g);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
public void retryConnect(View view) {
}
public void begin(View view) {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 0, "Exit App");
menu.add(0, 2, 0, "Close Menu");
menu.add(0, 3, 0, "Dashboard");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1: // log out
closeOptionsMenu();
return true;
case 2: // Exit App
finish();
return true;
case 3: // Exit App
gotoActivity(DashboardActivity.class);
return true;
}
return super.onOptionsItemSelected(item);
}
public void menu(View v) {
openOptionsMenu();
}
}
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_alignParentTop="true"
android:background="#drawable/line" >
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/header" >
<RelativeLayout
android:id="#+id/screen_wait"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:visibility="gone" >
<ImageView
android:id="#+id/img_wait"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:contentDescription="#string/wait"
android:src="#drawable/wait"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/screen_connect"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:visibility="gone" >
<ImageView
android:id="#+id/img_connect"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:contentDescription="#string/connect"
android:onClick="retryConnect"
android:src="#drawable/connect"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/screen_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:visibility="gone" >
<ScrollView
android:id="#+id/middle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/footer"
android:background="#2F2430" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2F2430" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/idea"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#21dbd4" />
<Button
android:id="#+id/but_begin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView01"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:onClick="begin"
android:text="Begin" />
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_alignParentBottom="true"
android:background="#drawable/line"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Pennies:" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_toRightOf="#+id/textView2" />
<Button
android:id="#+id/btnMenu"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="menu"
android:text="Menu" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
Your UI is not laid out until after onCreate(). Try moving the animations to onStart() or better, use a ViewTreeObserver to start them once layout is complete