v7 widget toolbar not showing, why? - android

I am so close. I have the android studio project set up correctly with libs, the project compiles, and the emulators and device run the app. I am trying to get rid of the bottom button popup menu of older devices and replace with v7 appcompat toolbar. The old action bar is gone but the menu inflate still inflates to the old popup menu.
I start off using a splashscreen then the app main shows the database view activity. This means I have to dynamically load the toolbar.
My thinking now is the only place I have left to work out is the styles/themes or the order of setcontent(this) to display the main app/database view screen.
Also I dont have the correct emulator for the HTC evo device so the device buttons dont show. I believe this is why alot of developers post that the toolbar doesnt show. When I install apk on real device i can the use the menu button. This is how I am able to see that the menu is not in the toolbar. The emulator shows no buttons plus no toolbar so a developer on an emulator of newer devices would never see this.
I hope this enough to go on for any one to catch my pitfall here.
Thanks in advance.
Here are codes snippets:
styles.xml:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<!--<style name="AppTheme" parent="AppBaseTheme">-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">true</item>
<item name="android:textColor">#000000</item>
</style>
</resources>
Toolbarmenu.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"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="ifRoom"
android:title="#string/app_name"/>
<item
android:id="#+id/reset"
android:icon="#android:drawable/ic_menu_revert"
app:showAsAction="ifRoom"
android:title="#string/action_settings"/>
<item
android:id="#+id/about"
android:icon="#android:drawable/ic_dialog_info"
app:showAsAction="ifRoom"
android:title="#string/hello_world">
</item>
<item
android:id="#+id/exit"
app:showAsAction="ifRoom"
android:title="#string/Menu">
</item>
</menu>
Manifest.xlm:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.loadrunner"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.loadrunner.SplashScreenActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.loadrunner.MainActivity"
android:exported="true"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
lrtoolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="#2196f3"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:minHeight="200dp">
</android.support.v7.widget.Toolbar>
</LinearLayout>
Layout_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=".SplashScreenActivity"
android:visibility="visible"
android:background="#color/black">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageViewSplashLogo"
android:src="#drawable/shoppingcartloading"
android:visibility="visible"
android:contentDescription="#string/app_name"
android:scaleType="fitCenter"
android:cropToPadding="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/black" />
</RelativeLayout>
And finally the code:
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
import com.Table.TableMainLayout;
public class MainActivity extends AppCompatActivity {
final String TAG = "MainActivity.java";
public static DatabaseHandler db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Log.d("SplashMainActivityonCre", "made it");
db = new DatabaseHandler(this);
db.getWritableDatabase();
db.insertFast(100);
int dbreccnt = db.countRecords();
//Log.d("SplashAfterDBCreate", "made it");
//Log.d("AppLoadrunner ", "Loadrunner record count " + dbreccnt);
Toolbar LrToolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(LrToolBar);
//getSupportActionBar().setDisplayShowHomeEnabled(true);
//LrToolBar.setNavigationContentDescription(getResources().getString(R.string.app_name));
setContentView(new TableMainLayout(this));
TableMainLayout start:
package com.Table;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
//import android.database.Cursor;
import com.loadrunner.DatabaseHandler;
import com.loadrunner.R;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class TableMainLayout extends RelativeLayout {
public final String TAG = "TableMainLayout.java";

I have maybe the answer : you just get your toolbar before setContentView() method...
Just try this :
setContentView(new TableMainLayout(this));
Toolbar LrToolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(LrToolBar);
//getSupportActionBar().setDisplayShowHomeEnabled(true);
//LrToolBar.setNavigationContentDescription(getResources().getString(R.string.app_name));
Hope it will help you

Make your Toolbar layout like thie:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="#2196f3"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:minHeight="200dp" />
and in your Activity code replace the Toolbar LrToolBar = (Toolbar) findViewById(R.id.toolbar); with the:
Toolbar toolbar = LayoutInflater.from(this).inflate(R.layout.toolbar_layout_name, null)
setSupportActionBar(toolbar);
Hope it works.

Try this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBar">false</item> //see this line
<item name="android:windowNoTitle">true</item>
</style>
Try setting <item name="windowActionBar">false</item>
I hope this helps!

Related

How to add a toolbar at the bottom?

I'm trying to have my toolbar at the bottom. I already tried some tips but the toolbar rests at the top.
So: What do I have to change so my toolbar appears at the bottom?
If you need more code - there isn't much more - or other informations, just say so.
My XML file:
<?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"
tools:context=".MapsActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testingmapingmarker23.MapsActivity"
/>
<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/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</LinearLayout>
And the .java:
package com.example.testingmapingmarker23;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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.ListView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import static com.example.testingmapingmarker23.R.id.map;
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(map);
mapFragment.getMapAsync(this);
final Toolbar customToolbar = (Toolbar) findViewById(R.id.toolbar);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
}
Styles.xml:
<resources>
<!-- Base application theme. -->
<!--<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!– Customize your theme here. –>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>-->
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" />
</resources>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testingmapingmarker23">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<permission android:name="android.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value=" API-Key (I removed mine)" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can use Relative layout like this,
<?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">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/toolbar"
tools:context="com.example.testingmapingmarker23.MapsActivity"
/>
<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/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</RelativeLayout>
First of all you need to Add navigationToolBar instead of ToolBar
try this Link
See This
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
app:itemIconTint="#color/nav_item_state_list"
app:itemTextColor="#color/nav_item_state_list"
app:menu="#menu/bottom_navigation_items"/>

Android: Missing overflow menu in Material Design

My overflow menu is not appearing in my action bar/App bar (even running emulator) but it only appears in menu_main.xml. What can I do?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mo.thesis">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
app_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="?actionBarSize"
android:background="#ccc">
</android.support.v7.widget.Toolbar>
MainActivity.java
package com.mo.thesis;
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.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
protected boolean onCreateOptionMenu(Menu menu) {
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);
}
}
activity_main.xml
<?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"
tools:context="com.mo.thesis.MainActivity">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="78dp"
android:layout_marginTop="119dp" />
</RelativeLayout>
activity_main.xml Screen Preview:
menu_main.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/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
menu_main.xml Screen Preview:
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
</style>
</resources>
Hope this helps, From your code did not found any issue.
There might be other issue ---
The on-screen overflow menu button only appears in the action bar on devices that lack an off-screen MENU button. Try pressing the MENU button on your device or emulator.
please read this Menu Button - Missing answer.
Your menu is set to never show
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
Change last line there to "always"

How to hide Title Android

i am trying to make a mini-game program and i cant get the title bar to go away i have already tried the following getActionBar().hide(); , requestWindowFeature(Window.FEATURE_NO_TITLE); here is what i have so far.
package com.example.marcus.game;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
XML
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
tools:context="com.example.marcus.game.MainActivity"
android:background="#752fdf">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Add this style to your styles.xml:
<style name="AppTheme.Fullscreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
You can also add :
<item name="android:windowTranslucentStatus">true</item>
to your v21 style.
Change your androidmanifest.xml to use the style:
<activity
android:name="MainActivity"
android:theme="#style/AppTheme.Fullscreen"/>
see About the Full Screen And No Titlebar from manifest
add
android:theme="#android:style/Theme.NoTitleBar"
to your activity or application in the manifest
and change
"extends AppCompatActivity"
to
"extends Activity"

Android App Compact Libary ToolBar do not show the Activity Title

I have been through all the posts here and the suggested solutions but nothing worked for me.I have already spent 2 days and on the verge of giving up Android. So here I am seeking help from you guys,any help you suggest is highly appreciated. Here is my code ,please let me know if I am missing anything.
build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<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/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:titleTextAppearance="#android:color/black"
android:minHeight="?attr/actionBarSize" />
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<!-- Customize your theme here. -->
</style>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#E64A19</color>
</resources>
activity_maim.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/toolbar"
/>
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/tool_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />``
</RelativeLayout>
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.toolbarexample" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.toolbarexample;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Groups history");
//also tested using
//this.setTitle("My ToolBar");
// toolbar.setTitle("My ToolBar");
}
}
}
You dont need to check if (toolbar != null), it always must be not null.
You dont need to set id to include layout <include layout="#layout/toolbar"/>. And you can calling R.id.toolbar.
You better use vertical orientation LinearLayout instead of RelativeLayout in your activity layout.
You better extends AppCompatActivity

in Android, getting inflation error when trying to use SwitchCompat

I'm trying to use android.support.v7.widget.SwitchCompat to create a switch in minimum SDK of 10 and I'm getting the error android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v7.widget.SwitchCompat In looking for solutions, I've found that it may have something to do with which style is declared? So I've changed the style to Theme.AppCompat but I'm still getting the error that it can't inflate. I'm fairly certain that I've got the correct support library added (I'm using Eclipse Kepler) because there are no errors at import android.support.v7.widget.SwitchCompat;. So here's all the relevant code, can anyone see where the problem is?
Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
....
Style:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
...
</resources>
XML:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="horizontal" >
<android.support.v7.widget.SwitchCompat
android:id="#+id/test1_SW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="25dp"
android:checked="false"
android:text="SwitchCompat"
android:textOff="OFF"
android:textOn="ON" />
</RelativeLayout>
Activity:
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.widget.CompoundButton;
public class SwitchTest extends Activity implements
CompoundButton.OnCheckedChangeListener {
private SwitchCompat test1_SW;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.cutin, R.anim.cutout);
setContentView(R.layout.switch_test);
// --- one
test1_SW = (SwitchCompat) findViewById(R.id.test1_SW);
test1_SW.setSwitchPadding(40);
test1_SW.setOnCheckedChangeListener(this);
// --- END one
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.test1_SW:
Log.i("test1_SW", isChecked + "");
break;
}
}
}
The problem is your Activity is extending android.app.Activity, and not the support Activity you need to use with the AppCompat libraries. Just change:
import android.app.Activity;
to
import android.support.v7.app.ActionBarActivity;
Or, since I think that's been deprecated in API 22:
import android.support.v7.app.AppCompatActivity;
Once you change those it should work.

Categories

Resources