Sitemap

Menüler

Options Menu Kullanımı ( Seçenekler Menüsü)

--

Options Menu activitelere bağlıdır. Her aktivitenin kendi options menusü olabilir. Default olarak Settings geliyor. ActionBar’daki üç nokta şeklinde gözükür.

android:orderInCategory="0"

menü itemlerını menüdeki sırasını belli etmek için kullanılan özellik.

app:showAsAction="always"

ifadesi actionbarda gözüküp gözükmediğini söyler. … yı açıp bakmadan actionbar’da gözükür. İconu varsa o gözükür. Üstüne basılı tutunca icon’un text’i de yazar.

app:showAsAction="ifRoom|withText"

anlamı ise; ifRoom icon için actionbar’da gösterebildiğini göster, diğerlerini …(owerflow menu) ile göster. withText ise menu isminin gösterebildiğini göster demek. [Mesela CTRL+F12 ile emülatörü yataya alınca icon da yazı da gözüküyor.]

menu_main.xml’in içerisi

<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="com.salihayesilyurt.udemycourseturkce.UcuncuEkran"
>
<item
android:id="@+id/action_settings"
android:icon="@android:drawable/ic_menu_preferences"
android:orderInCategory="0"
android:title="Ayarlar"
app:showAsAction="ifRoom|withText"
/>
<item
android:id="@+id/paylas"
android:icon="@android:drawable/ic_menu_share"
android:orderInCategory="2"
android:title="Paylaş"
app:showAsAction="ifRoom|withText"
/>
<item
android:id="@+id/sil"
android:icon="@android:drawable/ic_menu_delete"
android:orderInCategory="1"
android:title="Sil"
app:showAsAction="ifRoom|withText"
/>

</menu>

MainActivity.java dosyası

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "Ayarlar seçidi", Toast.LENGTH_SHORT).show();
break;
case R.id.paylas:
Toast.makeText(getApplicationContext(), "Paylas seçidi", Toast.LENGTH_SHORT).show();
break;
case R.id.sil:
Toast.makeText(getApplicationContext(), "Sil seçidi", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
Press enter or click to view image in full size
Press enter or click to view image in full size

Action Bar Kullanımı

onCreate() metodu

android.support.v7.app.ActionBar actionBar = getSupportActionBar();

dediğimizde daha önceki sürümlerde de kullanabilmek için. (support)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:angle="90"
android:endColor="@color/acikmavi"
android:startColor="@color/acikyesil"
></gradient>
</shape>

java dosyasında

android.support.v7.app.ActionBar ab = getSupportActionBar();
ab.setTitle("Baslik");
ab.setSubtitle("AltBaslik");
ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.arkaplan_actbar));

Contex Menu Kullanımı

Options Menu her aktivite de 1 tane olabilir. Contex Menu ise ne kadar view varsa o kadar, hangi elemanı istersek, ona contex menu verebiliriz.

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_context, menu);
}

Adapter’ı oluşturduğumuz listeye atadık .Görünümünü TextView şeklinde kendimiz layout oluşturarak ayarladık.

<?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="@color/yesil"
android:textColor="#FFFFFF"
android:padding="10dp"
>
</TextView>

string.xml’de personel isimli array oluşturduk.

registerForContextMenu(liste);

ile adapter eklenmiş listemizin her bir elamanı için ContextMenu oluşturduk.

Get Learning Zone’s stories in your inbox

Join Medium for free to get updates from this writer.

Açılan ContextMenu’den seçeneklere göre üzerine basılı tuttuğumuz liste elemanının text’ini aldık. →onContextItemSelected metodunu kullanarak

Her bir eleman Adapter ile oluşturulduğundan ve bunları seçerek ContextMenuyü açacağımız için →AdapterContextMenuInfo ifadesini kullanmamız ge.rekiyor

@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(),"Seçilen personel: "+ tv.getText().toString(),Toast.LENGTH_SHORT).show();
return true;
case R.id.duzenle:
return true;
case R.id.paylas:
return true;
}
return super.onContextItemSelected(item);
}

Yukardaki örnek basılı tutunca çıkan menu çeşidi. Şimdi de actionbar ile çıkan context menuye bakalım.

public class MainActivity extends ActionBarActivity {

private ActionMode actionMode;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tiklananYazi = (TextView) findViewById(R.id.tv);

tiklananYazi.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (actionMode != null) {
return false;
}
MyActionMode myActionMode = new MyActionMode();
actionMode = startActionMode(myActionMode);
v.setSelected(true);
return true;
}
});

}

class MyActionMode implements ActionMode.Callback {

//menüyü oluşturup, xml'e bagladık
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.menu_context, menu);
return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}

// actionbarda açılan menulerden herhangi biri seçilince
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.duzenle:
Toast.makeText(getApplicationContext(), "Düzenle iconuna tıklandı", Toast.LENGTH_LONG).show();
mode.finish();
return true;
case R.id.sil:
Toast.makeText(getApplicationContext(), "Sil iconuna tıklandı", Toast.LENGTH_LONG).show();
mode.finish();
return true;
default:
return false;
}
}

//actionbar context menu işlemi bitince
@Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
}
}

}

class MyActionMode implements ActionMode.Callback ifadesi ile inner class oluşturuldu. Bunun 4 tane implemente edilmesi gereken metodu var. Bu metodların açıklaması için → http://developer.android.com/reference/android/view/ActionMode.Callback.html

Aşağıdaki kodlar context menunun itemları. icon ile gösterdik. mode.getMenuInflater().inflate(R.menu.menu_context, menu); ifadesinde kullanılan menü.

<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="100"
android:title="Sil"
app:showAsAction="always"
android:icon="@android:drawable/ic_menu_delete"
/>
<item
android:id="@+id/duzenle"
android:orderInCategory="100"
android:title="Duzenle"
app:showAsAction="always"
android:icon="@android:drawable/ic_menu_edit"
/>

</menu>

HelloWorld yazısına uzun tıklanınca actionbar’da context menu açılıyor.
tiklananYazi.setOnLongClickListener(new View.OnLongClickListener() {}

--

--

Learning Zone
Learning Zone

learning zone, karalama defteri, anlık not