🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / Android / CONTEXT MENU KULLANIMI ÜZERİNE BASIK TUTTUĞUMUZDA ÇIKAR

 

1-) Android - CONTEXT MENU KULLANIMI ÜZERİNE BASIK TUTTUĞUMUZDA ÇIKAR

 

1-)itemler= 1 adet ListView

 

2-)string.xml in içi

 

   <resources>

 

    <string name="app_name">Haber</string>

 

    <string name="hello_world">Hello world!</string>

 

    <array name="personel">

 

    <item>Ali</item>

 

    <item>Veli</item>

 

    <item>Ayşe</item>

 

    <item>Ahmet</item>

 

    <item>Leyla</item>

 

    <item>Can</item>

 

   </array>

 

   </resources>

 

3-)listemiz.xml dosyamızı layout'dan textview şeklinde oluştur içi

 

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

 

   <TextView xmlns:android="http://schemas.android.com/apk/res/android"

 

    android:layout_width="match_parent"

 

    android:layout_height="match_parent"

 

    android:background="#4A148C"

 

    android:textColor="#FFFFFF"

 

    >

 

   </TextView>

 

4-)sol taraftan menu oluştur ismi menu_contex.xml içi

 

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

 

    android:orderInCategory="0"

 

    android:title="sil"

 

    app:showAsAction="never"

 

    />

 

    <item

 

        android:id="@+id/paylas"

 

        android:orderInCategory="0"

 

        android:title="paylaş"

 

        app:showAsAction="never"

 

        />

 

    <item

 

        android:id="@+id/duzenle"

 

        android:orderInCategory="0"

 

        android:title="düzenle"

 

        app:showAsAction="never"

 

        /></menu>

 

5-)OnCratın bulundugu yere gel ve hepsini kopyala yapıştır

 

    public class MainActivity extends AppCompatActivity {

 

    ListView liste;

 

    ArrayAdapter<String> adapter;

 

    @Override

 

    protected void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);

 

        setContentView(R.layout.activity_main);

 

        String personel[] = getResources().getStringArray(R.array.personel);

 

        liste = (ListView) findViewById(R.id.listView);

 

        adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.listemiz, personel);

 

        liste.setAdapter(adapter);

 

        registerForContextMenu(liste);    }

 

    @Override

 

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

 

        super.onCreateContextMenu(menu, v, menuInfo);

 

        getMenuInflater().inflate(R.menu.menu_contex, menu);    }

 

    @Override

 

    public boolean onContextItemSelected(MenuItem item) {

 

        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

 

        switch (item.getItemId()){

 

            case R.id.sil:

 

                TextView tv= (TextView) menuInfo.targetView;

 

                Toast.makeText(getApplicationContext(),"item silindi " + tv.getText(),Toast.LENGTH_SHORT).show();

 

                return  true;

 

            case R.id.duzenle:

 

                TextView tv1= (TextView) menuInfo.targetView;

 

                Toast.makeText(getApplicationContext(),"item duzenle "+tv1.getText(),Toast.LENGTH_SHORT).show();

 

                return  true;

 

            case R.id.paylas:

 

                TextView tv2= (TextView) menuInfo.targetView;

 

                Toast.makeText(getApplicationContext(),"item paylas "+tv2.getText(),Toast.LENGTH_SHORT).show();

 

                return  true;        }

 

        return super.onContextItemSelected(item);    }}

 2021 Ocak 18 Pazartesi
 400