Android : BottomNavigationView icons are not visible - android

Thanks for stopping by. So my problem here is, the icons from bottom_navigation_menu.xml from menu source file are not visible. I tried Google, Stackoverflow, and here are things that I have tried but still they are not visible: Restart Android Studio, Add selector, Change dependencies to androidX, Class to androidX, re-read the documentation.
Hope someone can help me out...thanks!!!!
I'm using Android Studio 3.6.3, Gradle 6.4
Class file
package com.example.todayilearnedbeta;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class AddEntriesActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addentries);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(2);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.ic_main:
Intent intent0 = new Intent(AddEntriesActivity.this, MainActivity.class);
startActivity(intent0);
break;
case R.id.id_search:
Intent intent1 = new Intent(AddEntriesActivity.this, SearchActivity.class);
startActivity(intent1);
break;
case R.id.ic_add_entries:
break;
case R.id.ic_past_entries:
Intent intent3 = new Intent(AddEntriesActivity.this, PastEntriesActivity.class);
startActivity(intent3);
break;
case R.id.ic_profile:
Intent intent4 = new Intent(AddEntriesActivity.this, ProfileActivity.class);
startActivity(intent4);
break;
}
return false;
}
});
}
}
Layout XML codes:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".AddEntriesActivity"
android:background="#drawable/add_entry_activity_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="#+id/bottomBar"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bottomNavView_Bar"
app:menu="#menu/bottom_navigation_menu"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemBackground="#color/colorPrimary"
app:itemTextColor="#drawable/nav_item_color_state">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
bottom_navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ic_main"
android:icon="#drawable/ic_main"
android:title="MainLogo"
/>
<item
android:id="#+id/ic_search"
android:icon="#drawable/ic_search"
android:enabled="true"
android:title="Search"
/>
<item
android:id="#+id/ic_add_entries"
android:icon="#drawable/ic_add_entries"
android:enabled="true"
android:title="AddEntries"
/>
<item
android:id="#+id/ic_past_entries"
android:icon="#drawable/ic_past_entries"
android:enabled="true"
android:title="PastEntries"
/>
<item
android:id="#+id/ic_profile"
android:icon="#drawable/ic_profile"
android:enabled="true"
android:title="Backup"
/>
Nav bar item colour selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="ffffff" android:state_checked="true" />
<item android:color="#000000" android:state_checked="false" />
</selector>
Dependencies (here just showing that I implement the latest material dependency.
dependencies {
implementation 'com.google.android.material:material:1.2.0-alpha06'
}

In your buttom_navigatiom_menu.xml , add this to set the visibility status
android:visible="true"
Make sure you add visibility status cod to each of the icon items
Example
<item
android:id="#+id/ic_add_entries"
android:icon="#drawable/ic_add_entries"
android:visible="true"
android:enabled="true"
android:title="AddEntries"
/>
If this does not work for you, try this:
Go-to File Menu in Android Studio
Invalid Caches & Restart
Open existing project

Related

BottomNavigationView using androidx

I have created some app and I had like to insert to it a BottomNavigationView.
The code worked perfectly however once I changed at my gradle to androidx it stopped working.
The component in my layout (activity_about):
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/bottom_navigation"
app:itemBackground="#color/colorWhite"
app:itemIconTint="#drawable/bottom_navigation_foreground"
app:itemTextColor="#drawable/bottom_navigation_foreground" />
The menu file is:
<?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/navigation_library"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Library"
android:icon="#drawable/ic_home_black_24dp"/>
<item
android:id="#+id/navigation_search"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Search"
android:icon="#drawable/ic_search_black_24dp"/>
<item
android:id="#+id/navigation_profile"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Profile"
android:icon="#drawable/ic_account_circle_black_24dp"/>
</menu>
The drawable file (bottom_navigation_foreground):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/colorPurpleFont" />
<item android:state_checked="false" android:color="#color/Gray" />
</selector>
and my code is as follows:
public class AboutActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_library:
startActivity(new Intent(AboutActivity.this, DiscoverActivity.class));
break;
case R.id.navigation_search:
Toast.makeText(AboutActivity.this, "Favorites", Toast.LENGTH_SHORT).show();
break;
case R.id.navigation_profile:
Toast.makeText(AboutActivity.this, "Nearby", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
}
}
Any reason it is not working since I changed to androidx?
Thank you
Using androidx you have to switch to BottomNavigationView in the Material components library.
Add the dependency in build.gradle
dependencies {
//..
implementation 'com.google.android.material:material:1.2.1'
}
use a material theme and add in your layout:
<com.google.android.material.bottomnavigation.BottomNavigationView
.../>
i have solved same problem by replacing
<android.support.design.widget.BottomNavigationView
>
change it to and also implement the dependencies too
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="56dp"
>
in build.gradle
implementation 'com.google.android.material:material:1.2.0'
refer android documentation
https://developer.android.com/reference/com/google/android/material/bottomnavigation/BottomNavigationView

Click on navigation does not open the activity

I'm working on bottom navigation. The problem is that, when I click on one icon on the navigation bar, it is open the activity I want but the icon is appeared it doesn't clicked it just by default the first icon appear as it clicked all the time
public class MainActivity
extends AppCompatActivity
implements
BottomNavigationView.OnNavigationItemSelectedListener {
BottomNavigationView nav;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nav = (BottomNavigationView) findViewById(R.id.navigation);
nav.setOnNavigationItemSelectedListener(this);
} // ------------------end of on create--------------------
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.prof:
Intent intent = new Intent(MainActivity.this,
profile.class);
menuItem.setIcon(R.drawable.profile);
nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
startActivity(intent);
break;
case R.id.hm:
Intent intent1 = new Intent(MainActivity.this,
MainActivity.class);
nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
startActivity(intent1);
break;
case R.id.consult:
Intent intent3 = new Intent(MainActivity.this, tabs.class);
nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
startActivity(intent3);
break;
case R.id.dash:
Intent intent2 = new Intent(MainActivity.this,
Dashboard.class);
nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
startActivity(intent2);
break;
}
return false;
}
}
my main activity layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context=".MainActivity">
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/purple"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemTextColor="#drawable/nav_item_color_state"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
my navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/hm"
android:icon="#drawable/hom"
android:enabled="true"
app:showAsAction="ifRoom"
tools:ignore="MenuTitle" />
<item
android:id="#+id/prof"
android:icon="#drawable/profile"
android:enabled="true"
app:showAsAction="ifRoom"
tools:ignore="MenuTitle" />
<item
android:id="#+id/consult"
android:icon="#drawable/consult"
android:enabled="true"
app:showAsAction="ifRoom"
tools:ignore="MenuTitle" />
<item
android:id="#+id/dash"
android:icon="#drawable/dashboard"
android:enabled="true"
app:showAsAction="ifRoom"
tools:ignore="MenuTitle" />
</menu>
this my nav_item_color_state.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white"
android:state_enabled="true"/>
<item android:color="#color/colorPrimaryDark"
android:state_enabled="false"/>
</selector>
this my interface in all my activity
enter image description here
please help me I update the question
You don't bind any onNavigationItemSelected() listener there; for example:
nav = (BottomNavigationView) findViewById(R.id.navigation);
nav.setNavigationItemSelectedListener(this);

Android Bottom Navigation Bar

I am new to android app development and in the process of making my first app.
I am trying to create a bottom navigation bar. I have created the actual navigation bar but i am trying to now get it so when i click on that icon it will go to a different page. I currently have it so it will show some text saying you clicked on this button etc. but i am unsure how to make it so it will go to a different page.
This is the code i currently have
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
Toast.makeText(MainActivity.this, "Action Add Clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.action_exercise:
Toast.makeText(MainActivity .this, "Action Exercise Clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.action_timer:
Toast.makeText(MainActivity.this, "Action Timer Clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
}
}
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gymapp.HomePage">
<ImageView
android:id="#+id/imageView"
android:layout_width="318dp"
android:layout_height="198dp"
app:srcCompat="#drawable/getinshape"
tools:layout_editor_absoluteX="33dp"
tools:layout_editor_absoluteY="16dp"
tools:ignore="ContentDescription" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemTextColor="#drawable/nav_item_color_state"
app:menu="#menu/bottom_nav_bar"
/>
</RelativeLayout>
This is also the code I'm using within the res/menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_add"
android:enabled="true"
android:icon="#drawable/ic_home"
android:title="Home"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/action_exercise"
android:enabled="true"
android:icon="#drawable/exercise"
android:title="Exercise"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/action_timer"
android:enabled="true"
android:icon="#drawable/ic_timer"
android:title="Timer"
android:showAsAction="ifRoom"
/>
</menu>

Creating a button in Android Toolbar

How can I create a button inside Android's Toolbar that looks like this iOS example?
ToolBar with Button Tutorial
1 - Add library compatibility inside build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
2 - Create a file name color.xml to define the Toolbar colors
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ColorPrimary">#FF5722</color>
<color name="ColorPrimaryDark">#E64A19</color>
</resources>
3 - Modify your style.xml file
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/ColorPrimary</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Customize your theme here. -->
</style>
</resources>
4 - Create a xml file like tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:elevation="4dp" />
5 - Include the Toolbar into your main_activity.xml
<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"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
<TextView
android:layout_below="#+id/tool_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/TextDimTop"
android:text="#string/hello_world" />
</RelativeLayout>
6 - Then, put it inside your MainActivity class
package com.example.hp1.materialtoolbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
/* When using AppCompat support library
* (you need to extend Main Activity to
* ActionBarActivity)
* ActionBarActivity has deprecated, use AppCompatActivity
*/
public class MainActivity extends ActionBarActivity {
// Declaring the Toolbar Object
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// Attaching the layout to the toolbar object
toolbar = (Toolbar) findViewById(R.id.tool_bar);
// Setting toolbar as the ActionBar with setSupportActionBar() call
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
7 - And finally, add your "Button Items" to the menu_main.xml inside of /res/menu/ directory
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="Search"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_user"
android:orderInCategory="300"
android:title="User"
android:icon="#drawable/ic_user"
app:showAsAction="ifRoom" />
</menu>
Toolbar customization can done by following ways
write button and textViews code inside toolbar as shown below
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_height_small"
android:text="Departure"
android:layout_gravity="right"
/>
</android.support.v7.widget.Toolbar>
Other way is to use item menu as shown below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Another possibility is to set the app:actionViewClass attribute in your menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/get_item"
android:orderInCategory="1"
android:text="Get"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.AppCompatButton"/>
</menu>
In your code you can access this button after the menu was inflated:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.sample, menu);
MenuItem getItem = menu.findItem(R.id.get_item);
if (getItem != null) {
AppCompatButton button = (AppCompatButton) getItem.getActionView();
//Set a ClickListener, the text,
//the background color or something like that
}
return super.onCreateOptionsMenu(menu);
}
I have added text in ToolBar :
menu_skip.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=".MainActivity">
<item
android:id="#+id/action_settings"
android:title="#string/text_skip"
app:showAsAction="never" />
</menu>
MainActivity.java
#Override
boolean onCreateOptionsMenu(Menu menu) {
inflater = getMenuInflater();
inflater.inflate(R.menu.menu_otp_skip, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.menu_item_skip:
Toast.makeText(this, "Skip selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
They are called menu items or action buttons in toolbar/actionbar. Here you have Google tutorial how it works and how to add them
https://developer.android.com/training/basics/actionbar/adding-buttons.html
You can actually put anything inside a toolbar. See the below code.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
Between the above toolbar tag you can put almost anything. That is the benefit of using a Toolbar.
Source: Android Toolbar Example
You could use actionLayout from the support library.
menu.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/button_item"
android:title=""
app:actionLayout="#layout/button_layout"
app:showAsAction="always"
/>
</menu>
button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Activity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.button_item);
Button btn = item.getActionView().findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Toolbar Button Clicked!", Toast.LENGTH_SHORT).show();
}
});
return true;
}
I was able to achieve that by wrapping Button with ConstraintLayout:
<androidx.coordinatorlayout.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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/top_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/white_color">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:layout_marginStart="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_publish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/publish"
android:background="#drawable/button_publish_rounded"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="10dp"
app:layout_constraintLeft_toRightOf="#id/cancel"
tools:layout_editor_absoluteY="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
You may create a drawable resourcebutton_publish_rounded, define the button properties and assign this file to button's android:background property:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/green" />
<corners android:radius="100dp" />
</shape>

What's wrong with this XML file? (keeps crashing aapt.exe)

I've had problems with my app and eclipse so I saved my code, deleting eclipse, and re-downloaded and extracted. Immediately after I added the XML file to res/menu, I got a windows message saying aapt.exe has stopped working; I'd been getting this message constantly before. I've researched it before so I know that if an XML file isn't written correctly, aapt.exe will keep crashing. Only thing is, I don't see the problem with the file.
createlgmenu.xml (used as a popup menu for a button event):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/Create_List"
android:title="#string/Create_List"/>
<item
android:id="#+id/Create_Food_Group"
android:title="#string/Create_Food_Group"/>
</menu>
|
|
Other files:
I have practically no code in my mainactivity.java:
package com.example.groceryrunner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void onCreateLGClick(View v) {
final int id = v.getId();
switch (id) {
case R.id.CreateLG:
//findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGPopup(v);
break;
/*case R.id.ListsButton:
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGMenu(v);
break;*/
}
}
public void createLGPopup(View v) {
PopupMenu LGMenu = new PopupMenu(this, v);
LGMenu.getMenuInflater().inflate(R.menu.createlgmenu, LGMenu.getMenu());
LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
String choice = new String((String) item.getTitle());
if (choice == "Create_List") {
//createListDialog();
}
else if (choice == "Create_Group") {
//createListDialog();
}
return false;
}
});
LGMenu.show();
}
}
Only one button so far in my activity_main.xml:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/CreateLG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="37dp"
android:layout_marginTop="21dp"
android:text="+"
android:textSize="40sp" />
</RelativeLayout>
In case you want to see my menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
If you are using the support library and a library project, then you need a custom namespace for the "showAsAction" tag for older platform versions.
So change this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
To this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
custom:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
Note that "custom" can be anything you want.
I think I've found my solution.
I had copied the xml and changed the items originally myself. So I deleted it and created it through Eclipse (right click on the res/menu folder > New > Other > Android XML File > Menu). I no longer seem to be getting the appt.exe error. I then added the items using Eclipse instead of writing or copying them myself.
The only difference in the two files is that the old one ended each item with this:
\>
And the one created by Eclipse uses this at the end of an item declaration:
></item>
As for Eclipse not recognizing my xml files, AdamM helped me find the solution to that problem as well:
1) I deleted the R import
2) Commented out the lines that were giving me errors in recognizing my xml files
3) Restarted Eclipse & Built All

Categories

Resources