I am trying to create a page where I have four buttons, each leading to a separate class in the file. So if button 1 is pressed it takes you to that class. I think I have done the basics but don't know about creating the menu.
package com.example.a.myapplication;
import android.content.Intent;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import android.content.Intent;
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.menu_main, menu);
return true;
super.onCreateOptionsMenu (menu);
MenuItem button = menu.add(0, 0, Menu.NONE, "MainActivity");
MenuItem button1 = menu.add(0, 1, Menu.NONE, "second");
MenuItem button2 = menu.add(0, 2, Menu.NONE, "third");
MenuItem button3 = menu.add(0, 3, Menu.NONE, "fourth");
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);
{
switch (item.getItemId())
{
case 0:
Intent i1 = new Intent(this, MainActivity.class);
startActivity (i1);
return true;
case 1:
Intent i2 = new Intent(this, second.class);
startActivity (i2);
return true;
case 2:
Intent i3=new Intent(this, third.class);
startActivity(i3);
return true;
case 3:
Intent i4=new Intent(this, forth.class);
startActivity(i4);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
}
Modify /res/menu/main.xml to define menu items
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_up"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:icon="#android:drawable/arrow_up_float"
android:title="Up"/>
<item
android:id="#+id/action_down"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:icon="#android:drawable/arrow_down_float"
android:title="Down"/>
<item
android:id="#+id/action_other"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_launcher"
android:title="Other"/>
In the main activity's onOptionsItemSelected, handle the menu items.
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) {
switch(item.getItemId()){
case R.id.action_settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity (intent);
break;
case R.id.action_up:
Intent intent = new Intent(this, SecondActivity.class);
startActivity (intent);
break;
case R.id.action_down:
Intent intent = new Intent(this, ThirdActivity.class);
startActivity (intent);
break;
case R.id.action_other:
Intent intent = new Intent(this, FourthActivity.class);
startActivity (intent);
break;
default:
super.onOptionsItemSelected(item);
break;
}
//Return false to allow normal menu processing to proceed,
//true to consume it here.
return false;
}
}
Related
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
i am trying to show some buttons in actionbar but they just dont show up
i have successfully implemented the navigation drawer and viewpager using fragments
the implementes for showing menu inflator and action bar buttons has also been done but they just wont show up
in menu/main.xml i have added app:showAsAction="always" but still it wont show up
here is the
detailactivity.java
package com.test.app;
import java.lang.reflect.Field;
import android.app.ActionBar;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
//import android.support.v7.app.ActionBar;
import com.Blog.gkgyan.parser.RSSFeed;
//public class DetailActivity extends FragmentActivity implements OnItemClickListener{
public class DetailActivity extends FragmentActivity implements OnItemClickListener{
RSSFeed feed;
int pos;
private DescAdapter adapter;
private ViewPager pager;
private DrawerLayout drawerLayout;
private ListView listView;
private String[] planets;
private ActionBarDrawerToggle drawerListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
//Drawer Layout
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListener = new ActionBarDrawerToggle(this, drawerLayout,R.drawable.ic_drawer,R.string.drawer_open, R.string.drawer_close){
#Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub
//super.onDrawerClosed(drawerView);
Toast.makeText(DetailActivity.this, "Drawer closed", Toast.LENGTH_LONG).show();
}
#Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
//super.onDrawerOpened(drawerView);
Toast.makeText(DetailActivity.this, "Drawer opened", Toast.LENGTH_LONG).show();
}
};
drawerLayout.setDrawerListener(drawerListener);
//getSupportActionBar().setHomeButtonEnabled(true);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);;
getActionBar().setDisplayHomeAsUpEnabled(true);
// try {
// ViewConfiguration config = ViewConfiguration.get(this);
// Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
// if (menuKeyField != null) {
// menuKeyField.setAccessible(true);
// menuKeyField.setBoolean(config, false);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) { e.printStackTrace(); }
}
listView = (ListView) findViewById(R.id.drawerlist);
planets = getResources().getStringArray(R.array.planets);
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, planets ));
listView.setOnItemClickListener(this);
// Get the feed object and the position from the Intent
feed = (RSSFeed) getIntent().getExtras().get("feed");
pos = getIntent().getExtras().getInt("pos");
// Initialize the views
adapter = new DescAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
// Set Adapter to pager:
pager.setAdapter(adapter);
pager.setCurrentItem(pos);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListener.onConfigurationChanged(newConfig);
}
#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;
//return super.onCreateOptionsMenu(menu);
}
//#Override
//public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (drawerListener.onOptionsItemSelected(item)) {
return true;
}
Intent i = null;
switch (item.getItemId()) {
case R.id.action_rate:
String webpage = "http://developer.android.com/index.html";
i = new Intent(Intent.ACTION_VIEW, Uri.parse(webpage));
startActivity(i);
//return true;
break;
case R.id.action_share:
i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_TEXT, "Hello from Hansel and Petal!");
i.setType("text/plain");
startActivity(i);
//return true;
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}
public class DescAdapter extends FragmentStatePagerAdapter {
public DescAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return feed.getItemCount();
}
#Override
public Fragment getItem(int position) {
DetailFragment frag = new DetailFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
bundle.putInt("pos", position);
frag.setArguments(bundle);
return frag;
}
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
if (position == 0) {
Intent intent = new Intent(this, GkcategoryListActivity.class);
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(this, GkcategoryListActivity.class);
startActivity(intent);
}
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
}
`
and 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="com.Blog.gkgyan.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_share"
android:icon="#drawable/ic_action_share"
android:orderInCategory="101"
android:title="#string/action_share"
app:showAsAction="always" />
<item
android:id="#+id/action_rate"
android:icon="#drawable/ic_action_important"
android:orderInCategory="101"
android:title="#string/action_rate"
app:showAsAction="always"/>
</menu>
i wan to show the action_share and Action_rate butons in action bar on detailactivity.java tried every thing nothing works on this page
however it does work perfectly fine in main activity.java
here is the code
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
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) {
// TODO Auto-generated method stub
Intent intent = null;
switch (item.getItemId()) {
case R.id.action_rate:
String webpage = "http://developer.android.com/index.html";
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webpage));
startActivity(intent);
break;
case R.id.action_share:
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hello from Hansel and Petal!");
intent.setType("text/plain");
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
public void latestActivity(View v){
//Intent intent = new Intent(this, SplashActivity.class);
Intent intent = new Intent(this, GkcategoryListActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
}
is it a issue with fragment activity or navigation drawer any help would be great please mention where is should make changes in my code
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);
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
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)]);