I'm trying to use an AppBar for my android studio project. In build gradle(module app) it implements :
com.google.android.material:material:1.1.0-alpha07 [I use this because i want
to use com.google.android.material.textfield.TextInputLayout on my xml/layout
files],
com.android.support.constraint:constraint-layout:1.1.3,
com.android.support.constraint:constraint-layout:1.1.3,
testImplementation 'junit:junit:4.12'.
In MainActivity.Java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar1 = findViewById(R.id.toolbar);
}
If I use 'toolbar.inflateMenu(R.menu.menu_bar);' or 'setActionBar();'[setActionBar(Toolbar toolbar)] when I ran my app it crashs immediately.
I've looked for solution but always in the build gradle(module app) was implemented 'com.android.support:appcompat-v7:28.0.0'.
And if I implement both of them my app crashs immediately
Can anyone help me to set my toolbar1 as an ActionBar?
This is my xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.example.MainActivity"
<Button
android:id="#+id/B_Back"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#android:color/black"
android:onClick="startsSecondActivity"
android:text="#string/B_Back_name"
android:textColor="#android:color/white"/>
<LinearLayout
android:id="#+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:hint="#string/hint">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="#string/app_name" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</LinearLayout>
Thanks,
Sbert.
Toolbar toolbar1 = findViewById(R.id.toolbar);
Works only when you have added a toolbar in your xml files with id toolbar. If you want to retrieve the default toolbar use
getSupportActionBar();
I personally prefer to create my own toolbar if you want a code example of this just ask me or check this link https://developer.android.com/training/appbar/setting-up
Related
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);
}
I am currently trying to center my Title in a new Android application. This is the basic title (top of the screen) that android has assigned to the app. The application is a very simple cookie-cutter "Tabbed" application created in Android Studio using the "Tabbed" template.
The title is simple text (about 30 characters). The title shows up in the main.xml when I select the "Design" option, but it is impossible for me to edit. Any help is appreciated.
Below is a copy of my main.xml:
<menu 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"
tools:context="com.my.app.MainActivity">
<item
android:id="#+id/action_settings"
android:layout_width="match_parent"
android:orderInCategory="100"
android:layout_gravity="center"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
You can create custom ToolBar as shown below
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/action_bar_bkgnd"
app:theme="#style/ToolBarTheme" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
You can set text of the TextView like below
TextView tv = (TextView) findViewById(R.id.toolbar_title);
tv.setText("Your Text");
You can use setTitle("my title") in activity. However there is no method to set it in center as it's not recommendend. But if you still want it then you can just pad with spaces. Like (" my title")
After playing around with the code, I figured out how to center the Toolbar Title & change the Toolbar Title text.
1st you have to edit the "onCreate" for the activity to turn off the default title provide by android. See sample below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
2nd you have to delete any references in the Toolbar XML (app:title = "your title" )for the app title. A TextView will then need to be added to the Toolbar XML. Below is a sample of the code that is working for me:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#67ccb2"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="22dp"
android:text="MY APP NAME"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
Notice in the TextView above that I have recreated the title, changed its text size, made it bold & centered it in the middle (where it looks awesome).
I hope this helps someone :)
I am trying to build a simple Android app one of whose activities will have a Google Places AutoCompleteTextView along with a Button and several other components. However, it seems to be the case that the AutoCompleteTextView component does not want to play friendly with anything else. Currently, I can only get one (the AutoCompleteTextView component) or the others rendering at a time. Here are the relevant portions of my code:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Please enter your place" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="#+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="#+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
GooglePlacesAutocompleteActivity.java
public class GooglePlacesAutocompleteActivity extends Activity implements OnItemClickListener {
// class fields are here ...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCurrentDateOnView(); // sets up the date picker
addListenerOnButton(); // adds a listener to a Button
// next three lines configure the Google Place autocomplete
AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.list_item));
autoCompView.setOnItemClickListener(this);
}
// more methods...
}
If you need more information to render an answer here, please feel free to ask me and I will post as soon as I can.
Hi You have a problem of orientation in your LinearLayout. I guess you want your AutocompleteTextView in top of the rest. If that is the case you should probably add a nested LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Please enter your place" >
<requestFocus />
</AutoCompleteTextView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
[your other items]
</LinearLayout>
</LinearLayout>
If this is not what you are looking for just change match_parent to wrap_content for thw width of your AutocompleteTextView.
I'm creating a search widget and a searchable activity. I followed the android developer guide and have this so far.
Here is my searchableActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
As mentioned, R.layout.search is creating the error. I don't have a search xml in layouts, and I don't understand what I am supposed to define within search.xml.
You need to have a layout named search.xml in your res/layout folder where you'll show the search results.
I my case I showed the search results in a RecyclerView and here's a sample layout of mine.
<FrameLayout 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:background="#color/color_background_search">
<LinearLayout
android:id="#+id/empty_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:tint="#color/color_primary_light"
android:src="#drawable/ic_action_search" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/empty_search_result"
android:textColor="#color/black"
android:textSize="20sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/search_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
You are trying to set the current layout to search.xml and you said yourself that you do not have this file present.
setContentView is setting the layout that you will see when running the app in this instance.
I am currently trying out the Android 1.5 SDK and I have seen a couple of examples on TabHost.
What I am trying to do is to use a different button on each Tab to do its tasks.
What I tried
was using onClickListiner() and onClick(). I think this is what all the developers use, but I keep getting a null exception on the LogCat every time the button is pressed. Also I have each XML Layout so I call the Tab as : tab.add(...setContent(R.id.firstTabLayout))
firstTabLayout = layout for Button and TextView.
What would be the best way to make a button/TextView work properly under the TabHost?
I'm not entirely sure where your problem is, but this is how I've set up my Tabbed activities before
layout.xml
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
Tab.java
public class InitialActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
TabHost th = getTabHost();
th.addTab(th.newTabSpec("tab_1").setIndicator("Tab1").setContent(R.id.tab1));
th.addTab(th.newTabSpec("tab_2").setIndicator("Tab2").setContent(R.id.tab2));
th.addTab(th.newTabSpec("tab_3").setIndicator("Tab3").setContent(R.id.tab3));
}
}
Then, you can just use findViewById() on any views within the tabs, or if there are shared names between them, you can do findViewById(R.id.tab1).findViewById(R.id.text)