Android sharing View Flipper content with other apps (twitter, gmail etc..) - android

I am trying to share a flip view content with other apps such as twitter, gmail etc. However the share button works but does not show the flashcard content instead it shows a text that says "android.widget.ViewFlipper#40523fc0". Any help please I have been struggling with for a while. Below is my Java code. The xml code contains only couple buttons and a text view.
public class Jokes extends Activity implements OnClickListener, OnTouchListener {
ViewFlipper flippy;
Random r = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jokes);
flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
flippy.setOnClickListener(this);
Button b = (Button) findViewById(R.id.bShare);
b.setOnTouchListener(this);
String s [ ]= {"abc", "DEF", "ghi"};
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int i = r.nextInt();
flippy.setDisplayedChild(r.nextInt(4));
flippy.showNext();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, ""+ flippy);
startActivity(Intent.createChooser(intent, "Udir/Share"));
return true;
}
}

I propose you to store the data you want to share as the tag of buttons.
b.setTag("abc"); // Whatever you want to share
This way you can get the data from the button in the event callback.
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.bShare: {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, (String) v.getTag());
startActivity(Intent.createChooser(intent, "Udir/Share"));
break;
}
}
}

Related

Allow command on "if statement" being satisfied

I currently have a command that will allow the user to proceed to the next page if a button is clicked, this button has a feature android:onClick="advancenext" which I have defined in the .java file to be the following
public void advancenext (View view) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
However, I only want the user to call advancenext if a variable i is greater than 5. This is what I tried
if (i>5) {
public void advancenext (View view) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
However, when I run the app, I can still advance even though i is not greater than 5. Does anyone know how to fix this, or better restrict advancenext in a different way?
EDIT Thanks for the great answers, but I encountered a different error when I changed my statement. When I try:
public void advancenext (View view) {
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
I get the errors Syntax error on token "(", ; expected and Syntax error on token ")", ; expected around the ( and ) of (View view).
EDIT dos Here's my full code
public class Prompt1 extends Activity {
int i; //Variable i stores the touch number
float[] X, Y; //Array that will store the touchpoints
int ultscore1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prompt1);
i=1;
touchLayout.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
i++; //increasing i value
public void advancenext (View view) {
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
}
return true;
}
});
}
I would do this
public class Prompt1 extends Activity implements OnTouchListener{
int i; //Variable i stores the touch number
float[] X, Y; //Array that will store the touchpoints
int ultscore1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prompt1);
i=1;
touchLayout.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event){
i++; //increasing i value
advancenext(v);
return true;
}
public void advancenext (View view) {
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
}
This allows your class to directly handle the touch event (and any other touch events for views contained in the classes set content view) by just adding the on touch listener to the view. You can handle the incrementing of your variable i in onTouch like you were and call your advancenext method with the view passed to onTouch. The method, advancenext, could very easily have its functionality moved to onTouch, that is
#Override
public boolean onTouch(View v, MotionEvent event){
i++; //increasing i value
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
return true;
}
But moving the functionality out of onTouch into it's own method will allow you to use advancenext with other io events (like onClick).
You have to evaluate your if statement inside your function. Be sure you have access to i variable.
public void advancenext(View view) {
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
You've got your statement backwards:
public void advancenext (View view) {
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
You should call your onClick() function no matter what, THEN check the condition. The way you have it now, you are ignoring the if statement completely.

Implementation of Splash screen with the help of Touch Events

I am following This Tutorial of implementing splash screen in my android project and i want splash screen is closed whenever user tap two time on screen. If this can be done using touch events then how i used them ?
You'll need to take a look at 'onInterceptTouch' from there you can 'finish();' as usual
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29
You could also set the LinearLayouts android:clickable="true"
and then use an onclicklistner
final LinearLayout MYLAYOUT = (LinearLayout) findViewById(R.id.MYLAYOUT);
MYLAYOUT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
You might also have to add android:duplicateParentState="true" to all the views INSIDE your linear Layout
The easiest way will be to use a gesturedetector.
Add an id to your splash LinearLayout so that you can get a reference to it in code
Then in onCreate of MainActivity you'll need to do:
#Override
public void onCreate() {
super.onCreate();
...
LinearLayout splashBg = (LinearLayout)findViewById(R.id.splash_bg);
GestureListener mGestureListener = new GestureListener();
GestureDetector mGestureDetector = new GestureDetector(this, mGestureListener);
splashBg.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
});
}//end onCreate
You'll need to create this custom GestureListener to listen for the double tap and then close the splash and continue to the next activity
private class GestureListener implements GestureDetector.OnDoubleTapListener {
#Override
public boolean onDoubleTap(MotionEvent e) {
MainActivity.this.finish();
if (!mIsBackButtonPressed) {
// start the home activity
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
MainActivity.this.startActivity(intent);
}
return true;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
}
use this code to set splashscreen
int touchcount=0;
LinearLayout layout;
layout=(LinearLayout) findViewById(R.id.main);
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
touchevent++;
if(touchcount==2)
{
Intent intent=new Intent(youractivity,this,nextactivity.class);
startActivity(intent);
finish();
}
return false;
}
});

How to change the button color of a gridView

I have a Gridview displaying buttons that get their content from an array, I would like to change the background color of these buttons when clicking/pressed on it. I tried onClick and onTouch, it does go in to the method, but color is not background is not set. What is wrong with my code? Please help.
private class ImageAdapter extends BaseAdapter {
private Context mContext;
btnView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("onClick","go");
btnView.setBackgroundColor(Color.rgb(12,11,12));
btnView.setBackgroundColor(Color.parseColor("#3614B3"));
Intent data = new Intent();
data.setData(Uri.parse("PictureStyle" + position));
setResult(RESULT_OK, data);
}
});
btnView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.d("OnTouch","go2");
btnView.setBackgroundColor(Color.parseColor("#3614B3"));
return false;
}
});
btnView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("Touch listener",String.valueOf(position));
btnView.setBackgroundColor(0xff00ff00);
Log.d("Touch listener","set");
if ( event.equals(MotionEvent.ACTION_DOWN) ) {
btnView.setBackgroundColor(0xffff0000);
}
return false;
}
});
try it with v.setBackgroundColor(Color.rgb(12,11,12)); in your onClick
You better use GridView.setOnItemClickListener instead of register a clickListener on each Button

Drag and drop images in gridview in android

I'am developing a sample android application to learn about drag & drop in android. On start of the app, i'am displaying few images on a grid view. Now i need to drag one image at a time over to the place of another. After dropping an image over another, the images should swap its places. How can i achieve it ? Please guide/help me.
You can easily achieve this by using thquinn's DraggableGridView
You can add your custom layout
public class DraggableGridViewSampleActivity extends Activity {
static Random random = new Random();
static String[] words = "the of and a to in is be that was he for it with as his I on have at by not they this had are but from or she an which you one we all were her would there their will when who him been has more if no out do so can what up said about other into than its time only could new them man some these then two first may any like now my such make over our even most me state after also made many did must before back see through way where get much go well your know should down work year because come people just say each those take day good how long Mr own too little use US very great still men here life both between old under last never place same another think house while high right might came off find states since used give against three himself look few general hand school part small American home during number again Mrs around thought went without however govern don't does got public United point end become head once course fact upon need system set every war put form water took".split(" ");
DraggableGridView dgv;
Button button1, button2;
ArrayList<String> poem = new ArrayList<String>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dgv = ((DraggableGridView)findViewById(R.id.vgv));
button1 = ((Button)findViewById(R.id.button1));
button2 = ((Button)findViewById(R.id.button2));
setListeners();
}
private void setListeners()
{
dgv.setOnRearrangeListener(new OnRearrangeListener() {
public void onRearrange(int oldIndex, int newIndex) {
String word = poem.remove(oldIndex);
if (oldIndex < newIndex)
poem.add(newIndex, word);
else
poem.add(newIndex, word);
}
});
dgv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
Toast.makeText(getApplicationContext(), "On clicked" +position, Toast.LENGTH_SHORT).show();
dgv.removeViewAt(position);
poem.remove(position);
}
});
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String word = words[random.nextInt(words.length)];
addView();
poem.add(word);
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String finishedPoem = "";
for (String s : poem)
finishedPoem += s + " ";
new AlertDialog.Builder(DraggableGridViewSampleActivity.this)
.setTitle("Here's your poem!")
.setMessage(finishedPoem).show();
}
});
}
public void addView()
{
LayoutParams mLayoutParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout mLinearLayout= new LinearLayout(DraggableGridViewSampleActivity.this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
mLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
ImageView mImageView = new ImageView(DraggableGridViewSampleActivity.this);
if(dgv.getChildCount()%2==0)
mImageView.setImageResource(R.drawable.child1);
else
mImageView.setImageResource(R.drawable.child2);
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
mImageView.setLayoutParams(mLayoutParams);
mImageView.setId(dgv.getChildCount());
mImageView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), v.getId()+"clicked ", Toast.LENGTH_SHORT).show();
return dgv.onTouch(v, event);
}
});
TextView mTextView = new TextView(DraggableGridViewSampleActivity.this);
mTextView.setLayoutParams(mLayoutParams);
mTextView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), v.getId()+"clicked text ", Toast.LENGTH_SHORT).show();
return dgv.onTouch(v, event);
}
});
TextView mTextViewLabel = new TextView(DraggableGridViewSampleActivity.this);
mTextViewLabel.setText(((dgv.getChildCount()+1)+""));
mTextViewLabel.setLayoutParams(mLayoutParams);
mTextViewLabel.setId(dgv.getChildCount());
mTextViewLabel.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getApplicationContext(), v.getId()+"clicked text ", Toast.LENGTH_SHORT).show();
return dgv.onTouch(v, event);
}
});
mLinearLayout.setTag(mTextViewLabel);
mLinearLayout.addView(mTextViewLabel);
mLinearLayout.addView(mImageView);
mLinearLayout.addView(mTextView);
dgv.addView(mLinearLayout);
}
}

Click issue in android

In my application i am displaying rows dynamically.
I am using the below code so as to focus the clicked row even if the user press back key.
But the issue is like if the row is not focussed we need two clicka to navigate.First to make it focussable and then next click to navigate.I need all at once any suggestions.
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int a=0;a>16;a++)
{
tr[a].setBackgroundColor(Color.BLACK);
}
flag=v.getId();
if(v.getId()==1)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==3)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==5)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==7)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
if(v.getId()==100)
{
Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
startActivity(i);
}
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
for(int a=0;a>16;a++)
{
tr[a].setBackgroundColor(Color.BLACK);
}
if(hasFocus)
{
((TableRow)v).setBackgroundColor(Color.rgb(255, 180, 40));
}
else
{((TableRow)v).setBackgroundColor(Color.BLACK);}
}
protected void onResume() {
super.onResume();
for(int a=0;a>16;a++)
{
tr[a].setBackgroundColor(Color.BLACK);
}
tr[flag].requestFocus();
tr[flag].setFocusableInTouchMode(true);
tr[flag].setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus)
{
((TableRow)v).setBackgroundColor(Color.rgb(255, 180, 40));
}
else
{((TableRow)v).setBackgroundColor(Color.BLACK);}
}
});
}
#Override
public void onPause() {
super.onPause();
}
Please let me know your valuable suggestions.
Thanks in advance.
If your only need for focus listener is to change background colors, you would do better setting their background to a drawable that has different backgrounds for focused and normal states, then you don't need to control it in code at all. As for requiring two clicks to navigate, I don't see anything in your code about it. It seems to start an activity as soon as a view is clicked once.

Categories

Resources