In an android game app I have the splash screen layout with start, resume and exit buttons. From the start button I go to a surfaceview and start a worker thread from there.
I want to implement the device back button properly. When the user touches the back button, I come back to the splash screen menu. The problem is this: when I return to the splash screen, the start, resume and exit buttons don't react from user interactions anymore. It seems like they loosed focus.
How can I set the Splash Screen to have focus again?
public class SplashScreen extends Activity {
...
...
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen_layout);
this.mRoadView = (RoadView) findViewById(R.id.road_view);
this.mStartButton = (Button) findViewById(R.id.start_button);
this.mResumeButton = (Button) findViewById(R.id.resume_button);
this.mExitButton = (Button) findViewById(R.id.exit_button);
prepareButtonListeners();
}
#Override
public void onBackPressed() {
if (mDoubleBackPressed) {
super.onBackPressed();
} else {
setContentView(R.layout.splash_screen_layout);
mDoubleBackPressed = true;
}
}
private void prepareButtonListeners() {
this.mStartButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mDoubleBackPressed = false;
setContentView(R.layout.road_view_layout);
}
});
...
...
...
Thank you very much,
Daniel.
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK);
view.dispatchTouchEvent(downEvent);
view.dispatchTouchEvent(upEvent);
Related
I'm working on a kotlin app, My activity is in fullscreen and I want to make a button in which the screen can't be exit or back or anything unless the button is clicked. Like the one in the video player lock type.
My first idea was to make boolean isLocked and after clicking button change this value to the opposite and override all functions/events etc which You would like to lock and if isLocked is true just don't execute them.
Here is a simple code where I locked back button (in java but it can be easily changed to Kotlin)
public class MainActivity extends AppCompatActivity
{
boolean isLocked = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
isLocked ^= true;
System.out.println(isLocked);
}
});
}
#Override
public void onBackPressed()
{
if (!isLocked)
{
super.onBackPressed();
}
}
}
I think that in a similar way You can lock every event.
You can also make something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (!isLocked)
{
return super.onKeyDown(keyCode, event);
}
else
{
return true;
}
}
This will cancel every event like clicking back button or changing volume
I am adding a Button to my first Wear OS application. When I follow a model from
my existing Android applications, there appears to be difference due to
"WearableActivity" versus "Activity". I cannot define the OnClickListener.
In my on create is this:
bottomButton = findViewById(R.id.bottomButton);
setListener();
later in the main activity source is this
void setListener()
{
bottomButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) // toggle between report and graph display
{ if (showGraph)
{showReport=true; showGraph=false;bottomButton.setText("show Report");}
else
{showReport=false;showGraph=true;bottomButton.setText("show Graph");}
} // end, onClick
}); // end, setOnClickListener
} // end, setListener()
In both cases the button is defined in XML and found and used as follows
OkButton = (Button) findViewById(R.id.OkButton);
OkButton.setOnClickListener(this);
it does compile anyway. And I can access the items in the button such as text. But when I change the text, it doesn't appear on the screen. And when I push the button in the emulator, it doesn't register any action.
The problem is not from your code but from the Android wear emulator. Your wear emulator is in the ambient(low power) mode. Click in the top of the emulator window to toggle between interactive(full power) and ambient modes(low power).
How can I define and use a button in the wear os application.
The same way it is done on the mobile side. Below is an example
public class MainActivity extends WearableActivity implements View.OnClickListener {
Button clickMeButton;
TextView textView;
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAmbientEnabled();
clickMeButton = findViewById(R.id.click_me_button);
textView = findViewById(R.id.test_textview);
clickMeButton.setOnClickListener(this);
}
#Override
public void onClick(View view) {
count++;
textView.setText("I am clicked: "+count);
}
#Override
public void onEnterAmbient(Bundle ambientDetails) {
// Handle entering ambient mode
super.onEnterAmbient(ambientDetails);
Log.e("Hello", "I'm ambient");
}
#Override
public void onExitAmbient() {
// Handle exiting ambient mode
super.onExitAmbient();
Log.e("Hello", "exit ambient");
}
#Override
public void onUpdateAmbient() {
// Update the content
super.onUpdateAmbient();
Log.e("Hello", "update ambient: " + isAmbient());
}
}
Below is the results of the above activity (NB: Click in the top of the emulator window to toggle between interactive and ambient modes):
EDIT
To avoid implementing View.OnClickListener in your activity you can pass an instance of View.OnClickListener when calling setOnClickListener for the OkButton like you are already doing for the bottomButton.
OkButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something when OkButton is clicked
}
});
This is the first time I'm ever dabbling in Android development so please bear with me.
My requirement is this:
I have two buttons on screen, A and B. If the user presses both buttons (order doesn't matter), I need another page to be displayed. Pressing either A or B should do nothing.
Is this possible? If so, how would I achieve this?
Thank you.
This is possible if you take a flag. (boolean)
You should set a flag in your button listeners.
public class Mtest extends Activity {
Button b1;
Button b2;
boolean flag_1 = false;
boolean flag_2 = false;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler1);
b2.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
flag_1 = true;
doSomething();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
flag_2 = true;
doSomething();
}
};
}
public void doSomething(){
if(flag_1 && flag_2)
{
//DO SOMETHING
}
}
}
Create two boolean's like button1isClickedand button2isClicked,then set an onClickListener for each Button. When the the Button is clicked set the value of these two boolean's to true, then simply create an if() statement that will chekc to see if both buttons have been clicked, like this:
if(button1isClicked == true && button2isClicked == true){
//display your new page
}
I have 3 screens, Screen 1 , Screen 2, Screen 3.
This is the kind of back navigation that I am implementing,
Screen 3 -> Screen 2 -> Screen 1.
Here is my below code for Screen 1
public class Screen1 extends Activity implements OnClickListener{
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
// This is where the intent starts to display the second Screen
case R.id.button1:
Intent started = new Intent(Screen1.this,Screen2.class);
startActivity(started);
break;
}
}
}
Screen 2
public class Screen2 extends Activity implements OnTouchListener{
ListView displaylist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
// Please ignore this part of the code it just displays the list of items
displaylist = (ListView)findViewById(R.id.listView1);
String[] listofitems = {"A","B","C","W"};
ArrayAdapter<String> lists = new ArrayAdapter<String>(Screen2.this, android.R.layout.simple_list_item_1, listofitems);
displaylist.setAdapter(lists);
displaylist.setOnTouchListener(this);
}
// This is where the intent starts to display the Third Screen
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Intent started2= new Intent(Screen2.this,Screen3.class);
startActivity(started2);
return true;
}
}
Screen 3
public class Screen3 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Displays some layout
}
}
My Current Problem
My current problem is that the back navigation(when I press back button) is in the following order
Screen3 -> Screen3 -> Screen2 -> Screen1
My Question
Why does the Screen3 display again when I press the back button ? Is this somekind of bug ?
From a similar question David Wasser comment says that
If your navigation is standard, then 1 starts 2 and the BACK button goes back to 1. 2 starts 3 and the BACK button goes back to 2. You don't need to do anything special to get this
Testing Device
Android Emulator
API Level:19
Note: I do not want to do this with Actionbar
I think it's because your onTouch event is being fired twice, when pressing and releasing.
Try this, or use some other event:
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
Intent started2= new Intent(Screen2.this,Screen3.class);
startActivity(started2);
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}
I have the following code:
public class SplashScreenActivity extends Activity {
private boolean animated ;
private Handler handler1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isPreviouslyLoggedIn()) {
setContentView(R.layout.splash);
final TextView revolution=(TextView) findViewById(R.id.textView1);
final Button login=(Button) findViewById(R.id.loginButton);
final Button signUp=(Button) findViewById(R.id.signUpButton);
login.setOnClickListener(loginListener);
signUp.setOnClickListener(signUpListener);
if (!animated) {
animated = true;
revolution.setVisibility(View.INVISIBLE);
login.setVisibility(View.INVISIBLE);
signUp.setVisibility(View.INVISIBLE);
ImageView image = (ImageView) findViewById(R.id.image);
TranslateAnimation slide = new TranslateAnimation(0, 0, 100, 0);
slide.setDuration(1000);
image.startAnimation(slide);
handler1 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
revolution.setVisibility(View.VISIBLE);
login.setVisibility(View.VISIBLE);
signUp.setVisibility(View.VISIBLE);
}
},1200);
}
}
else {
setContentView(R.layout.home);
Intent intent = new Intent(getApplicationContext(), PickUpActivity.class);
startActivity(intent);
}
}
When the user clicks on one of the buttons, it leads him to a different activity in the same app. However, when the user clicks back from the next activity, the animation is started again. How can I prevent the animation from showing again as i want it to occur only once when user opens the app?
You can use Application class.Declare a boolean var in it and set it "true" before starting second Activity.In your onCreate() of first Activity check this boolean and do animation only it is false(it means that user has not started second activity yet).For example create class with name App in your package:
public class App extends Application{
private static boolean animated;
#Override
public void onCreate() {
super.onCreate();
animated = false;
}
public static boolean getAnimated(){
return animated;
}
public static void setAnimated(boolean animated1){
animated = animated1;
}
}
Register App in manifest:
<application
android:icon= ...
android:label= ...
android:name="yourpackage.name.App" >
(I suppose that your package name is :"yourpackage.name")
Now change your code like this:
if (!App.getAnimated()) {
App.setAnimated(true);
revolution.setVisibility(View.INVISIBLE);
login.setVisibility(View.INVISIBLE);
signUp.setVisibility(View.INVISIBLE);
...
Or you can use sharedpreferences and retrieve a boolean from it when you want to start animation.You have to set it's default value "false" and when user start second Activity,you have to set it "true".
You can set a flag for this, first time keep the flag true and when the user clicks on any of the button, set the flag value to false.
Now start the animation if the flag value is true.