Action Bar change effect - android

I'm trying to reproduce the Google Keep, Gmail, etc ActionBar change effects.
Like image bellow, when the user select a note or a mail, the action bar changes using its effects.
I would like to do the same.
Any help?

Looks like it's relatively new ability, called Contextual action mode.
Simply put, it's just specific 'context' menu for long-selected item. I think that the link above provides enough information. However, below is some simple example on how to use it:
main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clipChildren="false"
android:id="#+id/root">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Do Long Press"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp" />
</RelativeLayout>
context_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:icon="#android:drawable/ic_menu_close_clear_cancel"
android:title="close"
android:showAsAction="always"
android:id="#+id/close_menu" />
</menu>
MyActivity.java:
public class MyActivity extends Activity implements ActionMode.Callback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button).setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(final View v) {
startActionMode(MyActivity.this);
return true;
}
});
}
#Override
public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
// TODO: auto-generated block
return false;
}
#Override
public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
switch (item.getItemId()) {
case R.id.close_menu:
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(final ActionMode mode) {
// TODO: auto-generated block
}
}
It looks like the following:
Execute Long press
Please note, that V item is default one and in context_menu.xml I've provided only X item.

Related

App shuts down when button is pressed. Objective is to set TextView with the input from EditText

I have to make a program where what I write in EditText shows up in TextView when I press a button. However when I press the button, the app shuts down. Where am I going wrong? I have tried doing it the way many similar questions' solutions said, but to no use. Please help. PS: I'm a total noob at android programming. Any help would be appreciated.
This is my main class:
public class abc extends Activity implements View.OnClickListener {
EditText editText;
TextView textView;
Button button;
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abc);
EditText editText=(EditText)findViewById(R.id.ed_edit);
TextView textView=(TextView)findViewById(R.id.text_tview);
Button button=(Button)findViewById(R.id.btn_button);
button.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_abc, 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);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_button:
name= editText.getText().toString();
textView.setText(name);
break;
}
}
}
This is the xml code:
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:text="ABD"
android:id="#+id/text_tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
/>
<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="HERE"
android:id="#+id/btn_button"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ed_edit"/>
</LinearLayout>
You are initializing local variables using findViewById().
Replace :
EditText editText=(EditText)findViewById(R.id.ed_edit);
TextView textView=(TextView)findViewById(R.id.text_tview);
Button button=(Button)findViewById(R.id.btn_button);
With :
editText=(EditText)findViewById(R.id.ed_edit);
textView=(TextView)findViewById(R.id.text_tview);
button=(Button)findViewById(R.id.btn_button);

Click event not working in android action bar item

I am trying to make a shopping cart android app and i want to show the cart with number of items.
attaching the screenshot of app below .
When clicking on the cart icon its does not going any where.. :(.
here is my code.
MainActivity.java
public class PandaActivity extends ActionBarActivity {
TextView notifCount;
RelativeLayout count;
int mNotifCount = 0;
protected ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.topbar));
actionBar.setHomeAsUpIndicator(getResources().getDrawable(R.drawable.backbutton));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_panda, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.badge) {
Intent cartIntent=new Intent(getApplicationContext(),CartShowActivity.class);
startActivity(cartIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_panda.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="com.sha.netstager.pandafoods.activity.PandaActivity">
<item
android:title="demo"
android:id="#+id/badge"
app:actionLayout="#layout/feed_update_count"
app:showAsAction="always">
</item>
</menu>
feed_update_count.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right">
<!-- Menu Item Image -->
<ImageView
android:layout_width="48dp"
android:layout_height="fill_parent"
android:clickable="true"
android:src="#drawable/cart" />
<!-- Badge Count -->
<TextView
android:id="#+id/actionbar_notifcation_textview"
android:layout_width="18dp"
android:layout_marginRight="4dp"
android:layout_height="22dp"
android:layout_alignParentRight="true"
android:text="99"
android:textSize="16dp"
android:background="#f7134e"
android:textColor="#fff"/>
</RelativeLayout>
I tried many ways to solve this problem with onclick methord and some others. but dont know what is the error is happening here.
There is no messages or warnings in logcat when I click on the menu item in action bar.
Many Thanks..
Try to add onclick in your onCreateOptionsMenu, instead of current solution as given below.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
RelativeLayout rl_viewBag = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
txt_bagCount = (TextView)rl_viewBag.findViewById(R.id.actionbar_notifcation_textview);
txt_bagCount.setText("10");
rl_viewBag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cartIntent=new Intent(getApplicationContext(),CartShowActivity.class);
startActivity(cartIntent);
}
});
return true;
}
for better look of actionbar use extends AppCompatActivity this is the same look and feel will be provided which is been used by gmail.

How to align action bar title in center of screen, not the center of action bar

I use this method to align action bar title to center
How to make android action bar title center aligned?
But my action bar has menu, and then action bar width is not same to screen
so the title is not exactly center.
Is there other way to center the title?
Code:
actionbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="YOUR ACTIVITY TITLE"
android:textColor="#000"
android:textSize="24sp" />
</LinearLayout>
the activity which used custom action bar
public class WriteText extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_text);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.actionbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.write_tag, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Maybe you can change the actionbar xml file to make one your own style of actionbar.

Show copy/paste context menu on text using code android

I have an EditText and its text gets selected from through code. But I want to allow users to cut/copy the selected text. However, the cut/copy context menu doesn't appear until user long presses on text. But then it loses the actual selection. So, I'm thinking to show the context menu as the text get selected by the code.
I tried this inside onFocusChanged, but nothing appeared.
openContextMenu(EditText);
IF I follow you usecase correctly, you can open context menu from the onFocusChangeListener registered on the testedEditText.
I prepared some small test for that which seems to be correctly supporting your usecase.
You need to openMenu on the hooks which are selecting the content in the EditText.
public class Main extends Activity {
private EditText testedEditText;
private Button selectingButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selectingButton = (Button) findViewById(R.id.button);
testedEditText = (EditText) findViewById(R.id.textView);
registerForContextMenu(testedEditText);
selectingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
testedEditText.setSelection(6, 11);
openContextMenu(testedEditText);
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.cmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.select_all:
return true;
case R.id.copy:
//do something
return true;
case R.id.cut:
//do something
return true;
case R.id.paste:
//do something
return true;
default:
return super.onContextItemSelected(item);
}
}
}
Weirdly enough registering testedEditText.requestFocus(), and setting onFocusChangedListener for EditText was not enough.
Additional xml files for reference:
cmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/select_all"
android:title="select all"
/>
<item android:id="#+id/copy"
android:title="copy"
/>
<item android:id="#+id/cut"
android:title="cut"
/>
<item android:id="#+id/paste"
android:title="paste"
/>
</menu>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World, Initial Text..."
android:layout_centerInParent="true"
/>
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="button"
/>
</RelativeLayout>

Click on EditText doesn't show softkeyboard

I am using ActionBarSherlock, in the ActionBar i have this custom layout to appear when i click on the searchIcon.
public boolean onCreateOptionsMenu(final Menu menu) {
LayoutInflater inflater = getLayoutInflater();
final View view = inflater.inflate(R.layout.on_search_icon_clicked,null, false);
view.findViewById(R.id.search_image).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View arg0) {
EditText searchET = (EditText) view.findViewById(R.id.search_editText);
String s = searchET.getText().toString();
Toast.makeText(getBaseContext(), "Searching for: " + s,1000).show();
}
});
menu.add("Search")
.setIcon(R.drawable.ic_search)
.setActionView(view)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
on_search_icon_clicked.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:src="#drawable/ic_search"
android:clickable="true"
android:contentDescription="#string/seach_icon" />
<EditText
android:id="#+id/search_editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/search_image"
android:hint="#string/search"
android:singleLine="true" />
</RelativeLayout>
So, i click on the SearchIcon ---> i get the custom layout with editText and ImageView ---> click on editText, opens softkeyboard ---> enters string and clicks the imageView ---> shows me the toast.
Its fine till here.
The problem is:
i close the softkeyboard using device back button ---> then close the custom layout using ActionBar Home icon click ---> now click the searchIcon again, which reopens the custom layout, with editText filled with previously searched string ---> click on the editText, the softkeyboard doesn't open up (here is the problem).
Please help, i have no idea why its not working as expected.
Thank You
I have faced this problem before and here is my solution
//your_menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:orderInCategory="0"
android:icon="#drawable/btn_search"
android:title="Search"
android:actionLayout="#layout/on_search_icon_clicked"
android:showAsAction="always|collapseActionView"
/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.your_menu, menu);
mSearchbar = (MenuItem) menu.findItem(R.id.menu_search);
View actionview = mSearchbar.getActionView();
mEtSearchbar = ((EditText) actionview.findViewById(R.id.search_editText));
mEtSearchbar.setOnEditorActionListener(this);
mEtSearchbar.addTextChangedListener(this);
super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
Intent intent;
switch (item.getItemId())
{
case R.id.menu_search:
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);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
mEtSearchbar ~ your searchET

Categories

Resources