How to make the top bar? - java

Why when I write this code the application crashing?
Toolbar mToolbar = findViewById(R.id.scroll_top_bar);
setSupportActionBar(mToolbar);
This is the xml code
<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"
tools:context=".DisplayInformations">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:liftOnScroll="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher_background" />
<androidx.appcompat.widget.Toolbar
android:id="#+id/scroll_top_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
</LinearLayout>
This is the java code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_informations);
Toolbar mToolbar = findViewById(R.id.scroll_top_bar);
setSupportActionBar(mToolbar);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_display_informations,menu);
return true;
}
"You may find a mistake in my question because I am new to the stack over flow platform"

Try this one article: https://guides.codepath.com/android/using-the-app-toolbar
And also this one:
https://www.programcreek.com/java-api-examples/?api=androidx.appcompat.widget.Toolbar

Related

SwipeRefreshLayout is Not Working On Real Devices Only Works on Emulator

activity_maian.xml
This Code Working Perfectly On Emulator But not Working on Real Devices And i can't Scroll Down Because of SwipeRefreshLayout Always comes in Middle when i am Scrolling Please Help me with this Problem
Thanks in Advance!!!
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/mains">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/swipe">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
tools:layout_editor_absoluteX="-21dp"
tools:layout_editor_absoluteY="-2dp" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/drawer_view">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
This is MainActivity I gave a normal swipe refresh layout code which is working in emulator but not in real devices..
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
LoadWed();
}
});
LoadWed();
public void LoadWed(){
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
swipe.setRefreshing(true);
webView.loadUrl("https://localhost/admin");
webView.getSettings().setDomStorageEnabled(true);
swipe.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
Added FrameLayout as a first child of DrawerLayout. And added TextView, ProgressBar, SwipeRefreshLayout as a child of that FrameLayout and now it works as expected.
Like This
<?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" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/mains">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/swipe">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
tools:layout_editor_absoluteX="-21dp"
tools:layout_editor_absoluteY="-2dp" />
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/drawer_view"></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

android - menu item not showing in action bar

I'm trying to add menu item "done" in the action bar. I just copied and modified the code from another activity that has menu items showing. So it should work properly.
Here's the activity layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="#color/md_blue_600"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:id="#+id/activity_credits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:mContext="com.getsaveit.saveit.activities.CreditsActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="#string/affiliator_category"/>
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintText="Select category"
android:id="#+id/affiliator_category_spinner"/>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rectangular_shap"
android:id="#+id/affiliators_listview"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
And choose_affiliator_menu.xml file :
<?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:title="#string/done"
android:orderInCategory="1"
android:id="#+id/next_btn"
app:showAsAction="always" />
</menu>
And the java code itself:
public class CreditsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_credits);
Toolbar actionBar = (Toolbar) findViewById(R.id.toolbar);
actionBar.setTitle("Memberships");
//....
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.choose_affiliator_menu, menu);
return true;
}
}
You should use below method after initialization of Toolbar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Center App Bar title on Android

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

Android - fragments don't show

I created one acivity and a few fragments. I try to show one of them after start-up but I see empty screen, without fragments (but with NavigationDrawer)
What do I do wrong?
I have no idea where my mistake is.
Thank you!
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content, new DashboardFragment()).commit();
...
public class DashboardFragment extends Fragment {
...
MainActivity.xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
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="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
Fragment:
<FrameLayout 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.romansytnyk.studentstudio.fragments.DashboardFragment">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/view">
<TextView
android:id="#+id/textView"
android:text="Week info \n\n"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
</FrameLayout>
first you should add fragment :
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add([FRAGMENT],[STRING TAG]);
ft.commit();

Scroll view hidden due to no view found error

I've just implemented a scrollview but then during deployment, I get this error and I don't know why + don't know what's wrong with my code.
No view found for id 0x7f0e006c (com.blah.blah:id/container) for fragment FragmentAbout
fragment_about.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarThumbVertical="#drawable/light_scrollbar">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:contentDescription="#string/app_name"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:src="#drawable/ic_launcher"
/>
<TextView
android:id="#+id/textView1"
android:text="#string/app_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
style="#android:style/TextAppearance.Large"/>
<TextView
android:id="#+id/textView3"
android:text="#string/version_no."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
style="#android:style/TextAppearance.Small"/>
<TextView
android:id="#+id/textView4"
android:text="#string/copyright_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="10dp"
style="#android:style/TextAppearance.Small"/>
<TextView
android:id="#+id/textView5"
android:text="#string/developer_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="10dp"
style="#android:style/TextAppearance.Small"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity_about.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/fragmentabout" >
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/actionBar"
android:elevation="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetEnd="0dp"
app:contentInsetStart="16dp" >
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="#+id/container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
AboutActivity.java
public class AboutActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Toolbar toolbar = (Toolbar) findViewById(R.id.actionBar);
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setBackgroundColor(Color.parseColor("#212121"));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
}
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentAbout())
.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_about, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
final Intent intent = NavUtils.getParentActivityIntent(this);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
NavUtils.navigateUpTo(this, intent);
return true;
}
int id = item.getItemId();
if (id == R.id.action_0) {
new AlertDialog.Builder(this)
.setTitle("Hello World")
.setMessage("Hello World")
.show();
}
if (id == R.id.action_1) {
new AlertDialog.Builder(this)
.setTitle("Hello World")
.setMessage("Hello World")
.show();
}
return super.onOptionsItemSelected(item);
}
}
You're setting your content view to the fragment's layout. That's not what you want. You want to set it to the activity's layout, R.id.activity_about

Categories