Android - How to start a new activity - java

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.

Related

Closing a view class and opening another view class

I call a view class from my activity. Then the view class calls the same activity. Here is the problem, once the activity comes back up, it won't register any more button pushes.(I'm trying to call another view class. Here is some code:
View Class
public class AnimationView extends View {
Activity myActivity;
//...
public AnimationView(Context context, Activity activity) {
super(context);
//...
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//...
myActivity.setContentView(R.layout.activity_home);
}
}
Home Activity
public class HomeActivity extends AppCompatActivity {
private AnimationView mDrawViewA;
///...
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mDrawViewA = new AnimationView(this,this);
start = (Button) findViewById(R.id.startButton);
//...
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//...
setContentView(mDrawViewA);
//calls more views
//......
});
}
I realize now maybe I should have been calling the view classes in different activities, but I would very much like a get all the view classes working within the same activity.
The problem is you're calling setContentView every time you press the "start" button. This method will overwrite the current layout (if any) with the new value you're setting.
What you can do to get the result you're expecting, which, from what I understand, is to add a new AnimationView to your current layout on every button click, you can try something like this:
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AnimationView animationView = new AnimationView(getApplicationContext());
// I'm using ConstraintLayout as an example, since I don't know exactly what layout you're using
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
// Set the layout params the way you want
addContentView(animationView, params); // This is where the magic happens
}
});
In short, addContentView is the method you should use when you want to add new views into your activity's root layout.
PS.: It's a terribly bad practice to let the views "know" the activity controlling it. It's always the opposite way around: the activity/fragment knows the view(s) it's controlling.

Views Navigation Using Swipe Gesture android

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.

How does setContentView work in android?

I am trying to display a button on my android app but everytime i run the app it crashes. i realise this is because i use setContentView multiple times? I dont understand how it works, and dont understand how i can fix this problem so my button will display. my code is below.
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
setContentView(l);
l.addView(new Draw(this));
//setContentView(R.layout.activity_main);
setUpBlockBtn();
}
private void setUpBlockBtn(){
setContentView(R.layout.activity_main);
Button addBlockButton = (Button)findViewById(R.id.btnBlock);
addBlockButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "you clicked the button");
//finish();
}
});
}
You try to access Button from android xml layout but you do not set this layout in Activity.
Put you button activity_main.xml and use this button in your activity.
Thanks
You can create one more layout and add Draw and Linear layout to that layout.
Something like this.
LinearLayout l1=new LinearLayout(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.addView(draw);
l1.addView(l2) // your linearLayout.
setContentView(l1)
Remember you can't use setContentView more than one time.
There should be top layout which includes subview and other layouts and then you can add that layout to your activity.

App crashes when setting layout background

I'm trying to set a drawable resource as background for my main relative layout via java, but whenever I do it, my app crashes.
Here is the part of the code which doesn't work fine:
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = (RelativeLayout) findViewById(R.layout.activity_game);
setContentView(R.layout.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Any idea?
Thanks in advance
Call findViewById() only after setContentView() and supply an identifier in R.id (and not R.layout) so that it can return something other than null.
Remember,you are trying to find a view by id but what you are doing is actually trying to inflate a layout the wrong way.Change the R.layout.activity_game to R.id.activity_game and make sure the relative layout is givent the
android:id = "#+id/activity_game"
The full code should be
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layout = (RelativeLayout) findViewById(R.id.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Hope it helps.Happy coding.

How to transfer data between Views in a Layout

I haven't beeen able to find the answer to the following question:
How do I get data from View A to View B, with View A and View B in the same LinearLayout? Is this even possible? Do I need to start working with threads?
I haven't been able to get the correct search phrase I guess, I'm probably not the first person that wants to do this, but I can't find it :(
Below is what I use now to create the views. In the TargetTrainer (which extends View) I'm letting the user give some input, and I'd like to be able to give feedback to the user in the TextView. How would I for instance show the coordinates of the onTouchEvent of TargetTrainer in the TextView?
Below is a clipped/simplified version of my program.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
LinearLayout linear;
linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
TextView text = new TextView(this);
text.setText("Test");
linear.addView(text);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
TargetTrainer t = new TargetTrainer(this, width, height);
linear.addView(t);
setContentView(linear);
}
As I can see from the snippet, you already pass Context in the constructor new TargetTrainer(this, width, height). Assuming that the code you provided is from activity called BaseActivity create reference to BaseActivity in the TargetTrainer constructor and call the update method from TargetTrainer.
public TargetTrainer extends View {
....
BaseActivity mBaseActivity = null;
public MyView(Context context, int width, int height) {
....
mBaseActivity = (BaseACtivity)context;
....
}
....
private void update(String text)
{
mBaseActivity.updateTextView(text);
}
}
In BaseActivity create updateTextView:
public void updateTextView(String updateText){
text.setText(updateText);
}
You should set the id of that TextView, listen to the touch events in your TargetTrainer, and when one occures, you use
final TextView tv = (TextView)TargetTrainer.this.findViewById(R.id.myTextView);
tv.setText(touchEvent.toString());
That's it.
Update
It would be much cleaner, if you'd build your main layout from an xml source.
You need to create a new layout xml inside the /res/layout that looks like the one you're creating inside your onCreate method:
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/myTextView" android:text="Test"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<!-- change the your.package part to match the package declaration
of your TargetTrainer class -->
<your.package.TargetTrainer android:id="#+id/myTargetTrainer"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
This way a new entry will be placed inside your R class' static layout class with the name main.
You can reference it by R.layout.main.
Note, that in this xml you have id attributes defined for both
your TextView: myTextView, and
your TargetTrainer:
'myTargetTrainer'.
The #+id inside the xml tag means that you are creating a new id with the name after the '/' sign.
This also will create new members inside your R class' static id class with the names you've provided: myTextView and myTargetTrainer that are accessible from now on from anywhere in your code.
If you've built this xml, your onCreate method will look like this:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// attach the OnTouchListener to your TargetTrainer view:
(TargetTrainer)findViewById(R.id.myTargetTrainer).setOnTouchListener(this);
}
You also have to extend your main activity class to implement the View.OnTouchListener interface, and add the necessary method at the end of your class:
#Override
public boolean onTouch(View view, MotionEvent event)
{
//note, that here the view parameter is the view the touch event has been dispatched to
final TextView tv = (TextView)findViewById(R.id.myTextView);
tv.setText(event.toString());
return true; //or false, if you are dealing further with this event in parent classes
}

Categories