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.
}
});
}
}
Related
My problem is the following.
my app has a 1 welcome screen where the user ckick the "continue" button and it goes to next screen. The next one contains a menu with several buttons.
my problem is that I can not open another activity on the second screen (on the first screen it opens normal)
more or less this scheme below
(| activity1> button continue | >> | activity2> button continue2 |> does not respond)
to compliment and test apk on a galaxy grand duos 4.2.2
code below
code 1 screen (welcome).
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button button7 = (Button) findViewById(R.id.button7);
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main4);
}
});}}
code 2 tela
public class Main4Activity extends AppCompatActivity {
private Button prova;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
prova = (Button) findViewById(R.id.button5);
prova.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent( Main4Activity.this, Main3Activity.class);
startActivity(intent);
}
});
}}
2 tela code xml button
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button4"
android:layout_marginTop="11dp"
android:text="tela 2"/>
First, I want to make sure that you understand what you are writing.
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main4);
}
});
In your onclick, you only set your view of Main2Activity to screen of activity_main4.xml. But you are still standing in Main2Activity (It means you are in Main2Activity with view activity_main4.xml).
In this case, Main4Activity hasn't initialized and the button prova hasn't been initialized too. So when you press prova button, it won't do anything.
Second, to solve your problem, make Main4Activity be initialized, you must start it. So, instead of using:
setContentView(R.layout.activity_main4);
in Main2Activity, which only change the view, not the Activity. You should use
Intent intent = new Intent(Main2Activity.this, Main4Activity.class);
startActivity(intent);
Hope you can understand this!
Your problem is that you can't use setContentView(R.layout.activity_main4); to open another activity .You can use startActivity method to open another activity .
You can try this .
1.remove the code in your Main2Activity
setContentView(R.layout.activity_main4);
2.add change to this
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(Main2Activity.this,Main4Activity.this);
startActivity(i);
}
});}}
I am trying to make a button on my homepage of an app that will lead to a search page, that will have a handful more buttons leading to other pages. However, I used the same code from my activity main for the button in my second page (seachpage) and now when I run the code, my first button on my main page, when clicked it just shuts the app down. I don't even know how to approach this properly because I copied the same code, just changed the "findViewById" and the "startActivity" accordingly with their new labels. Any recommendation or help would be massively appreciated!
Activity main Java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button yourButton = (Button) findViewById(R.id.TranslateButton);
if (yourButton == null) throw new AssertionError();
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SearchPage.class));
}
});
}
}
Activity main xml for the button:
Secondary page (searchpage) Java code:
public class SearchPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_page);
Button accommodationButton = (Button) findViewById(R.id.accommodationButton);
if (accommodationButton == null) throw new AssertionError();
accommodationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(SearchPage.this, Accommodation.class));
}
});
}
}
Secondary page xml for the button:
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="#string/accommodation"
android:id="#+id/accommodationButton"
android:layout_below="#+id/search_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_weight="1"/>
Thank you again for taking the time and consideration to read and/or respond to my question~!
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
I have many buttons in my calculator app. I am testing with only one button to start, that buttons id is "one" and should change colour when I click the blue theme button. I have tried following methods:
blueTheme = (Button) findViewById(R.id.blueTheme);
blueTheme.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
one.setBackgroundColor(Color.argb(175, 144, 202, 249));
one.setBackgroundColor(Color.parseColor(/*hex code here*/));
one.setBackgroundColor(Color.BLUE);
}
});
Nothing seems to do anything. I am trying to change the colour of the button in one activity via an option in another activity. Here's actual button one:
one = (Button) findViewById(R.id.one);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.append("1");
}
});
xml code of one in activity_main.xml:
<Button android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#CCCCCC"
android:text="1"
android:textColor="#FF6600"
android:textSize="50sp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp" />
Idea is that there will be a option in another intent where I can change colors of calculator, but testing on one button fails, can't proceed. Thank you for your time.
The problem is the click from one activity cant get through to the other activity unless you pass it over.
In the activity with the blue theme button
blueTheme.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//NOTE: Where I've put MainActivity that should actually be the name
// of whatever activity this code is nested in
Intent intent = new Intent(MainActivity.this, OtherActivity.class); //use your real class name
intent.putExtra(OtherActivity.EXTRA_COLOR, Color.BLUE);
startActivity(intent);
}
});
In your OtherActivity.class
public class OtherActivity extends Activity {
public static String EXTRA_COLOR = "EXTRA_COLOR";
public void onCreate(...) {
View one = (Button) findViewById(R.id.one);
//NOTE: if you add singleTop to this activity in the manifest
// you might need to do this on onNewIntent
Intent intent = getIntent();
if (intent.hasExtra(EXTRA_COLOR)) {
int color = intent.getIntExtra(EXTRA_COLOR, Color.WHITE);
one.setBackgroundColor(color);
}
}
}
Use this :
// If you're in an activity:
yourButton.setBackgroundColor(getResources().getColor(R.color.red));
// OR, if you're not:
yourButton.setBackgroundColor(yourButton.getContext().getResources().getColor(R.color.red));
If you want to set background color without using a pre-defined color resource, do it like so
one.setBackgroundColor(0xFFFF0000); // Red
one.setBackgroundColor(0xFF00FF00); // Green
Here, 0xFF00FF00 is equivalent to #ff00ff00 (#aarrggbb)
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? ;)