androidx BottomNavigationView not showing up - java

I cannot get the bottom navigation to show up in androidx at all. I have even copied examples from other answers that are supposed to work, and they show up in the preview. activity.xlm:
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/frameLayout"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/homeButton"
android:enabled="true"
android:icon="#android:drawable/ic_menu_agenda"
android:title="a funny item"/>
<item
android:id="#+id/favoritesButton"
android:enabled="true"
android:icon="#android:drawable/ic_menu_info_details"
android:title="favorites"/>
<item
android:id="#+id/ideaButton"
android:enabled="true"
android:icon="#android:drawable/ic_menu_agenda"
android:title="ideas"/>
</menu>
Styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.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>
activity code:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private AppBarConfiguration appBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
What am I missing? Also why would it show up in the preview in android studio and not show when the app runs? Anyhelp is greatly appreciated

Yor forgot add setContentView(R.layout.activity_main); in your MainActivity
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private AppBarConfiguration appBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

Related

How to make a clickable star-button in android studio?

I'm trying to make a clickable star button which will have a fill in color of red when clicked on. Before I click the build button, Android Studio does not highlight and error in my program. However, it is after I click the build button that Android Studio shows that everything is wrong with my styles.xml file but I am not sure how I can fix it.
MainActivity.java
package com.pace.importantbutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onDefaultToggleClick(View view){
Toast.makeText(this,"DefaultToggleClick", Toast.LENGTH_SHORT).show();
}
public void onCustomToggleClick(View view){
Toast.makeText(this,"CustomToggle", Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:onClick="onDefaultToggleClick"
/>
<ToggleButton
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:background="#drawable/toggle_selector"
android:onClick="onCustomToggleClick"
/>
</LinearLayout>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<Style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Theme will be customized here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</Style>
</resources>
toggle_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_baseline_star_outline_24" android:state_checked="false"></item>
<item android:drawable="#drawable/ic_baseline_star_24" android:state_checked="true"></item>
</selector>
ic_baseline_star_outline_24.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#android:color/holo_red_light"
android:pathData= "..."
/>
</vector>
ic_baseline_star_24.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#android:color/holo_red_light"
android:pathData= "..."
/>
</vector>
I do it in my project by using an ImageView.
ImageView favorite = findViewById(R.id.favoriteButton);
//Here you put the logic to start with the outline or the filled star
if (user.isFavorite(productKey)) favorite.setImageDrawable(getDrawable(R.drawable.ic_filled_star));
else favorite.setImageDrawable(getDrawable(R.drawable.ic_outline_star));
//Finally you set your listener to change the image
favProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (favorite.getDrawable().getID()==R.drwable.ic_filled_star){
favorite.setImageDrawable(getDrawable(R.drawable.ic_outline_star));
//The other things you want to do
}
else{
favProduct.setImageDrawable(getDrawable(R.drawable.ic_filled_star));
//The other things you want to do
}
}
});

Navigation View is not inside toolbar [duplicate]

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);

Not getting Custom Toolbar Title

I tried various solution to get toolbar title but none of them worked. like using and others. What went wrong with my code. can you advice. I m getting menu and their options but Title of toolbar no luck.
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("Hello");
Below is my style.xml code:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
My mainactivity code:
package jss.customtoolbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
#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(true);
getSupportActionBar().setTitle("Hello");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
menu.xml code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/miCompose"
android:icon="#mipmap/ic_launcher"
android:title="Compose"
app:showAsAction="ifRoom" />
<item
android:id="#+id/miProfile"
android:title="Profile" />
</menu>
Toolbar code:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:title="Hello">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</LinearLayout>
and activity_main.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="jss.customtoolbar.MainActivity">
<include
layout="#layout/tool"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
You can set title to toolbar for reference no need to get toolbar of support.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Hello");
setSupportActionBar(toolbar);
}
Toolbar xml
<?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:orientation="vertical"
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="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:title="Hello">
</android.support.v7.widget.Toolbar>
</LinearLayout>
activity xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="jss.customtoolbar.MainActivity">
<include
layout="#layout/tool"/>
</LinearLayout>

Android app takes only a part of screen

My app for some reason takes just a part of screen.
Simple onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
Slightly more complicated layout:
<RelativeLayout 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:id="#+id/content_layout" >
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<LinearLayout
android:id="#+id/func_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#drawable/border_right"
android:orientation="vertical"
android:paddingLeft="#dimen/function_layout_padding"
android:paddingRight="#dimen/function_layout_padding" >
</LinearLayout>
<RelativeLayout
android:id="#+id/graph_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#FFF"
android:alpha="1.0"
android:layout_weight="5">
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
And I got this:
I can't see what the trouble is. Thought that there is emulator problem but on real devices it looks the same.
Okay so try this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
You do not need that below because it will create this kind of windows.
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Thanks to #SunitKumarGupta #AnkitBansal and #Maxouille.
The problem was in Theme.
What it was:
<style name="Theme.Transparent" parent="AppBaseTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">false</item>
Problem item here is windowIsFloating. Removing it solved the issue.

Android - Styling Spinner within TabView

I've got an app that uses tabs for navigation, and on one of those tabs there is a spinner. However, when the spinner is selected and the actual select window comes up, all the text is white on a white background.
I've tried styling the layout but nothing I do changes the color of the font.
the main class
public class RealmsOfWickedry extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
firstTabSpec.setIndicator("Home").setContent(new Intent(this,FirstTab.class));
secondTabSpec.setIndicator("Catalog").setContent(new Intent(this,SecondTab.class));
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
}
public static View makeSpinner(Context context) {
View v = LayoutInflater.from(context).inflate(R.layout.spinner, null);
Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);
adapter.add("Item 1");
adapter.add("Item 2");
adapter.add("Item 3");
adapter.add("Item 4");
spinner.setAdapter(adapter);
return v;
}
}
the class with the spinner
public class SecondTab extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Second Tab Content */
TextView textView = new TextView(this);
textView.setText("Choose a Category");
setContentView(textView);
setContentView(RealmsOfWickedry.makeSpinner(getParent()));
}
}
tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost">
<LinearLayout android:id="#+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_height="fill_parent" android:layout_width="fill_parent"></FrameLayout>
</LinearLayout>
</TabHost>
spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/cat_prompt"
android:theme="#style/DropdownStyle"
/>
</LinearLayout>
themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="OverallStyle" parent="#android:Theme.Light">
<item name="android:windowBackground">#drawable/bg</item>
<item name="android:textColor">#color/white</item>
</style>
<style name="WelcomeStyle" parent="#android:Theme.Light">
<item name="android:typeface">monospace</item>
<item name="android:gravity">center</item>
</style>
<style name="CustomStyle" parent="#android:Theme.Light">
<item name="android:typeface">monospace</item>
<item name="android:gravity">top</item>
</style>
<style name="DropdownStyle" parent="#android:Theme.Light">
<item name="android:textColor">#color/red</item>
</style>
</resources>
Can anyone help me out with this?
It looks like you're trying to override the system theme to show a different color which is the right path to be on. Your spinner xml contains a reference to android:theme I haven't see that one before and it doesn't appear to be part of the API for this widget. To get your DropdownStyle to work, first, add it as part of your OverallStyle style with an item name of #android:attr/spinnerDropDownItemStyle. Second, change DropdownStyle's parent to #android:Widget.DropDownItem.Spinner. I'm assuming that OverallStyle is applied to the Activity or Application in the Manifest already. This will change the style for all Spinner drop down Items.
To apply only to this view's drop down items do only step two above then add style="#style/OverallStyle" to the spinner in its layout.
Additional Information:
<style name="DropdownStyle" parent="#android:Widget.DropDownItem.Spinner">
<item name="android:textColor">#color/red</item>
</style>
<style name="OverallStyle" parent="#android:Theme.Light">
<item name="android:windowBackground">#drawable/bg</item>
<item name="#android:attr/spinnerDropDownItemStyle">#style/DropdownStyle</item>
<item name="android:textColor">#color/white</item>
</style>
-OR-
themes.xml
<style name="DropdownStyle" parent="#android:Widget.DropDownItem.Spinner">
<item name="android:textColor">#color/red</item>
</style>
spinner.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/cat_prompt"
style="#style/DropdownStyle"
/>
</LinearLayout>

Categories

Resources