I want to add an autocompletetextview component inside the action bar of a AppCompatActivity. My code is:
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread.setDefaultUncaughtExceptionHandler(new GlobalException());
Toolbar tb = (Toolbar) findViewById(R.id.toolbar_search);
setSupportActionBar(tb);
}
layout/action_bar_search.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar_search"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="pin">
<AutoCompleteTextView
android:id="#+id/theSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/search_the"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete|textAutoCorrect"
android:textColor="#FFFFFF" >
<requestFocus />
</AutoCompleteTextView>
</android.support.v7.widget.Toolbar>
When my app opens, I see a blank white screen and after several seconds, it becomes a blank black screen that stays this way forever, until I stop it manually. Any idea?
Apparently, after Toolbar tb = (Toolbar) findViewById(R.id.toolbar_search); tb is null
Your Toolbar XML is not included in your R.layout.activity_main so findById will not work. This results in the Toolbar staying null and crashing your application. You can include layout resources with an <include> tag. In your case: <include layout="#layout/action_bar_search"/>.
Below is the example code for AutocompleteTextView:
AutoCompleteTextView text;
String[] languages={"Android ","java","IOS","SQL","JDBC","Web services"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(AutoCompleteTextView)findViewById(R.id.theSearch);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
text.setAdapter(adapter);
text.setThreshold(1);
}
I hope that will help you....
Related
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar saadaAB = getSupportActionBar();
saadaAB.setLogo(R.drawable.run);
saadaAB.setDisplayUseLogoEnabled(true);
saadaAB.setDisplayShowHomeEnabled(true);
}
}
I want to print a logo in action bar in my app but this code make that logo in center and remove text in action bar.
you can achieve it in 2 ways
1. if Apptheme is in
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
You can just call
getSupportActionBar().setHomeAsUpIndicator(R.drawable.squarebox);// set drawable icon
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2. or you can customize toolbar like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
</android.support.design.widget.AppBarLayout>
<!-- Here goes your UI -->
</LinearLayout>
with java
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.custom_icon);
This question already has answers here:
navigation drawer is always inflated when the app starts
(2 answers)
Why is my Android navigation drawer opening too wide?
(1 answer)
Clicking hamburger icon on Toolbar does not open Navigation Drawer
(1 answer)
Closed 3 years ago.
I am experiencing a weird issue. I created a navigation view inside a drawer layout, however navigation view is just visible once the activity starts. The toolbar hamburger menu is not clickable.
main_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view"
>
<group android:checkableBehavior="single">
<item android:id="#+id/menu_explore" android:title="#string/explore"
android:icon="#drawable/exploreicon" />
<item android:id="#+id/menu_profile" android:title="#string/profile"
android:icon="#drawable/profileicon"/>
<item android:id="#+id/menu_search" android:title="#string/search"
android:icon="#drawable/searchicon"/>
<item android:id="#+id/menu_settings" android:title="#string/settings"
android:icon="#drawable/settingsicon"/>
<item android:id="#+id/menu_bookmarked" android:title="#string/bookmarked"
android:icon="#drawable/bookmarkicon"/>
<item android:id="#+id/menu_logout" android:title="#string/log_out"
android:icon="#drawable/logouticon"/>
</group>
</menu>
activity_main.xml
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_gravity="start"
android:clickable="false"
android:clipToPadding="false"
android:fitsSystemWindows="true"
android:padding="16dp"
android:scrollIndicators="start"
android:visibility="visible"
tools:openDrawer="start">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FFF"
app:headerLayout="#layout/menu_header"
app:itemIconTint="#6b6b6b"
app:itemTextColor="#6b6b6b"
app:menu="#menu/main_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
style for main activity:
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
Main Activity:
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.mainToolbar);
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawerLayout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,
R.string.open,R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
}
I want the navigation view to be only visible when the hamburger icon in the toolbar is clicked. Every tutorial I followed did the same things with me, however mine doesn't show inside the toolbar.
Pass toolbar instance during initiation of ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout, toolbar, R.string.open,R.string.close);
First of all, I know this question has been asked before, I've spent an hour looking through other solutions, mostly from - getActionBar() returns null , and none have worked so another pair of eyes might be able to do something.
NavigationDrawer class which other classes inherit from:
public class NavigationDrawer extends AppCompatActivity {
String[] mDrawerTitles;
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
String activityTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerTitles = getResources().getStringArray(R.array.drawer_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
activityTitle = getTitle().toString();
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //here is where the error is thrown
getSupportActionBar().setHomeButtonEnabled(true);
addDrawerItems();
navDrawerSetup();
}
MainActiviy which extends the above:
public class MainActivity extends NavigationDrawer {
int[]images={R.drawable.user_manual,R.drawable.tips_tricks, R.drawable.troubleshooting};
Integer printer;
ListView myListView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_main, null, false);
mDrawerLayout.addView(contentView, 0);
}
Navigation Drawer .xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#999"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.shelleyd.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
content_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.shelleyd.myapplication.MainActivity"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/myList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
In NavigationDrawer activity you are performing setContentView(R.layout.navigation_drawer_layout), and in navigation_drawer_layout there is no any Toolbar, which leads to an exception.
Doing the following in you NavigationDrawer.class
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //This is null
setSupportActionBar(toolbar);
You are setting Activity toolbar to null, because there is no view in your R.layout.activity_main with R.id.toolbar ID. So when you try to set toolbar after the code above, you are trying to set a property in a null object, that produces a null pointer exception.
Remove the two lines above in your activity or declare a Toolbar view with R.id.toolbar ID on your R.layout.activity_main layout file. Doing that you will solve the problem.
I'm developing a material design app & I have declared a xml file for Settings option. I have done this task successfully, but after running & opening the SettingsActivity from menu, it is appearing like this:
The problem here is that the title which is 'Notification' & the checkBox's color is very light making them difficult to be seen.
Here's SettingsActivity.java file's code:
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
SpannableString s = new SpannableString("Settings");
s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(s);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
// do something
}
}
}
}
Here's activity_settings.xml file's code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xxx.abc.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_below="#id/toolbar"
android:layout_height="match_parent"/>
</RelativeLayout>
Here's settings.xml file's code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="prefNotification"
android:title="#string/prefNotification">
<CheckBoxPreference
android:id="#+id/prefNotifyCheckBox"
android:dependency="prefNotification"
android:key="prefNotify"
android:summary="#string/pref_notify_summary"
android:defaultValue="true"/>
</PreferenceCategory>
</PreferenceScreen>
As I'm new to Android Development, I'm unable to figure out the problem here.
Please let me know.
Thanks in advance.
PreferenceFragment uses the value stored in colorAccent of your app's theme to style widgets and titles by default. To resolve it, create a custom theme in your styles.xml file, and set the colorAccent attribute to an alternate colour:
<style name="theme_name" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/another_color</item>
<item name="colorAccent">#color/yet_another_color</item>
</style>
To apply it to your PreferenceFragment, call getActivity().setTheme(R.style.theme_name) in onCreate() of your PreferenceFragment.
I want to set FitsSystemWindows = true for my Toolbar or/and change notification bar color. But it does not work.
What I wanna do: image
My code here:
Styles
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/colorWhite</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
ActivityDrawerLayout - base Activity for other Activities
<android.support.v4.widget.DrawerLayout ...
android:fitsSystemWindows="true"
tools:context="com.peter.freshNews.activity.DrawerActivity">
<android.support.design.widget.NavigationView ...
android:background="#color/drawer_background"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
ActivityMain
<FrameLayout ...
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinatorLayout"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager.../>
<include layout="#layout/toolbar" />
</android.support.design.widget.CoordinatorLayout>
Toolbar
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="#android:color/transparent">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:collapsedViewOffset="0dp"
app:contentScrim="?attr/colorPrimary"
app:expandedViewOffset="0dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ViewSwitcher
...
</ViewSwitcher>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:fitsSystemWindows="true"
android:background="#android:color/transparent" />
<FrameLayout
android:id="#+id/collapsing_logo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">...</FrameLayout>
<android.support.design.widget.TabLayout.../>
</android.support.design.widget.CollapsingToolbarLayout>
MainActivity.java
public class MainActivity extends DrawerActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View contentView = inflater.inflate(R.layout.activity_main, null, false);
mDrawerLayout.addView(contentView, 0);
//...
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbarLayout);
collapsingToolbarLayout.setContentScrimColor(utils.getPageColor(0));
appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
//----------------------------------t_o_o_l_b_a_r-------------------
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
toolbar.setBackgroundResource(R.color.transparent);
setSupportActionBar(toolbar);
}
//---------------------------------a_c_t_i_o_n_-_b_a_r--------------
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED));
}
}
android:fitsSystemWindows is for making status bar and/or navigation bar transparent. I am not aware that it can be applied to Toolbar.
As for changing navigation bar's colour part, you can do it like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// finally change the color
window.setStatusBarColor(rgbColor);
}
And for Toolbar you can do it in XML by setting app:contentScrim="?attr/colorPrimary" property to CollapsingToolbarLayout, or in code:
collapsingToolbarLayout.setContentScrimColor(toolbarColor);
collapsingToolbarLayout.setExpandedTitleColor(textColor);
collapsingToolbarLayout.setCollapsedTitleTextColor(textColor);