On click interraction between screens android - android

I have created 2 main activity.java files in my source file.
In the first one i make the first screen appear and when i click a button i want to go
to another activity in order to get the second screen appear.
That's why i have created my MainActivity2.java file that is triggered when i click a button.
Although i have no compile errors, when i run my app it stops in the simulator and it does not even show the first screen. The code is the following :
MainActivity.java :
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button imageButton1 = (Button) findViewById(R.id.imageButton1);
imageButton1.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this, MainActivity2.class);
startActivity(i);
}
}
and for
MainActivity2.java :
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
, where activity_main is my first .xml file containing the forst screen and imageButton1 is
the button i have created in my xml file that i want to go to the MainActivity2, when clicked and trigger the second activity in order to show the second screen. And activity_main_2.xml contains the second screen.
Thanks in advance.

First of all, have you declared both of the activities in the manifest file of the application? That might be the reason why the application force closes.
Secondly,
In the onClick method you should check if that button has actually been clicked by checking for the id of the button.
public void onClick(View v) {
if(v.getId()==R.id._idofImageButton_){
Intent i=new Intent(MainActivity.this, MainActivity2.class);
startActivity(i);
}
}
Hope this helps.

First of all check if you have added second Activity to AndroidManifest file. If you did, then please, provide stack trace.

Related

How can i move to any other activity with jfeinstein slider library

I have implemented jfeinstein10 slider menu library in my application. With this piece of code I am successfully able to implement slider in my app.
Now my question is how can I move to next activity using this slider? The following image shows how my slider looks like:
So basically I want to move to next activity or any other activity when I click on any of the options from slider. Like for example, when I click on Comment or Post or Chat or any other option I want to go to the relevant screen. Hope this is clear. Still if you need more explanation you can ask.
Following is my code snippet.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.5f);
menu.attachToActivity(MainActivity.this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.activity_menu);
Button mButton = (Button) findViewById(R.id.slidingMenu);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
menu.showMenu();
}
});
}
#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;
}
}
You can define the onclick listenrs to the Menu layout that you have appended to the Main Activity like so:
menu.getMenu().findViewById(R.id.yourSideMenuOption).setOnClickLister(this);
Its advisable to use an abstract activity, so that you are not using the same code over and over in each of your activity.

Options menu persists in the second activity

After clicking a button to open a new Activity using this method:
setContentView(R.layout.activity_comunidades01);
The second Activity still display the same menu as the previous one.
I have already readed serval methods to fix it as the one related in here:
Android: How to enable/disable option menu item on button click?
But I discovered that the methods to intialize and create the menu are never called. I even try to follow this other link without success:
onCreateOptionsMenu is never called
I even deleted all the items in the menu.xml for this activity but still displaying the previous activity options.
I also clarify I am using android 4.4 as target API but level 10 as a minimum one since some devices that will be used are running android 2.3.
My second Activity is like this:
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_SecondActivity);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
System.out.println("EN ON PREPARE OPTIONS MENU");
(menu.findItem(R.id.sincronizar)).setEnabled(false);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
System.out.println("EN ON CREATE OPTIONS MENU");
getMenuInflater().inflate(R.menu.SecondActivity, menu);
return true;
}
}
To start second activity use this:
Button bt = (Button)findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
//finish(); //if you want to close FirstActivity after showing SecondActivity
}
});
After clicking a button to open a new Activity using this method:
setContentView(R.layout.activity_comunidades01)
This is not how you open another activity!
This way you only change the content of the current activity, nothing else.
Use Activity.startActivity() or Activity.startActivityForResult() as described in the docs to start another activity.

Android layout loaded twice

On button click event I'm using setContentView(R.layout.activity_main); it works correctly.
When if I try to start new activity with Intent and startactivity commands it loads layout twice it looks like layout loading correctly then 1 second same layout loaded again.
Before start activity its loaded single time.
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
setContentView(R.layout.activity_main);
startActivity(intent);
}
}
});
I'm guessing this line is the problem.-
setContentView(R.layout.activity_main);
setContentView will just change the layout for the current activity, so you're changing the current layout to activity_main, and then you open the Intent for MainActivity class.
Just remove that line.
When you are starting a new activity, there is no need of setContentView while starting the intent.
The intent which is getting started will have the code for loading the layout. So please remove this line.
I hope, in your MainActivity.class, you will already be writing setContentView(R.layout.activity_main) and this is enough for showing the required layout. So remove this extra line which you have included while starting the intent.
You can not set your second activity layout in your first activity before starting your second activity. It will set automatically in your second activity's onCreate() method. So you should write setContentView(R.layout.activity_main); in your MainActivity's onCreate() method. Just remove it from the onClick listener.
So write in your onClick as below:
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
startActivity(intent);
}
}
});
And in your MainActivity you have to set your layout as below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Shortcut to settings android

I'm interested about how is possible create a shortcut to different items of settings in android devices.
Specifically to advances settings of mobile data.
I want to do an app that allows user a quick access to switch menu 2g-3g.
I know that the app dosen't do switch, I only want the shorcut to this menu.
I don't know how It's made!
I'm very glad if somebody help me!
Thanks a lot!!!
public class MainActivity extends Activity implements OnClickListener
{
Button boton;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton = (Button) findViewById(R.id.boton);
boton.setOnClickListener(this);
}
#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 void onClick(View v)
{
if(v.getId() == R.id.boton)
{
startActivity(new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS));
}
}
}
I do this code by the explanation of Seraphim but dosen't work.
To switch to the 2G/3G settings page use this code inside the Button click handler:
startActivity(new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS));
or others, look at:
http://developer.android.com/reference/android/provider/Settings.html
Other interesting are:
android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS
android.provider.Settings.ACTION_NETWORK_OPERATOR_SETTINGS
android.provider.Settings.ACTION_WIRELESS_SETTINGS
android.provider.Settings.ACTION_APN_SETTINGS
i would like to suggest u that refer this three link(FIRST,SECOND,THIRD) and u would be able to change most of the settings by ur own . ANDROID SETTINGS mostly stores there respective values to this three tables.
If you want to enable or disable GPS than use
Settings.Secure.setLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER, true);

Android load multiple menu layout files possible?

I want to have a superclass activity that defines what basic menu items each activity should have in addition to their own.
Here is my code so far:
public abstract class SuperActivity extends FragmentActivity {
protected List<TextView> textView = new ArrayList<TextView>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentResource());
setTextViews(textView);
}
protected abstract void setTextViews(List<TextView> textViews);
protected abstract int getContentResource();
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menuitemsforallsubclasses, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.basicmenuitem1:
allSubclassActivitiesCanDoThis()
return true;
case R.id.basicmenuitem2:
allSubclassActivitiesCanDoThis2()
return true;
}
}
}
In the subclasses I would then just add something to onCreateOptionsMenu (an additional layout file) and onOptionsItemSelected, is that correct? Just want to make sure I am not on the wrong track here. Would it even be possible to load multiple layout file in the same activity?
FYI: In case you are wondering why I have a TextView array in the activity: In addition to this I want to have all TextViews of each subclass activity in an ArrayList, so that I can add listeners to all of them automatically so I know when something has changed in the activity or to do some other neat stuff.
Is what I am doing possible? Is it possible to load multiple menu layout files?

Categories

Resources