Reputation: 1481
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.
Upvotes: 0
Views: 513
Reputation: 3243
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);
}
Upvotes: 1
Reputation:
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
}
Upvotes: 2
Reputation: 3941
@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);
}
}
Upvotes: 0