Modifying Android Search for ListView - android

I'm new to android app development and currently trying to figure out how to add search to an app. I did some searching and the tutorial that was the simplest for me to understand is here.
The app runs and searches as it should. The only issue is that the tutorial is for searching a ListView. The activity that my app's search is hosted in has other details(itemview, textview) that need to be on the screen so a ListView won't work for what I'm trying to do. How do I modify this example to provide a dropdown of suggestions as the user types into the search box? Ideally, I would like a search as seen in this image here.
ItemSearch.java
import android.app.SearchManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
public class ItemSearch extends AppCompatActivity {
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_search_activity);
final String[] months = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December"};
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, months);
final ListView listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String text = (String) listview.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), text,
Toast.LENGTH_SHORT).show();
}
});
}
#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.menu_search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return true;
}
}
item_search_acitivity.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout 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:id="#+id/item_view_activity"
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.me.ItemSearch">
<ImageView
/>
<ImageView
/>
<ImageView
/>
<TextView
/>
<TextView
/>
<TextView
/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</ScrollView>
menu_search.xml under res/menu/menu_search
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:title="#string/hint_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
searchable.xml under res/xml/searchable
<?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"
android:searchSuggestIntentAction="android.intent.action.VIEW">
</searchable>
In AndroidManifest.xml I've added
<activity android:name=".ItemSearch" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>

For this type of dropdown suggestion functionality you would use AutoCompleteTextView.
Example usage below:
In your layout file:
<AutoCompleteTextView
android:id="#+id/auto_complete_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/key"/>
In a resource file such as arrays.xml or strings.xml:
<string-array name="suggestions">
<item>January</item>
<item>February</item>
<item>March</item>
...
</string-array>
In your fragment or activity:
AutoCompleteTextView autoCompleteTv;
...
AutoCompleteTextView autoCompleteTv = (AutoCompleteTextView) view.findViewById(R.id.auto_complete_tv);
autoCompleteTv.setAdapter(ArrayAdapter.createFromResource(view.getContext(), R.array.suggestions, android.R.layout.simple_dropdown_item_1line));

Related

How to create a drop-down list in Android Studio?

I am trying to create a drop-down list for an activity in Android Studio. I have tried using the Spinner.
Here is my xml code:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
Here is my Java code:
Spinner dropdown = findViewById(R.id.spinner1);
String[] items = new String[]{"1", "2", "3"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
This is a screenshot of what it looks like:
As you can see, the drop-down list appears as a pop-up, however, I want the drop-down list to appear in a different way. See the image below as an example of what I want:
Does anyone have any ideas on how I could do this?
Actually it does look like there is a way with the Material design library called ExposedDropdownMenu
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/menu"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
/>
</com.google.android.material.textfield.TextInputLayout>
Obviously this is with TextInput and not a spinner so there will still be some custom logic to prevent typing if you dont want that
I personally have not tried it myself but it looks like what you want
Some ideas:
As #tyczj said, that is android's default layout that is showing, so there are some alternatives:
Create tour own custom layout
Use an external library like SearchableSpinner, it incorporates the search function and the item layout is similar from what you want.
Here I have found a useful example for a ComboBox/Spinner on Android:
https://abhiandroid.com/ui/spinner
Here is the code:
https://github.com/abhisheksaini4/SpinnerExamples/blob/master/SpinnerExamples.7z
The main codes include MainActivity.java:
package example.abhiandriod.spinnerexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the bank name list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(), bankNames[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#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);
}
}
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"
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=".MainActivity">
<Spinner
android:id="#+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
</RelativeLayout>
menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
And AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.abhiandriod.spinnerexample" >
<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>
</manifest>

Android Action Bar, adding action buttons

I'm trying to add button to the Action Bar with no luck. There are no errors. Button is in the #drawable. But it doesn't show up.
What am I doing wrong?
(It asks me to add more details, but I have nothing more to add)
test activity :
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
public class test extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
setupCREATEbut();
}
private void setupCREATEbut() {
Button CREATEbut = (Button) findViewById (R.id.appenter2);
CREATEbut.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(intent,0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.topmenu, menu);
return super.onCreateOptionsMenu(menu);
}
}
test xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="rs.test">
<LinearLayout
android:id="#+id/centerblock"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relcenterblock"
android:layout_width="fill_parent"
android:layout_height="415dp" >
<Button
android:id="#+id/appenter2"
android:layout_width="160dip"
android:layout_height="160dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/bigroundbutton"
android:gravity="center"
android:text="#string/request"
android:textColor="#ffffff"
android:textSize="30sp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
topmenu xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_toprightmenu"
android:title="#string/toprightmenu"/>
</menu>
Try to use showAsAction=always or showAsAction=ifRoom.
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_toprightmenu"
android:title="#string/toprightmenu"
yourapp:showAsAction="always"/>
</menu>

Change the icon of default Search View android

I am facing the problem with default search view icon.I want to change the icon in my application and use custom drawable instead of default.
I am using the xml:
<SearchView
android:id="#+id/searchView1"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp" >
</SearchView>
Thanks for help.
here i am giving the code for searchView used in whatsApp like
your activity_main.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>
your searchview_in_menu.xml in menu folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionLayout="#layout/searchview_layout"/>
</menu>
your searchview_layout.xml in layout folder
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_view_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
your MainActivity.java is
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.SearchView;
public class MainActivity extends Activity {
private SearchView mSearchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
//find the search view item and inflate it in the menu layout
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
//set a hint on the search view (optional)
mSearchView.setQueryHint(getString(R.string.search));
//these flags together with the search view layout expand the search view in the landscape mode
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItem.SHOW_AS_ACTION_ALWAYS);
//expand the search view when entering the activity(optional)
searchItem.expandActionView();
return true;
}
it will produce result like the following screenshot
Hope it will serve your purpose.

Treasure hunt has stopped working

When I ran the app the first activity worked but as soon as it went to the second activity it said treasure hunt has stopped working, and there was nothing under logcat or console. I am pretty sure it is a problem with level_1.java but can't figure out what. Please help thanks.
Main Activity:
package com.example.treasurehunt;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
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;
}
public void Start(View view) {
Log.d("hi", "hello");
Intent intent = new Intent(this, Level_1.class);
startActivity(intent);
finish();
}
}
Mainxml:
<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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Treasure Hunt" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="82dp"
android:text="Start"
android:onClick="Start" />
</RelativeLayout>
`
level_1java:
package com.example.treasurehunt;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
public class Level_1 extends Activity {
private SharedPreferences settings;
private SharedPreferences.Editor editor;
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
String hint;
String answer;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("hi","hello");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_1);
int level = settings.getInt("level", 1);
switch (level) {
case 1: level1();
break;
default:
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.level_1, menu);
return true;
}
public void level1() {
editor.putInt("level", 1); // only needs to be done for level 1
editor.commit();
Clue.setText("Bartholdi was my creator, I carry fire and knowledge, what am I?");
hint = "July IV MDCCLXXVI";
answer = "statue of liberty";
}
public void answer() {
String useranswer = edittext.toString();
if (useranswer.equalsIgnoreCase(answer)) {// if they get the correct answer
int leveltest = settings.getInt("level", 1);
editor.putInt("level", leveltest+1); //adds one level to the current level
editor.commit();
}
}
public void hint() { //function that displays the hint
}
}
level_1 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=".Level_1" >
<TextView
android:id="#+id/Clue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="Clue" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Clue"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/editText"
android:layout_marginBottom="50dp"
android:text="Submit"
android:onClick="answer"/>
<Button
android:id="#+id/Hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Submit"
android:layout_alignBottom="#+id/Submit"
android:layout_alignLeft="#+id/editText"
android:text="Hint"
android:onClick="Hint"/>
</RelativeLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.treasurehunt"
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.treasurehunt.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.treasurehunt.Level_1"
android:label="#string/title_activity_level_1" >
</activity>
</application>
</manifest>
Seems you're having NullPointerException.
On the field declaration
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
Move the assignments to in onCreate()
public class Level_1 extends Activity
{
// Other members ....
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("hi","hello");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_1);
// Assignemnts should be here
Clue = (TextView)findViewById(R.id.Clue);
edittext = (EditText)findViewById(R.id.editText);
int level = settings.getInt("level", 1);
switch (level) {
case 1: level1();
break;
default:
break;
}
}
}

Android 4/ICS Context menu not showing up

I've looked at the other threads on this topic, and none of them have helped. I'm using an appropriate minSdkVersion, my menu xml appears setup correctly, my Manifest is calling Them.Holo.Light, and all of the other "typically overlooked" problems (that I'm aware of from other threads, anyway).
I'm using Eclipse with the latest SDK and following the basic tutorial from the Android dev wiki. Eclipse isn't showing any errors.
As you'll see, I'm referencing main.xml and main_activity.xml. main.xml is showing the default view when the app loads, which is a simple header bar and a picture of two ponies. The idea is that the user can use the context menu to swap between the pony picture and one of Lionel Richie (hey, it's a test app).
I've tried running it on the emulator, as well as my Xoom tablet and a phone, with no luck.
Code:
/***** Contents of testIceCreamS Manifest *****/
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.icecreams"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="11" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestIceCreamSActivity"
android:theme="#android:style/Theme.Holo.Light"
android:label="#string/app_name"
android:uiOptions="splitActionBarWhenNarrow" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
/***** Contents of TestIceCreamSActivity.java *****/
package test.icecreams;
import android.app.Activity;
import android.os.Bundle;
import test.icecreams.R;
public class TestIceCreamSActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
/***** Contents of ActionBarUsage.java *****/
import test.icecreams.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ActionBarUsage extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
LinearLayout bkgr = (LinearLayout)findViewById(R.id.LinearLayout1);
ImageView image = (ImageView)findViewById(R.id.imageView1);
switch (item.getItemId()) {
case R.id.buttonone:
image.setImageResource(R.drawable.ponies);
return true;
case R.id.buttontwo:
image.setImageResource(R.drawable.hello);
return true;
case R.id.buttonthree:
bkgr.setBackgroundResource(R.color.backgroundwhite);
return true;
case R.id.buttonfour:
bkgr.setBackgroundResource(R.color.background);
return true;
case R.id.buttonfive:
//The alert code goes here!
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
/***** Contents of main_activity.xml: *****/
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/buttonone"
android:title="#string/showimage1"
android:icon="#drawable/helloicon"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttontwo"
android:title="#string/showimage2"
android:icon="#drawable/poniesicon"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttonthree"
android:title="#string/showwhite"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttonfour"
android:title="#string/showblack"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttonfive"
android:title="#string/showalert"
android:showAsAction="ifRoom|withText"/>
</menu>
/***** Contents of main.xml: *****/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000"
android:orientation="vertical" >
<TextView
android:id="#+id/hellotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:debuggable="true"
android:text="#string/hello" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/cd"
android:src="#drawable/ponies" />
</LinearLayout>
I also have the content for strings.xml, but I figured it was probably the least important of all of these.
Any suggestions?
In fact, I understand that you want to create an application running on ICS with the layout main.xml and a contextual menu like *main_activity.xml*. You do not have to create two activities, only the main one which will be launched at application will be started.
Please try to make all in one Activity in the same file TestIceCreamSActivity.java :
package test.icecreams;
import test.icecreams.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class TestIceCreamSActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
LinearLayout bkgr = (LinearLayout)findViewById(R.id.LinearLayout1);
ImageView image = (ImageView)findViewById(R.id.imageView1);
switch (item.getItemId()) {
case R.id.buttonone:
image.setImageResource(R.drawable.ponies);
return true;
case R.id.buttontwo:
image.setImageResource(R.drawable.hello);
return true;
case R.id.buttonthree:
bkgr.setBackgroundResource(R.color.backgroundwhite);
return true;
case R.id.buttonfour:
bkgr.setBackgroundResource(R.color.background);
return true;
case R.id.buttonfive:
//The alert code goes here!
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
Please also change your target SDK version to level 14 (ICS 4.0 -> 4.0.2) or 15 (4.0.3) in your manifest :
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="15" />

Categories

Resources