How to access BaseActivity ToolBar in Activity in android - android

i want to access toolBar in MyActivity which extends BaseActivity can access Toolbar in MainActivity.
BaseActivity
public abstract class BaseActivity extends AppCompatActivity{
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_container_base);
Toolbar toolBar = (Toolbar) findViewById(R.id.tool_bar);
}
}
MyActivity
public class MyActivity extends BaseActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// here i want to access BaseActivity Toolbar
}

Set your toolbar with setSupportActionBar in your BaseActivity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_container_base);
Toolbar toolBar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
Then get the toolbar from getSupportActionBar after super.onCreate in MainActivity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
}
You can't cast an ActionBar to Toolbar, but if you really want to access to the toolbar, then change it to an instance variable:
protected Toolbar toolbar;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_container_base);
toolBar = (Toolbar) findViewById(R.id.tool_bar);
// ...
With protected modifier you can access to toolbar in your MainActivity.

Related

Custom Activity doesn't show any content

I made a BaseActivity class to initiate menus, toolbars etc on different activities. Using the BaseActivity works as needed for the toolbar and menu, but doesn't show any additional content I want to add there - like my textView. Can you help out please?
Here's my BaseActivity:
public abstract class BaseActivity extends AppCompatActivity
{
private DrawerLayout mDrawerLayout ;
private ActionBarDrawerToggle mToggle ;
private NavigationView mNavView ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
}
protected abstract int getLayoutResourceId();
public void initToolbar(int toolbarId)
{
Toolbar myToolbar = (Toolbar) findViewById(toolbarId);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
assert myToolbar != null;
}
public void initMenu() {
// Stuff in here
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId() ;
if(mToggle.onOptionsItemSelected(item)) {
return true ;
}
return super.onOptionsItemSelected(item);
}
And the actual Activity extending BaseActivity:
public class SaunaLight extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initToolbar(R.id.custombar);
initMenu();
TextView sauna = (TextView) findViewById(R.id.sauna);
}
#Override
protected int getLayoutResourceId() {
return R.layout.activity_sauna_light;
}
}
So currently my TextView sauna isn't displayed - I just get a white screen.
Had a very stupid mistake. Turned out, my custom action bar had a weird height and it was covering the whole layout. Got it fixed! Sorry for wasting your time!

Toolbar in BaseActivity

I have multiple activities and fragments. I would like to set toolbar in BaseActivity (so set it only once). But need an acces to the toolbar from fragment (method like - show, hide, changeTitle etc.)
Any suggestion?
I have tried solution below, but when I want to hide fragment, NPException is shown
public abstract class BaseActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
configureToolbar();
}
protected abstract int getLayoutResource();
private void configureToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
public void hideToolbar() {
toolbar.setVisibility(View.GONE);
}
My activity
public class MyActivity extends BaseActivity() {
}
I call hideToolbar in fragment like:
public class MyFragment extends Fragment() {
onCreate() {
((Myactivity)getActivity).hideToolbar();
}
I have include something like yours in my project. This is sample. You can take reference from it.
BaseActivity.java class:
public abstract class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
}
protected abstract int getLayoutResource();
}
ToolBarActivity.java class:
public abstract class ToolbarActivity extends BaseActivity {
protected Toolbar toolbar;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = findViewById(R.id.flToolbarContentContainer);
if (contentView instanceof ViewGroup) {
((ViewGroup) contentView)
.addView(LayoutInflater.from(this)
.inflate(getToolbarLayoutResource()
, (ViewGroup) contentView, false));
}
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
protected int getLayoutResource() {
return R.layout.activity_toolbar;
}
protected abstract int getToolbarLayoutResource();
public void showToolbar() {
toolbar.setVisibility(View.VISIBLE);
}
public void hideToolbar() {
toolbar.setVisibility(View.GONE);
}
}
activity_toolbar.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlToolbarContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_toLeftOf="#+id/pbToolbarActivity"
android:background="#color/blue_panel_day_background"
android:theme="#style/ToolbarTheme" />
<FrameLayout
android:id="#+id/flToolbarContentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar" />
</RelativeLayout>
MainActivity.java class:
public class MainActivity extends ToolbarActivity {
#Override
protected int getToolbarLayoutResource() {
return R.layout.activity_main;
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
homeFragment = HomeFragment.newInstance();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.llMainActivityContainer, homeFragment)
.commit();
}
}
activity_main.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llMainActivityContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Now, in Fragment class, apply:
((ToolbarActivity) getActivity()).showToolbar();
((ToolbarActivity) getActivity()).hideToolbar();
From Fragment
getActivity().getSupportActionBar();
Use:
public abstract class BaseActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
configureToolbar();
}
protected abstract int getLayoutResource();
private void configureToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
// changes made here, try to find if getSupportActionBar() is null or not after setting it - setSupportActionBar
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
// How and where you calling this method ?
public void hideToolbar() {
toolbar.setVisibility(View.GONE);
}
To access ActionBar from Fragment try the below method
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
You can now access all the methods of ActionBar

Use toolbar without using extends AppCompactActivity

i have using a toolbar that needs AppCompatActivity class and before it i used a FragmentActivity so i cant extends AppCompatActivity too
what is the solution to use setSupportActinBar that inherit from AppCompatActivity
this my code
public class currentActivity extends FragmentActivity implements
TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener{
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.current);
mToolbar = (Toolbar) findViewById(R.id.ctoolbar);
//setSupportActionBar(mToolbar); //problem is here
//getSupportActionBar().setDisplayShowHomeEnabled(true);
initialiseTabHost(savedInstanceState);
intialiseViewPager();
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}
}

How can I get the same drawerLayout on multiple types of activities without using fragments?

I want to have the same drawerlayout for multiple types of activities.
This works, but how would I be able to use this with a different type of Activity? A FragmentActivity for example.
My base activity:
public class BaseDrawerActivity extends Activity {
protected DrawerLayout _drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
....stuff with drawerlayout
}
}
And then I extend the class in my stat activity:
public class StartActivity extends BaseDrawerActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater startInflater = getLayoutInflater();
View contentView = startInflater.inflate(R.layout.activity_start, null, false);
_drawerLayout.addView(contentView);
}
}
Extending from different classes not allowed in Java. So in that case you'll need to extend it from FragmentActivity.

custom titlebar button in tabactivity android

Heey. How to add button in custom title bar through TabActivity ? I can set title TextView depending on which subactivity is called. But i cant figure out how to add button ?
My TabActivity:
public class TabsManager extends TabActivity {
static TabsManager mTab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTab = this;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.tabhostbottom);
}
public void setMyTitle(String string) {
((Activity) mTab).getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
TextView textView = (TextView) findViewById(R.id.header);
textView.setText(string);
}
And my subactivity:
#Override
protected void onResume() {
super.onResume();
TabsManager.mTab.setMyTitle("Activity1");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_club);
TabsManager.mTab.setMyTitle("Activity1");
}

Categories

Resources