My PopUpMenu doesn't do anything - java

I'm working on the Android Developer Tutorial and when I tried to open a activity it doesn't do anything.
Here is my Main.java:
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getApplicationContext(), "Probandooo", Toast.LENGTH_LONG).show();
}
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.main, popup.getMenu());
popup.show();
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.responder:
Intent i = new Intent (Main.this, Responder.class);
startActivity(i);
return true;
case R.id.reenviar:
Toast.makeText(getApplicationContext(), "Reenviando...", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
#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;
}
}
I already declare it on my manifest, I don't know what is

Related

How to create a search bar in android with a suggestion drop down appearing upon typing text in search bar?

I want to create a search bar with a drop down suggestion list, which appears upon typing text in the search bar. The suggestion list should completely or partially cover the activity screen(The drop down is not a new activity itself). The result should be populated from the backend, which contains a list of hard coded strings. I cannot find a solution for such a problem. Can someone please help me out here?
My MainActivity looks like this:-
import android.app.ActionBar;
import android.app.SearchManager;
import android.content.Intent;
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.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity
{
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.listViewBuilding);
ArrayList<String> arrayBuilding = new ArrayList<>();
arrayBuilding.addAll(Arrays.asList(getResources().getStringArray(R.array.array_buildings)));
adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, arrayBuilding);
lv.setAdapter(adapter);
lv.setVisibility(View.GONE);
lv.setTextFilterEnabled(true);
if(getSupportActionBar()!=null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query)
{
return false;
}
#Override
public boolean onQueryTextChange(String newText)
{
ListView lv = (ListView)findViewById(R.id.listViewBuilding);
lv.setVisibility(View.VISIBLE);
adapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}

Android Intent doesnt work

so I have these code
main.java
package com.example.kamusinggris_indonesiaidiom;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Main extends Activity {
private TextView teks;
private ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
teks = (TextView) findViewById(R.id.text);
list = (ListView) findViewById(R.id.list);
}
protected void onNewIntent(Intent intent) {
handleIntent(getIntent());
}
private void handleIntent(Intent intent) {
// TODO Auto-generated method stub
handleIntent(intent);
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Intent wordIntent = new Intent(this, Definisi.class);
wordIntent.setData(intent.getData());
startActivity(wordIntent);
} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
// TODO Auto-generated method stub
Cursor cursor = managedQuery(Provider.CONTENT_URI, null, null,
new String[] {query}, null);
if (cursor == null) {
// There are no results
teks.setText(getString(R.string.no_results, new Object[] {query}));
} else {
// Display the number of results
int count = cursor.getCount();
String countString = getResources().getQuantityString(R.plurals.search_results,
count, new Object[] {count, query});
teks.setText(countString);
// Specify the columns we want to display in the result
String[] from = new String[] { Database.KATA,
Database.DEFINISI };
// Specify the corresponding layout elements where we want the columns to go
int[] to = new int[] { R.id.kata,
R.id.definisi };
// Create a simple cursor adapter for the definitions and apply them to the ListView
SimpleCursorAdapter words = new SimpleCursorAdapter(this,
R.layout.hasil_pencarian, cursor, from, to);
list.setAdapter(words);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View tmp,
int posisi, long id) {
// TODO Auto-generated method stub
Intent definisi = new Intent(getApplicationContext(), Definisi.class);
Uri data = Uri.withAppendedPath(Provider.CONTENT_URI,
String.valueOf(id));
definisi.setData(data);
startActivity(definisi);
}
});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()) );
searchView.setIconifiedByDefault(false);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
}
definisi.java
package com.example.kamusinggris_indonesiaidiom;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.TextView;
public class Definisi extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_definisi);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
Uri uri = getIntent().getData();
Cursor kursor = managedQuery(uri, null, null, null, null);
if (kursor == null) {
finish();
} else {
kursor.moveToFirst();
TextView kata = (TextView) findViewById(R.id.kata);
TextView definisi= (TextView) findViewById(R.id.definisi);
int wIndex = kursor.getColumnIndexOrThrow(Database.KATA);
int dIndex = kursor.getColumnIndexOrThrow(Database.DEFINISI);
kata.setText(kursor.getString(wIndex));
definisi.setText(kursor.getString(dIndex));
} }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
case android.R.id.home:
Intent a = new Intent(this, Main.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
return true;
default:
return false;
}
}
}
its a dictionary, so when a search suggestion on a listview is clicked, it supposed to open the definisi.java and display the definition. But what I got here is when I clicked the search suggestion it displayed the main.java (its just go back to the previous activity). what's wrong on the intent part? please help me
Use passed intent variable in onNewIntent
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
You should also remove call to handleIntent from within handleIntent method

java.lang.StackOverFlowError in android studio

i am working as a beginner on new android application, i have done everything according to the tutorials, but im still getting this error. here is the code for MyAcitivty.java:
this is MYActivity.java
package com.example.ambuj.myfirstapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.ambuj.myfirstapp";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
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 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);
}
}
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);
}
public void openSearch(){
Toast.makeText(this, "Search Button Pressed", Toast.LENGTH_LONG).show();
}
public void openSettings(){
Toast.makeText(this, "Settings Button Pressed",Toast.LENGTH_LONG).show();
}
}
this is my logcat:
01-18 14:22:33.180 26194-26194/com.example.ambuj.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ambuj.myfirstapp, PID: 26194
java.lang.StackOverflowError
at android.util.SparseArray.get(SparseArray.java:111)
at android.util.SparseArray.get(SparseArray.java:102)
at android.content.res.StringBlock.get(StringBlock.java:70)
at android.content.res.AssetManager.getPooledString(AssetManager.java:275)
at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:730)
at android.content.res.TypedArray.getText(TypedArray.java:97)
at android.support.v7.internal.view.SupportMenuInflater$MenuState.readItem(SupportMenuInflater.java:374)
at android.support.v7.internal.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:168)
at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
at com.example.ambuj.myfirstapp.MyActivity.onCreateOptionsMenu(MyActivity.java:32)
at com.example.ambuj.myfirstapp.MyActivity.onCreateOptionsMenu(MyActivity.java:33)
Yout problem is here:
#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 onCreateOptionsMenu(menu); //<--- conflicting line
}
Change
return onCreateOptionsMenu(menu);
to
return true;
Hope it helps

Android: unable to create Media Player (using pls URL)

I'm trying to create an app that plays an internet radio stream from a .pls file. I know that the Android Media Player can't read .pls files, so I read it beforehand and obtained the URL that is stored in the .pls file. That's what's in the R.String.audio_stream variable I pass to setDataSource(). However, when I run my code on the simulator, it fails when I try to start the media player. LogCat returns the error message, "Unable to create media player." Does anyone know what might be the problem?
Here's my code:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build;
import android.media.MediaPlayer;
import android.util.Log;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
private Button mStartButton;
private Button mStopButton;
boolean isPlaying = false;
private static final String TAG = "MyActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mp = new MediaPlayer();
mStartButton = (Button)findViewById(R.id.start_button);
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isPlaying){
//do nothing
}else{
isPlaying = true;
try{
mp.setDataSource(getString(R.string.audio_stream));
mp.prepare();
mp.start();
}catch(IOException e){
Log.e(TAG, "prepare() failed.");
}
}
}
});
mStopButton = (Button)findViewById(R.id.stop_button);
mStopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isPlaying){
//do nothing
}else{
isPlaying = false;
mp.release();
}
}
});
}
#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);
}
}
Its easy. You must use before "setDataSource":
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

Trouble loading random image on button press

What i'm trying to do is just simply have the button load an image at random. However, the button doesn't seem to be working. When i first load the activity there it works fine and there is a random image....But when i press the button it's not loading another like i need it to. Any idea what i have wrong here? It looks fine to me :/
package com.my.package;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View.OnClickListener;
public class Randomimage extends Activity implements OnClickListener{
private Integer [] mImageIds = {
R.drawable.one,
R.drawable.two,
R.drawable.three,
};
private static final Random rgenerator = new Random();
private ImageView iv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
iv = (ImageView) findViewById(R.id.imageviewyeah);
iv.setImageResource(q);
View nextButton = findViewById(R.id.next_image_button);
nextButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.next_image_button:
iv.setImageResource(rgenerator.nextInt(mImageIds.length));
break;
}
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu3, menu);
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case R.id.menu:
startActivity(new Intent(this, Main.class));
return true;
case R.id.startnhie:
startActivity(new Intent(this, startnhie.class));
return true;
}
return false;
}
}
In your button press handling code, change
iv.setImageResource(rgenerator.nextInt(mImageIds.length));
to
iv.setImageResource(mImageIds[rgenerator.nextInt(mImageIds.length)]);

Categories