Android Gesture Recognition bad - android

I've developed an app with gestures in Android.
It simply have four different gestures. Up, down, left and right.
Left and right works well, but up and down is never recognized. It also directly gets uncertain (I set the color to red so I can debug a faster way).
Here's my controller code:
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class PreperationActivity extends Activity implements OnGesturePerformedListener {
private GestureLibrary gestureLib;
private ImageView ivProfileImage;
private TextView list1, list2, list3, list4;
private TextView tvUsername;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.activity_preperation, null);
gestureOverlayView.addView(inflate);
// gestureOverlayView.setGestureVisible(false);
gestureOverlayView.setGestureColor(Color.BLACK);
gestureOverlayView.setUncertainGestureColor(Color.RED);
gestureOverlayView.addOnGesturePerformedListener(this);
gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gestureLib.load()) {
finish();
}
setContentView(gestureOverlayView);
ivProfileImage = (ImageView) findViewById(R.id.ivProfileImage);
ivProfileImage.setImageBitmap(ListActivity.friends.peek().profileImage);
Intent intent = getIntent();
list1 = (TextView) findViewById(R.id.list1);
list2 = (TextView) findViewById(R.id.list2);
list3 = (TextView) findViewById(R.id.list3);
list4 = (TextView) findViewById(R.id.list4);
list1.setText(intent.getStringExtra("list1"));
list2.setText(intent.getStringExtra("list2"));
list3.setText(intent.getStringExtra("list3"));
list4.setText(intent.getStringExtra("list4"));
tvUsername = (TextView) findViewById(R.id.tvUsername);
tvUsername.setText(ListActivity.friends.peek().username);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.preperation, menu);
return true;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
#Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for (Prediction prediction : predictions) {
Log.i("ced", "PREDICTION: " + prediction.score);
if (prediction.score > 1.0) {
Friend friend =ListActivity.friends.pop();
ivProfileImage.setImageBitmap(friend.profileImage);
tvUsername.setText(friend.username);
Toast.makeText(getBaseContext(), prediction.name, Toast.LENGTH_SHORT).show();
}
}
}
}
And my view:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background"
tools:context=".PreperationActivity" >
<TextView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#color/list_area_background"
android:gravity="center"
android:text=""
android:textAlignment="center" />
<TextView
android:id="#+id/list2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:background="#color/list_area_background"
android:gravity="center"
android:text=""
android:textAlignment="center" />
<TextView
android:id="#+id/list3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/list_area_background"
android:gravity="center"
android:text=""
android:textAlignment="center" />
<TextView
android:id="#+id/list4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:background="#color/list_area_background"
android:gravity="center"
android:text=""
android:textAlignment="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivProfileImage"
android:layout_width="183dp"
android:layout_height="183dp"
android:src="#drawable/com_facebook_profile_picture_blank_square" />
<TextView
android:id="#+id/tvUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textColor="#ffffff"
android:layout_marginTop="5pt"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
R is just to see if it works and yes, it does.
Any ideas? Thank you!

Have u called this method : addOnGesturePerformedListener ?
I runned the legacy demo then I found that OnGesturePerformedListener causes single up&down gesture not be recognized.

Related

Android Relative layout spacing between elements

I have this Design in Relative Layout. I have to do some more things.
How can I remove that straight line between two elements ?
How can I increase space between two elements ?
How to display product side by side ?
I have tried several solutions from stackoverflow. Like I have used margin bottom but got no luck.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:background="#drawable/round_corner"
android:layout_marginRight="20dp"
>
<RelativeLayout
android:layout_width="200dp"
android:layout_height="250dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:paddingRight="20dp"
android:paddingLeft="20dp"
>
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/product_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:foregroundGravity="center"
app:civ_border_color="#d1b1b1"
/>
<View
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="1dp"
android:layout_marginTop="0dp"
android:background="#drawable/gradient" />
<TextView
android:id="#+id/product_name_english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/product_image"
android:layout_alignTop="#id/product_image"
android:layout_alignRight="#id/product_image"
android:layout_alignBottom="#id/product_image"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#FFFF"
android:fontFamily="#font/aerial"/>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/product_name_english"
android:layout_centerHorizontal="true"
android:layout_marginTop="-51dp"
android:layout_marginBottom="1dp"
android:fontFamily="#font/aerial"
android:text="Hello"
android:textColor="#FFFF" />
<Button
android:layout_width="250dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="194dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Add to cart"
android:textColor="#FFFFFF"
android:textSize="10sp" />
<Button
android:layout_width="250dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="View Product"
android:textColor="#FFFFFF"
android:textSize="10sp" />
</RelativeLayout>
</FrameLayout>
Here is the listview I am sending to my Adapter
listView = (ListView) findViewById(R.id.product_list);
mAdapter = new all_product_list_ArrayAdapter(all_product.this,data);
listView.setAdapter(mAdapter);
Here is my ArrayAdapter Class :
package com.example.yunus.ybazar_android;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.support.annotation.NonNull;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
public class all_product_list_ArrayAdapter extends ArrayAdapter<all_product_list_android_model> {
private final String Tag = "Filter" ;
private Context mContext;
private ArrayList<all_product_list_android_model> product_list ;
private final String urlMain = "http://118.179.70.235:28965/media/" ;
public all_product_list_ArrayAdapter(#NonNull Context context, ArrayList<all_product_list_android_model> list) {
super(context, 0, list);
mContext = context ;
product_list = list ;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null)
listItem = LayoutInflater.from(mContext).inflate(R.layout.all_product_list_arrayadapter_connection,parent,false);
all_product_list_android_model product_list_to_show = product_list.get(position);
ImageView image = (ImageView)listItem.findViewById(R.id.product_image);
new DownLoadImage(image).execute(urlMain.concat(product_list_to_show.product_main_image));
TextView name = (TextView) listItem.findViewById(R.id.product_name_english);
name.setText(product_list_to_show.product_name_english);
TextView price = (TextView) listItem.findViewById(R.id.price);
price.setText(("Taka ")
.concat(product_list_to_show.product_unit_price)
.concat("/")
.concat(product_list_to_show.product_unit));
final Button bt = (Button) listItem.findViewById(R.id.add_to_cart);
bt.setTag(product_list_to_show.id);
final Button bt2 = (Button) listItem.findViewById(R.id.view_product);
bt2.setTag(product_list_to_show.id);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
bt2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mContext, indi_product_show.class);
intent.putExtra("product_id",String.valueOf(bt2.getTag()));
mContext.startActivity(intent);
}
});
return listItem;
}
}
Set your ListView's divider to null and its Divider Height to 0 pixels:
listView = (ListView) findViewById(R.id.product_list);
listView.setDivider(null);
listView.setDividerHeight(0);
mAdapter = new all_product_list_ArrayAdapter(all_product.this,data);
listView.setAdapter(mAdapter);
more info can be found in the docs for ListView here :
ListView#setDivider(android.graphics.drawable.Drawable)
ListView#setDividerHeight(int)
use divider for ListView in this way:
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp"/>

Android app not able to open web link

I have a button at the end of this app that takes the user to their default browser and to a predetermined link. Unfortunately, that button is broken. I am unsure of whether this is a problem with Java or with XML, but the code for both is attached.
package com.example.ldsm3;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity
import android.content.DialogInterface;
import android.content.Intent;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class Finished extends Activity {
public void onClick(Button button1)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("URL HERE"));
startActivity(browserIntent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finished);
// tv is the ID for the TextView in the XML file
TextView tv = (TextView) findViewById(R.id.textView2);
// set the TextView to show the score
tv.setText(Misc.correct + "/" + Misc.total);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.finished, menu);
return true;
}
}
And the XML code...
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Finished" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="69dp"
android:text="#string/Finished" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView1"
android:layout_marginBottom="19dp"
android:text="Go to the game!" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="93dp"
android:text="0/0" />
</RelativeLayout>
How do I fix this? Is it because of XML or Java? Did I not properly define the intent?
You need to add this on your onCreate() method:
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Do your stuff here
}
});

How to reuse my xml relative layout in android

Hi I have an m file which is a relative layout and which contains 9 image buttons.
So I would like to reuse my layout multiple times just by changing the names and ID's.
I am able to do with gridview but my problem is I want use the same customs design which I have using relative layout.
So can any one help how to do it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="#+id/day0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/day1"
android:layout_marginLeft="14dp"
android:scaleType="fitXY"
android:src="#drawable/day0" />
<ImageButton
android:id="#+id/day3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day0"
android:layout_below="#+id/day0"
android:layout_marginTop="54dp"
android:src="#drawable/day3" />
<ImageButton
android:id="#+id/day6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day3"
android:layout_below="#+id/day3"
android:layout_marginTop="74dp"
android:src="#drawable/day6" />
<ImageButton
android:id="#+id/day4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/day6"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/day3"
android:src="#drawable/day4" />
<ImageButton
android:id="#+id/day7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day4"
android:layout_alignTop="#+id/day6"
android:src="#drawable/day7" />
<ImageButton
android:id="#+id/day1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day4"
android:layout_alignParentTop="true"
android:layout_marginTop="39dp"
android:scaleType="fitXY"
android:src="#drawable/day1" />
<ImageButton
android:id="#+id/day8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day5"
android:layout_alignTop="#+id/day7"
android:src="#drawable/day8" />
<ImageButton
android:id="#+id/day2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/day1"
android:layout_marginLeft="28dp"
android:layout_toRightOf="#+id/day1"
android:scaleType="fitXY"
android:src="#drawable/day2" />
<ImageButton
android:id="#+id/day5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/day6"
android:layout_alignLeft="#+id/day2"
android:src="#drawable/day5" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignLeft="#+id/day7"
android:text="Emotions"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView7"
android:layout_alignLeft="#+id/Ans6b"
android:text="Leadership"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/Ans6b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/question6a"
android:layout_alignBottom="#+id/question6a"
android:layout_alignRight="#+id/day1"
android:text="gruppstart"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/day3"
android:layout_toLeftOf="#+id/day4"
android:text="Adaption"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/day6"
android:layout_toLeftOf="#+id/day4"
android:text="Behavior"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/day8"
android:text="Satisfaction"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/day5"
android:layout_below="#+id/day5"
android:text="Insight"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day2"
android:layout_below="#+id/day2"
android:text="relationer"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/question6a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/day0"
android:layout_marginLeft="15dp"
android:text="Goal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/textView3"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
This is my xml file I would like to call this xml file for different activities but just by changing the names ID and some display image buttons.
How to do that can any one please suggest me
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import com.xylem.flygt.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MenuActivity extends Activity
{
static GridView gridView;
Bitmap bmp = null;
public ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
Context context;
ImageButton day0, day1, day2, day3, day4, day5, day6, day7, day8;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
day0 = (ImageButton) findViewById(R.id.day0);
day1 = (ImageButton) findViewById(R.id.day1);
day2 = (ImageButton) findViewById(R.id.day2);
day3 = (ImageButton) findViewById(R.id.day3);
day4 = (ImageButton) findViewById(R.id.day4);
day5 = (ImageButton) findViewById(R.id.day5);
day6 = (ImageButton) findViewById(R.id.day6);
day7 = (ImageButton) findViewById(R.id.day7);
day8 = (ImageButton) findViewById(R.id.day8);
gridView = (GridView) findViewById(R.id.grid_view);
gridView.setBackgroundColor(Color.parseColor("#E3E4FA"));
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
System.out.println("Clicked id " + position);
if(position == 0)
{
System.out.println(" Day 0 ");
}
if(position == 1) {
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "1");
System.out.println(" Day 1 ");
startActivity(i);
}
if(position == 2)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "2");
System.out.println(" Day 2 ");
startActivity(i);
}
if(position == 3)
{ Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "3");
System.out.println(" Day 3 ");
startActivity(i);
}
if(position == 4)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "4");
System.out.println(" Day 4 ");
startActivity(i);
}
if(position == 5)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "5");
System.out.println(" Day 5 ");
startActivity(i);
}
if(position == 6)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "6");
System.out.println(" Day 6");
startActivity(i);
}
if(position == 7)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "7");
System.out.println(" Day 7 ");
startActivity(i);
}
if(position == 8)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "8");
System.out.println(" Day 8 ");
startActivity(i);
}
}
});
new Checkday().execute();
}
#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;
}
class Checkday extends AsyncTask<String, String, String>
{
protected void onPreExecute()
{
super.onPreExecute();
}
#Override
protected String doInBackground(String... arg0)
{
return null;
}
protected void onPostExecute(String file_url)
{
gridView.setAdapter(new CalenderAdaptor(MenuActivity.this));
}
}
}
I have written my activity java it itself creates the image buttons in a gridview. But I use like this I want to use in such way that I call my customs layout in different activities because the layout is same only the IDs and some images will change.
If I understand it right - you have a custom design layout with buttons and you want to use this same layout in other screen as well.
You don't need to rename anything, just put the layout in a separate xml, let's say CommonButtonLayout.xml. This should contain like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
.... your layout .....
</merge>
Now you want to use this in other layout files. You need to include this xml directly there like the following.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
...
...
>
<include
android:layout_centerVertical="true"
layout="#layout/CommonButtonLayout" />
</RelativeLayout>

How to switch between activities/screens in Android

my first Activity Xm
<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"
android:background="#a5c63b"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#434d23"
android:text="Ok To Continue"
android:onClick="frontt"
android:textColor="#a5c63b" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/question" />
<TextView
android:id="#+id/txtview11"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Loading..."
android:textSize="12dp" />
</RelativeLayout>
This is my Second Activity Xml
<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"
android:background="#a5c63b"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="70dp"
android:onClick="onClick"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#434d23"
android:text="OK"
android:textColor="#a5c63b" />
<EditText
android:id="#+id/txtedit"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignLeft="#+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:background="#edf2db"
android:ems="10"
android:inputType="numberDecimal" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/question" />
<TextView
android:id="#+id/txtview"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignBottom="#+id/txtedit"
android:layout_alignLeft="#+id/txtedit"
android:layout_marginBottom="21dp"
android:text="Guess Single Digit Number"
android:textSize="12dp" />
<TextView
android:id="#+id/txtview2"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignLeft="#+id/btn"
android:layout_below="#+id/btn"
android:text="TextView" />
</RelativeLayout>
this is my first java file code
package com.example.game;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
public class MainActivity extends Activity {
MediaPlayer btnsound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front);
Button next = (Button) findViewById(R.id.btn1);
btnsound = MediaPlayer.create(MainActivity.this,R.raw.game);
btnsound.start();
next.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent nextScreen = new Intent(getApplicationContext(), CopyOfMainActivity.class);
startActivity(nextScreen);
//finish();
}
} );
}
#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;
}
}
this is my Second java file
package com.example.game;
import java.util.Random;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.view.Menu;
public class CopyOfMainActivity extends Activity {
MediaPlayer btnsound;
Random random = new Random();
int randnumber = random.nextInt(10);
Button b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnsound = MediaPlayer.create(CopyOfMainActivity.this,R.raw.game);
btnsound.start();
b2= (Button) findViewById(R.id.btn);
b2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
EditText input = (EditText)findViewById(R.id.txtedit);
TextView resultText = (TextView) findViewById(R.id.txtview2);
String inputstring = input.getText().toString();
int number = Integer.parseInt(inputstring);
if(randnumber==number)
{
resultText.setText("you win" );
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.win);
}
else if (number>randnumber)
{
resultText.setText("you guess high number" );
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.tryagain);
}
else if (number<randnumber)
{
resultText.setText("you guess low number" );
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.lose);
}
}
} );
}
#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;
}
}
I have one button on First Activity xml layout .The first Activity Layout display after run application but whenever i click on button on first xml first want to go on next laytout after clicking then this error show in LogCat tab and application stop working.
AndroidRuntime at dalvik.system.NativeStart.main(Native Method)
what i should do?
The right way to do this given below:
next.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent nextScreen = new Intent(MainActivity.this, CopyOfMainActivity.class);
startActivity(nextScreen);
//finish();
}
} );
Hope this will work fine.
Please add your both activities to Manifest file if you missed out.
<application>
<activity android:name="Activity1">
</activity>
<activity android:name="Activity2">
</activity>
public void butt591(View v) {
v.startAnimation(fivenineclick);
Button but591 = (Button) findViewById(R.id.button591);
but591.setBackgroundColor(Color.parseColor("#ee4035"));
Intent bu591 = new Intent();
bu591.setClass(this, level5q10.class);
startActivity(bu591);
finish();
}
hey, i copy pasted a part of code in my app here 591 denotes level 5 ninth question and on selecting first option
that is wrong answer so on clicking the option it will set background to #ee4035 which is red color and when the user presses this option it will switch to level 5q10
I hope you understood from this code

old listview still there

I have an activity that is works fine and displays data obtained from a table query. With an OnItemClick method, I have it set up so that if a row is selected, a new activity starts with a new ListView, but a lot of the elements from the original ListView are in the new listview, including a toggle button on each row, and a text view. Any idea what could be causing this?
This is the initial ListView. The buttons don't show up, just everything in the ListView:
<?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="wrap_content"
android:background="#81BEF7"
android:orientation="vertical" >
<Button
android:id="#+id/btnAddNurseToRoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add"
android:focusable="true"/>
<Button
android:id="#+id/btnRemoveNurseFromRoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btnAddNurseToRoster"
android:layout_toRightOf="#+id/btnAddNurseToRoster"
android:text="#string/delete"
android:focusable="true"/>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/btnAddNurseToRoster"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</RelativeLayout>
New ListView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#81BEF7"
android:orientation="vertical" >
<TextView
android:id="#+id/tvAssignmentsText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#FFFFFF"
android:text="Assignments"
android:gravity="center_horizontal"
android:background="#663399"
/>
<ListView
android:id="#+id/lvAssignmentsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/tvAssignmentsText"
android:paddingTop="20dp"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</
And here's the code that calls the new ListView:
package com.deadEddie.staffingmanagement;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ShowAssignments extends Activity {
DbCommunicator getAssignmentsList;
ListView assignmentsList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.assignments);
displayList();
}
private void displayList() {
// instantiate ListView object
assignmentsList = (ListView) findViewById(R.id.lvAssignmentsList);
// instantiate variables for CursorAdapter
int [] to = new int [] {R.id.rbAssignmentsList };
String [] from = new String [] {DbCommunicator.KEY_ROOM_NUMBER};
// instantiate getAssignmentsList with new DbCommunicator object and open
getAssignmentsList = new DbCommunicator(this);
getAssignmentsList.open();
// get and manage cursor
Cursor assignmentsCursor = getAssignmentsList.getAssignments(this);
startManagingCursor(assignmentsCursor);
// list adapter
ListAdapter assignmentsListAdapter = new SimpleCursorAdapter(this, R.layout.nurse_list, assignmentsCursor, from, to, 0);
assignmentsCursor.moveToNext();
// set ListView
assignmentsList.setAdapter(assignmentsListAdapter);
assignmentsList.setItemsCanFocus(true);
}
}
Adding the code used to populate the original ListView:
<?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="wrap_content" >
<ToggleButton
android:id="#+id/rosterDutyStatus"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:textOff="Off Duty"
android:textOn="On Duty"
android:textSize="10dp"
android:focusable="false"/>
<TextView
android:id="#+id/rosterListLname"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterDutyStatus"
android:layout_toRightOf="#+id/rosterDutyStatus"
android:paddingTop="6dp"
android:textColor="#FFFFFF"
android:textSize="18dp"
android:textStyle="bold"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="#+id/rosterListFname"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterListLname"
android:layout_toRightOf="#+id/rosterListLname"
android:paddingTop="6dp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="#+id/rosterListMI"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterListFname"
android:layout_toRightOf="#+id/rosterListFname"
android:paddingTop="6dp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="#+id/rosterListID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterListMI"
android:layout_toRightOf="#+id/rosterListMI"
android:visibility="invisible" />
<TextView
android:id="#+id/rosterViewAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rosterDutyStatus"
android:paddingRight="50dp"
android:text="#string/assigned"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="#+id/firstAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterViewAssignment"
android:layout_toRightOf="#+id/rosterViewAssignment"
android:text="#string/noAssignments"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/secondAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/firstAssignment"
android:layout_toRightOf="#+id/firstAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/thirdAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/secondAssignment"
android:layout_toRightOf="#+id/secondAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/fourthAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thirdAssignment"
android:layout_toRightOf="#+id/thirdAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/fifthAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/fourthAssignment"
android:layout_toRightOf="#+id/fourthAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/sixthAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/fifthAssignment"
android:layout_toRightOf="#+id/fifthAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>"
-->
</RelativeLayout>
Code that calls original layout I'm trying to change:
package com.deadEddie.staffingmanagement;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class EditRoster extends ListActivity implements OnClickListener {
String TAG = "EditRoster";
Button addNurse;
Button deleteNurse;
DbCommunicator rosterView;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_roster);
addNurse = (Button) findViewById(R.id.btnAddNurseToRoster);
deleteNurse = (Button) findViewById(R.id.btnRemoveNurseFromRoster);
displayNurseRoster();
displayDialog();
addNurse.setOnClickListener(this);
deleteNurse.setOnClickListener(this);
}
// method to put timer on dialog
public void timerDelayRemoveDialog(long time, final Dialog dialogView){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
dialogView.dismiss();
}
}, time);
}
private void displayDialog() {
Dialog d = new Dialog(this);
d.setTitle("Select Nurse to Adjust Assignments");
d.show();
timerDelayRemoveDialog(2000, d);
}
public void displayNurseRoster(){
listView = (ListView) findViewById(android.R.id.list);
// int variables filled with NURSE_TABLE data
int[] to_nurseTable = new int[] {
// int list for data from NURSE_TABLE fields
R.id.rosterDutyStatus,
R.id.rosterListLname,
R.id.rosterListFname,
R.id.rosterListMI,
R.id.rosterListID,
// int list for data from ASSIGNMENTS_TABLE fields
R.id.firstAssignment};
// String array holding data fields from NURSE_TABLE
String[] from_nurseTable = new String [] {
// fields from NURSE_TABLE
DbCommunicator.KEY_DUTY_STATUS,
DbCommunicator.KEY_LNAME,
DbCommunicator.KEY_FNAME,
DbCommunicator.KEY_MI,
DbCommunicator.KEY_NURSE_ROWID,
DbCommunicator.KEY_ROOM_NUMBER};
// instantiate instance of DbCommunicator object
rosterView = new DbCommunicator(this);
// open instance
rosterView.open();
// get & manage cursor for NURSE_TABLE data
Cursor nurseTableCursor = rosterView.getNurseRosterCursor(this);
startManagingCursor(nurseTableCursor);
// instantiate cursor adaptor
ListAdapter nurseTableAdapter = new SimpleCursorAdapter(this,
R.layout.nurse_list, nurseTableCursor, from_nurseTable, to_nurseTable);
nurseTableCursor.moveToNext();
// set listView
listView.setAdapter(nurseTableAdapter);
rosterView.close();
listView.setItemsCanFocus(true);
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.i(TAG, "onItemClickListener set");
Intent showAssignments = new Intent("com.deadEddie.staffingmanagement.SHOWASSIGNMENTS");
startActivity(showAssignments);
}
});
}// displayNurseRoster()
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnAddNurseToRoster:
Intent add = new Intent("com.deadEddie.staffingmanagement.ADDNURSETOROSTER");
startActivity(add);
break;
case R.id.btnRemoveNurseFromRoster:
break;
} // switch
}
}
Okay. Got it. Just realized that I mistakenly used the same layout (nurse_list) when I instantiate the ListAdapter in each method so it tried to apply my new data query to the original layout (which was screwed up of course). Thanks for taking a look though, really appreciate it.
Sorry if this wastes anyone's time. Maybe a lesson for other newbies out there.

Categories

Resources