How to reuse my xml relative layout in android - 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>

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"/>

How to Fix java.lang.ClassCastException: com.gc.materialdesign.views.ButtonFlat cannot be cast to android.widget.EditText

I am creating an android application that consists of registration form in dialog. Here, I placed a drop down spinner in registration dialog. But,here it was showing type casting error even i did the correct type casting. Please check it and make it solve. I am trying to solve since 2 hours. This is my code:
package com.example.testingsample;
import com.gc.materialdesign.views.ButtonFlat;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity
{
Button checking,spinner_check;
EditText username,password,confirm,seq_answer;
ButtonFlat clear,register;
Spinner spinner;
String[] questions = {
"What is your first name.?",
"What is your first pet name.?",
"What is your school name.?",
"What is your date of birth.?",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checking = (Button)findViewById(R.id.button1);
checking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
android.app.Dialog registration_dialog = new android.app.Dialog(MainActivity.this);
registration_dialog.setContentView(R.layout.signup);
registration_dialog.setTitle("MedeQuip Registration");
username = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_username);
password = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_password);
confirm = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_confirmpassword);
spinner = (Spinner)registration_dialog.findViewById(R.id.spinner1);
ArrayAdapter adpter = new ArrayAdapter(MainActivity.this ,android.R.layout.simple_dropdown_item_1line,questions);
spinner.setAdapter(adpter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id)
{
Toast.makeText(MainActivity.this, questions[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
seq_answer = (EditText)registration_dialog.findViewById(R.id.editText1);
clear = (ButtonFlat)registration_dialog.findViewById(R.id.dialog_button_clear);
register = (ButtonFlat)registration_dialog.findViewById(R.id.Dialog_button_register);
//seq_answer = (EditText)registration_dialog.findViewById(R.id.editText1);*/
/*spinner_check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(MainActivity.this, "Checking", Toast.LENGTH_LONG).show();
}
});*/
registration_dialog.show();
}
});
}}
This is my dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<EditText
android:id="#+id/Dialog_edittext_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Dialog_edittext_username"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/Dialog_edittext_confirmpassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Dialog_edittext_password"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword" />
<EditText
android:id="#+id/Dialog_edittext_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="User Name" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="345dp"
android:layout_height="60dp"
android:layout_below="#+id/Dialog_edittext_confirmpassword"
android:layout_centerHorizontal="true" />
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/Dialog_button_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/dialog_button_clear"
android:text="Register"
android:textColor="#ffffff" >
</com.gc.materialdesign.views.ButtonFlat>
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/dialog_button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginRight="52dp"
android:layout_marginTop="59dp"
android:layout_toLeftOf="#+id/Dialog_button_register"
android:text="clear"
android:textColor="#ffffff" >
</com.gc.materialdesign.views.ButtonFlat>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/spinner1"
android:layout_marginTop="67dp"
android:ems="10"
android:hint="Enter your answer for security question" />
</RelativeLayout>
Same problem i faced but i solved that problem with following solutions
-> If you are implementing in Eclipse once clean the project then test it.
-> If above also not works once restart your Eclipse.
Hope it will helps you.

Android Gesture Recognition bad

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.

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.

Populating Custom List View with data from multiple Edit Texts

So I made a form with 3 EditTexts and two Buttons, "Ok" and "Cancel"
Here's my Code for it:
package com.examples.edtTxtCustomList;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class InputPage extends Activity implements OnClickListener{
private Button btnGo,btnCancel;
EditText et_name,et_email,et_phone;
List<String> data = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
et_name = (EditText) findViewById(R.id.et_name);
et_email =(EditText)findViewById(R.id.et_email);
et_phone = (EditText)findViewById(R.id.et_phno);
btnGo = (Button) findViewById(R.id.btn_ok);
btnGo.setOnClickListener(this);
data.add("Welcome");
}
#Override
public void onClick(View src) {
String nm=et_name.getText().toString();
String no = et_email.getText().toString();
String mail= et_phone.getText().toString();
Intent i = new Intent(this,CustomLayout.class);
i.putExtra("name", nm);
i.putExtra("email", mail);
i.putExtra("phone", no);
startActivity(i);
}
} `
And here's the XML file of my form:
<?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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip"
android:text="Name :"
android:textSize="20dip"/>
<EditText
android:id="#+id/et_name"
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv_name"
android:layout_alignTop="#id/tv_name"
android:layout_marginBottom="10dip"
android:layout_marginLeft="8dip"
android:textSize="15dip"/>
<TextView
android:id="#+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip"
android:layout_below="#id/tv_name"
android:text="E-Mail :"
android:textSize="20dip"/>
<EditText
android:id="#+id/et_email"
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv_email"
android:layout_alignTop="#id/tv_email"
android:layout_below="#id/et_name"
android:inputType="textEmailAddress"
android:layout_marginLeft="6dip"
android:layout_marginBottom="10dip"
android:textSize="15dip"/>
<TextView
android:id="#+id/tv_phno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip"
android:layout_below="#id/tv_email"
android:text="Phone :"
android:textSize="20dip"/>
<EditText
android:id="#+id/et_phno"
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv_phno"
android:layout_alignTop="#id/tv_phno"
android:layout_below="#id/et_email"
android:inputType="phone"
android:layout_marginLeft="5dip"
android:layout_marginBottom="10dip"
android:textSize="15dip"/>
<Button
android:id="#+id/btn_ok"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/et_phno"
android:layout_marginTop="30dip"
android:layout_marginLeft="50dip"
android:text="OK"
android:textSize="20dip"/>
<Button
android:id="#+id/btn_cancel"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/et_phno"
android:layout_marginTop="30dip"
android:layout_marginRight="50dip"
android:text="Cancel"
android:textSize="20dip"/>
</RelativeLayout>
</LinearLayout>
now what I want is,when i click on "OK" all the data from all three of the edit texts should be displayed in the customlistview.
I tried to make a dynamic listview as follows:
package com.example.customlist;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class CustomListLayoutActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.custom_row_layout,
new String[] {"Name","Email","Phone"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
populateList();
setListAdapter(adapter);
}
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
private void populateList() {
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("Name","Anurag Kulkarni");
temp.put("Email", "kulkarni_anurag#yahoo.com");
temp.put("Phone", "+91-9904475805");
list.add(temp);
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("Name","Rahul Shah");
temp1.put("Email", "rahul201290#gmail.com");
temp1.put("Phone", "+91-9898434909");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("Name","Pratik Thakkar");
temp2.put("Email", "iamcool#cooldood.com");
temp2.put("Phone", "+91-8539524925");
list.add(temp2);
HashMap<String,String> temp3 = new HashMap<String,String>();
temp3.put("Name","Utsav Patel");
temp3.put("Email", "patel_in_a_hotel#dhokla.com");
temp3.put("Phone","+91-9843500999");
list.add(temp3);
HashMap<String,String> temp4 = new HashMap<String,String>();
temp4.put("Name","Karan Mohan");
temp4.put("Email", "masala_dosa#sankalp.co.in");
temp4.put("Phone", "+91-9944843974");
list.add(temp4);
}
}
Can you please help me with it? I am still learning the basics,so any help would be appriciated.
Thanks.
As in another post of mine...
Whenever you want to do processing with the views in a ListView you
need to create a custom adapter that will handle your logic
implementation and pass that information to the views as necessary.
A custom adater would inflate the views piece by piece, this can be
dynamic of fixed.
Example:
Link 1

Categories

Resources