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!
Related
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
In my App call demo extends ListActivity and I want to use back button navigation icon . Please suggest me how could be possible
example: public class demo extends ListActivity and I don't want to change ListActivity to extends another activity
write this line:
assert getActionBar() != null;
getActionBar().setDisplayHomeAsUpEnabled(true);
in ListActivity getSupportActionBar is not supported. so you should use Actionbar
or you can use delegate method for that like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatCallback callback = new AppCompatCallback() {
#Override
public void onSupportActionModeStarted(ActionMode actionMode) {
}
#Override
public void onSupportActionModeFinished(ActionMode actionMode) {
}
#Nullable
#Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
};
AppCompatDelegate delegate = AppCompatDelegate.create(this, callback);
delegate.onCreate(savedInstanceState);
delegate.setContentView(R.layout.saved_report_activity);
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
delegate.setSupportActionBar(toolbar);
delegate.getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(SomeActivity.this);
}
});
and make sure that your AppCompatCallback import v7 lib.
import android.support.v7.app.AppCompatCallback;
import android.support.v7.app.AppCompatDelegate;
best approach is use Delegate for ListActivity.
I'm trying to create a global variable as shown in mutliple other stack answers, but when I follow all those instructions, I get "cannot resolve method getApplication()" when trying to get or set this variable in any other activity. What am I doing wrong and how do I then get/set this variable? Here's the MainActivity code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
private int globalVariable_Move;
public int getGlobalVariable_Move() {
return globalVariable_Move;
}
public void setGlobalVariable_Move(int value) {
globalVariable_Move = value;
}
And here's the another Activity code:
public class PlayActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
gameView.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
int tempGlobalVariableMove = ((MainActivity) this.getApplication()).getGlobalVariable_Move();
return true;
}
});
}
}
You need to implement it in Application custom class not in Activity. Something like this:
public class CustomApplication extends Application {
private Tracker mTracker;
public Tracker getDefaultTracker() {
if (mTracker == null) {
mTracker = new Tracker();
}
return mTracker;
}
}
And get this from any Activity class like:
CustomApplication app = (CustomApplication) getApplication();
app.getDefaultTracker();
Or better use Singleton pattern to store global state and variables.
its a stupid question, but how can i reset toolbar title form another class. Not from MainActivity?
public class MainActivity extends AppCompatActivity {
public Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name);
And i want to change title from TabAdapter.class. How can i do it?When i'm trying to do it i'm getting a nullpointerexception.
public class TabAdapter extends FragmentPagerAdapter{
MainActivity main = new MainActivity();
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return MainFragment.getInstance();
case 1:
main.toolbar.setTitle("hello world");
return SearchFragment.getInstance();
}
return null;
}
Inside your SearchFragment in onResume() set the title using getActivity():
public void onResume(){
super.onResume();
((Toolbar) this.getActivity().findViewById(R.id.toolbar))
.setTitle("hello world");
}
You shouldn't try to initialise MainActivity in your TabAdapter class.
You should access the toolbar from your fragment and change it there. Here is an example:
public class MainActivity extends Activity {
private Toolbar mToolbar;
public Toolbar getToolbar() {
return mToolbar;
}
public void onCreate(Bundle savedInstanceState) {
// initialisation logic
mToolbar = ... // get toolbar from layout
}
}
public class SomeFragment extends Fragment {
public void onResume() {
// get the hosting activity
final MainActivity activity = (MainActivity) getActivity();
// get it's toolbar
final Toolbar toolbar = activity.getToolbar();
// set the title
toolbar.setTitle("My Title");
}
}
By MainActivity main = new MainActivity(); this line you are creating a new object of activity that cant be used to update current.
So.
You can set the toolbar title by passing the current activity reference to the adapter.
After creating the adpater call the mathod to set the activity reference and do this in following way.
public class TabAdapter extends FragmentPagerAdapter {
private AppCompatActivity activity;
public void setActivity(AppCompatActivity activity) {
this.activity = activity;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
//here you can change title of toolbar
return MainFragment.getInstance();
case 1:
//main.toolbar.setTitle("hello world");
updateToolbarTitle("Hello World");
return SearchFragment.getInstance();
}
return null;
private void updateToolbarTitle(String title) {
activity.getSupportActionBar().setTitle(title);
}
}
}
And in your MainActivity pass the reference of activity to the adapter like this
TabAdapter adapter = new TabAdapter(/*Your params here*/);
adapter.setActivity(this);
better use this.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(getSupportActionBar() != null){
getSupportActionBar().setTitle(mAdapter.getPageTitle(position));
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
And in your FragmentPagerAdapter
#Override
public CharSequence getPageTitle(int position) {
return title; //title for that position
}
Anyone have an example of an Android ListView GreenDroid (GDListActivity)?
Thanks
public class MoodleUcTarefasActivity extends GDListActivity {
private MoodleEventAdapter eventAdapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.initActionBar();
if (eventAdapter == null)
eventAdapter = new MoodleEventAdapter(this);
setListAdapter(eventAdapter);
}
private void initActionBar() {
setTitle("Your action bar title");
}
}
It's simple. Simple extend the GDListActivity to your activity, and link your adapter to the list.