I create custom view for action bar: action_bar_bets.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:weightSum="2"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/actionBarProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="10dp"
android:src="#drawable/ic_action_user" />
<ImageButton
android:id="#+id/actionBarBets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="10dp"
android:src="#drawable/ic_action_betslip" />
</LinearLayout>
here is my menu: action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/mybetsCount"
android:actionLayout="#layout/action_bar_bets"
android:showAsAction="always"/>
</menu>
I create action bar in code:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
View view = getLayoutInflater().inflate(R.layout.action_bar_bets, null);
actionBar.setCustomView(view)
I tried set OnClickListener to ImageButtons:
like this:
ImageButton profile = (ImageButton) view.findViewById(R.id.actionBarProfile);
profile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Profile", Toast.LENGTH_SHORT).show();
}
});
and like this:
actionBar.getCustomView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.actionBarProfile:
Toast.makeText(getApplicationContext(), "Profile", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
But nothing happened
Can you help me to fix this problem?
ADD
Also in this class i have ListNavigation
PackageManager pm = getPackageManager();
String label;
try {
ActivityInfo activityInfo = pm.getActivityInfo(getComponentName(),
0);
label = activityInfo.loadLabel(pm).toString();
} catch (NameNotFoundException e) {
label = null;
e.printStackTrace();
}
String[] navigations = { label, "Sports", "Profile" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(), R.layout.custom_spinner_title_bar,
android.R.id.text1, navigations);
adapter.setDropDownViewResource(R.layout.custom_spinner_title_bar);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition,
long itemId) {
switch (itemPosition) {
case 1:
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
break;
case 2:
intent = new Intent(getApplicationContext(),
Activity_profile.class);
startActivity(intent);
break;
default:
break;
}
return false;
}
};
actionBar.setListNavigationCallbacks(adapter, navigationListener);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
return true;
}
im not sure where you initialize your buttons, but when it is in something like onCreate() then replace
ImageButton profile = (ImageButton) view.findViewById(R.id.actionBarProfile);
with
ImageButton profile = (ImageButton) findViewById(R.id.actionBarProfile);
Try this. This is example with a Switch but it's the same way for other component
getMenuInflater().inflate(R.menu.export, menu);
MenuItem item = menu.findItem(R.id.myswitch);
Switch switchOnActionBar = (Switch)item.getActionView().findViewById(R.id.switchForActionBar);
switchOnActionBar.setOnCheckedChangeListener(this);
SwitchForActionBar is in switch_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<Switch
android:id="#+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Manuel"
android:textOn="Auto"
android:gravity="center_vertical"
android:thumb="#drawable/switch_thumb"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:background="#android:color/transparent"
android:layout_marginRight="10dp"/>
</RelativeLayout>
And this is the menu layout
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/myswitch"
android:title=""
android:checkable="true"
android:showAsAction="always"
android:actionLayout="#layout/switch_layout"
/>
</menu>
try ontouchlistener for imagebutton :
Change
setOnClickListener(new OnClickListener()
to :
setOnTouchListener(new OnTouchListener()
I found where i has error.
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
actionBar.setCustomView(view, lp);
and delete this code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
return true;
}
My navigation list overlaps the buttons
Thank you all for the help
Related
I am trying to display a pop up window when i click on a menu overflow item. I have written some code also for that. But it outputs nothing.
Here is some code:
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.settings_id:
displayPopupWindow();
return true;
case R.id.about_us_id:
return true;
case R.id.logout_id:
startActivity(new Intent(HomePage.this,MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void displayPopupWindow() {
PopupWindow popup = new PopupWindow(this);
View layout = getLayoutInflater().inflate(R.layout.popup, null);
popup.setContentView(layout);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
My Xml file Code is below:
<?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:background="#color/colorPrimary">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="165dp"
android:id="#+id/textView"
tools:text="This is a Pop Up Window!"
android:textSize="36sp"
android:textStyle="normal|bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:textAlignment="center"
android:fontFamily="sans-serif" />
</RelativeLayout>
Where is the error? Please help me.
Here is your solution,
MainActivity.java
public class MainActivity extends Activity {
private PopupWindow mPopupWindow;
private Button btnClosePopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
break;
case R.id.gamepad:
Toast.makeText(getBaseContext(), "You selected Gamepad", Toast.LENGTH_SHORT).show();
break;
case R.id.camera:
Toast.makeText(getBaseContext(), "You selected Camera", Toast.LENGTH_SHORT).show();
initiatePopupWindow();
break;
case R.id.video:
Toast.makeText(getBaseContext(), "You selected Video", Toast.LENGTH_SHORT).show();
break;
case R.id.email:
Toast.makeText(getBaseContext(), "You selected EMail", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private void initiatePopupWindow() {
try {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,
(ViewGroup) findViewById(R.id.popup_element));
mPopupWindow = new PopupWindow(layout, 300, 370, true);
mPopupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mPopupWindow.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
popup.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp">
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="Hello!" />
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
I am trying to implement multiple items selection in a ListView. The List is managed using the CursoAdapter. I was following the tutorials at http://developer.android.com/guide/topics/ui/menus.html#CAB. However, when I run the app, only 1 item is selected (highlighted). Can you point out, What am I doing wrong?
MainActivity.java
public class MainActivity extends ActionBarActivity {
DbHelper db;
ListView myList;
ActionMode mActionMode;
int checkedCount;
private static final String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DbHelper(this);
myList = (ListView) findViewById(R.id.newList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, GENRES);
myList.setAdapter(adapter);
loadData();
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
myList.setItemsCanFocus(false);
myList.setOnLongClickListener(new View.OnLongClickListener() {
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
// Start the CAB using the ActionMode.Callback defined above
// mActionMode = getActivity().startActionMode(mActionMode.Callback);
view.setSelected(true);
view.setBackgroundColor(25);
return true;
}
});
myList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
// Log.v(ListView.getCheckedItemPositions());
if(checked)
{
checkedCount++;
}
else checkedCount--;
mode.setTitle(checkedCount+" selected");
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
// deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
menu.clear();
checkedCount=0;
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_main, menu);
return true;
}
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
}
public void onResume()
{
super.onResume();
loadData();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.clear();
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#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);
}
public void loadData()
{
Cursor cursor = null;
try {
cursor = db.fetchData();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.tasks,
cursor,
new String[]{db._ID, db.COLUMN_1, db.COLUMN_2},
new int[]{R.id.idnum, R.id.c1, R.id.c2}, 0);
myList.setAdapter(myAdapter);
}
public void addNew(View v)
{
Intent intent = new Intent(this,AddActivity.class);
startActivity(intent);
}
}
activity_main.xml
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/welcome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:id="#+id/topic"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/newList"
android:layout_below="#+id/topic"
android:layout_above="#+id/MainButtons"
android:choiceMode="multipleChoice"
android:listSelector="#color/background_material_dark"
></ListView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/MainButtons"
>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/add"
android:id="#+id/addButton"
android:onClick="addNew"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/edit"
android:id="#+id/editButton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/del"
android:id="#+id/deleteButton"
/>
</LinearLayout>
</RelativeLayout>
tasks.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/idnum"
android:paddingRight="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/c1"
android:layout_toRightOf="#+id/idnum"
android:paddingRight="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/c2"
android:layout_toRightOf="#+id/c1"
/>
</RelativeLayout>
menu_main.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="always"
/>
</menu>
Please comment if I need to add more details.
Edit: I looked at the duplicate question. However, majority of answers suggested using an ArrayAdapter, whereas items in my list are managed using SQLite, and each item consists of 3 separate items (no, text1, text2). How can I make use of an ArrayAdapter here? Also, one highly voted answer says to use CheckedTextView. So shall I use CheckedTextView for all the 3 parts of a list item?
I finally solved this by using a selector. Here is the selector.xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#color/link_text_material_dark"
/>
<item android:state_active="true"
android:drawable="#color/primary_dark_material_dark"
/>
<item android:state_selected="true"
android:drawable="#color/highlighted_text_material_dark"
/>
<item android:state_activated="true" android:drawable="#android:color/white" />
</selector>
The most important one is the state_activated item. It is used to highlight all those items which are selected.
Also, in order to use this selector, add the following line to tasks.xml or wherever you have defined each row (item):
android:background="#drawable/selector"
I have modified you code, added little changes. If you still have issues, please follow the sample in you sdk samples folder it will be in the following location.
sdks\samples\android-14\ApiDemos\src\com\example\android\apis\view\List16.java
public class MainActivity extends ActionBarActivity {
private static final String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",![enter image description here][2]
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myList = (ListView) findViewById(R.id.newList);
/**
* the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on ListView
* couple with the new simple_list_item_activated_1 which uses a highlighted border for selected
* items.
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, GENRES);
myList.setAdapter(adapter);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
myList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.action_settings:
// deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
}
}
![enter image description here][1]
menu_main.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="always" android:icon="#drawable/ic_launcher" />
</menu>
activity_main.xml is same as your layout.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:icon="#drawable/ic_launcher"
android:title="#string/action_settings"/>
<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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="62dp"
android:layout_marginTop="50dp"
android:text="Show Popup" />
//
/res/layout/activity_main.xml
public class MainActivity extends Activity {
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
//popup.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon);
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//this is main activity
popup working properly but android:icon="#drawable/ic_launcher" not working no image/icon appearing in popmenu, Please tell me how can i set icon in popmenu in android .
i am new in android.
Change line:
android:showAsAction="never"
to
android:showAsAction="ifRoom"
or
android:showAsAction="always"
Help me out to show Actionbar menu item on the Top of the Actionbar. Without showing on overflow drop down menulist.kindly give me a suggestion to do like that?
Use this in your activity
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
LayoutParams lp = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
View customNav = LayoutInflater.from(this).inflate(R.layout.img, null);
EditText et = (EditText) customNav.findViewById(R.id.et);
temppaths = new ArrayList<String>();
Button iv = (Button) customNav.findViewById(R.id.iv);
iv.setClickable(true);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);
and xmlLayout name it img.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:gravity="right" >
<EditText android:id="#+id/et"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:gravity="center"
android:layout_gravity="right"
android:hint="info#xxxxxx, 1800xxxxx"
android:background="#FF8000" />
<Button
android:id="#+id/iv"
android:layout_gravity="right"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:text="Search"
android:background="#FF8000" />
</LinearLayout >
okk then
add these methods in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "LogOut").setShortcut('0', 'o')
.setIcon(android.R.drawable.ic_menu_edit);
menu.add(0, 1, 0, "Save").setShortcut('1', 's')
.setIcon(android.R.drawable.ic_menu_save);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
Toast.makeText(getApplicationContext(), "Log out clicked", 0).show();
// save();
// showNewOrOpenDialog();
break;
case 1:
Toast.makeText(getApplicationContext(), "Save", 0).show();
// save();
break;
}
return false;
}
I have an ActionLayout for a searchIcon of the MenuItems. This ActionLayout has an AutoCompleteTextView which has imeOption = "actionSearch".
But when i click the menuItems and ActionLayout appears, the soft keyboard pops up, but it still has newline key instead of searchIcon.
Activity code:
private AutoCompleteTextView mEtSearchbar;
private MenuItem mSearchbar;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchIcon:
mEtSearchbar.clearFocus();
(new Handler()).postDelayed(new Runnable() {
public void run() {
mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN, 0, 0, 0));
mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
0, 0, 0));
}
}, 100);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
inflater.inflate(R.menu.m_menu, menu);
mSearchbar = (MenuItem) menu.findItem(R.id.searchIcon);
View actionview = mSearchbar.getActionView();
mEtSearchbar = ((AutoCompleteTextView) actionview
.findViewById(R.id.search_editText));
final ImageView searchImage = ((ImageView) actionview
.findViewById(R.id.search_image));
searchImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String s = mEtSearchbar.getText().toString();
Intent intent = new Intent(getSherlockActivity(),
SearchActivity.class);
intent.putExtra("search_string", s);
startActivity(intent);
}
});
mEtSearchbar
.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
searchImage.performClick();
return true;
}
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
*menu layou*t
m_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/searchIcon"
android:actionLayout="#layout/search_icon_actionview"
android:icon="#drawable/search_icon"
android:showAsAction="ifRoom|collapseActionView"
android:title="Search"/>
<item
android:id="#+id/notificationIcon"
android:icon="#drawable/notification_icon"
android:orderInCategory="0"
android:showAsAction="ifRoom|collapseActionView"
android:title="Notifications"/>
</menu>
search_icon_actionview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/search_image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:clickable="true"
android:contentDescription="Search icon"
android:src="#drawable/search_icon" />
<AutoCompleteTextView
android:id="#+id/search_editText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="#id/search_image"
android:hint="Search"
android:imeOptions="actionSearch" />
</RelativeLayout>
How to have a searchIcon or atleast a done button on the soft keyboard, which on click must perform the click of the searcIcon i have in ActionLayout
Thank You
I have to add
android:singleLine="true"
also