Background
In a rather simple Android App a Settings Activity is used for just one ListPreference. When clicking the menu item in the header a new screen shows up without any preference or header.
compiledSdkVersion & targetSdkVersion = 25
minSdkVersion = 10
Code
Here is the code that I currently have.
SettingsActivity.java: Here the onCreate method is never called.
public class SettingsActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {
/**
* {#link PreferenceActivity#onCreate(Bundle)}
*/
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
addPreferencesFromResource(R.xml.pref_movies);
}
/**
* {#link android.preference.Preference.OnPreferenceChangeListener#onPreferenceChange(Preference, Object)}
*/
#Override
public boolean onPreferenceChange(Preference preference, Object o) {
return false;
}
}
pref_movies.xml: Is located in res/xml.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:title="#string/setting_sort_label"
android:key="#string/setting_sort_key"
android:entries="#array/movies_sort"
android:entryValues="#array/movies_sort_values" />
</PreferenceScreen>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.brainchest.udacity.popularmovies">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
android:label="#string/general_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="net.brainchest.udacity.popularmovies.MainActivity"></meta-data>
</activity>
</application>
</manifest>
onOptionsItemSelected in my Fragment: Here the intent is created and startActivity is called. The debugger runs through it, no exceptions.
/**
* {#link Fragment#onOptionsItemSelected(MenuItem)}
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// open the settings
if (item.getItemId() == R.id.action_settings) {
Intent settingsIntent = new Intent(getContext(), SettingsActivity.class);
startActivity(settingsIntent);
}
return super.onOptionsItemSelected(item);
}
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="movies_sort">
<item>Most popular</item>
<item>Highest rated</item>
</string-array>
<string-array name="movies_sort_values">
<item>0</item>
<item>1</item>
</string-array>
</resources>
something else I need to show here?
What I tried so far
Searching the web I found some common mistakes that refer either to a missing AndroidManifest.xml entry or mixing up some resource IDs. I double checked this to my best knowledge but still it doesn't work.
What did I miss?
Try wrapping the ListPreference in a PreferenceCategory like this:
<PreferenceCategory
android:key="<YOUR_KEY>"
android:title="<YOUR_TITILE>" >
<ListPreference
android:title="#string/setting_sort_label"
android:key="#string/setting_sort_key"
android:entries="#array/movies_sort"
android:entryValues="#array/movies_sort_values" />
</PreferenceCategory>
Finally found out what the issue is. I used the wrong overload. Instead of
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
addPreferencesFromResource(R.xml.pref_movies);
}
which is not called, I had to use
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_movies);
}
Now the settings are shown.
Related
I have been following the mybringback tutorials on youtube and I tried implementing what I learned. Trying to get a button on my main page to open another page. Finally got the program to run without errors but now when I press the button nothing opens.
Main .xml file where my button is
<Button
android:id="#+id/btnChpt3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Appearance and Grooming Policies"
android:textSize="18sp"
android:textStyle="bold|italic"
android:gravity="center"
/>
Name of .xml file im trying to get to is chapter3.xml
Menu.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button chapterThree = (Button) findViewById(R.id.btnChpt3);
chapterThree.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.th3ramr0d.learnar670_1.CHAPTER3"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
And my manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.learnar670_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<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=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Chapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
So button ided as btnChpt3 wont open up my .xml file named chapter3.xml. Thanks for the help.
Here is my Chapter3.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.os.Bundle;
public class Chapter3 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter3);
}
}
Here is my MainActivity.java
package com.th3ramr0d.learnar670_1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#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);
}
}
Copy this and paste in your AndroidManifest and try,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.learnar670_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Menu"
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=".Chapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
To help you understand the problem,
<category android:name="android.intent.category.LAUNCHER" />
The code above in the AndroidManifest defines the activity to be launced when the App Icon is pressed. As per your earlier manifest, it launches the activity MainActivity which also sets it setContentView(R.layout.activity_main); by default as the IDE creates a Hello World program.
So when you launch you app, its MainActivity (that looks the same layout you have designed) which is loading and not Menu activity which you want to load. Hence making few changes in the manifest where we declare the Menu activity as the launcher now launches Menu activity which has the piece of code to process your button click.
I hope this helped!
Actually you can try a more convenient way of starting activities inside your application:
startActivity(new Intent(Menu.this, Chapter3.class))
Also you can read more how it works here:
http://developer.android.com/training/basics/firstapp/starting-activity.html
Good day.
Try replacing the line :
startActivity(new Intent("com.th3ramr0d.learnar670_1.CHAPTER3"));
with the code below :
Intent intent = new Intent(Menu.this, Chapter3.class);
startActivity(intent);
Try reverting your code to the original code, copy your layout activity_main.xml and rename it menu.xml.
now in the layout menu.xml change this line:
android:text="Appearance and Grooming Policies"
to:
android:text="Go to menu"
and the line:
android:id="#+id/btnChpt3"
with:
android:id="#+id/btnMenu"
and replace the line:
setContentView(R.layout.activity_main);
in Menu.java with:
setContentView(R.layout.menu.xml);
finally in MainActivity.java add the following to your oncreate method:
Button btnGoToMenu = (Button) findViewById(R.id.btnMenu);
btnGoToMenu.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Menu.class);
startActivity(intent);
}
});
and re-run the application.
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
I'm trying to follow along with the official Android tutorial (http://developer.android.com/training/basics/firstapp/starting-activity.html) but I'm failing miserably. Everything worked OK until I needed to add a second Activity. First, the Activity I created in Android Studio does not show up in my list of java files. I clicked New -> Activity and nothing appeared. To get around this I opened up Windows Explorer and copied/renamed MainActivity.java to DisplayMessageActivity.java and added the code in the tutorial.
After following along the app does not run and I get multiple "Gradle" errors such as:
Gradle: error: cannot find symbol class Activity
Gradle: error: cannot find symbol class Bundle
What do I need to fix in order to get this to run? Here is the relevant code:
MainActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
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) {
// Do something in response to button
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);
}
}
Display MessageActivity.java
package com.example.myfirstapp;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="#string/app_name"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
when you create an activity, make sure you insert it into mainifest.xml as well, so your manifest should be like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="#string/app_name"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
<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.MessageActivity>
</activity>
</application>
</manifest>
On each error you got (on red as on image) go and press Alt+Enter to import missing classes or declare the variables.
Please note that all files (.java) have to be not red underlined if they are error free.
Maybe some tutorial´s names of classes´s changed. then you need put the name your Activity before the variable "extra_message" like that MyActivity.EXTRA_MESSAGE
on the main Activity put...
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
or simple use any value. like that
String message = intent.getStringExtra('xxx');
Go on through the tutorial, you'll be taught to create one later.
I cannot seem to get my onSearchRequest() working. I think i have all of the coding correct, would someone be able to check please.
Manifest file
<activity android:name="SearchActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
</activity>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint">
</searchable>
SearchActivity.java
public class SearchActivity extends android.app.Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if(Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
Toast.makeText(this, query,Toast.LENGTH_LONG).show();
}
}
}
Method thats called when the button is clicked
public void search(View view)
{
onSearchRequested();
Toast.makeText(this, "text",Toast.LENGTH_LONG).show();
}
Can anyone sport any mistakes anywhere??? as from what i can see, it should all be working perfectly.
I am still learning to code, but apps i code persistently can't work on 2 diferent devices. Can Eclipse (latest android sdk) be the error or i code wrong every single app?
Its force close on 2 devices
public class MainActivity extends Activity {
Button dugme = (Button) findViewById(R.id.dugme);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dugme.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Cao();
}
});
}
private void Cao(){
Intent Cao = new Intent(this, Cao.class);
startActivity(Cao);
}}
This is Cao class
public class Cao extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.cao);
}}
And Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.book1"
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.book1.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.book1.Cao"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.Action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application></manifest>
Try to put
Button dugme = (Button) findViewById(R.id.dugme);
this line in your onCreate method. A class shoud have only variables and methods. This line is definition. So I think that is only problem in your code. Other than there is nothing wrong in your code.
Put this line
Button dugme = (Button) findViewById(R.id.dugme)
After
setContentView(R.layout.activity_main)
Android takes each reference of view_id according to its each layout xml
Think if android takes view reference_id before setting up layout to the given activity then we can not even keep same name of view ID for two different layout xml .
This line is wrong:
Button dugme = (Button) findViewById(R.id.dugme);
not because it not located in the onCreate method, but because it appears before this line:
setContentView(R.layout.activity_main);
And the reason for that is: if you didn't set your layout as the content view of your activity you can't use the findViewById method, because there is no place that this view could be found (you layout is not yet set).