How to remove view that is injected? - java

I have the following layout, activity_main.xml:
<merge
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">
<com.lorentzos.flingswipe.SwipeFlingAdapterView
android:id="#+id/frame"
android:background="#d1d1d1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rotation_degrees="15.5"
tools:context=".MainActivity"
android:layout_gravity="top"/>
<TextView
android:id="#+id/tv_noJobsLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#474747"
android:textAlignment="center"
tools:text="Nothing Left to Swipe!"
android:layout_gravity="center" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include layout="#layout/buttons" />
</merge>
In my main activity, I have the following code for injection using ButterKnife:
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
I want to remove the <include> part of the layout because I do not need those buttons anymore, however, when I remove the <include> line I get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lorentzos.swipecards.example/com.lorentzos.swipecards.MainActivity}: java.lang.RuntimeException: Unable to inject views for com.lorentzos.swipecards.MainActivity#f356341
What am I doing wrong?
Is there some reason why I cannot remove the <include>?
EDIT:
#InjectView(R.id.frame) SwipeFlingAdapterView flingContainer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//color the notification bar with our company colors
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.laborswipe_notificationbar));
//remove title from action bar and add the logo to the top left of the action bar
setUpActionBar();
setContentView(R.layout.activity_main);
ButterKnife.inject(this);

After removing your <include layout=.../> also remove the binded view from the layout in your activity.
Notes:
Binding views with ButterKnife means that you have views in a layout with the binding codes in your main activity. So there should be a code with:
#BindView(R.id.your_view_in_include_button) View yourViewName;
Remove this according with the view in your 'include layout.
Then rebuild your app.
Suggestion:
Upgrade your ButterKnife to current version.

Related

How to make the toolbar appear when switching between fragments using CoordinatorLayout, Toolbar and fragment

I'm using the layout below, The CoordinatorLayout holds inside it AppBarLayout (With Toolbar) and the fragment layout.
I'm using app:layout_scrollFlags in my Toolbar and app:layout_behavior="#string/appbar_scrolling_view_behavior" in the fragment, so the Toolbar appear and disappear (hide/show) with scrolling my fragment content.
My problem: when I scroll in fragment A and therefore the toolbar disappear, then I use the BottomNavigationView to go to fragment B the toolbar remains hidden.
I want the toolbar to appear every time I open new fragment.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/containerMain"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/AppBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_gravity="top"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="#+id/nav_host_fragment"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="#dimen/nav_bar"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:itemIconTint="#drawable/bottom_color_nav"
app:itemTextColor="#drawable/bottom_color_nav"
app:layout_constraintBottom_toBottomOf="#id/main_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
I would assume that the xml you posted is your layout for your MainActivity(), if so create a reference to your Toolbar in your MainActivty and create a function in your MainActivty() to access/change your Toolbar collapse states.
In your Fragments, create a reference to your MainActivity to then access the function to change the state of your Toolbar.
I cannot test as from work, but I have used same code flow to do the same stuff when using only MainActivity with Inner Fragments as the navigation flow of the app.
Below not exact working or tested code, but same idea/flow
//my fragment function to access MainActivity().changeToolbarState(boolean)
changeToolbar(state: boolean) {
if(state){
//true
//show toolbar
MainActivty().changeToolbarState(true)
}else{
//false
//collapse/hidetoolbar
MainActivty().changeToolbarState(false)
}
}
The best alternative could be enabling setExpanded property of app bar layout to true with the following code inside the fragment. Here, MainActivity.appBarLayout is the static variable defined in Main Activity referenced to App Bar Layout.
#Override
public void onPause() {
super.onPause();
MainActivity.appBarLayout.setExpanded(true);
}

My actionBar does not appear

I have been having trouble with my action bars, I got it set up on my xml front end like this:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:label="#string/dadosCadastrais"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
And on the Java activity:
Toolbar toolbar = findViewById(R.id.toolbar4);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
I have already tried changing the theme the app uses, the theme the layout uses and even the activities theme to allow/forbid a toolbar to show up. Even though my java class extends AppCompatActivity it does not show!
PS: Got it working now! The problem was the setup on java code itself, there was an hidden method which was overwritting my setup, thx for the help!
Make sure your activity inflates the correct layout containing your ToolBar.
protected void setContentView() {
setContentView(R.layout.your_activity_layout);
}
Your activity layout should contain your ToolBar. Here is a code example:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>

Custom layout in Toolbar

Recently I migrated from the old ActionBar to the new Toolbar.
I have a custom activity (actually a fragment inside my main activity) in which I'm allowing user to click on an icon in the toolbar, and then a search box appears (in the toolbar), using custom layout.
Problem is, it worked fine before the migration, but now once I click the button and switch to the custom action bar layout with the search box, the layout spans on all screen, instead of the normal size of the toolbar.
Here's the code I'm using to replace the toolbar:
LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View actionBarView = inflator.inflate(R.layout.map_action_layout, null);
actionBar = ((DrawerActivity)getActivity()).getSupportActionBar();
actionBar.setCustomView(actionBarView);
Here's the map action layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
android:id="#+id/autoComplete"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:hint="#string/search_adress_hint"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete|textAutoCorrect"
android:paddingRight="50dp"
android:textColor="#android:color/white"
android:textCursorDrawable="#null" >
<requestFocus />
</AutoCompleteTextView>
<ImageButton
android:id="#+id/clear_address"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="17dp"
android:background="#drawable/ic_action_cancel"
android:gravity="center_horizontal|center_vertical"
android:scaleType="fitCenter" />
</RelativeLayout>
Note: no matter what value I put in the height of the above, whether it's fixed dp value or wrap_content, the layout still spans on whole screen.
Here's my toolbar layout being replaced:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"
android:elevation="4dp"
app:layout_collapseMode="pin">
</android.support.v7.widget.Toolbar>
The toolbar resides inside an AppBarLayout.
Does anyone have an idea what am I doing wrong? How can I achieve the result I'm looking for? Thanks.
It is standard to have the toolbar height set to ?attr/actionBarSize.
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />

Trying to include toolbar to Application that used NavDrawer template

I am trying to move an old app over to the new Material design guidelines. I used the Navigation Drawer Template previously to create the Main Activity. However I am having some massive problems trying to implement the Toolbar to the main Activity. I am continuously getting NullPointer Exceptions.
I am creating a toolbar xml file like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue" >
</android.support.v7.widget.Toolbar>
After that I am trying to add it to my main layout using the include command:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.medinfo.main.MainActivity" >
<include layout="#layout/toolbar_database_edit" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/navigation_drawer"
android:name="com.medinfo.fragments.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
In my activity_main I am doing the following:
The NPE is occuring when I call setSupportActionBar or when the Setup NavDrawer is called.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.layout.toolbar_database_edit);
setSupportActionBar(toolbar);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
}
I tried wrapping the DrawerLayout in a linear-layout, what that did was create the toolbar behind the container that contains my fragments, when I started the activity I saw the toolbar rendered in the background then hidden once the Container and fragments were loaded. It did however remove the NPE. I also tried creating the toolbar within the layout instead of using a separate toolbar XML file. I still got the same NPE error.
While I am asking about the new Material Design Stuff, I would also like to know if there is a way to get my EditText Widgets and Spinner Widgets to use the old Holo Theme/Style.
I am really hoping there is a simple way to revert to those styles.
Thank you for the help everyone
1) First, your layout is incorrect. DrawerLayout accepts two children: layout container and drawer layout itself.
Simplified,
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/activity" />
<include layout="#layout/drawer" />
</android.support.v4.widget.DrawerLayout>
Second, you incorrectly findViewById your toolbar.
Toolbar toolbar = (Toolbar) findViewById(R.layout.toolbar_database_edit);
Here you're referencing to layout, but should reference to id.
Given the layout resource above, we have activity.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/blue"
android:foreground="?android:attr/windowContentOverlay"
app:theme="#style/Toolbar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
. . . contents of activity layout
</FrameLayout>
</LinearLayout>
In code:
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
2) To use old Holo style, you should use Holo theme as parent of your app theme in styles.xml. But users would expect native theme on each Android version, so it's a bad practice.

Creating Custom Action bars for each activity

I need to create custom title bars for each and every Screen in my android application. For an example, in GMail, Yahoo Mail android applications, have you noticed that their title bar differs from activity to activity? This is what I need.
I went through number of tutorials, unfortunatly, they are to replace the title bar of the entire application with one custom title bar.
Following is what I have tried
Cuatom_title.xml
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/bileeta_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/_title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#579bef"
android:textSize="50sp" />
<EditText
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="right"
android:layout_marginRight="30dp"
android:ems="10"
android:hint="#string/search_hint"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
MainActivity.java
//Setting custom title bar
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_home);
//Set the custom title bar
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
But this just generated errors.
How can I create custom title bars for each and every activity?
in your onCreate()
ActionBar actionBar = getActionBar(); //for higher version
actionBar.setDisplayShowCustomEnabled(true);
View customView=getLayoutInflater().inflate(R.layout.yourlayout, null);
actionBar.setCustomView(customView);
OR
ActionBar actionBar = getSupportActionBar(); //to support lower version too
actionBar.setDisplayShowCustomEnabled(true);
View customView=getLayoutInflater().inflate(R.layout.yourlayout, null);
actionBar.setCustomView(customView);
In your manifest file, while declaring activity you can add this line android:label="#string/your_title_string"
Example:
<activity
android:name=".MyActivity"
android:label="#string/your_title_string" >
</activity>

Categories

Resources