Show layout on clicking an item in Actionbar - Android - android

My activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.myactionbar.actionbar.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<!-- Enable this layout on clicking the icon in actionbar -->
<RelativeLayout
android:id="#+id/rl_ListView1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#color/black"
android:visibility="invisible">
</RelativeLayout>
</RelativeLayout>
Main.xml - It shows an icon at actionbar
<item
android:id="#+id/show_info"
android:orderInCategory="100"
android:title="#string/search"
app:showAsAction="always"
android:icon="#drawable/ic_icon"
/>
On clicking the ic_icon icon in actionbar, i need to show the hidden Relativelayout with id=rl_ListView1.
I don't know how to find the layout with id on clicking an item in actionbar.
Please help me to do this.

You need to override onOptionsItemSelected method in our activity
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
//Code for showing layout
}
return super.onOptionsItemSelected(item);
}
=========
Updated
if (id == R.id.show_info) {
if(r1.getVisibility()== View.VISIBLE)
{
//Hide your layout
}
else
{
//Show your layout
}

Override onCreateOptionsMenu() and onOptionsItemSelected() in your activity . For e.g:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu, 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
switch (id) {
case R.id.show_info:
// Change visibilty of your RelativeLayout here
if(rl.getVisibility() == View.VISIBLE){
rl.setVisibility(View.INVISIBLE);
}else{
rl.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}

#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
try {
switch (item.getItemId()) {
case R.id.menu:
//do here
break;
default:
return super.onOptionsItemSelected(item);
}
} catch (Exception e) {
log(e);
}
}

Related

Action on item in Toolbar?

I have ToolbarCustom that has two items and an item can be clicked to open or close the layout. I am wondering how can I use the same item for opening and closing layout?
This my Code, but the variable openLayout is not defined:
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();
linearLayout = findViewById(R.id.show);
boolean openLayout=true;
if (id == R.id.action_categure) {
if (openLayout) {
linearLayout.setVisibility(View.VISIBLE);
openLayout=false;
} else {
linearLayout.setVisibility(View.GONE);
openLayout =true;
}
}
if (id==R.id.action_card){
return true;
}
return super.onOptionsItemSelected(item);
}
boolean firstTimeClick = true; //global variable
boolean secondTimeClick = false; //global variable
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();
linearLayout = findViewById(R.id.show);
if (id == R.id.action_categure) {
if (firstTimeClick) {
firstTimeClick = false;
secondTimeClick= true;
linearLayout.setVisibility(View.GONE);
} else if (secondTimeClick){
firstTimeClick= true;
secondTimeClick= false;
linearLayout.setVisibility(View.VISIBLE);
}
}
if (id==R.id.action_card){
return true;
}
return super.onOptionsItemSelected(item);
}
Is that what you want to achieve?Hope I helped you.

App Constant in application

I created Listview and menu options in android application with App Constants to minimize the number of classes
Ex:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,prgmNameList));
lv.setOnItemClickListener(this);
}
OnClickListener
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position)
{
case 0:
Intent intent = new Intent(this, First.class);
intent.putExtra(AppConstants.MAIN_ACTIVITY_TAG,lv.getItemAtPosition(position).toString());
startActivity(intent);
break;
}
But i need apply app constants to Menu Options too
But i don't know how to do that:
Here is the code for Menu Options
// create action options for options
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, 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_about) {
Intent intent = new Intent(this,Extras.class);
startActivity(intent);
}
if (id == R.id.action_advertise) {
return true;
}
if (id == R.id.action_contact) {
return true;
}
if (id == R.id.action_help) {
return true;
}
return super.onOptionsItemSelected(item);
}
I want to do same as listview but for menu options.
Please help me solve this problem.
Just put your string in string.xml .
<string name="menu_name">Show result</string>
and you directly access it in 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=".ContainerActivity">
<item
android:id="#+id/menu_action_show"
android:layout_width="wrap_content"
android:icon="#drawable/youricon"
android:title="#string/menu_name"
app:showAsAction="always"/>
You can also access all resources using java code with context :
String menu_name=getString(R.id.menu_name);

Menu and options menu together

how can I create a mainmenu in the actionbar and also an "options" menu that will open on hardware button click?
Like Whatsapp. There's an actionbar with avatar, name and and icon and if you press "menu" button, it will appear another menĂ¹ with "Show contact, media, search" etc.
This is my actual code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calendar, 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();
if (id == R.id.action_legend) {
// Open dialog
} else if(id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
} else if(id == R.id.action_add) {
startActivity(new Intent(this, AddActivity.class));
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
// Here open the options menu
return true;
}
return super.onKeyDown(keycode, e);
}

Android back option in action bar

When I created an activity it automatically added a back to main activity option in the action bar. It looks like those are the functions that do it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.app_settings, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Is there a way to make it go back using finish()? Right now it looks like it resets some values that are saved and I want to keep them. When I tried working with finish() it didn't do it but i'm not sure how to use it in the action bar.
This is simpler and it works perfect:
public class BackButtonExample extends Activity {
#SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.duahs);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0B4C5F")));
bar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Try this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home {
finish();
return true; }
else{
return super.onOptionsItemSelected(item);
}
}

registerForContextMenu for menuItems

Ok I'm sure this is a dumb question but I couldn't find the answer online. I want to register one of the menu Items for a context Menu, but I don't know how to can't figure out how to access the MenuItem as a view. So when I click one of the buttons on the ActionBar of my application, I want a context menu to pop up. I'm guessing this has to be done in OnCreateOptionsMenu?
Edit: Update... Adding this code works partially but overrides my Drawable.
XML
<item android:id="#+id/Favorites"
android:title="favorite_label"
android:icon="#android:drawable/ic_menu_myplaces"
android:actionViewClass="android.widget.ImageButton"
android:showAsAction="always"
/>
Main Activity
FavoriteButton = (ImageButton) menu.findItem(R.id.Favorites).getActionView();
FavoriteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
registerForContextMenu(v);
}
});
Hi follow below link exmple link
ListView list = (ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, Countries);
list.setAdapter(adapter);
registerForContextMenu(list);
change registerForContextMenu(list); to registerForContextMenu(buttonname);
i hope it useful to you.
In the resource/menu/main.xml add the below code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<menu>
<item
android:id="#+id/gray"
android:title="#string/gray" />
<item
android:id="#+id/green"
android:title="#string/green" />
<item
android:id="#+id/red"
android:title="#string/red" />
<item
android:id="#+id/orange"
android:title="#string/orange" />
<item
android:id="#+id/purple"
android:title="#string/dark_blue" />
</menu>
</item>
</menu>
and in the main activity you can access this by overridding the belo method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.gray:
color = Color.parseColor("#FF666666");
return true;
case R.id.green:
color = Color.parseColor("#FF96AA39");
return true;
case R.id.orange:
color = Color.parseColor("#FFF4842D");
return true;
case R.id.purple:
color = Color.parseColor("#FF5161BC");
return true;
}
return super.onOptionsItemSelected(item);
}
Here is what I have made:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// "menu_main" is the menubar of my actionbar menu
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// "item" is the menu button I have pressed
int id = item.getItemId();
// "Settings" button
if (id == R.id.action_settings) {
return true;
}
// Difficulty button to change the difficulty of my game
else if (id == R.id.action_difficulty) {
View view = findViewById(R.id.action_difficulty);
registerForContextMenu(view);
openContextMenu(view);
}
return super.onOptionsItemSelected(item);
}
This works fine for me!
BUT! If your menubar button is behind the "three dots" button, the line
registerForContextMenu(view);
will crash your application. I'm figuring out why..

Categories

Resources