Simple Android SearchView not functioning properly - android

I am trying to follow the search interface manual from android.com. I have few problems and I am not sure what I did wrong exactly.
If you want to look, here is the project as zip file.
1- The android:hint is not visible when I touch search icon. It comes empty. Why? If I set it programmatically it works eg. searchView.setQueryHint(getString(R.string.search_hint)); Why it doesn't work from XML?
2- Search field does not get focus and keyboard does not appear automatically when I touch search icon. I need to touch to the search textfield to get focus. I believe it should get focus automatically?
3- When I write something and touch the search icon on the keyboard. I do not see any log entries logged at logcat window. What is missing?
4- (SOLVED) I tried android:iconifiedByDefault="false" in XML and also searchView.setIconifiedByDefault(false); and it is always iconified when I start the application. Why is that happening?
I found out that iI need to remove collapseActionView from app:showAsAction otherwise setIconifiedByDefault(false) does not work. Also if you remove collapseActionView and set setIconifiedByDefault(false) then it never collapses.
I am guessing I missed something somewhere but I am not sure exactly what...
So below are the steps I followed:
I started a new project with 'Basic Activity' in Android Studio 3.
It has a ready toolbar and 'Hello World' in the middle.
Then to xml folder I added the searchable.xml file with contents:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="Search hint text"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
</searchable>
Then changed AndroidManifest.xml file and added an activity:
<activity android:name=".SearchableActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
Then created a simple SearchableActivity.class. Just to log some lines.
package com.test.myapplication;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SearchableActivity extends ListActivity {
private static String TAG = SearchableActivity.class.getSimpleName();
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
Log.d(TAG, "handleIntent");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
}
Then added to existing menu_main.xml the necessary item:
<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="com.test.myapplication.MainActivity">
<item android:id="#+id/search"
android:title="#string/app_name"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
Update: If I change to app:actionViewClass="android.support.v7.widget.SearchView" and also in MainActivity.class if I import import android.support.v7.widget.SearchView; then when I click on the search icon, it shows default Search... text gets focus automatically. But does NOT show hint from XML and still there are no log entries when I submit any information.
UPDATE:
I realized that if I put the intent-filter and the meta part under MainActivity I am getting the search intent inside main activity. But I don't understand why it does not start the SearchableActivity. Because manual page for SearchView clearly says:
When a user executes a search from the search dialog or widget, the system starts your searchable activity and sends it a ACTION_SEARCH intent.

Use this
app:actionViewClass="android.support.v7.widget.SearchView"
Instead of this
app:actionViewClass="android.widget.SearchView"

Apparently all I needed to do was to forward the intent to correct activity from onCreateOptionsMenu()
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
ComponentName componentName = new ComponentName(this, SearchableActivity.class);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
if (searchManager != null)
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));

Related

How to make a FullScreen Activity on Android

Ok so I am trying to replicate the look and feel of the Muzei Live Wallpaper App by Roman Nurik which is open source.
(Check out his GitHub repository here - https://github.com/romannurik/muzei/ )
When the App Launches there is a subtle svg path tracing animation along with a Ken Burns effect that goes on in the background.
You can notice that the activity bleeds into the Status Bar and Navigation Bar.
I've been able to achieve the background animation but haven't been able to figure out how to make the activity full screen like shown in the 2nd GIF below
I need help making this activity fullscreen/ bleed into the status bar and navigation bar.
Here's what I have been able to achieve
This what I want to implement
Here's my code
MainActivity.Java
package devexchanges.info.kenburnview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Toast;
import com.flaviofaria.kenburnsview.KenBurnsView;
import com.flaviofaria.kenburnsview.RandomTransitionGenerator;
import com.flaviofaria.kenburnsview.Transition;
public class MainActivity extends AppCompatActivity {
private KenBurnsView kenBurnsView;
private boolean isPlay = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kenBurnsView = (KenBurnsView) findViewById(R.id.image);
AccelerateDecelerateInterpolator ACCELERATE_DECELERATE = new AccelerateDecelerateInterpolator();
RandomTransitionGenerator generator = new RandomTransitionGenerator(11000, ACCELERATE_DECELERATE);
kenBurnsView.setTransitionGenerator(generator); //set new transition on kenburns view
kenBurnsView.setTransitionListener(onTransittionListener());
}
private KenBurnsView.TransitionListener onTransittionListener() {
return new KenBurnsView.TransitionListener() {
#Override
public void onTransitionStart(Transition transition) {
//Toast.makeText(MainActivity.this, "start", Toast.LENGTH_SHORT).show();
}
#Override
public void onTransitionEnd(Transition transition) {
//Toast.makeText(MainActivity.this, "end", Toast.LENGTH_SHORT).show();
}
};
}
}
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">
<com.flaviofaria.kenburnsview.KenBurnsView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/saigon"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button android:background="#drawable/circle_button"
android:layout_height="#dimen/intro_activate_button_size"
android:layout_width="#dimen/intro_activate_button_size"
android:text="ACTIVATE"
android:textAllCaps="true"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:textSize="18dp"
android:textColor="#333"
android:id="#+id/activate_muzei_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="103dp"
android:elevation="2dp" />
Just add this in your style.xml:
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
Just add this to your onCreate() method:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
To make an activity fullscreen put this in your manifest:
<activity android:name=".ActivityName"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"/>
Source:
Fullscreen Activity in Android?
Well turns out there is a simple solution to the problem. I just needed to make the status bar and navigation bar transparent.
Post API 21 we can do it programmatically like this -
getWindow().setStatusBarColor(Color.TRANSPARENT);
for making it work on lower android versions, I just needed to add the transparency via xml in the /res/values-v21/styles.xml
<item name="android:statusBarColor">#android:color/transparent</item>
Here's the final effect
use #style/Theme.AppCompat.Light.NoActionBar if you are using AppCompat activity
<activity android:name=".view.activity.SplashActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Just add this code in your activity onCreate()
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
You can do it programatically:
public class ActivityName extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
Read This Content: Fullscreen Activity in Android?
Kotlin: programmatically for API 21: Put it inside onCreate()
#TargetApi(21)
window.statusBarColor = Color.TRANSPARENT
In 2021 all codes are either deprecated or not working. For example:
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
every line deprecated. some other some need API level R. So use style.
Search some time on google I found
<style name="FullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/design_default_color_primary</item>
<item name="colorPrimaryDark">#color/design_default_color_primary_dark</item>
<item name="colorAccent">#color/teal_200</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">#color/white</item>
</style>
now use this style in the Manifest file
<activity
android:name=".FullScreenActivigy"
android:theme="#style/FullScreenTheme"/>
I am using this block of code in my production app which is starts from API 21 to 30. work fine. Even user interact with their SMS or notification. when they back to my app it become full screen again.
If you're in the latest version of android in themes.xml change it to
parent="Theme.MaterialComponents.DayNight.DarkActionBar"
to
parent="Theme.MaterialComponents.DayNight.NoActionBar"

ActionBar: setting button not appear in actionbar on samsung device

I implement layout for actionbar, including one button and other is setting button. I view in preview window (Intellij IDEA 13) I see as I expected but not on real device (samsung note 3).
Here is my layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- chat list. should appear as action button -->
<item android:id="#+id/action_chat_list"
android:icon="#drawable/ic_menu_chat_list"
android:title="#string/action_chat"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
In Preview windows. I see as I expected:
But when I run on real device (Samsung Note 3).I cannot see Setting button:
I cannot understand why. Please tell me how to fix this.
Thanks :)
It because samsung has still a menu button. AFAIK there is no workaround for this
Because you got the options button down to the left of your homebutton. When you thouch it the overflow menu will show on the screen. I guess it implemented in the background so it doenst show on devices that have a "real hardware" options button.
Heres a picture of the button i found on the web. http://cdn.webcazine.com/wp-content/uploads/2012/06/samsung_galaxy_s3_option_keys1.jpg?00d8d4
As #Blackbelt has mentioned, because samsung has a menu button, so actionbar will automatically hide this. And I successfully follow this link to fix this problem. This really a small hack because you use java reflection api to change value of private attribute.
You put this code first when you start to run your application. You can subclass Application and put this code in onCreate
public class MyApplication extends Application {
private static final String TAG = "MyApplication";
private static Context mContext;
#Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
}
}
And you declare this class in your android manifest file under application tag:
<application
android:label="#string/app_name" android:icon="#drawable/ic_launcher" android:allowBackup="true"
android:name=".MyApplication">
</application>
Hope this help :)

Search View uses app icon instead of logo

Android documents explain that app logo is used everywhere when it is defined. But when the search view expands, app icon is used instead of app logo and I can't find a way to show app logo when search view is in expanded state.
Here are the relevant parts.
Manifest file
<application
android:name="MyApp"
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:logo="#drawable/app_logo"
android:theme="#style/Theme.MyTheme" >
searchable.xml (setting icon doesn't change anything)
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:icon="#drawable/app_logo"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:icon="#drawable/ic_search"
android:showAsAction="always|collapseActionView" />
Activity Code
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
menu.findItem(R.id.menu_search).setActionView(searchView);
NOTE: I use ABS SearchView but same thing happens when I switch to default SearchView.
EDIT : I added some screenshots for clarity.
Here I used the star image for app logo, and android image for app icon.
The first screenshot is the default view of activity.
The second one is the view when I click search button.
It shows the android image while I expect it to be the star image.
Set the logo drawable as your icon. Via theme attribute
<item name="android:icon">#drawable/ab_logo</item>
or in code
getSupportActionBar().setIcon(R.drawable.ab_logo);
You're using a completely different SearchView you declared in menu.xml.
With this code you create new SearchView, which is not what you want.
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
So try creating menu like this.
this.getSupportMenuInflater().inflate(R.menu.menu, menu);
final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
final MenuItem searchItem = menu.findItem(R.id.menu_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(info);
And for the ActionBar, instead of setDisplayUseLogoEnabled(true) and others, try set all options at once
final ActionBar bar = getSupportActionBar();
bar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_TITLE |
ActionBar.DISPLAY_USE_LOGO |
ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_HOME_AS_UP);
This is the setup I have in my current project and never experienced any problems like these.
I had the same problem and I solved it using the following code:
menu.findItem(R.id.menu_search).setOnActionExpandListener(this);
// Your Activity/Fragment must implement the MenuItem.OnActionExpandListener interface
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
getSherlockActivity().getSupportActionBar().setIcon(R.drawable.uvweb_logo);
return true;
}
Let me know how this works for you.
I achieved the solution with this simple logic:
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_search:
getActionBar().setIcon(R.drawable.your_logo);
return true;
}
return super.onOptionsItemSelected(item);
}
Just remove collapseActionView from your menu Xml.
To replace the icon with a logo, specify your application logo in the manifest file with the android:logo attribute, then call setDisplayUseLogoEnabled(true) in your activity.
refer this document1
document2
And please see this answer for more details
The only thing you need to have set is this (in onCreate of activity):
getSupportActionBar().setIcon(R.drawable.logo);
...not there is also a setLogo.
Regardless to if you have the icon in the manifest set to something different, this will tell the activity to use a different image during runtime.
I tested this with the following menu item and it works:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search_hint"
android:orderInCategory="1"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always|collapseActionView"/>
</menu>
Inflated like normal in onCreateOptionsMenu:
getSupportMenuInflater().inflate(R.menu.search_menu, menu);
I had the same problem, (for ABS) I had:
ActionBar actionBar = ((SherlockFragmentActivity) activity).getSupportActionBar();
actionBar.setLogo(R.drawable.logo);
And I just added:
actionBar.setIcon(R.drawable.logo);
And the issue is gone.
I found the best way to solve this problem was to add the SearchView programatically to the menu like this:-
SearchView searchView = new SearchView(getActionBar().getThemedContext());
menu.add("Search")
.setIcon(R.drawable.ic_action_search)
.setActionView(searchView)
.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
getActionBar().setIcon(R.drawable.ic_action_willyweather);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
})
.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
And then use the onActionExpandListener to change the icon/logo to the correct value when then SearchView is expanded.
This solution may also work if you define the SearchView in menu.xml, but you'd then still need to setup the onActionExpandListener() in code to do the same as the above example.
Andy.

Android Building Your First App Basics

I'm having a difficult time getting the results that should be displayed as described in the second step of the First App project on the android developer website: developer.android.com/training/basics/firstapp/starting-activity.html#receivetheintent
I've created the first intent and copied all other code however upon running the project I receive a blank android screen with no input elements. Here's what the emulator looks like:
http://s1278.beta.photobucket.com/user/cetmrw791346/media/1_zps116f17a9.png.html
I've set the Run Configuration under the Nexus type with an allocation of 512MB RAM so I'm not exactly sure if this might have something to do with an installation problem regarding the Java SDK (7.0) (JDK not the JRE) or if it could possible be the Android SDK. I'm fairly certain I've set everything up correctly. I'm using The Eclipse (I'm pretty sure it's an IDE) for Mobile Developers then creating a new Android App project from File, New Project. Here's what my Package Explorer looks like: http://s1278.beta.photobucket.com/user/cetmrw791346/media/2_zps0f2b94a2.png.html
I'm unsure as how to further troubleshoot the problem and would really appreciate any additional help. Thanks again for the help.
And here are the relevant files:
**AndroidManifest.xml**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.firstapp.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.firstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.firstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.firstapp.MainActivity" />
</activity>
</application>
</manifest>
**MainActivity.java**
package com.example.firstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
**activity_main.xml**
<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:orientation="horizontal"
tools:context=".MainActivity" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
**strings.xml**
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">DisplayMessageActivity</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
activity_display_message.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=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
**DisplayMessageActivity.java**
package com.example.firstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
It doesn't look like your emulator has started up yet.
Wait for it to boot to the homescreen, and then your app should run.
A couple of points:
It seems you aren't letting your app actually start. The first screen you posted is just the "boot" screen of your emulator
Have you tried switching to the debug perspective in Eclipse? At the bottom you'll see what Eclipse is actually doing. You have to switch to the console view and/or view the logcat to see a bit more detail, but that should actually help you in your efforts.
If you have trouble starting up your Emulator, you can test it by itself. You have (for instance) the option to select the second of the two Android icons that are in the upper bar in Eclipse. It should be the one that says "Android Virtual Device Manager". When you select it, it shows you your configured Emulators, though you can configure new ones as well. You can start one of those in advance and see how they work.
It seems that you have not still executed your app (the emulator is still booting).
I'm quite new to both Java and Android (just a few weeks on it, following an online course) but I found the emulator really slow and I'd really advice you to plug in a real device and use it for running the app.
When connecting my Galaxy S2 to Linux and clicking RUN, Eclipse allows you to use it for execute the app. In the examples of the course I'm following, the apps starts in just a couple of seconds, while running them in the emulator is painful.
If you still need to use the emulator, you can speed it up by editing the properties of your virtual device in ADT and switching on the flag "[X] Use snapshot". By activating this flag, you won't "power off" and "power on" the "virtual device" each time: when you close it, its current state will be saved to disk as an snapshot and when you run it again, you won't need to wait for it to boot. The snapshot will be used and the virtual device will startup very fast.
Got some similar problem with real device. After have been working well on helloworld, keep on displaying HelloWorld after some changes in the code(building the UI). That is the stack i've no idea to resolve...

How do I change the android actionbar title and icon

I'm trying to do some things on the ActionBar in Android.
I've already added new items in the right side of the action bar.
How can I change the left side of the action bar? I want to change the icon and the text, and I want to add a "Back Button" in the action bar for the other screens
This is very simple to accomplish
If you want to change it in code, call:
setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
And set the values to whatever you please.
Or, in the Android manifest XML file:
<activity android:name=".MyActivity"
android:icon="#drawable/my_icon"
android:label="My new title" />
To enable the back button in your app use:
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
The code should all be placed in your onCreate so that the label/icon changing is transparent to the user, but in reality it can be called anywhere during the activity's lifecycle.
To make a single icon be usable by all your action bars you can do this in your Android Manifest.
<application
android:logo="#drawable/Image">
...
</application>
You just need to add these 3 lines of code. Replace the icon with your own icon. If you want to generate icons use this
getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_back_arrow);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
In Android 5.0 material design guidelines discourage the use of icon in actionBar
to enable it add the following code
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
credit goes to author of this article
If you want to change the Action bar title just give the following 1 line code in the onCreate() of your Activity
getActionBar().setTitle("Test");
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(getString(R.string.titolo));
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);
You can change the icon in your by adding whatever icon you want to your respective drawable folders, then changing this line in your AndroidManifest.xml file:
android:icon="#drawable/ic_launcher"
to match whatever the name of your icon is in there. Or put your icon as ic_launcher, if they're the same icon. As for what it says, add or change whatever strings match up to that in your res/values/strings.xml file. Then, once again in your AndroidManifest.xml file, change this line:
android:label="#string/app_name"
to whatever the string you have in their. You'll have to do this for the application as a whole, and whichever activities you want, but the lines are the same.
Hope this helps.
For that, you can do it in 2 ways: XML or Java. See here: How to change the text on the action bar
So:
XML:
<activity android:name=".Hello_World"
android:label="This is the Hello World Application">
</activity>
Java:
public class TitleBar extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("NEW TITLE");
// user can also set color using "Color" and then "Color value constant"
// myTitleText.setBackgroundColor(Color.GREEN);
}
}
}
For set Title :
getActionBar().setTitle("Title");
For set Icon :
getActionBar().setIcon(R.drawable.YOUR_ICON_NAME);
Add the below code inside an onCreate function in your activity.
setTitle("NewName");
I used the following call inside onNavigationItemSelected:
HomeActivity.this.setTitle(item.getTitle());
Go to manifest in which specific activity you want to change Action bar Title name and write
android:label="Title name"
This work for me:
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);
The action bar title will, by default, use the label of the current activity, but you can also set it programmatically via ActionBar.setTitle().
To implement the "Back" (more precisely, "Up") button functionality you're talking about, read the "Using the App Icon for Navigation" section of the Action Bar developer guide.
Finally, to change the icon, the guide covers that as well. In short, the action bar will display the image supplied in android:icon in your manifest's application or activity element, if there is one. The typical practice is to create an application icon (in all of the various densities you'll need) named ic_launcher.png, and place it in your drawable-* directories.
I got non-static method setTitle(CharSequence) cannot be referenced from a static context error because I used setTitle() in static PlaceholderFragment class. I solved it by using getActivity().getActionBar().setTitle("new title");
You can also do as follow :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
setSupportActionBar(toolbar)
setTitle("Activity 2")
}
Go to AndroidManifest.xml file.
Find the <application> tag
There you can see a attribute
android:label="#string/app_name"
Now go to res > values > strings.xml
Change the
<string name="app_name">MainActivity</string>
to
<string name="app_name">Your Desired Name</string>
Example
AndroidManifest.xml
<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"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SubmitForm">
</activity>
</application>
strings.xml
<resources>
<string name="app_name">Your Desired Name</string>
<string name="action_settings">Settings</string>
</resources>

Categories

Resources