Menu with Radio Button

Menu with Radio Button Example in Android

MainClass

package com.example.malik.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

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

}

public void selectNetwork(View view) {
registerForContextMenu(view);
openContextMenu(view);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.network_menu, menu);
MenuItem item_wifi = menu.findItem(R.id.id_wifi);
MenuItem item_bluetooth = menu.findItem(R.id.id_bluetooth);
MenuItem item_mobile_data = menu.findItem(R.id.id_mobile_data);
if(item_selection == 1){
item_wifi.setChecked(true);
} else if(item_selection == 2){
item_bluetooth.setChecked(true);
} else if(item_selection == 3){
item_mobile_data.setChecked(true);
}

}

@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.id_wifi:
Toast.makeText(getApplicationContext(), “Wifi selected”, Toast.LENGTH_SHORT).show();
item.setChecked(true);
item_selection = 1;
return true;

case R.id.id_bluetooth:
Toast.makeText(getApplicationContext(), “Wifi bluetooth”, Toast.LENGTH_SHORT).show();
item.setChecked(true);
item_selection = 2;
return true;

case R.id.id_mobile_data:
Toast.makeText(getApplicationContext(), “Wifi mobile data”, Toast.LENGTH_SHORT).show();
item.setChecked(true);
item_selection = 3;
return true;

}

return super.onContextItemSelected(item);
}
}

MainXML

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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”
tools:context=”com.example.malik.test.MainActivity”
android:id=”@+id/relative_layout”
>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Network selection”
android:id=”@+id/button”
android:layout_marginTop=”88dp”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:onClick=”selectNetwork”
/>
</RelativeLayout>

 

network_menu XML

<?xml version=”1.0″ encoding=”utf-8″?>
<menu xmlns:android=”http://schemas.android.com/apk/res/android”>

<group android:checkableBehavior=”single”>
<item
android:id=”@+id/id_wifi”
android:title=”Use Wifi”></item>
<item
android:id=”@+id/id_bluetooth”
android:title=”Use Bluetooth”></item>
<item
android:id=”@+id/id_mobile_data”
android:title=”Use Mobile Data”></item>
</group>

</menu>

 

Output :

pic

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.