In my app, I use NoActionBar style, and I want to add action Bar to the one of my fragment.
In Activity I can do this like:
public class MyActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
How do this in fragment ?
I tried so:
#Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Toolbar actionBar = (Toolbar) getActivity().findViewById(R.id.my_app_bar);
((AppCompatActivity) getActivity()).setSupportActionBar(actionBar);
return inflater.inflate(R.layout.fragment_training, container, false);
}
But it doesn't work.
UPD
I think the problem in this line:
Toolbar actionBar = (Toolbar) getActivity().findViewById(R.id.my_app_bar);
((AppCompatActivity) getActivity()).setSupportActionBar(actionBar);
Old question)), should be:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.my_app_bar);
//((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
//return inflater.inflate(R.layout.fragment_training, container, false);
View v = inflater.inflate(r.layout.fragment_training, container, false);
Toolbar toolbar = (Toolbar) v.findviewbyid(r.id.my_app_bar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
return v;
}
May be you need to,
((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle();
These 2 links will be helpful for you,
Set up toolbar as actionbar in fragment
How to get Toolbar from fragment?
Hope this helps.
Related
I have two toolbar, one in my mainActivity, another in my fragment and the MenuIteMs of toolbar in my mainActivity apper in my fragment toolbar and i want to exclude the menu in my Fragment toolbar.
Screenshot of my app with two toolbar:
MainActivity
#SuppressLint("SetTextI18n")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
barra_ferramenta = findViewById(R.id.principal_toolbar);
setSupportActionBar(barra_ferramenta);
getSupportActionBar().setTitle("CME App");
Fragment
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragmento_chat, container, false);
toolbar = view.findViewById(R.id.frag_chat_barra);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
ActionBar action_bar = ((AppCompatActivity)getActivity()).getSupportActionBar();
action_bar.setDisplayShowCustomEnabled(true);
LayoutInflater layout_inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view_action_bar = layout_inflater.inflate(R.layout.barra_chat, null);
action_bar.setCustomView(view_action_bar);
Kindly guide me for this.
Thank You
Don't set toolbar in Fragment,Remove code from fragment
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragmento_chat, container, false);
please follow below code
#Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
lvRSS = (ListView)findViewById (R.id.lvRSS)
lvRSS.setOnItemClickListener (new AdapterView.OnItemClickListener(){
public void onItemClick (AdpaterView<?>parent,View view, int position,
long id)
}
it will work on listview but i cant to mimic the same for toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setOnItemClickListener (new AdapterView.OnItemClickListener(){
public void onItemClick (AdpaterView<?>parent,View view, int
position, long id)
}
please help
Toolbar does not contain the setOnItemClickListener () event. If you want to make the event click on Toolbar, try this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I want that the back icon in the toolbar is on the left side and it returns to the previous page. And the menu icon in the toolbar should be on the right side and to show the navigationdrawer.
I already have:
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar=(android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//drawerlayout
mDrawerLayout=(DrawerLayout) findViewById(R.id.activity_main3);
ActionBarDrawerToggle mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar,R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("NOSTRILS");
Solved the issue by making a lot of changes to code:
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private EndDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/* Toolbar */
getSupportActionBar().setTitle("Nostrils");
toolbar.setTitleTextColor(0xFFFFFFFF);
// Back icon
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
initNavigationDrawer();
}
drawerLayout = (DrawerLayout)findViewById(R.id.activity_main3);
drawerToggle = new EndDrawerToggle(this,
drawerLayout,
toolbar,
R.string.open,
R.string.close);
drawerLayout.addDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
int id = item.getItemId();
// Back icon clicked
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
finish();
return super.onOptionsItemSelected(item);
}
}
How can I refresh a webview from another class?
Webview class:
WebView myWebView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_plan1, container, false);
myWebView = (WebView) rootView.findViewById(R.id.wvheute);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("TEST URL");
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
I want to refresh this webview from my mainactivity
public class Hauptfenster extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hauptfenster);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FragmentManager fn = getFragmentManager();
fn.beginTransaction().replace(R.id.content_frame, new fragment_plan1()).commit();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hauptfenster, menu);
return true;
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager fm = getFragmentManager();
int id = item.getItemId();
if (id == R.id.nav_plan1) {
fm.beginTransaction().replace(R.id.content_frame, new fragment_plan1()).commit();
// I WANT THAT THE REFRESH HAPPENS HER :)
} else if (id == R.id.nav_plan2) {
fm.beginTransaction().replace(R.id.content_frame, new fragment_plan2()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Can somebody help me??
Save an instance of your other Fragment and pass it to your replace
private fragment_plan1 fragment=new fragment_plan1();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hauptfenster);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FragmentManager fn = getFragmentManager();
fn.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
In your WebView fragment create a new method
public void refreshPage()
{
myWebView.reload();
}
Call the method in your Activity when you want to
fragment.refreshPage();
When I go back from the next activity, always savedInstanceState is null and when I rotate screen or turn back , I get this error:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I have tried to use in MyFragment
View fragment = (View) inflater.inflate(R.layout.recycler_view, null);
Or
View fragment = (View) inflater.inflate(R.layout.recycler_view, container, false);
or
View fragment = (View) inflater.inflate(R.layout.recycler_view, container);
but i get the same error.
Error log:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.support.v4.view.ViewPager.addView(ViewPager.java:1342)
at android.view.ViewGroup.addView(ViewGroup.java:3360)
at android.view.ViewGroup.addView(ViewGroup.java:3336)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1094)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:447)
....
MainActivity
public class MainActivity extends AppCompatActivity {
static public DrawerLayout mDrawerLayout;
private ArrayList<ArrayList<Article>> articlesHomeList;
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.d("--MainActivity", "onSaveInstanceState");
// Save the user's current game state
savedInstanceState.putSerializable("articlesHomeList", articlesHomeList);
savedInstanceState.putString("test", "teststetststs");
// Always call the superclass so it can save the view hierarchy state
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
Log.d("--MainActivity", "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState, persistentState);
this.articlesHomeList = (ArrayList<ArrayList<Article>>) savedInstanceState.getSerializable("articlesHomeList");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
articlesHomeList = (ArrayList<ArrayList<Article>>) bundle.getSerializable("articlesHomeList");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
// Setting ViewPager for each Tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// Set Tabs inside Toolbar
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
Bundle bundleAll = new Bundle();
bundleAll.putSerializable("articles", (Serializable) this.articlesHomeList.get(2));
myFragment MyFragment = new MyFragment();
myFragment.setArguments(bundleAll);
AdapterTabs adapter = new AdapterTabs(getSupportFragmentManager());
adapter.addFragment(myFragment, "All");
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Log.e("Item selected", Integer.toString(id));
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} else if (id == android.R.id.home) {
mDrawerLayout.openDrawer(GravityCompat.START);
}
return super.onOptionsItemSelected(item);
}
myFragment:
public class MyFragment extends Fragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragment = (View) inflater.inflate(R.layout.recycler_view, container, false);
recyclerView = (RecyclerView) fragment.findViewById(R.id.my_recycler_view);
articlesList = (List<Article>) this.getArguments().getSerializable("articles");
ContentAdapter adapter = new ContentAdapter(articlesList);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
...
}
EDIT
I have added to MainActivity:
if (savedInstanceState != null) {
this.articlesHomeList = (ArrayList<ArrayList<Article>>) savedInstanceState.getSerializable("articlesHomeList");
this.renderTabs(viewPager);
} else if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getExtras();
articlesHomeList = (ArrayList<ArrayList<Article>>) bundle.getSerializable("articlesHomeList");
this.renderTabs(viewPager);
} else {
this.loadArticlesHome(viewPager);
}
But always savedInstanceState is null.
In the onCreateView method of your MyFragment fragment, change
return recyclerView;
to
return fragment;
You should return the fragment's view container, not the RecyclerView's. You see : the onCreateView method is called to have the fragment instantiate its user interface view, not just a single component of it (in your case the RecyclerView).
EDIT:
As #BlackBelt said, the app crashes because the RecyclerView has already a parent, defined in the layout, and it can't have two (the one in the layout and the ViewPager) : which is clearly what the exception says
java.lang.IllegalStateException: The specified child already has a
parent