Android click on the button to go to another page? - java

I need to link the button with the page (not the main page) like when I click on the button to go to the page for example (location page)?
private void setupLocationPageButton() {
Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
LocationPageButton.setOnClickListener(new View.OnClickListener()

If your goal is to open another activity this is what you are going to want to do.
In your xml file you are going to want something like this
...
<Button
android:id="#+id/activity_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClicked"
android:text="#string/text_of_button" />
...
Then in your activity you want
public void onButtonClicked(View view) {
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
}

what i think you trying to do: is when you click on button it's change the MainActivity
Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
LocationPageButton.setOnClickListener(new View.OnClickListener({
public void onClick(View _view) {
Intent i = new Intent(MainActivity.this,TheActivityTheclassNameYouWannGoTo.class);
startActivity(i);
}
}));
but first you have to creat activity and class inherit Activity like MainActivity
initialize the class in AndroidMainfest.xml
i hope this help you

You can use fragment, each fragment contains a specific page, see this for Android fragment.

Related

Simple Button click navigate to another activity xml page

I'm a beginner with Android development and want to create a simple onClick event to bring me to a blank activity page. I have named my new activity page InsurancePage and added a button called insurance to my xml file. When I click on this button I want to to bring me to the InsurancePage.
<Button
android:id="#+id/insurance"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="46.24"
android:background="#drawable/curvebutton"
android:lines="1"
android:text="#string/insurance"
android:textColor="#ffffff"
android:onClick="onClickbtnInsurance" />
To add on click event to button you can look at the next link:
how to add button click event in android studio or android eclipse button OnClick event
To start new activity on button click you can look at the next link:
How to start new activity on button click
Next, you can just search in Google before :)
Let's say that you have a button called myButton and this is in you MainActivity class:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(Bundle savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = findViewById(R.id.new_button);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, InsurancePage.class));
}
}
You can read more about activities here : http://developer.android.com/training/basics/firstapp/starting-activity.html

How do I get a button to open another activity?

I've added a button to my activity XML file and I can't get it to open my other activity. Can some please tell me step by step on how to do this?
A. Make sure your other activity is declared in manifest:
<activity
android:name="MyOtherActivity"
android:label="#string/app_name">
</activity>
All activities must be declared in manifest, even if they do not have an intent filter assigned to them.
B. In your MainActivity do something like this:
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
}
});
Using an OnClickListener
Inside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity.
Button yourButton = (Button) findViewById(R.id.your_buttons_id);
yourButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}
});
This is probably most developers preferred method. However, there is a common alternative.
Using onClick in XML
Alternatively you can use the android:onClick="yourMethodName" to declare the method name in your Activity which is called when you click your Button, and then declare your method like so;
public void yourMethodName(View v){
startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}
Also, don't forget to declare your new Activity in your manifest.xml. I hope this helps.
References;
Starting Another Activity (Official API Guide)
If you declared your button in the xml file similar to this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next activity"
android:onClick="goToActivity2"
/>
then you can use it to change the activity by putting this at the java file:
public void goToActivity2 (View view){
Intent intent = new Intent (this, Main2Activity.class);
startActivity(intent);
}
Note that my second activity is called "Main2Activity"
Button T=(Button)findViewById(R.id.button_timer);
T.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),YOURACTIVITY.class);
startActivity(i);
}
});
Write code on xml file.
<Button android:width="wrap_content"
android:height="wrap_content"
android:id="#+id/button"
android:text="Click"/>
Write Code in your java file
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(getApplicationContext(),Secondclass.class));
/* if you want to finish the first activity then just call
finish(); */
}
});
use the following code to have a button, in android studio, open an already existing activity.
Button StartButton = (Button) findViewById(R.id.YOUR BUTTONS ID GOES HERE);
StartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, YOUR ACTIVITY'S ID GOES HERE.class));
}
});
I did the same that user9876226 answered.
The only differemce is, that I don't usually use the onClickListener. Instead I write following in the xml-file: android:onClick="open"
open is the function, that is bound to the button.
Then just create the function open() in your activity class. When you click on the button, this function will be called :)
Also, I think this way is more confortable than using the listener.
Apply the following steps:
insert new layout xml in folder layout
rename window2
add new button and add this line: android:onClick="window2"
mainactivity.java
public void openWindow2(View v) {
//call window2
setContentView(R.layout.window2);
}
}
Use following steps to add the new activity (Manifest file will be automatically update)
File > New > Activity > Empty Activity
In your MainActivity.java file add the following code inside protected void onCreate(Bundle savedInstanceState).
Make sure you call finish(); function at the end. So when you tap the Back button, it will not go back to the previous activity.
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
finish();
}
});

making a textview viewable when clicked

i was wondering how can i make a textview viewable where the user can click on it and when he click on it the textview creates another page where the text is by her own and the user can select the textview now, because i have more than 30 textviews and i want to make each of them when they get clicked they have their own layout where user can select them now and add more stuff like sharing the textview and this is all in one layout for each textview alone
i hope you can help me and thanks in advance
Make a new activity and use put/get extra to pass on data to new activity.
try following
http://developer.android.com/training/basics/firstapp/index.html
you should create list view
and in on item click of the list you can simply start another activity which will have layout according to the item you have clicked.
click here
and there onitemclick you can start new activity
Intent intent = new Intent(this, NewActivity.class);
intent.purStringExtra("key",rowItems.get(position));
startActivity(intent);
try it and ask if you have any doubt or it is not clear
You can use this code to make your TextView visible on click event
myTextView.setVisibility(View.VISIBLE);
try using Buttons instead of TextViews for getting TextViews...
example
you have Buttons "A" and "B" and want to show Button "B" only when when A Clicked and then want to show TextView "C" only when when B Clicked
in this way (A->B->C )
then try using these codes:
only important parts are written...
in activity_main.xml :
<Button
android:id="#+id/button_A"
android:onClick="do_A"/>
<Button
android:id="#+id/button_B"
android:onClick="do_B"/>
<TextView
android:id="#+id/Textview_C" />
in MainActivity.java :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button A = findViewById(R.id.button_A);
Button B = findViewById(R.id.button_B);
TextView C = findViewById(R.id.Textview_C);
B.setVisibility(View.GONE);
C.setVisibility(View.GONE);
}
public void do_A (View v){
B.setVisibility(View.VISIBLE);
}
public void do_B (View v){
C.setVisibility(View.VISIBLE);
}
}

Defining destination Activity for a Button

I found this code very usefull to get from one activity to the other but the problem is, that I don't see where the destination is mentioned. I would really appreciate it if some one pointed out how to change the destination.
Here is the code:
Button getTaxi = (Button) findViewById(R.id.GetTaxi);
getTaxi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
final Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
The respective part in the xml:
<Button
android:id="#+id/GetTaxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="GetTaxi" >
</Button>
Thank you very much in advance!
Actually, the code that executes on the Button's click states that your Activity (which is a sub-activity here by the way) has done its job and now finishes with the result code RESULT_OK. It means that there was another Activity that has started this actual Activity for some kind of result. So, when you'll click the Button your Activity will finish. To start some other Activity on the Button's click you should create an Intent, specifying explicitly the Activity you want to start, or just the action you want to perform over some data, letting the Android resolve the final Activity for you. Then you should call startActivity(), passing in the Intent you've created. Hope this helps.
You can use something like this:
Button getTaxi = (Button) findViewById(R.id.GetTaxi);
getTaxi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
Intent intent = new Intent();
intent.setClass(this, GetTaxiActivity.class);
startActivity(intent);
//call finish() if the current activity is something like a loading page
}
});
The code piece above which you mentioned is a sub-activity which is called using
StartActivityForResult();

Creating a menu but one of the two buttons does not properly work

public class SuperActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button registerButton = (Button) findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(SuperActivity.this, Register.class);
startActivity(myIntent);
}
});
Button loginButton = (Button) findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(SuperActivity.this, Login.class);
startActivity(myIntent);
}
});
}
}
My register buttons works but not the login button. Is something wrong with my code?
The code posted looks fine. What exactly doesn't work? Do you get an error?
Since you mention it works for 'register' but not for 'login', you may want to double check if you didn't forget to add Login to your manifest.
Is button with id login_button declared in main layout? If I had to guess this button is not declared in main layout is declared somewhere else.
double check if button with id login_button declared in main layout
Since you are not posting your logcat view,it's very difficult to see where the problem this.I'm suggesting you few things which you should make sure that are inplace as said by me.If not then please correct them and let me know if that solved your problem or not.
Firstly,you should make sure that you have declared a button with same id in your main.xml file.It will look something like this:
<button android:id="#+id/login_button" >
</button>
Secondly you should make sure that you have declared your Login.class in your AndroidManifest.xml file.It will look something like this:
<application>
<activity android:name=".Login"></activity>
</application>
Check if these things are in place or not.
Have you declared all the activities? ;)

Categories