[Q] Help with starting an activity - Java for Android App Development

Could you help. I am trying to make an app that when a button is pressed it opens a certain activity in one app, a different one. How would you make that. I am stuck. It can be hard coded. I am not home now but later I will post the code I have so far. Thanks.

JonanomisK said:
Could you help. I am trying to make an app that when a button is pressed it opens a certain activity in one app, a different one. How would you make that. I am stuck. It can be hard coded. I am not home now but later I will post the code I have so far. Thanks.
Click to expand...
Click to collapse
Code:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context, NewActivity.class));
}
});
Simples

Jonny said:
Code:
Button button = (button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context, NewActivity.class));
}
});
Simples
Click to expand...
Click to collapse
Thanks! I'll try implementing that later.

Related

I need help making a longpress Context menu

Basiclly I need help making it so people can longpress on a button and choose to set as ringtone or notfication tone.
here is some of my code I use so far for the sound:
Code:
MediaPlayer mpButtonClick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up the button sounds
mpButtonClick = MediaPlayer.create(this, R.raw.money);
Button bmoney = (Button) findViewById(R.id.money);
bmoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick.start();
}
});
View.setOnLongClickListener is all you need. As you should know Button extends TextView, which in turn extends View, so you can use that method on a Button too
martino2k6 said:
View.setOnLongClickListener is all you need. As you should know Button extends TextView, which in turn extends View, so you can use that method on a Button too
Click to expand...
Click to collapse
sorry but still kind of confused because I added the following before you mentioned this and got a force close:
Code:
Button money = (Button) findViewById(R.id.money);
Button registerForContextMenu;
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Action 1");
menu.add(0, v.getId(), 0, "Action 2");
}
Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it
I am wondering what did I do wrong now? Here is the code I used:
Code:
Button SoundButton1 = (Button) findViewById(R.id.money);
registerForContextMenu(SoundButton1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();
}
I hate to bump an old thread but I am trying to add notifications and ringtones to a soundboard, I was able to successfully use the example here:
http://stackoverflow.com/questions/...ode-only-setting-first-button-as-ringtone-etc
but I have only been able to get it to save the audio from the first button. Could anyone point out how to get it working for each sound rather than just the first?
I would not rely on "==" returning what you think you want....use .equals() instead.
And I would probably just call one method and pass in the item id and let that method handle your processing. function1 and function2 are redundant.
I wonder how exactly you thought that just showing a toast with the Ringtone id will save it in device settings. in function1() and function2() you must add handlers to get resource from the id, set that stream as the desired ringtone, notification.
vamp6x6x6x said:
Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it
I am wondering what did I do wrong now? Here is the code I used:
Code:
Button SoundButton1 = (Button) findViewById(R.id.money);
registerForContextMenu(SoundButton1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();
}
Click to expand...
Click to collapse

Switching to and from layouts using buttons

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button display = (Button) findViewById(R.id.button);
TextView tv = (TextView) findViewById(R.id.textView);
display.setOnClickListener( new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main2);
// TODO: Implement this method
}
});
Button ba = (Button) findViewById(R.id.buttonBac);
ba.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main);
// TODO: Implement this method
}
});
}
}
Anyway, the first button works and when clicked, displays main2 makin 2 whoum d have a textview and a button that switches back 2 main but when the onclick listener whTever is aplyed to the code it wont even disllag the first layout and simply crashes... hitting the wall again
Focusedrelaxaation87 said:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button display = (Button) findViewById(R.id.button);
TextView tv = (TextView) findViewById(R.id.textView);
display.setOnClickListener( new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main2);
// TODO: Implement this method
}
});
Button ba = (Button) findViewById(R.id.buttonBac);
ba.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main);
// TODO: Implement this method
}
});
}
}
Anyway, the first button works and when clicked, displays main2 makin 2 whoum d have a textview and a button that switches back 2 main but when the onclick listener whTever is aplyed to the code it wont even disllag the first layout and simply crashes... hitting the wall again
Click to expand...
Click to collapse
You can't do a second setContentView call in an activity. What you can do is define two Layouts within a LinearLayout in the XML, set the first one to "android:visibility="gone"", the second one to "visible" and switch between them onClick
Code:
[user=439709]@override[/user]
public void onClick(View v) {
switch (v.getId()){
case R.id.btnOne:
mLayoutFirst.setVisibility(View.GONE);
mLayoutSecond.setVisibility(View.VISIBLE);
break;
case (R.id.btnTwo):
mLayoutFirst.setVisibility(View.VISIBLE);
mLayoutSecond.setVisibility(View.GONE);
break;
}
}
Zatta said:
You can't do a second setContentView call in an activity. What you can do is define two Layouts within a LinearLayout in the XML, set the first one to "android:visibility="gone"", the second one to "visible" and switch between them onClick
Click to expand...
Click to collapse
Alternatively you could use a ViewSwitcher and call it's .next() and .previous() methods. This would probably be necessary when you have more than 2 views.
octobclrnts said:
Alternatively you could use a ViewSwitcher and call it's .next() and .previous() methods. This would probably be necessary when you have more than 2 views.
Click to expand...
Click to collapse
Thanks, never heard of before. I use that method for hiding/unhiding complete ViewGroups, Buttons and what not but I'll look into that, might be more easy.
Try naming the activity other than the "main" attribute, that is only for the first or "parent" activity. You need it to be a child activity.
Sent from my HUAWEI-M835 using xda app-developers app
Cool, learned something new

Need some help with preference manager please.

Alright guys, first app here. I hit a bump in the road, can someone tell me how to use preference manager to save and restore my switch states?
A snippet of my code,
Code:
public class MainActivity extends Activity implements View.OnClickListener {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PreferenceManager.getDefaultSharedPreferences(this).getBoolean("sw1",false);
}
public void onClick(View v) {}
public void sw1(View view) {
// Is the view now checked?
boolean checked = ((Switch) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.sw1:
if (checked) try {
//Toast.makeText(this, "SW1 Checked", Toast.LENGTH_SHORT).show();
Runtime.getRuntime().exec(new String[] { "su","-c","mod1" });
PreferenceManager .getDefaultSharedPreferences(this).edit().putBoolean("sw1",true).commit();
} catch (IOException e) {
e.printStackTrace();
}
else try {
//Toast.makeText(this, "SW1 NOT Checked", Toast.LENGTH_SHORT).show();
Runtime.getRuntime().exec(new String[] { "su","-c","mod1b" });
PreferenceManager .getDefaultSharedPreferences(this).edit().putBoolean("sw1",false).commit();
} catch (IOException e) {
e.printStackTrace();
break;}
}
}
tmlhodge said:
Alright guys, first app here. I hit a bump in the road, can someone tell me how to use preference manager to save and restore my switch states?
Click to expand...
Click to collapse
Try this:
Code:
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean checked = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("sw1",false);
Switch mSwitch = (Switch )findViewById(R.id.sw1);
mSwitch.setChecked(checked);
}
I'm not sure where you setup your click/check listeners.. If you could provide some more details, that would be great.
Hope this helps!
alobo said:
Try this:
Code:
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean checked = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("sw1",false);
Switch mSwitch = (Switch )findViewById(R.id.sw1);
mSwitch.setChecked(checked);
}
I'm not sure where you setup your click/check listeners.. If you could provide some more details, that would be great.
Hope this helps!
Click to expand...
Click to collapse
Alright, the on click listener is right before the list of buttons, you can actually see it in the code in the OP.
Also I am starting to get excited because that actually made my switch turn on! But now it's just on every time I turn it off and exit app..
Sent from my HTC One using Tapatalk HD
That got me on the right track and I figured the rest out! Thank you very much good sir/mam @alobo

multiple buttons to send predefined SMS

Hi guys i am new to programming. I am trying to have multiple button to send different predefined SMS to predefined number. I am not sure how to have multiple setOnClickListener(new OnClickListener() as the 2nd setOnClickListener(new OnClickListener() gave me error.
public class SendSMSActivity extends Activity {
Button buttonSend;
Button buttonSend2;
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend2 = (Button) findViewById(R.id.buttonSend2);
buttonSend.setOnClickListener(new OnClickListener() {
buttonSend2.setOnClickListener(new OnClickListener() {
@override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonSend:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "abc");
sendIntent.putExtra("address", "9909990");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
break;
case R.id.buttonSend2:
Intent sendIntent1 = new Intent(Intent.ACTION_VIEW);
sendIntent1.putExtra("sms_body", "def");
sendIntent1.putExtra("address", "012345678");
sendIntent1.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent1);
break;
}
}
});
});
}
}
@stewypost
You cant write statements anywhere inside an anonymous inner class anyways ignoring the poor syntax
To do this first declare your
OnClickListner listner = (View v) ->
{
// your code
};
then call
button1.setOnClickListener(listner);
button2.setOnClickListener(listner);
Sent from my GT-S5302 using Tapatalk 2

changing from a window to another

Hi Im just starting to learn android code and I dont understand why this doesnt work. Just for as a test I wanted to create 2 pages (or 1 page and 1 popup page but i thought 2 pages would be easier to make), and a way to get from one to the other, which I thought could be solved easly with a button on each page which takes me to the other page.
So I started with this:
HTML:
final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
setContentView(R.layout.layout2);
} });
Basicually I start in a layout called activity_my which has a button and when I press it I get to the next page called layout2. I thought I could just make a similiar function to get back to my original page:
HTML:
final Button button2 = (Button) findViewById(R.id.button);
button2.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
setContentView(R.layout.activity_my);
}
});
But now I get error for whatever reason
Hi,
Basically in android, a 'page' is an Activity (class where you attach a XML layout).
So you must create 2 activity with 2 layout.
To switch activity use this foloowing code :
HTML:
Intent intent = new Intent(this, YourSecondActivity.class);
startActivity(intent);

Categories

Resources