Share array item from fragment

+2 lonlon . · December 8, 2015
Hello,
I've a problem with sharing array item from fragment  in swipe view app, I don't know how to implement that correctly.
Please help me!

Post a Reply

Replies

Oldest  Newest  Rating
0 lonlon . · December 10, 2015
@Gary Whitney

Thank you for trying helping me! I really appreciate it.
+1 Gary Whitney · December 10, 2015
Now you are providing some serious details.
I am not familiar with with pager view as I am a novice and have only focused on what I need to know.

However I think these links may help you.
http://developer.android.com/training/sharing/send.html

http://developer.android.com/training/sharing/shareaction.html
0 lonlon . · December 9, 2015
@Gary Whitney 
Thank you for your reply!
Actually I used pager view and I want to enable share intent for each page  in order to share the text with social media apps.

 I just need to learn how to implement share intent correctly so I can share the text or image inside each page, now in Main Actvity.java when I try to press share the output is weird number and I don't know what is the right way to showing the content (text or images). 

Here is the code:  

Main Activity.java



package com.leenah.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    // SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link android.support.v4.view.ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;
    DevicePagerAdapter devicePagerAdapter;
    ShareActionProvider mShareActionProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        devicePagerAdapter= new DevicePagerAdapter(getSupportFragmentManager(),getApplicationContext());
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        //mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager_title_strip);
        //mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setAdapter(devicePagerAdapter);



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        // Retrieve the share menu item
        MenuItem menuItem = menu.findItem(R.id.action_share);

        // Get the provider and hold onto it to set/change the share intent.
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

        if(mShareActionProvider !=null){
            Log.d(MainActivity.class.getSimpleName(), "INTO share Provider--" + mShareActionProvider.toString());
            mShareActionProvider.setShareIntent(createShareIntent());
        }
        else{
            Log.d(MainActivity.class.getSimpleName(), "NO share Provider--");
        }
        return true;

    }

    private Intent createShareIntent() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_result) + "\n" + (R.id.tvDeviceDescription)
        + "\n" + getString(R.string.about_you));
        return shareIntent;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }




}

Device Fragment.java


package com.leenah.app;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Lonlon on 12/06/2015.
 */
public class DeviceFragment extends android.support.v4.app.Fragment {
    public static final String ImageIDKey ="imagekey";
    public static final String DescriptionKey ="descriptionkey";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_device,container,false);
        Bundle bundle=getArguments();
        if(bundle != null){
            int imageID=bundle.getInt(ImageIDKey);
            String description= bundle.getString(DescriptionKey);
            setValues (view,imageID,description);

        }
        return view;
    }

    private void setValues(View view, int imageID, String description) {
        ImageView imageView = (ImageView) view.findViewById(R.id.ImageViewDevice);
        imageView.setImageResource(imageID);
        TextView textView=(TextView)view.findViewById(R.id.tvDeviceDescription);
        textView.setText(description);

    }
}

DevicePagerAdapter.java


package com.leenah.app;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * Created by Lonlon on 12/07/2015.
 */
public class DevicePagerAdapter extends FragmentPagerAdapter {
    String[] devices;
    String[] devicesDescription;

    public DevicePagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        Resources resources = context.getResources();
        devices = resources.getStringArray(R.array.devices);
        devicesDescription = resources.getStringArray(R.array.device_description);

    }

    @Override
    public Fragment getItem(int position) {
        Bundle bundle = new Bundle();
        bundle.putString(DeviceFragment.DescriptionKey,devicesDescription[position]);
        bundle.putInt(DeviceFragment.ImageIDKey,getDeviceImageID(position));

        DeviceFragment deviceFragment = new DeviceFragment();
        deviceFragment.setArguments(bundle);
        return deviceFragment;
    }
private int getDeviceImageID(int position){
    int id =0;
    switch (position){
        case 0:
            id = R.drawable.android;
            break;
        case 1:
            id = R.drawable.windowsphone;
            break;
        case 2:
            id = R.drawable.apple;
            break;
    }
    return id;
}
    @Override
    public CharSequence getPageTitle(int position) {
        return devices[position];
    }

    @Override
    public int getCount() {
        return devices.length;
    }
}

activity_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager_title_strip"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="#62FA3C"
    tools:context=".MainActivity">
</android.support.v4.view.ViewPager>

fragment_device.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:id="@+id/ImageViewDevice"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/android"/>
<TextView
    android:id="@+id/tvDeviceDescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   />


</LinearLayout>

res>values>Array.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="devices">
        <item>windows Phone - Wp</item>
        <item> Android - Linux</item>
        <item>iPhone - Apple</item>
    </string-array>
    <string-array name="device_description">
        <item>number1</item>
        <item>number2</item>
        <item>number3</item>

    </string-array>
</resources>

res>menu>main_menu.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

String.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">App</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="action_share">Share</string>
    <string name="share_result">something</string>
    <string name="about_you">somthing</string>


</resources>

0 Gary Whitney · December 8, 2015
Not sure what you are asking?
Fragments shouldn't talk to one another.
Swipes are Gestures. They are detected by velocity and direction.
Or are you scrolling a list and you want to forward the selection to a global variable?
OR None of the above.

I'm just saying: To get specific help you need to provide specific information.
The only advise I can offer you based on your post is to watch the tutorials on lists, meme fragment, and gestures.
  • 1

Java / Android Development

107,140 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator