Navigation Drawer on click of menu icon search in Fragment - android

I Implemented the Navigation Drawer in my Fragment but it always shows a grey padding above it .Same was the case with Fragment but i removed it by adding android:fitsSystemWindows="false". But this fails to remove the padding above the Navigation Drawer . How can i avoid this ?
This is my Fragment :
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.dummy.R;
public class QueriesActivity extends Fragment {
Context context;
DrawerLayout mDrawerLayout;
boolean mSlideState=false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_queries, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
context = getActivity();
mDrawerLayout = (DrawerLayout)view.findViewById(R.id.queries_drawer_layout);
setHasOptionsMenu(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(View view, float v) {
}
#Override
public void onDrawerOpened(View view) {
mSlideState=true;
}
#Override
public void onDrawerClosed(View view) {
mSlideState=false;
}
#Override
public void onDrawerStateChanged(int i) {
}
});
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.menu_filter, menu);
super.onCreateOptionsMenu(menu, menuInflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
if(mSlideState){
mDrawerLayout.closeDrawer(Gravity.END);
}else{
mDrawerLayout.openDrawer(Gravity.END);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I want to implement this as attached in the image.
https://i.stack.imgur.com/BtHKx.png

Related

Menu in a custom toolbar not showing in a fragment

So I have had asked this multiple times but I can't get a decent answer to my question.
Can someone please tell me why my menu is not showing in my custom toolbar?
PurchaseItemList.java
package com.example.devcash.Fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.devcash.R;
/**
* A simple {#link Fragment} subclass.
*/
public class PurchaseItemListFragment extends Fragment implements SearchView.OnQueryTextListener, MenuItem.OnActionExpandListener {
Toolbar itemListToolbar;
Spinner itemListSpinner;
public PurchaseItemListFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//inflate the menu
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_purchase_item_list, container, false);
itemListToolbar = (Toolbar) view.findViewById(R.id.toolbar_purchaseitemlist);
itemListSpinner = (Spinner) view.findViewById(R.id.spinner_allcategories);
///
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.custom_spinner_item,
getResources().getStringArray(R.array.dropdownitempurchase));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
itemListSpinner.setAdapter(myAdapter);
itemListSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),
itemListSpinner.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
//handles the search menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.searchmenu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
searchView.setQueryHint("Search..");
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
}
and this is a snippet of my custom toolbar in my fragment_purchase_item_list.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_purchaseitemlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
style="#style/PrimaryHeaderBar"
android:elevation="4dp">
<Spinner
android:id="#+id/spinner_allcategories"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</android.support.v7.widget.Toolbar>
I have tried checking all the related posts from the internet and can't find a decent tutorial on YT. Can someone tell me what is going on here?
Add following line in onCreateView() method
setHasOptionsMenu(true)
Also add below line in onViewCreated() method
(activity as AppCompatActivity?)!!.setSupportActionBar(customToolbar as Toolbar?)
Where customToolBar is the id of the toolbar added in xml file
Add this in your onCreateView method
setHasOptionsMenu(true);

Android Switch widget force close

Trying to create a switch in android with default ON state and with OnCheckedChangeListener. But application is force closing. What is wrong? please help. New in android development.
package com.tiumca2013.smarthousecontrol;
import android.app.Activity;
import android.app.Fragment;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends Activity{
private Switch switch1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switch1 = (Switch) findViewById(R.id.Switch1);
switch1.setChecked(true); // force close
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() // force close again
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
Toast.makeText(getApplicationContext(),"switch 1 ON",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),"switch 1 OFF",Toast.LENGTH_SHORT).show();
}
});
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}

navigation drawer icon animation

I started writing an app for Android. I have a problem with the Animation of the NavigationDrawer icon at the top left. It works correctly when the application is launched for the first time but when I pick an item from the list in the NavgationDrawer its icon stays in the "open" state even though the `NavigationDrawer closes as it should.
Here is my MainActivity:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.app.ActionBar;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.res.*;
public class MainActivity extends FragmentActivity {
ActionBarDrawerToggle icon;
final String[] listContent ={"Accueil","Fiche technique","Pilotes","Ecuries"};
final String[] fragments ={
"com.mycompany.f1holo.MainPageFragment",
"com.mycompany.f1holo.FirstFragment",
"com.mycompany.f1holo.SecondFragment",
"com.mycompany.f1holo.ThirdFragment"};
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> ad = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, listContent);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView list = (ListView) findViewById(R.id.drawer);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this,
drawer,
R.drawable.navdrawer,
R.string.open,
R.string.close);
drawer.setDrawerListener(actionBarDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
list.setAdapter(ad);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
#Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
transition.replace(R.id.mains, Fragment.instantiate(MainActivity.this, fragments[position]));
transition.commit();
}
});
drawer.closeDrawer(list);
}
});
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId())
{
case R.id.mainMenuAbout:
Toast.makeText(this, "F1 Holo", Toast.LENGTH_SHORT).show();
return true;
case R.id.mainMenuQuitter:
finish();
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
getMenuInflater().inflate(R.menu.main_about, menu);
return true;
}
}
And here is my FirstFragment:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
public static Fragment newInstance(Context context) {
FirstFragment frag = new FirstFragment();
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.first_fragment_layout, null);
return root;
}
}
I have found the solution and it is so obvious I could kick myself for not noticing before...
The problem is here:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener() {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
transition.replace(R.id.mains, Fragment.instantiate(MainActivity.this, fragments[position]));
transition.commit();
}
});
drawer.closeDrawer(list);
}
});
In this OnClickListener you are setting a new SimpleDrawerListener and therefore overriding this line:
drawer.setDrawerListener(actionBarDrawerToggle);
This disconnects the ActionBarDrawerToogle from the DrawerLayout and as a result stops the animations from playing... This is all you need:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
transition.replace(R.id.mains, Fragment.instantiate(MainActivity.this, fragments[position]));
transition.commit();
drawer.closeDrawer(list);
}
});
Why would you want to do this anyway? It just causes a delay between the user picking the item and the content actually changing. If it is because of performance issues - maybe that the close animation of the NavigationDrawer is not playing correctly - then doing something like this might be appropriate but in any case if you decide to implement this do it like this:
First create global variable called drawerItemSelection:
private String drawerItemSelection = null;
And then implement your ItemClickListener like this:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
drawerItemSelection = fragments[position];
drawer.closeDrawer(list);
}
});
And finally in your onCreate() method implement the ActionBarDrawerToogle like this:
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, R.drawable.navdrawer, R.string.open, R.string.close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if(drawerItemSelection != null) {
FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
transition.replace(R.id.mains, Fragment.instantiate(MainActivity.this, drawerItemSelection));
transition.commit();
drawerItemSelection = null;
}
}
};
If you have any further questions feel free to ask!

Issue with ViewPager inside Fragment and ActionBar (API level 16 Only)

Issue that I have found reproduce only on devices and emulators with API level 16 (4.1.2) + support library v4.
There is quite complicated structure: ActionBarActivity->Fragment(Outer)->ViewPager->Fragment(Inner, several instances).Each fragment inside ViewPager (Inner Fragments) changes ActionBar title and adds its own MenuItems. After replacing Outer fragment (that owns ViewPager) with another one (the same class: Outer) MenuItems (that first Inner fragment has added) become invisible and title trimmed(view that contain title do not re-sized). After swiping to the second Inner fragment and back everything return in proper state.
package com.bug.in.fragment.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
static String[] titles = {"[*****]", "[**********]", "[***************]",};
static int titleIndex;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new Outer()).commit();
}
}
private void changeOuterFragment() {
titleIndex++;
if (titleIndex > 2) {
titleIndex = 0;
}
getSupportFragmentManager().beginTransaction().replace(R.id.container, new Outer()).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add("A");
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
return true;
}
public static class Outer extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.f_outer, container, false);
ViewPager viewPager = (ViewPager) v.findViewById(R.id.container);
viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
#Override
public Fragment getItem(int position) {
return new Inner();
}
#Override
public int getCount() {
return 3;
}
});
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem item = menu.add("O.F");
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}
}
public static class Inner extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
((ActionBarActivity) getActivity()).getSupportActionBar().setTitle(titles[titleIndex]);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.f_inner, container, false);
v.findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity) getActivity()).changeOuterFragment();
}
});
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem item = menu.add("I.F");
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}
}
}
I created simple project that illustrated this issue:
https://github.com/yatsevskiy/bug_in_fragment.git

cannot resolve method setVisibilty(int)

I am new to android development.I am trying to create an activity method on clicking a button.
Below is my MainActivity.java file code.
I am getting errors
1."cannot resolve method'setVisibility (int)'" and
2."cannot resolve symbol 'TextView'"
please guide.
MainActivity.java
package com.AndroidLove;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void onLoveButtonClicked(View view) {
TextView textView =(TextView) findViewById(R.id.textView);
textView.setVisibility(View.VISIBLE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#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.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Import this package as well
import android.widget.TextView;

Categories

Resources