I want when I click on the menu button a menu appears from the bottom of the screen.
Any suggestions?
Create custom view
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_view"
android:layout_width="match_parent"
android:layout_height="64dp"
android:visibility="gone"
android:layout_alignParentBottom="true"
android:clickable="true">
<!-- Anything you need -->
</RelativeLayout>
In onCreate method initialize view like
mCustomView = (RelativeLayout) findViewById(R.id.custom_view);
In onOptionsItemSelected method do next:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.some_menu) {
mCustomView.setVisibility(View.VISIBLE);
// do some animation there
}
return super.onOptionsItemSelected(item);
}
Related
I want to add some buttons on top of the activity layout (marked it in the picture) but could not find how to do this. What phrases should i search for?
The buttons that appear there (both text and icons) are items in what's called the Options Menu. Developer guides for creating options menus are here: https://developer.android.com/guide/topics/ui/menus.html#options-menu
As Ben P said, thats called Menu.
You need to create an XML with the options, and in the activity render the XML.
Example, lets call this menu_test.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_download"
android:title="#string/download_information"
android:orderInCategory="100"
android:icon="#drawable/ic_file_download_white_24dp"
app:showAsAction="always" />
</menu>
As you can see on the guide, showAsAction will display the icon if have one, or the title if it hasnt. If you remove that line, its added to the three points button.
Now in the activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_test, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_download) {
//YOUR METHOD HERE
return true;
}
return super.onOptionsItemSelected(item);
}
Hope it helps.
You need to remove actionbar from activity. You can set NoActionBar theme for the activity. And in your layout xml, you can add toolbar which includes buttons like following code.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#131313"
android:minHeight="?attr/actionBarSize">
<LinearLayout
android:layout_width="wrap_content"
android:background="#00aaaaaa"
android:layout_gravity="right"
android:layout_height="match_parent">
<Button
android:text="Delete"
android:layout_width="wrap_content"
android:background="#000"
android:textColor="#fff"
android:layout_height="wrap_content"
android:textSize="16dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
And in onCreate() function, you can add following code:
Toolbar topToolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(topToolBar);
ActionBar actionBar = getSupportActionBar();;
actionBar.setDisplayHomeAsUpEnabled(true);
I am using a NavigationView in drawer and there is a item in the Menu which has app:actionLayout property directing to a LinearLayout containing a TextView, and the TextView has property android:visibility="invisible". I want this textview to be visible when click on that item in Menu. but it is not working, i have added a Toast to check if click listener is working or not and find click listener is working but that textView is not being visible.
Here is click Listener (Activity layout name is: main_activity.xml):
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_kitchen_key:
View kitchenKeyLayout = getLayoutInflater().inflate(R.layout.nav_kitchen_key, null);
TextView kitchenKeyTextView = (TextView)kitchenKeyLayout.findViewById(R.id.show_kitchen_key);
kitchenKeyTextView.setVisibility(View.VISIBLE);
break;
}
return true;
}
});
Here is the Menu item :
<item
android:id="#+id/nav_kitchen_key"
android:title="Kitchen Key"
android:icon="#drawable/if_key"
app:actionLayout="#layout/nav_kitchen_key" />
The property of menu item app:actionLayout directing to nav_kitchen_key.xml and here is nav_kitchen_key.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/show_kitchen_key"
android:textColor="#color/white"
android:text="2592"
android:visibility="invisible"
android:background="#drawable/kitchen_key_background"/>
</LinearLayout>
You should not create new View, you should get from MenuItem, MenuItem.getActionView()
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_kitchen_key:
LinearLayout kitchenKeyLayout = (LinearLayout) item.getActionView();
TextView kitchenKeyTextView = (TextView)kitchenKeyLayout.findViewById(R.id.show_kitchen_key);
kitchenKeyTextView.setVisibility(View.VISIBLE);
break;
}
return true;
}
});
You get a view from getActionView
LinearLayout kitchenKeyLayout = (LinearLayout) item.getActionView();
// This returns the container layout
TextView kitchenKeyTextView = (TextView)kitchenKeyLayout.findViewById(R.id.show_kitchen_key);
kitchenKeyTextView.setVisibility(View.VISIBLE);
kitchenKeyTextView.setVisibility(View.VISIBLE);
I’m working on a project in which I have to do a shared transition.
I want to perform a transition on an ImageView from my splash Activity [First Activity] to Login Activity [Second Activity] which has the ImageView in a Fragment. The Image in my 1st Activity and The Image in the fragment of my 2nd Activity are same and Shares The Same Transition Name. I’m unable to perform the transition.
My Splash Activity Code is This
public class Splash extends Activity implements View.OnClickListener{
private ImageView imageView;
private String SharedElementname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getWindow().setExitTransition(R.transition.shared_element_transition_a);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setSharedElementExitTransition(TransitionInflater.from(this).inflateTransition(R.transition.shared_element_transition_a));
}
setContentView(R.layout.activity_splash);
imageView = (ImageView)findViewById(R.id.splashimage);
SharedElementname = getString(R.string.sharedname);
imageView.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_splash, 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);
}
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation(this,v,SharedElementname);
Intent intent = new Intent(this, LoginActivity.class);
ActivityCompat.startActivity(this, intent, compat.toBundle());
}
My Login Activity Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(BuildConfig.MINT_API_ENABLED) {
Mint.initAndStartSession(LoginActivity.this, BuildConfig.MINT_API_KEY);
}
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.shared_element_transition_a));
}
setContentView(R.layout.activity_login);
}
Some More Codes….
*****************************************************************************
My Fragment Code Within Login Activty
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginTab = new LoginTab();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setSharedElementReturnTransition(TransitionInflater.from(
getActivity()).inflateTransition(R.transition.shared_element_transition_a));
setExitTransition(TransitionInflater.from(
getActivity()).inflateTransition(android.R.transition.fade));
loginTab.setSharedElementEnterTransition(TransitionInflater.from(
getActivity()).inflateTransition(R.transition.shared_element_transition_a));
loginTab.setEnterTransition(TransitionInflater.from(
getActivity()).inflateTransition(android.R.transition.fade));
}
}
Some More Codes….
*************************************************************************************
My Splash Activity XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_new"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:id="#+id/SplashLayout"
>
<ImageView
android:id="#+id/splashimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/eventifyd_logo"
android:transitionName="#string/sharedname"
/>
</RelativeLayout>
</LinearLayout>
My Transition XML File [Shared_element_transition]
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000">
<changeTransform />
<changeBounds />
<!—
I also tied ChangeImageTranform But Nothing Happened
-->
</transitionSet>
My Fragment XML File
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="#+id/profile_pic_login"
android:layout_above="#+id/login_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/eventifyd_logo"
android:transitionName="#string/sharedname"
/>
<TextView
android:layout_marginTop="30dp"
android:id="#+id/login_text"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/login_text"
style="#style/thin.white.large"/>
</RelativeLayout>
</LinearLayout>
I couldn't do it the way i was try. I changed the Fragments to Activity and it worked as a charm. I think Making transition from Activity to a Fragment of another activity is not Possible. Anyway, My this approach worked.
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.
I keep getting a crash when I change my set content view to an xml layout and not sure whats wrong. The program is supposed to have two fragments, a list view on the left and a webview on the right when it is in landscape and just the list in portrait. Sorry for all the code, I just can not figure out what is going on.
Here is my main activity
public class MainActivity extends Activity {
static final String LOGTAG = MainActivity.class.getSimpleName() + "_TAG";
static Resources mRes = null;
static FragmentManager mFrgmntMngr = null;
static MainActivity mThisAct = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager.enableDebugLogging(true);
setContentView(R.layout.activity_main_1view);
mRes = getResources();
mFrgmntMngr = getFragmentManager();
mThisAct = this;
}
static boolean isInLandscapeOrientation() {
return mRes.getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE;
}
#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;
}
#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);
}
public void displayTwainText(int mCurPosition) {
// TODO Auto-generated method stub
if ( isInLandscapeOrientation() ) {
// Check what fragment is shown, replace if needed.
TwainTitleWebViewFragment tTxtFrgmnt = (TwainTitleWebViewFragment)
mFrgmntMngr.findFragmentById(R.id.twain_title_list);
if (tTxtFrgmnt == null || tTxtFrgmnt.getDisplayedTwainIndex() !=mCurPosition)
// Make new fragment to show this selection.
tTxtFrgmnt = TwainTitleWebViewFragment.newInstance(mCurPosition);
// Execute a transaction, replacing any existing
// fragment inside the frame with the new one.
Log.d(LOGTAG, "about to run FragmentTransaction...");
FragmentTransaction frag_trans
= mFrgmntMngr.beginTransaction();
frag_trans.setCustomAnimations(R.animator.bounce_in_down,
R.animator.fade_out);
frag_trans.setCustomAnimations(R.animator.bounce_in_down,
R.animator.slide_out_right);
frag_trans.replace(R.id.list, tTxtFrgmnt);
frag_trans.commit();
}
} else {
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
intent.setClass(mThisAct, TwainTitleViewActivity.class);
intent.putExtra(mRes.getString(R.string.twain_index_key), mCurPosition);
this.startActivity(intent);
}
}
}
This is activity_main1view.xml in the lay out folder
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.example.hw_07_final_.TitleListFragment"
android:id="#+id/twain_title_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
this is the one in layout-land
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.example.hw_07_final_.TitleListFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#00550033"/>
<FrameLayout
android:id="#+id/twain_text"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
this is the log cat
You CANNOT have numbers or uppercase letters when naming your xml files.
activity_main1view.xml should be activity_mainview.xml excluding "1" from the layout name.
Change
<fragment class="com.example.hw_07_final_.TitleListFragment"
To
<fragment android:name="com.example.hw_07_final_.TitleListFragment"
Run your app and it will inflate your fragment.