I have made an Android app containing a CoordinatorLayout with a AppBar and a NestedScrollView. In my NestedScrollView I have a list of items, and a button which scroll to an item in my NestedScrollView.
When clicking the button, the Android app scroll down to the item, but doesn't show the whole item, only partially: http://i.stack.imgur.com/8kuq0.png
I expected something like the following: http://i.stack.imgur.com/k0A5N.png
It seems like the amount the app needs to scroll is about the same size as the AppBar, in order to show the whole view. If I remove the scroll flag in my layout file below, I get the expected behavior.
What do I do wrong?
Update (12/01/2016): I've updated the pictures, so they are bigger. Furthermore as I wrote in Herry's response, I'm in doubt if View.requestRectangleOnScreen() is the right method to call, or if I should use a different layout then NestedScrollView.
My activity is coded as follows:
package com.example.sij.coordinatorlayoutbug;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView itemToNavigateTo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
itemToNavigateTo = (TextView)findViewById(R.id.itemToNavigateTo);
Button navigatorButton = (Button)findViewById(R.id.navigator_button);
navigatorButton.setOnClickListener(navigateToItemListener());
}
View.OnClickListener navigateToItemListener() {
return new View.OnClickListener() {
#Override
public void onClick(final View v) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
itemToNavigateTo.requestRectangleOnScreen(
new Rect(
0,
0,
itemToNavigateTo.getWidth(),
itemToNavigateTo.getHeight()));
}
});
}
};
}
}
And the layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
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="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/navigator_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/scroll_to_item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
<!-- Items 2 to 5 omitted for brevity -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item6"/>
<TextView
android:id="#+id/itemToNavigateTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
you need to add android:fillviewport="true" in your
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true">
Related
I have made an bottom app bar app with fab and bottom sheet and I have implemented the state expanded and state hidden from navigation on item selected listener.The question is how to make bottom sheet overlay on bottom app bar the following result I have got is.
This is main activity 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"
android:id="#+id/activity_CoordinatorLayout"
android:layout_width="match_parent"
android:background="#FFB7B7B7"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Widget.MaterialComponents.BottomAppBar.Colored"
android:layout_gravity="bottom"
android:elevation="26dp"
app:navigationIcon="#drawable/ic_menu_black" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:elevation="26dp"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_add"
app:layout_anchor="#id/bottom_app_bar"
app:menu="#menu/menu_demo"
/>
<FrameLayout
android:id="#+id/bottom_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="16dp"
android:visibility="visible"
app:behavior_hideable="true"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/menu_demo" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Main activity class
package com.bab.BottomAppBar;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import android.view.View;
import com.google.android.material.navigation.NavigationView;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
BottomAppBar bab;
BottomSheetBehavior<View> bsb;
CoordinatorLayout cl;
NavigationView nv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nv=findViewById(R.id.navigation_view);
cl=findViewById(R.id.activity_CoordinatorLayout);
View bottomDrawer=cl.findViewById(R.id.bottom_drawer);
bab=findViewById(R.id.bottom_app_bar);
bsb=BottomSheetBehavior.from(bottomDrawer);
bsb.setState(BottomSheetBehavior.STATE_HIDDEN);
bab.setNavigationIcon(R.drawable.ic_menu_black);
bab.replaceMenu(R.menu.menu_demo);
bab.setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View p1) {
bsb.setState(BottomSheetBehavior.STATE_HALF_EXPANDED);
}
});
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(MenuItem p1) {
return false;
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
bsb.setState(BottomSheetBehavior.STATE_HIDDEN);
}
}
Result I got:
This is the result
Result I wanted:
See in this the frame layout as bottom sheet is overlaying on bottom app bar
Someone please help me
You need to set BottomSheet's elevation more than BottomAppBar's.
Elevation means how your views lay above each other
I'm pretty new to coding so I might not express myself correctly, please bear with me.
I have a fragment inflated inside a layout that includes a toolbar. When I open the fragment, I want to add a button (not a menu, just a button to navigate to another fragment).
My issue is I don't know how to instantiate the button from my Fragment class to add an icon and an onClickListener.
Here's my xml files:
The layout that contains my fragment:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="be.ac.ulg.mobulis.Activities.MainActivity"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/blue"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<Button
android:id="#+id/toolbar_button"
android:layout_width="#dimen/margin"
android:layout_height="#dimen/margin"
android:layout_gravity="right"
android:layout_marginRight="#dimen/innerLargeMargin"
/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</LinearLayout>
The layout in which the fragment is inflated:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="be.ac.ulg.mobulis.Activities.MainActivity"
tools:showIn="#layout/app_bar_main"
android:background="#color/orange">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#f1f1f1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
design:menu="#menu/bottom_nav_items"
android:background="#color/orange"
android:backgroundTint="#color/white"
design:itemIconTint="#color/white"
design:itemTextColor="#color/white"
design:itemBackground="#drawable/bottom_bar_tint_selector"
/>
</LinearLayout>
So what I want to do is something like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
toolBarButton = (???).findViewById(R.id.toolbar_button)
}
activity_main.xml to add Button inside android.support.v7.widget.Toolbar
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FFA000">
<Button
android:id="#+id/toolbarbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="right"/>
</android.support.v7.widget.Toolbar>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button toolBarBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Toolbar with button example");
toolbar.setSubtitle("Toolbar subtitle");
toolbar.setLogo(android.R.drawable.ic_menu_info_details);
toolBarBtn = (Button)findViewById(R.id.toolbarbtn);
toolBarBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),
"Button in ToolBar clicked",
Toast.LENGTH_LONG).show();
}
});
}
}
Set This onClick Listener in your parent activity of Fragment.
try this to access your button of toolbar from activity
private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
Button toolbar_button = (Button) toolbar.findViewById(R.id.toolbar_button);
setSupportActionBar(toolbar);
toolbar_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ChatActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
you have to write like below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
toolBarButton = (Button) view.findViewById(R.id.toolbar_button);
where view is your view which you bind at your onCreateView in your Fragment.
What I do is to bind the View inside the activity and then create an interface who works with that View.
public class Activity implements ButtonListener{
//findViewbyId or Butterknife
void doSomething(){
toolBarButton.something()
}
Interface
}
public interface ButtonListener{
void doSomething();
}
Fragment
public class Fragment extends Fragment{
public ButtonListener mButton;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
mButton = (ButtonListener)getActivity();
super.onCreate(savedInstanceState);
}
}
You can access button directly without toolbar using (Button) findViewById(R.id.toolbar_button).
Toolbar toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
Button toolbar_button = (Button) findViewById(R.id.toolbar_button);
setSupportActionBar(toolbar);
I've created a Scrolling Activity using the Android Studio template, this created an activity.xml and also a content.xml which is included on the activity.xml who has a NestedScrollView into which I've added a ListView feed from an adapter, at first it only showed one item so I programmatically made the size of it to a fixed value so that it actually shows all the items, I was able to do that without a problem and the NestedScrollView wraps the ListView with no problem, however there is a blank space below the NestedScrollView that I realize was from the CoordinatorLayout by adding background colors to all elements, the CoordinatorLayout is the main tag in the activity.xml but I am not able to figure out how to remove this blank space since if I use wrap_content on the CoordinatorLayout it will actually trim my content to half the screen for some reason.
The other things that are worth to mention are that this blanks space is not showing if I rotate my phone horizontally, is only coming up on a vertical position. Also,
Does anyone know what might be causing this? Is not a big deal and I might even end up leaving it like this but it's just annoying to me I'm not able to figure out what's going on.
This is my activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
android:fitsSystemWindows="true"
android:background="#00ff00" //Green
tools:context=".VenueActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/frog"
android:scaleType="centerCrop"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_venue"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_favorite_white_24dp"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
This is my content.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".VenueActivity"
android:background="#ff0000" //Blue
tools:showIn="#layout/activity_venue">
<LinearLayout
android:id="#+id/venueList"
android:background="#ffff00" //Yellow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:layout_width="match_parent"
android:layout_height="55dp"
android:id="#+id/searchView"
android:queryHint="Search venues">
</SearchView>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/songList" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Finally my java file:
package com.example.android.myApplication;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class VenueActivity extends AppCompatActivity {
//Set up my own database manually
String[] db_ids = {"12345", "12345", "12345", "12345", "12345", "12345"};
String[] db_songs = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"};
int[] db_thumbs = {R.drawable.ic_place_white_24dp, R.drawable.ic_place_white_24dp,
R.drawable.ic_place_white_24dp, R.drawable.ic_place_white_24dp,
R.drawable.ic_place_white_24dp, R.drawable.ic_place_white_24dp};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_venue);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Toast.makeText(VenueActivity.this, getIntent().getStringExtra("venue") + " was choosen", Toast.LENGTH_SHORT).show();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Venue has been added to your favourites", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
/**
* LISTVIEW TO DISPLAY DIFFRENT VENUES
*/
ListView lv = (ListView) findViewById(R.id.songList);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView selectedSong = (TextView) view.findViewById(R.id.songName);
// TODO Third Fragment must be changed to the Venue Home or something different than this
Toast.makeText(VenueActivity.this, selectedSong.getText(), Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(view.getContext(),VenueActivity.class);
// intent.putExtra("venue", selectedVenue.getText());
// startActivity(intent);
}
});
SongArrayAdapter ad = new SongArrayAdapter(db_ids, db_songs, db_thumbs, getApplicationContext());
lv.setAdapter(ad);
ListViewInScrollView.adapt(lv);
}
}
I'm trying to add a dialog to my android app which is full screen on small devices (e.g. mobile phone) but a standard dialog on large devices (e.g. tablets). This follows the logic laid out in the material design specification.
Using the official android dialog guide, using a DialogFragment, I end up with a transparent dialog that overlays the action bar:
Below is the source code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Use ThemeOverlay to make the toolbar text white -->
<android.support.design.widget.AppBarLayout
android:id="#+id/abl_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="10dp"
android:src="#drawable/ic_add"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
package com.example.fsdialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class MainActivity extends AppCompatActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup AppBar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
// FAB
FloatingActionButton fab = (FloatingActionButton) findViewById(
R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
MyDialogFragment dialog = new MyDialogFragment();
if (false) {
// The device is using a large layout, so show the fragment
// as a dialog
dialog.show(fm, "MyDialogFragment");
} else {
// The device is smaller, so show the fragment fullscreen
FragmentTransaction tx = fm.beginTransaction();
tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// To make it fullscreen, use the 'content' root view as
// the container for the fragment, which is always the root
// view for the activity
tx.add(R.id.content, dialog)
.addToBackStack(null).commit();
}
}
});
}
}
my_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="2"
android:columnCount="2">
<TextView
android:id="#+id/text_date1"
android:layout_row="0"
android:layout_column="0"
android:text="17/06/2015"
style="#android:style/Widget.Holo.Spinner"/>
<TextView
android:id="#+id/text_time1"
android:layout_row="0"
android:layout_column="1"
android:text="09:35"
style="#android:style/Widget.Holo.Spinner"/>
<TextView
android:id="#+id/text_date2"
android:layout_row="1"
android:layout_column="0"
android:text="17/06/2015"
style="#android:style/Widget.Holo.Spinner"/>
<TextView
android:id="#+id/text_time2"
android:layout_row="1"
android:layout_column="1"
android:text="11:00"
style="#android:style/Widget.Holo.Spinner"/>
</GridLayout>
MyDialogFragment.java
package com.example.fsdialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.view.Window;
public class MyDialogFragment
extends DialogFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_dialog, container, false);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fsdialog"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<activity android:name="MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
UPDATE 1 (24-06-2015)
I'm now using the android Theme.AppCompat.NoActionBar theme and have a FrameLayout in the main activity. I still get the same problem (image updated).
Is it wrong for me to assume that the built in theme will contain a background colour for the dialog? You know, a sane default? I know for sure that if I just show a dialog the normal way it gets a background colour.
I assume the CoordinatorLayout I added causes the dialog to overlay the action bar even though I'm using an embedded FrameLayout. Why is that? I need the CoordinatorLayout so the FloatingActionButton is in the correct position.
#cutoff was correct. My update failed because the root layout is a CoordinatorLayout instead of a LinearLayout or something equally good. The example below uses a DrawerLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Use ThemeOverlay to make the toolbar and tablayout text
white -->
<android.support.design.widget.AppBarLayout
android:id="#+id/abl_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_view"/>
</android.support.v4.widget.DrawerLayout>
More details about this solution can be found in this question, including why a DialogFragment is insufficient for anything other than completely full-screen.
here is the code, please can anyone simply tell me how to make the onClick event run?
xml file of the activity, which has a navigation-drawer fragment, An Image view, A TextView, An ImageButton
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/drawer_layout"
android:adjustViewBounds="true"
android:contentDescription="#string/ContentDescriptionProfileImage"
android:src="#drawable/profile_pic" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:background="#FFFFFF"
android:orientation="horizontal"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:padding="3dp"
android:text="#string/profileName"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/profileSettings"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#null"
android:contentDescription="#string/settingsIconDescription"
android:padding="8dp"
android:onClick="onSettingsIconPressed"
android:src="#drawable/settings32" />
</RelativeLayout>
</LinearLayout>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/app_bar">
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="studio25.materialdesign.NavigationDrawerFragment"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer"></fragment>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
The image of the Screen Looks like this
here is the activity class
package studio25.materialdesign;
import android.annotation.TargetApi;
import android.os.Build;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.Toast;
public class ProfileView extends ActionBarActivity {
private Toolbar toolbar;
String title = "";
ImageButton setttingsButton;
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_view);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
this.getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
setttingsButton= (ImageButton) findViewById(R.id.profileSettings);
/*
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.primaryColor));
*/
}
public void onSettingsIconPressed(View v)
{
/*
PopupMenu popupMenu=new PopupMenu(this,v);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.profile_settings_popup_menu,popupMenu.getMenu());
popupMenu.show();
*/
Toast.makeText(this,"Event Click Occured",Toast.LENGTH_SHORT).show();
}
}
I basically wanted a menu popup on buttonclick, but on debugging realized that my button click is not itself working.
Some tutotials say for popUp menu , minSdkVersion should be 11, even changed that.
Nothing is helping me.
Is my xml erroneous or what?
I usually like to hook up my callbacks in code (just my personal preference).
setttingsButton= (ImageButton) findViewById(R.id.profileSettings);
setttingsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(this,"clicked!",Toast.LENGTH_SHORT).show();
}
});
With this you can remove the onClick attribute in the xml layout.
If you want a popup window then PopupWindow is a great option.
popupWindow = new PopupWindow(some_view_to_show, ViewGroup.LayoutParams.MATCH_PARENT, (int)popupHeight, false);
...
popupWindow.showAtLocation(view_to_inject_into, Gravity.NO_GRAVITY, 0, (size.y) - (int) popupHeight);
then you call popupWindow.dismiss(); to remove the popup
Your issue could also be that since you are using a RelativeLayout each element gets "stacked" on top of each other so you have essentially "buried" your ImageButton that you want to click on. So in other words some other view is blocking or consuming your clicks.
Try moving your ImageButton to be one of the last elements that you add to the xml file so that it is "on top" of everything else so that it can be clicked.
Alternatively you can try making the containing LinearLayout clickable instead of the ImageButton. So you would move the android:onClick="onSettingsIconPressed" attribute to the LinearLayout that contains the ImageButton.