I have this piece of code to start a new activity in my android test app
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnpriv = (Button)findViewById(R.id.btn_priv);
btnpriv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), PrivateActivity.class);
startActivity(i);
}
});
This works as it should. At button press it changes to the new PrivateActivity. But it is a no-animation change. How can I make it f.x. slidein the new activity to have a nice transition?
Related
for example i have made a Nodemcu server and there are 3 links on the main webpage(192.168.18.100).
the thing i want to try is that when i click an android button this link get accessed without opening any new activity i.e i want to stay on the main activity.
this is my code so far that works but not according to what i want it.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button on = (Button) findViewById(R.id.button);
Button off =(Button) findViewById(R.id.button2);
Button pink = (Button) findViewById(R.id.button3);
on.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16236607"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_SEND, uri);
startActivity(intent);
}
});
off.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16203967"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
pink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16214167"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
You can try to implement a WebView on your MainActivity. When you click on any of your button, load the WebView with your desired url.
You can find the way to implement WebView in this link
I created a project that use RecyclerView and CardView (for PointOfInterest). These 5 activities are relate to each other :
PointOfInterest.java
PlacesAdapter.java
Places.java
layout_poi.xml
activity_point_of_interest.xml
Meanwhile in activity_main.xml I design the Main Menu together with some buttons. One of the button named Rapid Penang (id: rapid_btn). I call an activity of Rapid Penang (from MainActivity.java) like below:
public class MainActivity extends AppCompatActivity {
private Button button_for_rapid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Rapid Penang class
button_for_rapid = (Button) findViewById(R.id.rapid_btn);
button_for_rapid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openRapid();
}
});
}
public void openRapid()
{
Intent intent_rapid = new Intent(this, RapidPenang.class);
startActivity(intent_rapid);
}
}
RapidPenang consist of only one activity and it is success. But when I try to do exactly the same to PointOfInterest activites (as mention above), suddenly the app were crashed.
This is how I try to open PointOfInterest activites from a button in MainMenu called Point Of Interest:
private Button button_for_poi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Point Of Interest class
button_for_poi = (Button) findViewById(R.id.poi_btn);
button_for_poi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openPOI();
}
});
}
public void openPOI()
{
Intent intent_poi = new Intent(this, PointOfInterest.class);
Intent intent_poi2 = new Intent(this, PlacesAdapter.class);
Intent intent_poi3 = new Intent(this, Places.class);
startActivity(intent_poi);
}
Firstly check your activity is define in Android manifest file and then call
StartActivity(new Intent(getApplicationcontext(),RapidPenang.class));
That's it
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSighin = (Button) findViewById(R.id.btnSignIn);
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent j = new Intent(MainActivity.this, SignUp.class);
startActivity(SignUp);
}
});
btnSighin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent k = new Intent(MainActivity.this, SignIn.class);
startActivity(SignIn);
}
});
}
I am working on an app that allows customers to book appointments and in order to do so, they need to sign up or sign in. I have activities made for both of them and I can't run it without getting the "Expression Expected" and I'm new to Android Studio and have no idea what to do. Any help?
You are missing some information. Anyway, i see that you have mistakes in lines
startActivity(SignUp);
and
startActivity(SignIn);
The correct way should be startActivity(j) and startActivity(k) respectively
You have to pass the intent object to the start activity method not the signup or signin class
So it should be startActivity(i) and startActivity(j)
Probably something I do not understand.
In the application programs (from google play), if we move from the main activity to the second and then back to the main, then after pressing the "back" button on the phone, the application closes.
I tried to make my own applications with two activities, but it did not work as it should. When the main activity goes to the second and then I go back to the main, then after pressing the "back" button on the phone, the application instead of closing, it goes back to the second activity, then back to the main and it just closes.
What am I doing wrong ?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Act2Butt = (Button) findViewById(R.id.Act2Butt);
Act2Butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
}
.
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button Act1Butt = (Button) findViewById(R.id.Act1Butt);
Act1Butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, MainActivity.class );
startActivity(intent);
}
});
}
}
To go back to MainActivity from Main2Activity, you need to call onBackPressed() or finish(), instead of startActivity.
When I click on the rate design button it stops, I doubt is my manifest because I have check it already below are my codes. I have also enter the error log. Can someone help me instead of rating down for this question
Your forget to add
setContentView(R.layout.yourlayout);
before Button initialization in onCreate(....)
Correct:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
Button btnRating = (Button) findViewById(R.id.rd);
btnRating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(NearablesDemoActivity.this, MainActivityRating.class);
startActivity(intent);
}
});
}