I'm developing an android app, it has a Splash Screen that runs for 2500ms.
I want to add the functionality for a User touch screen and skip this Activity.
I could made it with a button, but for pretty objective I just want to add a screen touch listener (Don't know how.)
My SplashScreen:
public class Splash extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 2500;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashactivity);
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}, SPLASH_TIME_OUT);
//Skip this intro
RelativeLayout root_layout = (RelativeLayout) findViewById(R.id.root_splash);
root_layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
return true;
}
});
}
}
My splashactivity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash_screen">
</RelativeLayout>
Add a full screen view named ll_root in R.layout.splashactivity,then in Splash Activity using findViewById and setOnTouchListener for ll_root.
Delete the splash screen. That's so 2007.
Add onTouchListener on Splash screen layout and call your class on the event.
Hope it will work for you
Thanks..
Related
How to handle click on ScrollView in Fragment?
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ExampleFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myScroll">
</ScrollView>
</FrameLayout>
And in Fragment I'm trying:
#Override
public void onStart() {
super.onStart();
ScrollView refresh = (ScrollView) getActivity().findViewById(R.id.myScroll);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(getActivity().getApplicationContext(), "TEST", Toast.LENGTH_LONG);
toast.show();
}
});
}
But after clicking nothing happens.
Any other idea to handle a click on a fragment? This does not have to be a ScrollView. I tried also for FragmentLayout but it returns a lot of bugs.
The reason for the onClickListener of ScrollView being ignored is explained here.
You could add an onTouchListener instead like this.
refresh.setOnTouchListener(new View.OnTouchListener() {
/**
* Called when a touch event is dispatched to a view. This allows listeners to
* get a chance to respond before the target view.
*
* #param v The view the touch event has been dispatched to.
* #param event The MotionEvent object containing full information about
* the event.
* #return True if the listener has consumed the event, false otherwise.
*/
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast toast = Toast.makeText(getActivity().getApplicationContext(), "TEST", Toast.LENGTH_LONG);
toast.show();
return false;
}
});
use this code in onCreateView
ScrollView refresh = (ScrollView) getActivity().findViewById(R.id.myScroll);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(getActivity().getApplicationContext(), "TEST", Toast.LENGTH_LONG);
toast.show();
}
});
Based on json ArrayList size I'm creating TextView's.
By using the class Display , made each TextView height and width to cover the entire screen.
MOTTO
Only 1 TextView should be visible on the screen. By swiping it
should move to next view which will again occupy the entire screen.
Swipe down and Swipe up will move the screens i.e., views... swipe left and swipe right should do some other tasks,such as changing activity
Swipe is enabled by using GestureDetector.SimpleOnGestureListener
So far I've tried using ViewFlipper, TextView array to enable switching between TextView.But FAILED :(
Code snippet:
for(int i=0;i<name.size();i++)
{
text = new TextView(MainActivity.this);
text.setText(name.get(i));
text.setId(i);
text.setTextColor(Color.parseColor("#000000"));
text.setLayoutParams(new LinearLayout.LayoutParams(realWidth, realHeight));
text.setGravity(Gravity.CENTER);
text.setTextSize(40);
text.setClickable(true);
vf.addView(text);
/*
//I've tried the following code while using TextView array
myTextViews[i] = text;
myTextViews[i].setId(i);
myTextViews[i].setTextColor(Color.BLACK);
myTextViews[i].setText(name.get(i));
myTextViews[i].setGravity(Gravity.CENTER);
myTextViews[i].setTextSize(40);
myTextViews[i].setLayoutParams(new LinearLayout.LayoutParams(realWidth, realHeight));
myTextViews[i].onWindowFocusChanged(false);
LL.addView(myTextViews[i]);
*/
View lines = new View(getApplicationContext());
lines.setBackgroundColor(Color.parseColor("#000000"));
lines.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
vf.addView(lines);
final int finalI = i;
text.setOnTouchListener(new MainActivity()
{
#Override
public void onSwipeLeft()
{
if (vf.getDisplayedChild() == 0)
Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
else
vf.showNext();
}
#Override
public void onSwipeRight()
{
if (vf.getDisplayedChild() == 0)
Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
else
vf.showPrevious();
}
});
}
Errors:
While using ViewFlipper
E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
Array:
E/InputEventReceiver﹕ Exception dispatching input event. -- java.lang.ArrayIndexOutOfBoundsException
EDIT
I found this Question related to ios. Searching the same for android
I'm trying to develop a app similar to SimplEye
which will be used by Visually disabled people.
For that, I need to control the swipes on the screen so that entire app could be handled only through the help of swipes.
ViewPager , ViewFlipper , SimpleOnGestureListener are not matching the requirement.
Kindly suggest what Technique should be used.
Thank you
bases on the question what i can suggest is use ViewPager
which is alternative for your MOTTO not the solutions of your issue
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ViewPagerActivity
public class ViewPagerActivity extends Activity {
String text[] = {"A", "B",
"C", "D",
"E", "F",
"G", "H"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter(this, text);
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
//set Page Change Listner. to get callback on page changed or swiped
myPager .setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.e("Page Changed ", " YES ");
/// here you can check & perform on changed
Log.e("Current TextView Text ", text[position]);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
MyPagerAdapter
public class MyPagerAdapter extends PagerAdapter {
Activity activity;
int txtarray[];
public MyPagerAdapter(Activity act, int[] imgArra) {
txtarray = imgArra;
activity = act;
}
public int getCount() {
return txtarray.length;
}
public Object instantiateItem(View collection, int position) {
TextView view = new TextView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
view.setText(txtarray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
Use ViewPager for sliding the views (only by swipe gesture; if you need swipe during time then ViewFlipper is better approach). Make your ViewPager layout_width/layout_height attrs both match_parent (define ViewPager in xml layout not via code). Also make your TextView layout_width/layout_height match_parent too thus you don't need Display class at all.
EDIT. According to latest edition TS needs to handle gesture navigation across the whole app. I suggest the following:
try to not use any widgets which handle gestures (click) by themselves (button, checkbox etc.). Use only uninteractable Views and implement onTouchEvent method inside your Activity which will receive all touch events in this case. In that method you can add any gesture handling you want.
You can create your own ViewGroup implementation and override there onInterceptTouchEvent/onTouchEvent methods and perform there any gesture handling manually.
In both cases you need to create all gesture detection logic by yourself.
These links may be helpful
Creating a simple Gesture Application in Android
How to add our own gestures in android?
Detecting Common Gestures
Never did it but the first link seems to be the most useful. It says that you can firstly create some kind of gesture description and then gesture API can check any gesture performed for match to that description.
I am trying to implement a navigation drawer in a BaseActivity, that shows different options depending on the activity that is currently showing. For that, I developed a BaseActivity, that implements the navigation drawer, and decides what to show, depending on the activity that is currently showing. The purpose of this, is to make all other activities that need to use the navigation drawer, expand the BaseActivity.
The following code, shows no errors, but the navigation drawer shows itself completely empty, and does not show when I click the 'home' button, neither the 'menu' button, a functionality that I implemented with the 'onKeyDown' method. It just shows, when I use the following gesture: move the finger from the left to the right in the left side of the screen.
When I do the same in each class I need, instead of using a BaseActivity, everything works perfeclty fine.
I have been trying this for days now and I still do not understand why, the content of the navigation drawer is still not showing. I would appreciate some help please. Thanks in advance.
Here, the core classes of the problem:
BaseActivity.java
public abstract class BaseActivity extends ActionBarActivity
{
public DrawerLayout drawerLayout = null;
public ActionBarDrawerToggle drawerToggle = null;
public Activity currentActivity = null;
public ArrayList<Item> navDrawerItems = new ArrayList<Item>();
public ItemListAdapter adapter = null;
public ListView drawerList = null;
int id = 0;
protected void onCreate(Bundle savedInstanceState, int resLayoutID)
{
super.onCreate(savedInstanceState);
setContentView(resLayoutID);
currentActivity = this;
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(currentActivity,
drawerLayout,
R.drawable.ic_drawer,
R.string.open_menu,
R.string.close_menu)
{
public void onDrawerClosed(View view)
{
Log.e("", "Close drawer");
getSupportActionBar().setTitle(getTitle());
ActivityCompat.invalidateOptionsMenu(currentActivity);
}
public void onDrawerOpened(View drawerView)
{
Log.e("", "Open drawer");
getSupportActionBar().setTitle(getTitle());
ActivityCompat.invalidateOptionsMenu(currentActivity);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// Populate navigation drawer depending on the activity
// that is currently showing.
if (this.getClass().getSimpleName().equals("Z"))
setUpNavigationForZActivity();
}
private void setUpNavigationForZActivity()
{
Log.e("", "In setUpNavigationForZActivity");
// Prepare list items.
id = R.string.title_activity_A;
navDrawerItems.add(new NavigationDrawerItem(getString(id), Utils.activityIcon().get(id)));
id = R.string.title_activity_B;
navDrawerItems.add(new NavigationDrawerItem(getString(id), Utils.activityIcon().get(id)));
// Populate view.
drawerList = (ListView) findViewById(R.id.left_menu);
adapter = new ItemListAdapter(currentActivity, navDrawerItems;
drawerList.setAdapter(adapter);
drawerList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
Intent intent = null;
switch(position)
{
case 0:
intent = new Intent(currentActivity, A.class);
startActivity(intent)
break;
case 1:
intent = new Intent(currentActivity, B.class);
startActivity(intent)
break;
default:
}
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (drawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent e)
{
Log.e("", "onKeyDown");
if (keyCode == KeyEvent.KEYCODE_MENU)
{
if(!drawerLayout.isDrawerOpen(Gravity.LEFT))
drawerLayout.openDrawer(Gravity.LEFT);
else
drawerLayout.closeDrawer(Gravity.LEFT);
}
return super.onKeyDown(keyCode, e);
}
}
Z.java
public class Z extends BaseActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState, R.layout.Z);
setContentView(R.layout.Z);
//Other things to do...
}
}
Z.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Main content view -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
// Other layout and views configurations...
</ScrollView>
<!-- Navigation drawer -->
<ListView
android:id="#+id/left_menu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/gray_7_5"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
I do not exactly know why you would want to implement it that way but if you still want to do it this way i would suggest making your navdrawer use a listview with an adapter to draw items from an array and then have your base activity have a getData() call that you can override in your derived activities that will supply the data that the Adapter so that the listview will then draw the appropriate items in the navdrawer. You will then have to implement the onItemClick event for each listview per activity.
My android application has two intents.
First one: main window,
second one contains the game view (SurfaceView).
When user push "return" button, main windows appears before the game view indent suspends (surfaceDestroyed).
However I need to run a method in game view before the main windows appears. I've stuck with this question.
Here code:
MainWindow class:
#Override
public void onClick(final View v) {
if (v.getId() == R.id.StartBtn) {
final Intent myIntent = new Intent(getApplicationContext(),
StartActivity.class);
this.startActivity(myIntent);
}
}
StartActivity class
public class StartActivity extends Activity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
}
GameView class
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
#Override
public void surfaceDestroyed(final SurfaceHolder arg0) {
//Do some operations before destroing
}
}
Layout activity_start
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity" >
<com.somepackage.appname.GameView
android:id="#+id/game_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
So I need to start surfaceDestroyed in GameView before onResume on MainWindow intent
i am not sure if that is really what yiu are looking for, but i will give it a try:
You need to have an instance inside the other Activity before you can call any method there. You can achieve it by using this:
(OtherActivity) getActivity.myMethod();
Use Activity.this instead of getApplicationContext(). That May be the problem. If not please provide the code.
simply use
Intent intent = new Intent(MainAWindow.this,StartActivity.class);
startActivity(intent);
and where is ur OnResume method?... explain your question properly what u want to achieve exactly..?
I'm trying to make a bull's eye with random color, and instead of circles I will use squares.
But the thing is that when I run the app on the emulator and when he starts the new activity it stops responding.
This is the main activity, the one that starts the DrawActivity.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent coiso = new Intent(this, Draw.class);
startActivity(coiso);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And this is the Draw activity, the one that I want to start. (It doesn't have the things that I want to do. Because I can't, the problem is ahead)
public class Draw extends View {
public Draw(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
}
}
Can someone help me? Sorry for the english.
You have this
public class Draw extends View
Your class does not extend Activity
Instead you can do as below
Draw draw = new Draw(this);
setContentView(draw);
Or have a layout linear or relative and place it where you like add your Draw view to the layout after initializing.
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
// linear layout or relative layout in activity_main.xml.
// place the layout ahere you want along with other views
Draw draw = new Draw(this);
ll.addView(draw);
// add your customview to linearlayout
Edit:
Remove this
Intent coiso = new Intent(this, Draw.class);
startActivity(coiso);
In your activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
// customize linear layout to your needs.
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/linearLayout"
android:orientation="vertical" >
</LinearLayout>
// other widgets
</RelativeLayout>
In your onCreate
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
Draw draw = new Draw(this);
ll.addView(draw);
startActivity requires an activity. I would suggest going through the docs
http://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)
Your Draw class needs to extend Activity rather than View. As you want to start a new Activity, the Draw class, this means that this should extend Activity. Also, you need to Override onCreate() within the Draw class.
If your Draw class is a View, then I would suggest that you add the view to the Layout that you are using using the addView()
You need to make sure that you change Draw extends Activity
You cant intent to a new activity with no layout and no OnCreate As far as I know.
try creating a regular activity which extends Activity and implement your Draw there.
public class DrawActivity extends Activity {
#SuppressLint("ShowToast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
Toast.makeText(DrawActivity.this, "YO", Toast.LENGTH_LONG);
}
and from there implement your draw functions.
or create a JAVA class which implements your draw needs and use it in main screen.