المشاركة هي شيء مهم في حياتنا، لا سيما مشاركة التكنولوجيا والمعرفة. فمثلا إذا أنشأت تطبيقًا لتعديل الصور ، فستشارك الصور على تطبيقات الشبكات الاجتماعية بعد التعديل. هذه الوظيفة تعد الشيء الأكثر أهمية في تطوير (Android). لذلك، سأوضح لك في هذا المثال كيفية إضافة خيار “المشاركة” في تطبيقات (Android) باستخدام منصة أندرويد ستوديو.

 

  • الخطوة 1: إنشاء مشروع جديد

قم بالدخول الى تطبيق اندرويد ستوديو، و اختر مشروع جديد، تم اختر النشاط الأساسي(Basic Activity) ثم انقر فوق التالي.

  • الخطوة 2: activity_main.xml

في النشاط (activity_main.xml)، انسخ الكود التالي.

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout 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"  
    android:orientation="vertical"  
    tools:context="ganeshannt.sharing.MainActivity">  
  
    <EditText  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:hint="Enter the text that you want to share!"  
        android:textSize="20sp"  
        android:id="@+id/editText"/>  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_margin="80dp"  
        android:text="Share"  
        android:onClick="Clicked"  
        android:layout_gravity="center_horizontal"/>  
</LinearLayout>

 

  • الخطوة 2: MainActivity.java

في ملف MainActivity.java ، انسخ الكود التالي.

package your package name;  
  
import android.content.Intent;  
import android.os.Bundle;  
import android.support.v7.app.AppCompatActivity;  
import android.view.View;  
import android.widget.EditText;  
  
public class MainActivity extends AppCompatActivity {  
    private EditText editText;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        editText = (EditText)findViewById(R.id.editText);  
  
    }  
  
    public void Clicked(View view)  
    {  
       /* ACTION_SEND: Deliver some data to someone else. 
        createChooser (Intent target, CharSequence title): Here, target- The Intent that the user will be selecting an activity to perform. 
            title- Optional title that will be displayed in the chooser. 
        Intent.EXTRA_TEXT: A constant CharSequence that is associated with the Intent, used with ACTION_SEND to supply the literal data to be sent. 
        */  
        Intent sendIntent = new Intent();  
        sendIntent.setAction(Intent.ACTION_SEND);  
        sendIntent.putExtra(Intent.EXTRA_TEXT,editText.getText().toString());  
        sendIntent.setType("text/plain");  
        Intent.createChooser(sendIntent,"Share via");  
        startActivity(sendIntent);  
    }  
}

 

  • الخطوة 3: dimens.xml

قم بإنشاء ملف dimens.xml جديد في مجلد  (ملف ⇒ New ⇒Activity —values file file) في حالة لم تجده متوفرا لديك.

قم بنسخ الكود أدناه داخله.

<resources>  
    <!-- Default screen margins, per the Android Design guidelines. -->  
    <dimen name="activity_horizontal_margin">16dp</dimen>  
    <dimen name="activity_vertical_margin">16dp</dimen>  
</resources

الان قم بتجربة التطبيق.