Java Code Examples for android.support.v7.app.AppCompatActivity#getSupportActionBar()

The following examples show how to use android.support.v7.app.AppCompatActivity#getSupportActionBar() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WeatherFragment.java    From AndroidSchool with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_weather, container, false);
    ButterKnife.bind(this, root);

    AppCompatActivity activity = (AppCompatActivity) getActivity();
    activity.setSupportActionBar(mToolbar);
    if (activity.getSupportActionBar() != null) {
        activity.getSupportActionBar().setTitle("");
    }
    mToolbarTitle.setText(getString(R.string.default_city));
    mLoadingView = LoadingDialog.view(getFragmentManager());

    return root;
}
 
Example 2
Source File: SnippetDetailFragment.java    From android-java-snippets-rest-sample with MIT License 6 votes vote down vote up
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (null != getActivity() && getActivity() instanceof AppCompatActivity) {
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if (null != activity.getSupportActionBar()) {
            activity.getSupportActionBar().setTitle(mItem.getName());
        }
    }
    if (null != savedInstanceState && savedInstanceState.containsKey(STATUS_COLOR)) {
        int statusColor = savedInstanceState.getInt(STATUS_COLOR, UNSET);
        if (UNSET != statusColor) {
            mStatusColor.setBackgroundColor(statusColor);
            mStatusColor.setTag(statusColor);
        }
    }
}
 
Example 3
Source File: SensorsDataPrivate.java    From AutoTrackAppViewScreen with Apache License 2.0 6 votes vote down vote up
@TargetApi(11)
private static String getToolbarTitle(Activity activity) {
    try {
        ActionBar actionBar = activity.getActionBar();
        if (actionBar != null) {
            if (!TextUtils.isEmpty(actionBar.getTitle())) {
                return actionBar.getTitle().toString();
            }
        } else {
            if (activity instanceof AppCompatActivity) {
                AppCompatActivity appCompatActivity = (AppCompatActivity) activity;
                android.support.v7.app.ActionBar supportActionBar = appCompatActivity.getSupportActionBar();
                if (supportActionBar != null) {
                    if (!TextUtils.isEmpty(supportActionBar.getTitle())) {
                        return supportActionBar.getTitle().toString();
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 4
Source File: DyDirectoryGameIView.java    From MeiZiNews with MIT License 6 votes vote down vote up
private void initToolbar(final AppCompatActivity appCompatActivity)
    {
        toolbar.setTitle(appCompatActivity.getIntent().getStringExtra("title"));
        appCompatActivity.setSupportActionBar(toolbar);

        if (appCompatActivity.getSupportActionBar() == null) {
            return;
        }

        appCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

//        [android:ToolBar详解(手把手教程)](http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html)
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed(appCompatActivity);
            }
        });
    }
 
Example 5
Source File: AndroidUtils.java    From go-bees with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Setup toolbar.
 *
 * @param act    current activity.
 * @param isHome if it is home activity.
 * @param title  title.
 * @return ActionBar.
 */
public static ActionBar setUpToolbar(AppCompatActivity act, boolean isHome, int title) {
    Toolbar toolbar = (Toolbar) act.findViewById(R.id.toolbar);
    act.setSupportActionBar(toolbar);
    ActionBar ab = act.getSupportActionBar();
    if (ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
        if (isHome) {
            ab.setHomeAsUpIndicator(R.drawable.ic_menu);
            ab.setDisplayShowTitleEnabled(false);
        } else {
            ab.setDisplayShowHomeEnabled(true);
        }
        if (title != -1) {
            ab.setTitle(R.string.about_title);
        }
    }
    return ab;
}
 
Example 6
Source File: ZhiHuDetailIView.java    From MeiZiNews with MIT License 6 votes vote down vote up
/**
     * 设置左上角的返回键与它的点击效果
     *
     * @param appCompatActivity
     */
    private void initToolbar(final AppCompatActivity appCompatActivity) {

        appCompatActivity.setSupportActionBar(toolbar);

        if (appCompatActivity.getSupportActionBar() == null) {
            return;
        }

        appCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

//        [android:ToolBar详解(手把手教程)](http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html)
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    appCompatActivity.finishAfterTransition();
                } else {
                    appCompatActivity.finish();
                }
            }
        });

    }
 
Example 7
Source File: ViewUtils.java    From BlueBoard with Apache License 2.0 5 votes vote down vote up
/**
 * 显示Toolbar 添加自定义标题
 */
public static void setToolbar(AppCompatActivity context, Toolbar toolbar, Boolean WithHomeButton, String title) {
    if (title != null) {
        toolbar.setTitle(title);
    }
    context.setSupportActionBar(toolbar);
    if (WithHomeButton) {
        ActionBar actionBar = context.getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }
}
 
Example 8
Source File: TrendingRepositoryFragment.java    From gito-github-client with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_trending, container, false);
    unbinder = ButterKnife.bind(this, root);

    int columnCount = getResources().getInteger(R.integer.grid_column_count);
    StaggeredGridLayoutManager staggeredGridLayoutManager =
            new StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

    mAdapter = new TrendingRepositoryListAdapter(null, this);
    mAdapter.setHasStableIds(true);
    mRecyclerView.setAdapter(mAdapter);

    if (getActivity() instanceof AppCompatActivity) {
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if (activity.getSupportActionBar() != null) {
            activity.getSupportActionBar().setTitle(mPresenter.getTitle(getContext()));
        }
        mCoordinatorLayout = activity.findViewById(R.id.coordinator_layout);
        setUpBadges();
    }

    setUpBottomNavigationBar();

    mPresenter.start(savedInstanceState);
    return root;
}
 
Example 9
Source File: TrafficFragment.java    From gito-github-client with Apache License 2.0 5 votes vote down vote up
private void setTitle(String title) {
    if (getActivity() instanceof AppCompatActivity) {
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if (activity.getSupportActionBar() != null) {
            activity.getSupportActionBar().setTitle(title);
        }
    }
}
 
Example 10
Source File: SessionDetailFragment.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
private void initToolbar() {
    AppCompatActivity activity = ((AppCompatActivity) getActivity());
    activity.setSupportActionBar(binding.toolbar);
    ActionBar bar = activity.getSupportActionBar();
    if (bar != null) {
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setDisplayShowHomeEnabled(true);
        bar.setDisplayShowTitleEnabled(false);
        bar.setHomeButtonEnabled(true);
    }
}
 
Example 11
Source File: DevWeekDetailIVew.java    From MeiZiNews with MIT License 5 votes vote down vote up
/**
     * 设置左上角的返回键与它的点击效果
     *
     * @param appCompatActivity
     */
    private void initToolbar(final AppCompatActivity appCompatActivity) {

        toolbar.setTitle(appCompatActivity.getIntent().getStringExtra("title"));
        appCompatActivity.setSupportActionBar(toolbar);

        if (appCompatActivity.getSupportActionBar() == null) {
            return;
        }

        appCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);


//        [android:ToolBar详解(手把手教程)](http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html)
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    appCompatActivity.finishAfterTransition();
                } else {
                    appCompatActivity.finish();
                }
            }
        });


    }
 
Example 12
Source File: ScreenUtil.java    From weex with Apache License 2.0 5 votes vote down vote up
public static int getDisplayHeight(AppCompatActivity activity) {
    int height = 0;
    if (activity != null && activity.getWindowManager() != null && activity.getWindowManager().getDefaultDisplay() != null) {
        Point point=new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(point);
        height=point.y;
    }

    Log.e(TAG, "isSupportSmartBar:" + isSupportSmartBar);

    if (isSupportSmartBar) {
        int smartBarHeight = getSmartBarHeight(activity);
        Log.e(TAG, "smartBarHeight:" + smartBarHeight);
        height -= smartBarHeight;
    }

    if (activity.getSupportActionBar() != null) {
      int actionbar= activity.getSupportActionBar().getHeight();
      if(actionbar==0){
        TypedArray actionbarSizeTypedArray=activity.obtainStyledAttributes(new int[]{android.R.attr.actionBarSize});
        actionbar= (int) actionbarSizeTypedArray.getDimension(0,0);
      }
      Log.d(TAG, "actionbar:" + actionbar);
      height -= actionbar;
    }

    int status = getStatusBarHeight(activity);
    Log.d(TAG, "status:" + status);

    height -= status;

    Log.d(TAG,"height:"+height);
    return height;
}
 
Example 13
Source File: ToolbarCancel.java    From px-android with MIT License 5 votes vote down vote up
private void init() {
    final Activity activity = getActivity();
    if (activity instanceof AppCompatActivity) {
        final AppCompatActivity appCompatActivity = (AppCompatActivity)activity;
        appCompatActivity.setSupportActionBar(this);
        final ActionBar supportActionBar = appCompatActivity.getSupportActionBar();
        supportActionBar.setHomeAsUpIndicator(R.drawable.ic_close);
        supportActionBar.setHomeActionContentDescription(R.string.px_label_close);
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setDisplayShowTitleEnabled(false);
        setNavigationOnClickListener(v -> appCompatActivity.onBackPressed());
    }
}
 
Example 14
Source File: UIManagerAppCompat.java    From document-viewer with GNU General Public License v3.0 5 votes vote down vote up
public static void setToolbarVisible(AppCompatActivity activity, boolean visible) {
    ActionBar bar = activity.getSupportActionBar();
    if (visible) {
        bar.show();
    } else {
        bar.hide();
    }
}
 
Example 15
Source File: BasePreferenceFragment.java    From VCL-Android with Apache License 2.0 5 votes vote down vote up
@Override
public void onStart() {
    super.onStart();
    final AppCompatActivity activity = (AppCompatActivity)getActivity();
    if (activity != null && activity.getSupportActionBar() != null) {
        activity.getSupportActionBar().setTitle(getString(getTitleId()));
    }
}
 
Example 16
Source File: ScreenUtil.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
private static int getSmartBarHeight(AppCompatActivity activity) {
    ActionBar actionbar = activity.getSupportActionBar();
    if (actionbar != null)
        try {
            Class c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("mz_action_button_min_height");
            int height = Integer.parseInt(field.get(obj).toString());
            return activity.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
            actionbar.getHeight();
        }
    return 0;
}
 
Example 17
Source File: ScreenUtil.java    From WeexOne with MIT License 5 votes vote down vote up
private static int getSmartBarHeight(AppCompatActivity activity) {
    ActionBar actionbar = activity.getSupportActionBar();
    if (actionbar != null)
        try {
            Class c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("mz_action_button_min_height");
            int height = Integer.parseInt(field.get(obj).toString());
            return activity.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
            actionbar.getHeight();
        }
    return 0;
}
 
Example 18
Source File: AbstractSubsequentActivity.java    From ShaderEditor with MIT License 5 votes vote down vote up
public static void initToolbar(AppCompatActivity activity) {
	Toolbar toolbar = (Toolbar) activity.findViewById(R.id.toolbar);
	activity.setSupportActionBar(toolbar);

	ActionBar actionBar = activity.getSupportActionBar();
	if (actionBar == null) {
		return;
	}
	actionBar.setDisplayHomeAsUpEnabled(true);
}
 
Example 19
Source File: BaseFragmentListener.java    From octoandroid with GNU General Public License v3.0 5 votes vote down vote up
protected void setActionBarTitle(String title) {
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    ActionBar actionBar = activity.getSupportActionBar();
    if (actionBar != null) {
        actionBar.setTitle(title);
    }
}
 
Example 20
Source File: UIManagerAppCompat.java    From document-viewer with GNU General Public License v3.0 4 votes vote down vote up
public static void setProgressSpinnerVisible(AppCompatActivity activity, boolean visible) {
    ActionBar bar = activity.getSupportActionBar();

    if (bar.getCustomView() == null) {
        ProgressBar spinner = new ProgressBar(activity);
        spinner.setIndeterminate(true);
        bar.setCustomView(spinner);
    }

    bar.setDisplayShowCustomEnabled(visible);
}