Android background image - imageview - android

I have one activity, and I set imageview element to match parent with one of my background image with dimensions: 1440x2560 . I need to make this image on fullscrean. But whenever I put that image using scaleType="fitXY" I get this:
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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="match_parent"
tools:context="com.backgroundApp.SecondActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#drawable/background"
android:scaleType="fitXY"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Activity:
package com.backgroundApp;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004d40")));
}
}

Use adjustViewBounds=true property in ImageView tag like
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#drawable/background"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>

use setAdjustViewBounds
setAdjustViewBounds(boolean adjustViewBounds)
Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
<ImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
or try to set programatically
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setAdjustViewBounds(true);

change
app:srcCompat="#drawable/background"
to
android:src="#drawable/xxxxx"

Related

Scrolling to view in CoordinatorLayout shows only partially the view

I have made an Android app containing a CoordinatorLayout with a AppBar and a NestedScrollView. In my NestedScrollView I have a list of items, and a button which scroll to an item in my NestedScrollView.
When clicking the button, the Android app scroll down to the item, but doesn't show the whole item, only partially: http://i.stack.imgur.com/8kuq0.png
I expected something like the following: http://i.stack.imgur.com/k0A5N.png
It seems like the amount the app needs to scroll is about the same size as the AppBar, in order to show the whole view. If I remove the scroll flag in my layout file below, I get the expected behavior.
What do I do wrong?
Update (12/01/2016): I've updated the pictures, so they are bigger. Furthermore as I wrote in Herry's response, I'm in doubt if View.requestRectangleOnScreen() is the right method to call, or if I should use a different layout then NestedScrollView.
My activity is coded as follows:
package com.example.sij.coordinatorlayoutbug;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView itemToNavigateTo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
itemToNavigateTo = (TextView)findViewById(R.id.itemToNavigateTo);
Button navigatorButton = (Button)findViewById(R.id.navigator_button);
navigatorButton.setOnClickListener(navigateToItemListener());
}
View.OnClickListener navigateToItemListener() {
return new View.OnClickListener() {
#Override
public void onClick(final View v) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
itemToNavigateTo.requestRectangleOnScreen(
new Rect(
0,
0,
itemToNavigateTo.getWidth(),
itemToNavigateTo.getHeight()));
}
});
}
};
}
}
And the layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/navigator_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/scroll_to_item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
<!-- Items 2 to 5 omitted for brevity -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item6"/>
<TextView
android:id="#+id/itemToNavigateTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
you need to add android:fillviewport="true" in your
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true">

How to reference ID from other layout

Following this tutorial I moved my ActionBar (Toolbar) from my main layout to another layout and I include the toolbar to my main layout like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.marcelo.notemuevas.MainActivity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/my_toolbar">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Servicios"
android:id="#+id/serviciosTxt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimaryDark"
android:textColor="#FFFFFF"
android:gravity="center"
android:paddingTop="18dp"
android:paddingBottom="18dp"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/serviciosTxt">
</ListView>
</RelativeLayout>
</RelativeLayout>
And my toolbar layout is this:
<?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.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/CustomActionBarTheme"/>
</LinearLayout>
But I got the following error
Why I cant make reference of my toolbar if it has the same id? Can I include my toolbar and make reference so I can put elements below the toolbar?
Edit 1 - Activity Code
package com.marcelo.notemuevas;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] itemname ={
"Carpinteria"
};
String[] descripcion={
"Reparación"
};
Integer[] imgid={
R.mipmap.icono1
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
myToolbar.setLogo(R.mipmap.ic_launcher);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid, descripcion);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MainActivity.this, carpinteria.class);
myIntent.putExtra("key", 5); //Optional parameters
MainActivity.this.startActivity(myIntent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
Edit 2 - Solution
Now I make it work, I added a new Id to the include then I make reference to it like James Lockhart and Phan Văn Linh suggest (dont know why the first time didnt work) so my new question is, the IDs works like variables? only exist where it has been declared?
This is the working xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.marcelo.notemuevas.MainActivity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbarLayout"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbarLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Servicios"
android:id="#+id/serviciosTxt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimaryDark"
android:textColor="#FFFFFF"
android:gravity="center"
android:paddingTop="18dp"
android:paddingBottom="18dp"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/serviciosTxt">
</ListView>
</RelativeLayout>
</RelativeLayout>
In your XML
<include
android:id=#"+id/toolBarLayout" // set the id for layout that contains toolbar
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolBarLayout"> // change my_toolbar to toolBarLayout
Then you access to your Toolbar by change
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
to
LinerLayout myLinear = (LinerLayout) findViewById(R.id.toolBarLayout);
Toolbar myToolbar = (Toolbar) myLinear.findViewById(R.id.my_toolbar);
Please try this:
In your toolbar layout file, in the LinearLayout tag, set the 'android:id' attribute to a unique layout id name, for example: android:id="#+id/toolbarLayout"
Once you have completed (1), in your main layout refer to your toolbar layout file as follows: android:layout_below="#id/toolbarLayout"
Hope this helps.

how to hide header and footer on scroll in android

there is two LinearLayouts header and footer in main activity
also RecyclerView inside swipeRefreshLayout between them
now i want to hide header and footer with use LayoutParam, but it shows gap after changing layoutParam and swipeRefreshLayout's height doesn't changes.
how to fix it?
I have try to solve your problem use the demo.
the 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="#fff"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:onClick="click"
android:background="#fffe4a" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:background="#3489ff" />
<View
android:id="#+id/view_bottom"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fffe4a" />
</LinearLayout>
the java file.
package com.example.wangze.instantdemo1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
View view_bottom = findViewById(R.id.view_bottom);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view_bottom.getLayoutParams();
layoutParams.height = 20;
view_bottom.setLayoutParams(layoutParams);
}
}
When i click the top view. The bottom is smaller,and not have gap. Maybe it can help you.

Android OnClick fails every new project

First post. I'm following a few courses on Android programming so fairly new but not a complete newbie. I have a background in programming from long ago..
For some odd reason, older projects have the OnClick running fine but every project I created today, with either Genymotion or AVD the OnClick never fires even in the following barebone example I created.
https://www.dropbox.com/s/js1qh263vldte0z/deleteme.zip?dl=0
Here's the original project I'm working on which I can't even click on the buttons (as if there's a transparency in front or something).
If someone can explain why when I open older projects based on 22 everything works but running now on 23 (with 16 as backward) OnClick doesn't work that'd be very nice of you. Thanks!
The Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="ca.shaarable.planetxerox.admin_add_products_hardware">
<LinearLayout
android:orientation="vertical"
android:id="#+id/admin_add_printer_first"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2">
<TextView
android:id="#+id/admin_add_printer_tv_name"
android:text="#string/admin_add_printer_tv_name"
android:textSize="12sp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/admin_add_printer_et_name"
android:text="#string/admin_add_printer_et_name"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="#+id/admin_add_printer_second"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.65">
<TextView
android:id="#+id/admin_add_printer_tv_thumbnail"
android:text="#string/admin_add_printer_tv_thumbnail"
android:textStyle="italic"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/admin_add_printer_image"
android:src="#drawable/igen5"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="#+id/admin_add_printer_third"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15">
<LinearLayout
android:id="#+id/admin_add_printer_sublayout"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_weight="1"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/admin_add_printer_CANCEL"
android:text="#string/admin_add_printer_CANCEL"
android:background="#F99F1C"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/admin_add_printer_ADD"
android:text="#string/admin_add_printer_ADD"
android:background="#2ABDBA"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And here's the code for the class:
package ca.shaarableapps.presssupport;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class AdminAddPrinter extends AppCompatActivity implements View.OnClickListener
{
private EditText printerName;
private ImageView printerThumbnail;
private Button cancelBtn, addBtn;
private static final String TAG = "MyActivity";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_add_printer);
printerName = (EditText)findViewById(R.id.admin_add_printer_et_name);
printerThumbnail = (ImageView)findViewById(R.id.admin_add_printer_image);
cancelBtn = (Button)findViewById(R.id.admin_add_printer_CANCEL);
addBtn = (Button)findViewById(R.id.admin_add_printer_ADD);
}
#Override
public void onClick(View v)
{
Log.v(TAG, "Nooooooooooooo");
switch (v.getId())
{
case R.id.admin_add_printer_ADD:
addBtn.setText("Good Job");
break;
}
}
}
This is the layout as seen by the user...
Try to add .setOnClickListener(this); inside onCreate(...) method.
protected void onCreate(Bundle savedInstanceState){
...
addBtn.setOnClickListener(this);
}
Hope this help
The problem is here in you code.
<Button
android:id="#+id/admin_add_printer_ADD"
android:text="#string/admin_add_printer_ADD"
android:background="#2ABDBA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick" // add this line
/>

Android ImageButton ClickEvent not working

here is the code, please can anyone simply tell me how to make the onClick event run?
xml file of the activity, which has a navigation-drawer fragment, An Image view, A TextView, An ImageButton
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/drawer_layout"
android:adjustViewBounds="true"
android:contentDescription="#string/ContentDescriptionProfileImage"
android:src="#drawable/profile_pic" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:background="#FFFFFF"
android:orientation="horizontal"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:padding="3dp"
android:text="#string/profileName"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/profileSettings"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#null"
android:contentDescription="#string/settingsIconDescription"
android:padding="8dp"
android:onClick="onSettingsIconPressed"
android:src="#drawable/settings32" />
</RelativeLayout>
</LinearLayout>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/app_bar">
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="studio25.materialdesign.NavigationDrawerFragment"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer"></fragment>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
The image of the Screen Looks like this
here is the activity class
package studio25.materialdesign;
import android.annotation.TargetApi;
import android.os.Build;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.Toast;
public class ProfileView extends ActionBarActivity {
private Toolbar toolbar;
String title = "";
ImageButton setttingsButton;
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_view);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
this.getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
setttingsButton= (ImageButton) findViewById(R.id.profileSettings);
/*
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.primaryColor));
*/
}
public void onSettingsIconPressed(View v)
{
/*
PopupMenu popupMenu=new PopupMenu(this,v);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.profile_settings_popup_menu,popupMenu.getMenu());
popupMenu.show();
*/
Toast.makeText(this,"Event Click Occured",Toast.LENGTH_SHORT).show();
}
}
I basically wanted a menu popup on buttonclick, but on debugging realized that my button click is not itself working.
Some tutotials say for popUp menu , minSdkVersion should be 11, even changed that.
Nothing is helping me.
Is my xml erroneous or what?
I usually like to hook up my callbacks in code (just my personal preference).
setttingsButton= (ImageButton) findViewById(R.id.profileSettings);
setttingsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(this,"clicked!",Toast.LENGTH_SHORT).show();
}
});
With this you can remove the onClick attribute in the xml layout.
If you want a popup window then PopupWindow is a great option.
popupWindow = new PopupWindow(some_view_to_show, ViewGroup.LayoutParams.MATCH_PARENT, (int)popupHeight, false);
...
popupWindow.showAtLocation(view_to_inject_into, Gravity.NO_GRAVITY, 0, (size.y) - (int) popupHeight);
then you call popupWindow.dismiss(); to remove the popup
Your issue could also be that since you are using a RelativeLayout each element gets "stacked" on top of each other so you have essentially "buried" your ImageButton that you want to click on. So in other words some other view is blocking or consuming your clicks.
Try moving your ImageButton to be one of the last elements that you add to the xml file so that it is "on top" of everything else so that it can be clicked.
Alternatively you can try making the containing LinearLayout clickable instead of the ImageButton. So you would move the android:onClick="onSettingsIconPressed" attribute to the LinearLayout that contains the ImageButton.

Categories

Resources