why custom menu icon is not responding - android-custom-view

I want to add a logout option to the tool bar, which has the image view. due to fixed size in tool bar menu, i gone for a custom menu by specifying
app:actionLayout="#layout/custom_menu"
i specified this in my , and did a custom menu in custom_menu.xml.
now when i click the item, i want to perform some task, lets call something like a toast message for testing.
but when i click the in my tool bar, nothing responses.
what am i doing wrong here?
MyJavaActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.logout, 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.logOut) {
Toast.makeText(UserActivity.this, "logout clicked", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
logout.xml (menu for items)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="#+id/logOut"
android:title="logout"
app:actionLayout="#layout/custom_menu"
app:showAsAction="ifRoom">
</item>
</menu>
custom_menu.xml (customizing my menu for toolbar)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_gravity="center">
<ImageView
android:id="#+id/logout"
android:layout_width="#dimen/menu_item_icon_size"
android:layout_height="#dimen/menu_item_icon_size"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_logout"
android:clickable="true"
android:elevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Actually Our custom menu item isn’t automatically given the same padding as a normal menu item. So the area that receives touch events is greatly reduced. I was just hunting around to click in the right area. We can fix this by adding a FrameLayout to our custom view.
make sure you have FrameLayout height and width is greater than the custom icon height and width.
this was the change i did in my custom_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="#dimen/menu_item_size"
android:layout_height="#dimen/menu_item_size">
<FrameLayout
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_gravity="center">
<ImageView
android:id="#+id/logout"
android:layout_width="#dimen/menu_item_icon_size"
android:layout_height="#dimen/menu_item_icon_size"
android:layout_gravity="center"
android:elevation="5dp"
android:src="#drawable/ic_logout" />
</FrameLayout>
</FrameLayout>
here is the result.

Related

WearableActionDrawer peek view does not hide

I'm trying to use WearableActionDrawer in my app. Previously I was using 'com.google.android.support:wearable:2.0.0-alpha1' and my layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.drawer.WearableDrawerLayout
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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="#layout/rect_activity_main"
app:roundLayout="#layout/round_activity_main"
tools:context="com.example.grant.wearableclimbtracker.MainActivity"
tools:deviceIds="wear">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id = "#+id/content_frame">
<android.support.wearable.view.GridViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
<android.support.wearable.view.drawer.WearableNavigationDrawer
android:id = "#+id/top_navigation_drawer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/dark_grey"/>
<android.support.wearable.view.drawer.WearableActionDrawer
android:id="#+id/bottom_action_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey" />
</android.support.wearable.view.drawer.WearableDrawerLayout>
After I switched to 'com.google.android.support:wearable:2.0.0' the behavior changed. The action drawer used to stay out of view until I swiped up to reveal the action menu, but now there is a peek view of the drawer that doesn't go away unless I open the menu and select something. Is this the intended behavior? It takes up a lot of real-estate:
Any advice would be much appreciated.
It was as I suspected, when NestedScrollingChild height matched the parentview, the action drawer peeks. The only way I could get it to hide was to increase the NestedScrollingChild view by 1dp:
// get the screen width
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
// set height of framelayout to be slightly larger than view
mSessionLayout = rootView.findViewById(R.id.sessionLayout);
mSessionLayout.setMinimumHeight(metrics.heightPixels+1);

Is there any difference between ViewStub and View?

I am new to Android Development.
I read about ViewStub from https://developer.android.com/training/improving-layouts/loading-ondemand.html
It mentions that it is cheap and less memory to use it.
I have the following:
MainLayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Layout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/blue"
android:orientation="vertical" >
.....
.....
<Button android:id="#+id/ShowBackground"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<Button android:id="#+id/ShowBackground2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/title_close" />
<View
android:id="#+id/ShowView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/blue" />
<ViewStub
android:id="#+id/ShowViewStub"
android:layout="#layout/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</RelativeLayout>
test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ShowViewLayout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/blue_dimmer"
android:orientation="vertical" >
</RelativeLayout>
MainActivity:
private ViewStub mViewStub;
private View mView;
private Button mBackground1;
private Button mBackground2
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MainLayout);
mView = (ViewStub) findViewById(R.id.ShowView);
mViewStub = (ViewStub) findViewById(R.id.ShowViewStub);
mBackground1 = (Button)this.findViewById(R.id.ShowBackground);
mBackground1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mView.setVisibility(View.VISIBLE);
}
});
}
mBackground2 = (Button)this.findViewById(R.id. ShowBackground2);
mBackground2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mViewStub.setVisibility(View.VISIBLE);
}
});
}
Questions:
View and ViewStub are working fine in the MainActivity.
I do understand ViewStub is good for the view that is rarely used.
If ViewStub is called at least 1 time in the MainActivity, shouldn't it be more memory usage since the test.xml layout is added to the activity?
As I can see is that ViewStub is always visible unless it is called View.Gone....
Can someone please explain what the differences between the two?
I greatly appreciate for your help,
Thank you.
The < include /> will just include the xml contents in your base xml file as if the whole thing was just a single big file. It's a nice way to share layout parts between different layouts.
The < ViewStub /> is a bit different because it is not directly included, and will be loaded only when you actually use it/need it, ie, when you set its visibility to VISIBLE (actually visible) or INVISIBLE (still not visible, but its size isn't 0 anymore). This a nice optimization because you could have a complex layout with tons of small views or headers anywhere, and still have your Activity load up really fast. Once you use one of those views, it'll be loaded.

Controlling showAsAction of MenuItem under AppCompat v21 and using a Toolbar

I am trying adopt the v21 AppCompat libraries and leverage the better control of the ActionBar using a Toolbar as described here and here. But no matter how I modify the menu XML, I can only get the menu items to show up in the overflow area. I would like to see the MenuItem represented as an icon on the Toolbar/ActionBar.
My menu declaration looks like this (note the showAsAction line):
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:res-auto="http:/>/schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item android:id="#+id/action_add"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/action_add"
android:orderInCategory="2"
res-auto:showAsAction="always" />
</menu>
I have also tried this (with a slightly different showAsAction line):
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:res-auto="http:/>/schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item android:id="#+id/action_add"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/action_add"
android:orderInCategory="2"
android:showAsAction="always" />
</menu>
But the only way I have successfully been able to get the icon to show up on the Toolbar/ActionBar is to force it in 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.main, menu);
// Try to manually force the item onto the bar instead of allowing it in the overflow
// for some reason, android is not picking up the setting in the menu declaration
// using android:showAsAction="always" (even when using the auto-res namespace)
MenuItem addItem = menu.findItem(R.id.action_add);
MenuCompat.setShowAsAction(addItem, MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
I am using the android.support.v4.view.MenuCompat static methods to achieve this. But I believe I should be able to do this declaratively in the XML. I have tried using both "always" and "ifRoom" but for no effect. I can only assume that the XML attribute is not being picked up.
I guess it is really something simple or related to the fact that I am using the new Toolbar to function as the holder of the menu. The Toolbar layout is very simple:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
and the usage seems quite standard:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Any help would be appreciated. And I am happy to share more of the project content if it helps to get to the bottom of it.

ProgressBar multiple displaying

I have an AsyncTask linked to a refresh Button (when I click on my refresh button my AsyncTask is called).
I have on my Layout a LinearLayout field for my ProgressBar :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/grey">
<Button android:id="#+id/refresh_p"
android:text="#string/refresh_promo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_button1"/>
<LinearLayout android:id="#+id/linearlayoutProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#color/tabTransparent"
android:cacheColorHint="#00000000"/>
<!-- <ListView android:id="#+id/list_promo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>-->
</LinearLayout>
In my AsyncTask :
#Override
protected void onPreExecute()
{
super.onPreExecute();
pb = new ProgressBar(context);
LinearLayout ll = (LinearLayout) ((Activity) context).findViewById(R.id.linearlayoutProgressBar);
ll.addView(pb);
pb.setVisibility(View.VISIBLE);
}
#Override
protected void onPostExecute(ArrayList<HashMap<String, String>> promoList)
{
...
if (pb!=null)
{
pb.setVisibility(View.GONE);
((LinearLayout)pb.getParent()).removeView(pb);
}
}
The problem I have is when I make more than 2 clicks on my Refresh Button then Multiple ProgressBar are displaying into the screen.. I just want that the new ProgressBar replace the old at the same position ..
My guess is that you are probably starting your AsyncTask in a manner similar to this:
new RefreshTask().execute(params);
Instead of that, create an instance variable in your activity. Let's say this is named mTask and substitute the above code for the following call:
if(mTask != null && mTask.getState != AsyncTask.State.RUNNING){
mTask = new RefreshTask();
mTask.execute(params);
}
This way you will ensure that only one instance of your task is running at a given time and the user will have to wait until the refresh is finished before he can start a new refresh.
If you want the user to be able to cancel an existing refresh task and run a new one, you will have to cancel the old one before starting a new one:
if(mTask != null){
if(mTask.getStatus() == AsyncTask.Status.RUNNING){
mTask.cancel(true);
}
mTask = new RefreshTask();
mTask.execute(params);
}
This way the old task and it's ProgressBar will be properly replaced by the new one.

Not able to set the background to a ListView

I am trying to set background for my List view . I have taken a image from the drawable and set it as background to the layout file then it is giving the result as following screen shot
and when i just remove the background am getting the thing which i required but no background.
and screen shot is
Is there is any other Syntax to set background for ListView.
And my Code for layout is
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:text="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/label"
android:textColor="#FF0000"
android:textSize="30px"></TextView>
</LinearLayout>
And my java code and that is
String[] names = new String[] { "India", "Malaysia" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen3,
R.id.label, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
if(keyword.equals("India"))
{
Intent ima1=new Intent(screen3.this,new21.class);
startActivity(ima1);
}
else
{
Intent ima2=new Intent(screen3.this,new22.class);
startActivity(ima2);
}
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
.show();
}
And lastly i need is I want the background image with proper listview showing one listitem after another. Thanks in advance
}
Try this code, Instead of extending ListActivity you can set layout that contains List View and separate layout for contents of List View.
Java Code :
public class Table_Layout extends Activity {
ListView list_view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);
list_view=(ListView)findViewById(R.id.list_view);
String[] names = new String[] { "India", "Malaysia" };
list_view.setAdapter(new ArrayAdapter<String>(this, R.layout.screen3,R.id.label, names));
}
}
Main XML that contains List View :table_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="fill_parent" android:background="#drawable/uploading">
<ListView android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/list_view"
/>
Then Contents of List View :screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:text="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/label"
android:textColor="#FF0000"
android:textSize="30px"></TextView>
</LinearLayout>
Set you Background for the table_layout.xml so that it will applied for entire List View as you expected.
All looks well with your xml file. Try implementing the below mentioned:-
Make an image of the size you want your listview's each row of. And set it as the parent LinearLayout's background. Set:- android:layout_width="fill_parent" android:layout_height="wrap_content".
Hope this helps !! :)

Resources