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
Related
So, I want a button like this in my app:
I don't know how to add code for button in the android-studio, and I only have the scanner code. I don't know how to put 2 function in the same project. What should I do? Can someone help me?
It sounds like you need to add a function to handle the OnClick event. The Android documentation has a nice example on how to set and OnClick listener:
<Button
android:id="#+id/button_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct" />
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
// This should be where you add your button logic.
}
});
}
}
I am trying to develop an android app but I have some problems. I know a little bit about Java but I'm perfect with the XML part. I made an imagebutton and I want a browser to open a certain url when the user clicks on that button. Could someone please explain me how to do this step by step?
This is the button code taken from my main_activity.xml
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/donate"
android:src="#drawable/donate"
android:text="#string/about_link"
android:autoLink="all"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true" />
Please remember that im not very good at Java so I will just copy and paste to my activitymain.java
Use this on imgebutton click listner
webview=(WebView)findViewById(R.id.webView1);
webview.loadUrl("http://www.google.com");
You could use something like that:
//any code
private ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//Only if you will use for something else
imageButton = (ImageButton) findViewById(R.id.donate);
//any code
}
// any code
private void callBrowser(View view) {
String url = "http://www.example.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
//any code
And inside your xml add this attribute:
android:onClick="callBrowser"
I am developing an android app and in that i want to hyperlink the youtube video by button so that when i click that button a particular url will open which i provide to it. I want to build many buttons so also tell how to hyperlink a button with another button.
Help me out.
You can set an onClickListener on your button which will start the Intent:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com/"));
startActivity(i);
}
});
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.
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);
}
}