Display App Icon in action bar under api 11 - android

I'm writing simple tutorial app and for some reason my App icon is not showing up (I want to use it as navigate up button instead of the arrow).
As stated in the topic, I want to do it on Api level 11, however all the sollutions I've found rely on setIcon method.
getActionBar().setIcon(R.drawable.ic_launcher); //only on API >= 14
The fragment preview in Android studio 1.0.2 does show the icon, however the emulator does not, so I'd expect the resource is ok. Also, the search and delete icons are working like a charm. I've tried setting this both as Icon and a logo to no avail.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yamba"
android:versionCode="1"
android:versionName="1.0"
>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light"
android:logo="#drawable/ic_launcher"
>
<activity
android:parentActivityName=".MainActivity"
android:name=".StatusActivity"
android:label="#string/app_name" >
</activity>
<!--- The labe is what's displayed at the taskbar!-->
<activity
android:parentActivityName=".MainActivity"
android:name=".SettingsActivity"
android:label="#string/title_activity_settings" >
</activity>
<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>
activity_settings.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.yamba.SettingsActivity">
<TextView android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
menu_settings.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="com.example.yamba.SettingsActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
SettingsActivity.java
package com.example.yamba;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SettingsActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null){
SettingsFragment fragment = new SettingsFragment();
getFragmentManager().beginTransaction().add(android.R.id.content, fragment, fragment.getClass().getSimpleName()).commit();
setContentView(R.layout.activity_settings);
// getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
// only for API>=14
// getActionBar().setDisplayShowHomeEnabled(true);
// getActionBar().setIcon(R.drawable.ic_launcher);
}
}
#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_settings, menu);
// return true;
// 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();
switch(id){
case R.id.action_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
case R.id.action_tweet:
startActivity(new Intent(this, com.example.yamba.StatusActivity.class));
return true;
default:
return false;
}
}
}
SettingsFragment.java
package com.example.yamba;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
/**
* Created by me on 19-01-2015.
*/
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences prefs;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
public void onStart(){
super.onStart();
prefs= PreferenceManager.getDefaultSharedPreferences(getActivity());
prefs.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
}
}
I've stumbled upon suggestion that I may be able to define custom style based on one of the existing styles, but can't quite figure out how to approach it.
If you need any other file, let me know and thanks for your time.

since it's API 11..
did you try using the support libraries?
try switching
getActionBar().setIcon(R.drawable.ic_launcher);
with :
getSupportActionBar().setIcon(R.drawable.ic_launcher);
tell me if it works and we'll continue from there :)

Related

Creation of action bar in Android Studio

I am new to the Android development app. I was following the tutorials given in the official site. So far everything was good but when I started to create and modify the action bar, this error started to arise:
Error:Execution failed for task ':app:processDebugManifest'.
Manifest merger failed : Main AndroidManifest.xml at AndroidManifest.xml manifest:package attribute is not declared
My manifest file is this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="14"/>
package="com.mycompany.myfirstapp" >
<application>
<!-- The/main activity (it has no parent activity) -->
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
<activity
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.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.hpcore.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.hpcore.myfirstapp.MyActivity" />
</activity>
</application>
</manifest>
And my main activity Java code is this:
package com.example.hpcore.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import com.mycompany.myfirstapp.DisplayMessageActivity;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE= "com.hp core.myfirstapp.MESSAGE";
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);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_Activity_actions,menu);
return super.onCreateOptionsMenu(menu);
}
#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.
switch (item.getItemId())
{
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I guess the problem in one of these files.
You package attribute should be part of Manifest Tag-
Change your manifest Tag like below-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myfirstapp" >
<uses-sdk android:minSdkVersion="14"/>

err cache miss when trying to view a website using web viewer

i am trying to make an application whereby when an user opens the application, they will be directed to a mobile anime website and i encountered this problem. Help !
For Main Activity
package com.example.ahliang125.kissanime;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.kissanime.com/m");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
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);
}
}
For Activity Main
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<WebView android:id="#+id/activity_main_webview" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
For Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ahliang125.kissanime" >
<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>
</application>
<uses-permission android:name="android.permission.internet"/>
</manifest>
Could be that "android.permission.internet" should be "android.permission.INTERNET"

My app is crashing when I use the Theme.NoTitleBar in the menefest file

I've done this on my other apps and it worked fine. On my new app, when I run it I get a message box that says
"Unfortunately livebitcoinscharts has stopped",
The project is just from the template code generated by eclipse, I did not add anything to it
I'm using eclipse and tried putting a break point on the first line of code, which never went off.
Manifest file:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<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>
code:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#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;
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Layout:
<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="com.example.bitcoinslivecharts2.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
I think this is because you are usin ActionBarActivity which is under android suuport library v7 appcompat library which has to inflate a theme under Theme.AppCompat like Theme.AppCompat.Light.DarkActionBar
You can just inherit from Activity if you don't want actionbar.

Contextual Actionbar with DialogFragment

I've been trying to implement a contextual action bar along with a dialog fragment.
Similar to the downloads widget in android.
I've tried to set android:windowActionModeOverlay to be true in the theme.
But it doesnt seem to work. Is there any way I can achieve it??
The downloads window that you have in your screenshot is actually an Activity using the #android:style/Theme.Holo.Dialog theme which makes it look like a dialog. To achieve the same look as the downloads window, your Activity need only use the same theme.
You can set this theme in your manifest like so:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Example implementation excluding string and drawable resources.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mceley.dialog.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Dialog" >
<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.mceley.dialog.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
findViewById(R.id.context_button).setOnClickListener(this);
}
#Override
public void onClick(View v) {
ExampleMode mode = new ExampleMode();
startActionMode(mode);
}
public class ExampleMode implements ActionMode.Callback {
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
}
}
main_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<Button android:id="#+id/context_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/show_context_bar" />
</LinearLayout>
main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
Result:
I don't think you can add ActionBar in DialogFragment.
But We can Try that using Activity As Dialog.
I have tried this with ActionBarSherlock and I have added R.style.Sherlock___Theme_Dialog as my Activity's theme but it looks like this:
After doing Above thing i understud, we can't Add ActionBar in Dialog or DialogFragment.

Launching activity from android options menu

I have looked through these forums to find a solution to this problem, and even though there appear to be solutions, none of them seem to be working for me. So here goes.
I am a newbie to Android development. I have an app with an options menu. When I click on one to the options, I want it to launch a new activity - but I keep getting the error
Intent cannot be resolved to a type
in home.java on the line:
Intent intent = new Intent(this, about.class);
Below is all of my code that I believe is relevant. Please let me know if you need to see anything else. As I said, I have tried to follow other questions, but none of them seem to work for me (as-in the below code seems to work for everyone else). Any help would be awesome.
I have my menu defined in res/menu/main_menu.xml by:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/home"
android:icon="#drawable/ic_menu_home"
android:title="#string/home" />
<item android:id="#+id/about"
android:icon="#drawable/ic_menu_about"
android:title="#string/about" />
</menu>
I have two activities - home.java and about.java. Home.java is the activity that is launched when the app is launched and is shown below.
package ca.example.home;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class home extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.home:
return true;
case R.id.about:
Intent intent = new Intent(this, about.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
About.java is the new activity to be launched and is shown below:
package ca.brianmccain.nbla;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class about extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
}
I have changed the manifest to:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.example.home"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true">
<activity android:name=".home"
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=".about">
</activity>
</application>
</manifest>
You have to import the intent class.
import android.content.Intent;
If you ever get a similar error, and if you are using eclipse,
press Ctrl-Shift-O ("Organize imports") - this searches for all required imports and adds them to the file.
I just solved my menu problem. The menu page would open but when I clicked on an individual menu option it would not proceed to the next page / activity.
It was all due to a simple addition of a "space" right at the end of the statement after the "." just before the +cheese I deleted the space and it worked.
BEFORE
Class ourClass = Class.forName("com.example.androidtutorial2. " +cheese);
AFTER
Class ourClass = Class.forName("com.example.androidtutorial2." +cheese);

Categories

Resources