As you can see on the image below, I am having a hard time to figure out on how to deselect the first menu item since it is active by default on the BottomNavigationView.
I already tried everything but still the first menu is still raised.
Java Code.
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomNavigation);
bottomNavigationView.setBackground(null);
bottomNavigationView.getMenu().setGroupCheckable(0, false, true);
bottomNavigationView.setElevation(0);
//bottomNavigationView.setSelectedItemId(R.id.menu2);
}
}
Main XML
<?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=".MainActivity">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/main_bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="10dp"
app:fabCradleVerticalOffset="10dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
app:menu="#menu/main_bottom_menu"
/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:backgroundTint="#color/_Surface"
app:tint="#color/_OnSurface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/dashboard_24"
app:layout_anchor="#+id/main_bottom_appbar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
If you have any ideas on how to normalize the menu items on the BottomNavigationView, I will really appreciate the help.
Thanks.
Try to add this in your bottomNavigationView in main.xml
app:labelVisibilityMode="unlabeled"
So your view will be like this:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
app:menu="#menu/main_bottom_menu"
app:labelVisibilityMode="unlabeled"
/>
You can see more details about what changes you are allowed to make on view here:
https://material.io/components/bottom-navigation/android#using-bottom-navigation
public static void setCheckable(BottomNavigationView view, boolean checkable) {
final Menu menu = view.getMenu();
for(int i = 0; i < menu.size(); i++) { // Your Menu 0
menu.getItem(i).setCheckable(checkable);
}
}
Since the related menu is 0, you can arrange the code according to 0.
Related
The following code is trivial but simulates the problem.
activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/btnGoToExtra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="266dp"
android:text="Go To Extra"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout_extra.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnBackToMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="186dp"
android:text="Back To Main"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnGoToExtra)
.setOnClickListener(v -> {
setContentView(R.layout.layout_extra);
findViewById(R.id.btnBackToMain)
.setOnClickListener(vv -> {
setContentView(R.layout.activity_main);
});
});
}
}
Question
I can navigate to the layout_extra from activity_main and return to activity_main. Unfortunately, after this round trip, I can no longer be able to navigate to layout_extra anymore.
What causes this problem and how to fix it?
Explanation will follow in chat but that is not how it is supposed to be done, For views to change, You have other options like using Fragments or using new activity entirely, I would also like to suggest moving to kotlin.
public class MainActivity extends AppCompatActivity {
View mainView;
View extraView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainView = getLayoutInflater().inflate(R.layout.activity_main, null);
extraView = getLayoutInflater().inflate(R.layout.layout_extra, null);
setContentView(mainView);
mainView.findViewById(R.id.btnGoToExtra)
.setOnClickListener(v -> {
setContentView(extraView);
});
extraView.findViewById(R.id.btnBackToMain)
.setOnClickListener(vv -> {
setContentView(mainView);
});
}
}
Reading through the snippet above, here's how everything went
The line
findViewById(R.id.btnGoToExtra)
.setOnClickListener
Registers Button with id btnGoToExtra for click events and when that happens you have the nested
findViewById(R.id.btnBackToMain)
.setOnClickListener
Which registers the element with id backToMain for click events and that's all
Calling setContentView() multiple times is not recommended. The normal way to switch between layouts in the same activity is to use a ViewFlipper or FrameLayout
From here
You then have:
XML
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="#drawable/icon"
android:scaleType="fitCenter"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<TextView
android:text="Learn-Android.com"
android:textSize="24sp"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>
code
private void SwitchLayout2() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}
private void SwitchLayout1() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}
This question already has answers here:
How to set Navigation Drawer to be opened from right to left
(15 answers)
Closed 5 years ago.
I'm new on this site.
I'm now working on application in android studio, I'm looking for a nice menu and found the Navigation Drawer, I searched how can I create my own Navigation but I want it to be on the right side of the screen.
How can I do it?
Same Question about checkbox, I have the Checkbox on the left side and the text on the right side/ I want it to be the opposite.
Hope you understand my problem.
XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello Drawer"
android:layout_gravity="center"
android:gravity="center"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="right">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Java:
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(mToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
About the right side navigation drawer...I remember struggling with this question. I checked many answers from anywhere. Mostly they say: use setLayoutDirection method which works fine but for api>=17. I do the following:
0. Support Libraries
dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:design:25.3.1'
}
1. Layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:openDrawer="end">
<!--content of your Activity. Could be fragment container. Your toolbar is placed there-->
<include
layout="#layout/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right|end">
<LinearLayout
android:id="#+id/navigationLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--navigation header. like user profile image &...-->
<include
layout="#layout/drawer_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--recycler of your navigation items-->
<android.support.v7.widget.RecyclerView
android:id="#+id/navigationRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
main_content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/toolbarRL"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/toolbarHamburger"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#null"
android:src="#drawable/hamburger" />
</RelativeLayout>
... other stuff ...
</RelativeLayout>
2. Activity
//Hamburger icon of your toolbar
ImageButton hamburger= (ImageButton) findViewById(R.id.toolbarHamburger);
hamburger.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
if (drawerLayout.isDrawerOpen(Gravity.RIGHT))
drawerLayout.closeDrawer(Gravity.RIGHT);
else
drawerLayout.openDrawer(Gravity.RIGHT);
}
});
Also you should close drawer using drawerLayout.closeDrawer(Gravity.RIGHT) when a recycler's item is clicked.
About CheckBox I don't know if there's any solution to switch positions of text and CB. But I wouldn't bother looking for it. I'd rather using a linearLayout containing CB & TV :)
Or maybe if I see it repetitive I create a custom view.
CB: CheckBox, TV: TextView
I have the following list item that is used within a RecyclerView that has a vertical LinearLayout Manager. I am using espresso, and I would like to simulate clicking the id/action_save menu item. Below is my latest attempts at figuring this one out. I have tried using onData method combined with onChildView, but a MenuItem is not a View. The onView and withId methods works well if the menu item is located in a toolbar that is in the action bar, but while being embedded inside of a RecyclerView.
I have tried recording using the espresso recording feature in android studio, however, it inject code similar to this Espresso click menu item. When running the test case again causes the test to raise an exception.
List Item
<android.support.v7.widget.CardView 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"
app:cardCornerRadius="0dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/MyDarkToolbarStyle"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ProgressBar
android:id="#+id/progress"
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true" />
<android.support.v7.widget.AppCompatImageView
android:id="#+id/imgPalette"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="250dp" />
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="-2dp"
android:elevation="4dp"
android:outlineProvider="bounds"
android:paddingTop="2dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Menu
<?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/action_save"
android:title="#string/save"
app:showAsAction="never" />
</menu>
Main Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/banner" />
...
</RelativeLayout>
Java (Espresso / JUnit Test Case)
...
// mProducts is a list that is backed by the adapter and is of Type Product
for (int i = 0; i < mProducts.size(); i++) {
onView(allOf(withId(R.id.recyclerView), withParent(withId(R.id.container))))
.perform(actionOnItemAtPosition(i, clickOverflow(R.id.toolbar)));
// Add delay for system to respond to overflow being opened
SystemClock.sleep(1000);
onView(withId(R.id.recyclerView)).perform(actionOnItemAtPosition(i, clickMenuItem(R.id.toolbar, R.id.action_save)));
}
...
public static ViewAction clickMenuItem(final int toolbarId, final int id) {
return new ViewAction() {
#Override
public Matcher<View> getConstraints() {
return null;
}
#Override
public String getDescription() {
return "Click on a child view with specified id.";
}
#Override
public void perform(UiController uiController, View view) {
Toolbar toolbar = (Toolbar) view.findViewById(toolbarId);
for (int i = 0; i < toolbar.getChildCount(); i++) {
if (toolbar.getChildAt(i) instanceof ActionMenuView) {
ViewGroup viewGroup = ((ViewGroup) toolbar.getChildAt(i));
for (int j = 0; j < viewGroup.getChildCount(); j++) {
if (viewGroup.getChildAt(j).getId() == id) {
viewGroup.getChildAt(j).performClick();
break;
}
}
}
}
}
};
}
public static ViewAction clickOverflow(final int toolbarId) {
return new ViewAction() {
#Override
public Matcher<View> getConstraints() {
return null;
}
#Override
public String getDescription() {
return "Click on a child view with specified id.";
}
#Override
public void perform(UiController uiController, View view) {
Toolbar toolbar = (Toolbar) view.findViewById(toolbarId);
((ViewGroup) toolbar.getChildAt(0)).getChildAt(0).performClick();
}
};
}
Error
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.variable.inspire:id/recyclerView
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.support.v7.widget.MenuPopupWindow$MenuDropDownListView{92d26ac VFED.VC.. .F...... 0,0-515,252}
Update #1
This does not solve my question, but provides a workaround by displaying the menu item directly and not inside the overflow menu. My question still is using espresso can you open the overflow menu and click an item in it.
onView(allOf(withId(R.id.recyclerView), withParent(withId(R.id.container)))).perform(actionOnItemAtPosition(i, clickMenuItem(R.id.toolbar, R.id.action_save)));
I'm trying to create my own custom app bar with a center-aligned title. Android Studio's editor shows my title is properly centered, but it is not on my actual device.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.encounter_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.encounter_menu_bar_layout);
}
encounter_menu_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
android:layout_gravity="center" />
</LinearLayout>
encounter_menu_bar.xml doesn't have anything in it:
<?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">
</menu>
EDIT
A lot of you are recommending that I change android:layout_gravity to android:gravity. I've tried that as well and I'm still not getting it to line up properly.
I'm enabling the app bar to show up on a specific fragment, as shown here:
EncounterFragment.java
public class EncounterFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_encounter, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.encounter_menu_bar, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO: This
return super.onOptionsItemSelected(item);
}
}
I also forgot to mention that I'm attaching the menu in a fragment. This is its layout:
fragment_encounter.xml
<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/encounter_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</LinearLayout>
Try adding android:gravity="center".
This happens because you have to center the whole text and not just the layout
Use android:layout_gravity="center"
Example:
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
NOTE: layout_gravity, not gravity
In your case:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
android:layout_gravity="center" />
</LinearLayout>
Change the textview to this
make it match parent for width, And make gravity center to center the text
<TextView
android:id="#+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
android:gravity="center" />
You want to change your xml view to this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
#this should be horizontal, not vertical.
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/encounter_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
/>
To start, you need to make your linear layout horizontal, not vertical. By making it vertical, you are aligning it to the left.
Then, you need to make sure that your textview width and height match the parent, rather than just wrap the content. Then you can use the gravity attribute to make sure that the text is centered. When you used gravity before, the textview simply wrapped the content, and thus it didn't center in the textview. By matching the parent view bounds, you will be able to then center the text as you need to.
This is how your Toolbar XML should be
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:textColor="#android:color/white"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_gravity="center"
/>
</android.support.v7.widget.Toolbar>
Then in your Activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
So my layout is still visible, maybe someone sees mistake? I want to dispear it when infoButton(imagebutton) is clicked.
FrameLayout infolayout;
infolayout = (FrameLayout) findViewById(R.id.infoLayout);
public void infoPressed(View v){//info button is pressed by user
//infoLayout.setVisibility(View.GONE);
infolayout.setVisibility(View.INVISIBLE);
}
<FrameLayout
android:layout_width="193dp"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/serviceLayout"
android:id="#+id/infoLayout">
I wacthed: How to change visibility of layout programaticly
EDITED:
wrong code part, still not working for me
*Try this**
<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="com.example.demo.MainActivity" >
<FrameLayout
android:id="#+id/serviceLayout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#000000" />
<Button
android:id="#+id/btnHide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="infoPressed"
android:text="Hide" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity
{
FrameLayout infolayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infolayout = (FrameLayout) findViewById(R.id.serviceLayout);
}
public void infoPressed(View v){ //info button is pressed by user
//infoLayout.setVisibility(View.GONE);
infolayout.setVisibility(View.INVISIBLE);
}
}
if your ID declaration is correct I guess you have to change the line
infolayout.setVisibility(View.INVISIBLE);
to
infolayout.setVisibility(FrameLayout.INVISIBLE);