I have the follow error in main code
java.lang.RuntimeException:
Unable to start activity
ComponentInfo{lili14.myapplication/lili14.myapplication.MainActivity}:
java.lang.IllegalStateException: This Activity already has an action
bar supplied by the window decor. Do not request
Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in
your theme to use a Toolbar instead.
I trying add the material design to my xml
this is my main.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
this is the mail.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"
tools:context="lili14.myapplication.MainActivity">
<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="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
This is the 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" />
</resources>
This is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lili14.myapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityTwo"
android:theme="#style/AppTheme"></activity>
<activity android:name=".MainActivity"
android:theme="#style/AppTheme"></activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
<activity android:name="com.startapp.android.publish.list3d.List3DActivity"
android:theme="#android:style/Theme" />
<activity android:name="com.startapp.android.publish.OverlayActivity"
android:theme="#android:style/Theme.Translucent"
android:configChanges="orientation|keyboardHidden|screenSize" />
<activity android:name="com.startapp.android.publish.FullScreenActivity"
android:theme="#android:style/Theme"
android:configChanges="orientation|keyboardHidden|screenSize" />
</application>
</manifest>
Related
style.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" />
</resources>
In my new activities my action bar wont show, im not sure why this is happening. It only appears on the first activity. And it does not appear in any other activities. Im new to android development so this may be anoob question. But its really bothering me. I have included my code for my project. thank you for reading!
<?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"
tools:context="com.karanvir.search.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="59dp"
android:text="AutoCompleteTextView" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/autoCompleteTextView"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="48dp"
android:layout_toEndOf="#+id/progressBar"
android:layout_toRightOf="#+id/progressBar"
android:onClick="jump"
android:text="Button" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<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="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
package com.karanvir.search;
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.webkit.WebView;
import android.webkit.WebViewClient;
import static com.karanvir.search.MainActivity.urlGlobal;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//finding webview
WebView webView=(WebView) findViewById(R.id.web1);
//whole bunch of settings you might want to do if you .
//do this because javascript is so wildely used that if you dont use this anywebsites you display wont be displayed properly
webView.getSettings().setJavaScriptEnabled(true);
//this is because on a number of phones if you dont do this it will jump to the devices default browser, and ddisplay the websview their instead.
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.google.ca/?gws_rd=ssl#q="+urlGlobal);
//reminder ask permission
//you can load content using loaddata
//then add type of data
//then add character encoded were using
}
}
package com.karanvir.search;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Intent intentGoogle;
Random rn;
SharedPreferences urls;
AutoCompleteTextView searchBar;
public static String urlGlobal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchBar=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
Button button=(Button) findViewById(R.id.button);
rn= new Random();
urls=this.getSharedPreferences("com.karanvir.search", Context.MODE_PRIVATE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings4) {
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intentGoogle);
return true;
} else if(id ==R.id.action_settings2){
Intent intentGoogle= new Intent(getApplicationContext(),Yahoo.class);
startActivity(intentGoogle);
return true;
}else if (id==R.id.action_settings3){
Intent intentGoogle= new Intent(getApplicationContext(),MainActivity.class);
startActivity(intentGoogle);
return true;
}else if(id==R.id.action_settings1){
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.alert_dark_frame)
.setTitle("About")
.setMessage("stuff");
return true;
}
return super.onOptionsItemSelected(item);
}
public void jump(View view){
//intnet changing target of our code
urlGlobal=searchBar.getText().toString();
Log.i("stuff",urlGlobal);
//public static String urlGlobal=
/* urls.edit().putString("url",searchBar.getText().toString()).apply();
String Stringurls=urls.getString("url","");*/
int pageJump = rn.nextInt(3)+1;
if (pageJump==1){
//google
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intentGoogle);
} else if (pageJump==2){
//YAHOO
Intent intentGoogle= new Intent(getApplicationContext(),Yahoo.class);
startActivity(intentGoogle);
} else if(pageJump==3){
//GOOGLE
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intentGoogle);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.karanvir.search">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Yahoo"
android:label="#string/title_activity_yahoo"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
<?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"
tools:context="com.karanvir.search.Main2Activity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/web1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<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="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
try above in style.xml
<resources>
<!-- Base application theme. -->
<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>
</style>
and update your AndroidManifest file like above
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.karanvir.search">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
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>
<activity
android:name=".Main2Activity"
android:label="#string/title_activity_main2"
/>
<activity
android:name=".Yahoo"
android:label="#string/title_activity_yahoo"
></activity>
</application>
Add this in your styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/grey_400</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
And in your Manifest add this in every Activity
<activity
android:name=".YourActivity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme" />
Try this with activity theme as AppTheme.NoActionBar, hope this help you.
<?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">
<LinearLayout
android:id="#+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay">
<TextView
style="#style/Base.TextAppearance.Widget.AppCompat.Toolbar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/login_title"
android:textColor="#color/colorPrimary"
/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
When you are using Android Studio and add a new Activity try:
File -> New -> Activity -> Basic Activity
Then it has the ActionBar configurated by default.
I have two classes in two different files, one is MainActivity and the other one is SearchResultsActivity.
For some reason when i press the search button it doesn't start SearchResultsActivity, I'm assuming this because i put several breakpoints (even some at the beginning of the class) and the debugger doesn't seem to reach them.
MainActivity.Java
package myapp.myapp;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
ProgressBar theProgressBar;
TextView stInstTxt;
public void sendMessage(View view) {
Intent intent = new Intent(this, MainActivity.class);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_element).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item){
// Handle item selection
switch (item.getItemId()) {
case R.id.search_element:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//text view
stInstTxt = (TextView) findViewById(R.id.textView01);
//progress bar
theProgressBar = (ProgressBar) findViewById(R.id.progressBar01);
theProgressBar.setVisibility(View.INVISIBLE);
//toolbar
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
}
SearchResultsActivity.Java
package myapp.myapp;
public class SearchResultsActivity extends Activity{
TextView stInstTxt;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
searchItems(query);
}
}
public void searchItems(String query) {
stInstTxt.append(query);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myapp.myapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<activity android:name="myapp.myapp.SearchResultsActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value="myapp.myapp.SearchResultsActivity"/>
</activity>
</application>
</manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#FFF9C4">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:paddingTop="70dp"
android:layout_height="wrap_content">
<TextView android:text="#string/help_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView01"
android:scrollbars="vertical"
android:visibility="visible"/>
</RelativeLayout>
</RelativeLayout>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_element"
android:orderInCategory="200"
android:title="#string/search_hint"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
Could there be something wrong with AndroidManifest.xml?
After almost 6 hours of trying to implement this (starting with the android documentation as a guide), and trying dozens of different ways to do it, i feel i maybe could help someone else with a similar problem save some time, I came up to this tutorial
I read it all but what I ended up doing was downloading the code and comparing it with mine until i found that I was missing a couple of important things. The official android documentation is incomplete, and pretty much there was no way i could have came up with a solution without seeing a real and complete working example.
this is my manifest files:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iadmo.dahan"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="preferExternal"
>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:name="com.iadmo.dahan.application.MyApplication"
android:icon="#drawable/appicon"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:name="com.iadmo.dahan.activity.WelcomeActivity"
android:label="#string/app_name"
android:windowSoftInputMode="adjustPan|stateHidden"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:clearTaskOnLaunch="false"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.RecommendActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.PersonalActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.LoginActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.RegisterActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.DownloadedActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.SortedActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.ClassesActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.SearchedActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.ProductDetailsActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.PerClassActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.ProductCommentActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.ProductSimpleInfoActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.CommentDetailsActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
/>
<activity android:name=".activity.DownloadingActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
/>
<service android:name="com.iadmo.dahan.service.DownloadService"/>
</application>
and this is the xml for layout of productdetailsActivity:
public class ProductDetailsActivity extends TabActivity {
private TabHost mHost;
private String appId, userId;
private ListView per_class_simple_info;
private List<BaseInfo> datas;
private DetailsAdapter adapter;
private TextView txtview1, txtview2;
private Button back;
private TextView title_name;
private Dialog dialog;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_productdetail);
MyApplication.getInstance().addActivity(this);
Bundle bundle = getIntent().getExtras();
appId = bundle.getString("appId");
userId = AppUtil.getString(getApplicationContext(), "userid", "");
initCompanents();
initViews();
initDatas();
Intent intent = new Intent();
intent.setAction("GOODID");
intent.putExtras(bundle);
getApplicationContext().sendBroadcast(intent);
initEvents();
}
#SuppressWarnings("deprecation")
private void initViews() {
back = (Button) findViewById(R.id.back);
title_name = (TextView) findViewById(R.id.title_name);
per_class_simple_info = (ListView) findViewById(R.id.per_class_simple_info);
mHost = this.getTabHost();
mHost.addTab(mHost.newTabSpec("mhost_job").setIndicator(txtview1)
.setContent(new Intent(this, ProductSimpleInfoActivity.class)));
mHost.addTab(mHost.newTabSpec("mhost_company").setIndicator(txtview2)
.setContent(new Intent(this, ProductCommentActivity.class)));
}
private void initCompanents() {
txtview1 = new TextView(this);
txtview1.setText("简介");
txtview1.setTextColor(getResources().getColor(R.color.white));
txtview1.setGravity(Gravity.CENTER);// 文字居中
txtview1.setTextSize(15);// 字体大小
txtview1.setPadding(15, 16, 15, 15);// 四周填充
txtview1.setBackgroundResource(R.drawable.tabhost_bg);
txtview2 = new TextView(this);
txtview2.setText("评论");
txtview2.setTextColor(getResources().getColor(R.color.black));
txtview2.setGravity(Gravity.CENTER);// 居中
txtview2.setTextSize(15);// 字体大小
txtview2.setPadding(15, 16, 15, 15);// 填充
txtview2.setBackgroundResource(R.drawable.tabhost_bg);
LayoutParams param1 = new LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
param1.setMargins(0, 10, 0, 0);
param1.weight = 1;// 权重
txtview1.setLayoutParams(param1);
LayoutParams param2 = new LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
param2.setMargins(0, 10, 0, 0);
param2.weight = 1;// 权重
txtview2.setLayoutParams(param2);
}
and this is the productSimpleInfoActivity's code:
public class ProductSimpleInfoActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simpleinfo);
MyApplication.getInstance().addActivity(this);
initViews();
registerReceivers();
initEvents();
}
this is the productDetail xml layout,i think this is right ,i tired it ,it worked,but today i tired again,it failed:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
</TabWidget>
<View
android:layout_width="fill_parent"
android:layout_height="#dimen/s_5"
android:background="#color/red_light" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Try the following code.It hopefully works.
main.xml file :
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabhost" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"></FrameLayout>
</LinearLayout>
</TabHost>
In Class File add this code in your onCreate():
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Personal.class);
spec = tabHost.newTabSpec("personal").setIndicator("Personal").setContent(intent);
tabHost.addTab(spec);
I have checked and double checked my manifest according to the docs and other questions. I cannot understand why setSearchableInfo returns NullPointerException
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
// current activity is not searchable activity
ComponentName cn = new ComponentName(this, SearchResultsActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
return true;
}
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:label="#string/title_main_activity">
<intent-filter>
<action android:name="com.csgiusa.treatmentmanager.csgitreatmentmanager.RoomEditDetailed" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.csgiusa.treatmentmanager.csgitreatmentmanager.MainActivity" />
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
<activity
android:name=".SearchResultsActivity"
android:label="#string/title_activity_search_results">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/app_name" >
</searchable>
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" />
<item android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
Are you using AppCompat and AppCompatActivity ? If yes, you should use the AppCompat version of SearchView (the one in the support.v7 package) and define it in your menu with app:actionViewClass instead of android:actionViewClass.
Then you retrieve it using:
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
action bar not showing in main activity while the same code present for another activity and another thing action bar overflow not showing by list in right corner
image 1 : main activity with menu key pressed
image 2 : show message activity
code for main activity:
public class MainActivity extends ActionBarActivity {
EditText t;
public final static String sendMessageextra="com.example.example2.textmessage";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(EditText) findViewById(R.id.textmessage);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
public void sendMessage(View v){
Intent sendmessageintent=new Intent(this,showMessage.class);
String messagestring=t.getText().toString();
sendmessageintent.putExtra(sendMessageextra, messagestring);
startActivity(sendmessageintent);
}
}
show message activity:
public class showMessage extends Activity{
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sendmessage);
t=(TextView) findViewById(R.id.showmessage);
showmessage();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return true;
}
public void showmessage(){
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.sendMessageextra);
t.setText(message);
Log.i("Set New Text Box", "Text BOx");
}
}
activity main actions.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search Widget -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
<!-- Location Found -->
<item android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:title="#string/action_location_found"
android:showAsAction="never" />
<!-- Help -->
<item android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help"
android:showAsAction="never"/>
</menu>
android menifest file:
Activity Manifest File:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<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>
<activity
android:name="com.example.example2.showMessage"
android:label="Show Message" android:parentActivityName="com.example.example2.MainActivity"/>
</application>
</manifest>
If you want to show the search icon in the right of the actionbar in every activity then just make a change in the actions.xml instead of android:showAsAction="ifRoom" change it to android:showAsAction="always"
ActionBar not showing
Do this in your oncreate()
ActionBar actionBar = getActionBar();
actionBar.show();
ActionBar overflow
there you can see your menu
To display menu, In your activity main actions.xml,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:showAsAction="always"
android:title="#string/action_search"
android:icon="#android:drawable/ic_action_search"
android:actionViewClass="android.widget.SearchView"/>
</menu>
Like this you need to do changes in main actions file