ActionBar button to right of Home - android

I need to place a button (home) to the right of the back arrow on my action bar. Requirement is that the left arrow (typically the home button) be captured and perform an onBackPressed which I have working. Now how do I add a button to the right of the left arrow so that when I tap it I can capture it on the onOptionsItemSelected and have it perform the Home function. I tried to create a relative layout with a single image view in it as a test, and then after infating the view setting it with actionBar.setCustomView(v); however it does appear to show up.
<?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="match_parent"
android:layout_gravity="fill_horizontal">
<ImageButton
android:id="#+id/bookmark_home_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/common_google_signin_btn_icon_dark" />
</RelativeLayout>
So ended up creating a custom actionbar as follows:
<?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="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="#+id/bookmark_actionbar_left_btn"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#null"
android:src="#drawable/navigation_back" />
<ImageButton
android:id="#+id/bookmark_actionbar_logo_btn"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#null"
android:paddingLeft="20dp"
android:src="#drawable/app_launcher" />
</LinearLayout>
and then in the onCreate
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.bookmark_actionbar);
ImageButton goBackBtn = (ImageButton) findViewById(R.id.bookmark_actionbar_left_btn);
goBackBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
ImageButton goHomeBtn = (ImageButton) findViewById(R.id.bookmark_actionbar_subrosa_btn);
goHomeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NavUtils.navigateUpFromSameTask(BookmarkList.this);
}
});

So I ended up using a Custom ActionBar - See Update Above

Related

SlidingDrawer underlap in RelativeLayout

I used a SlidingDrawer like in this tutorial
It works in a LinearLayout, but there is always a whitespace depending of the hight of the slider and all other stuff show up below the whitespace.
The soloution should be to use a Relative Layout. This deletes the whitespace but the handlerbutton from the slider seems to be under the content in the scrollview and don't do anything by clicking on it. See the screenshot
MainActivity.xml
<RelativeLayout 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="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/tbar"
layout="#layout/primerdivisor" />
<SlidingDrawer
android:id="#+id/SlidingDrawer"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:content="#+id/contentLayout"
android:handle="#+id/slideButton"
android:orientation="vertical"
android:padding="10dip"
android:rotation="180">
<!-- Handle button -->
<Button
android:id="#+id/slideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:ems="10"
android:rotation="180"
android:text="Show" android:clickable="true"/>
<!-- Content Layout -->
<LinearLayout
android:id="#+id/contentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFCC"
android:gravity="center"
android:orientation="vertical"
android:padding="10dip">
<Button
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#drawable/button_selector"
android:text="Button 1"
android:textColor="#color/textBlack" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#drawable/button_selector"
android:gravity="center"
android:padding="5dp"
android:text="Text View Item"
android:textColor="#color/textBlack" />
</LinearLayout>
</SlidingDrawer>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical">
....
MainActivity.java
...
private void createSlider(){
slideButton = (Button) findViewById(R.id.slideButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
b1 = (Button) findViewById(R.id.Button01);
textView = (TextView) findViewById(R.id.text);
// Setting Listeners to all buttons and textview
setListeners();
// Listeners for sliding drawer
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
// Change button text when slider is open
Log.i("----","blaaaaaa open");
slideButton.setText("Close");
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
// Change button text when slider is close
slideButton.setText("Open");
}
});
}
// Listeners method
void setListeners() {
b1.setOnClickListener(this);
textView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Toast shown on sliding drawer items click
if (v.getId() == R.id.text) {
Toast.makeText(MainActivity.this, textView.getText() + " Clicked",
Toast.LENGTH_SHORT).show();
} else {
Button b = (Button) v;
Toast.makeText(MainActivity.this, b.getText() + " Clicked",
Toast.LENGTH_SHORT).show();
}
}
...
The slider should overlap the scrollview when its whiped out
In view hierarchy terms, views defined at the beginning of your layout XML are "deeper" than those defined later.
In your case your ScrollView is added to the hierarchy after your SlidingDrawer so what your seeing is to be expected.
Move your SlidingDrawer to below your ScrollView and it should be above it.

Right way with two imageView onClick?

The problem is next, when I typed in ImageView mIV_WishItem "heart", caused onClick background ImageView mIV_WishItem
The code this buttons:
holder.mIV_WishItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setFavorites(holder, sectionsItem, true);
}
});
holder.mIV_pictureGoodsItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SectionsItem sectionsItem = sectionsItems.get(position);
sectionItemId = sectionsItem.getId();
getDataForItemDetails(mContext);
}
});
And xml for this buttons
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/_200sdp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="end"
>
<ImageView
android:id="#+id/iV_Wish"
android:layout_width="#dimen/bottom_navigation_icon"
android:layout_height="#dimen/bottom_navigation_icon"
/>
</LinearLayout>
<ImageView
android:id="#+id/iV_pictureGoods_Item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
/>
</RelativeLayout>
Please help me find solution
You need to add attribute in your ImageView(mIV_WishItem) :
android:focusable="true"
android:focusableInTouchMode="true"
Viewholder item itself focusable, so we need to set focus on imageview. After that we can enable click on imageview.

how to add an ImageButton inside a button in android

i want to make a button in another button ,
for example :
we have 2 buttons : 1.imageButton (100 * 50)dp , 2.button (100 * 100)dp
so my question is how can i put my imageButton inside my button ?
You can use RelativeLayout and just put second ImageButton over first ImageButton.
Update
Or You can use magrin in LinearLayour, for example:
<?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">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true" />
<ImageButton
android:layout_marginLeft="-100dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentTop="true" />
</LinearLayout>
Hope this may help you..
You should use Frame Layout for this.
In XML file , do like this
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="100dp" />
<ImageButton
android:id="#+id/imagebutton"
android:layout_width="100dp"
android:layout_height="50dp"
android:src="#drawable/buttonsample"/>
</FrameLayout>
And now in java file, declare the instance of Button and ImageButton
ImageButton imagebutton;
Button button;
In onCreate() function of java class, do....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imagebutton = (ImageButton)findViewById(R.id.imagebutton);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do your stuff
}
});
imagebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do your stuff
}
});
}
Set z-elevation property if you want the buttons to overlap and both needs to be visibile. android:elevation="2dp"

Replace the content of Linear layout at onclick button android

I want to replace the content of the linear layout which has button and textview with an editText type="phone" after an onclick event of button. They're all in the located in the same page.
Is there a way to do that?
Use following code to remove all Views.
lauout.removeAllViews();
You'll have to make two separate fragments and use a fragmenttransaction to replace one with the other.
See fragmenttransactions:
http://developer.android.com/reference/android/app/FragmentTransaction.html
What I can understand from your question
1) There are 3 views Button, TextView and EditText under one LinearLayout
Like this:
<?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:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My TextView " />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" // with input type phone
android:visibility="gone" />
</LinearLayout>
And You want to remove the textview and replace it with Edit Text on Button Click
like this
tv=(TextView)findViewById(R.id.textView1);
et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setVisibility(View.GONE);
et.setVisibility(View.VISIBLE);
}
});
place all of your three elements in the layout in your xml_layout, and set the EditText visibility to "gone", then when clicking the Button you mentioned, just set the Button and the TextView visibility to "gone" and the EditText visibility to "visible":
<LinearLayout android:id="#+id/layout"
.
.
.>
<Button android:id="#+id/button"
.
.
.
android:visibility="visible"/>
<TextView android:id="#+id/text"
.
.
.
android:visibility="visible"/>
<EditText android:id="#+id/edit_text"
.
.
.
android:inputType="phone"
android:visibility="gone"/>
</LinearLayout>
when clicking the Button:
public OnClickListener onButClick = new OnClickListener() {
#Override
public void onClick(View v) {
button.setVisibility(View.GONE);
text.setVisibility(View.GONE);
text_view.setVisibility(View.VISIBLE);
}
};

Tap preview thumbnail to view high resolution image

I am a new android developer and I am trying to create an android app for android in which menu items will be displayed along with description cost and image.
I have implemented List activity to display the items in a list. I made an image Button which will show the image in a thumbnail. I want to be able to view a high resolution image of the thumbnail when I tap on the image button. I have implemented an onClickListener to listen to the tap event but I do not know the methods that will make the image zoom out on tap.
I have followed this link
but it is quite ambiguous to understand. The Animator class doesn't exist. Can someone guide me out o fthis?
I found the solution after searching for almost a week.
I used PopupWindow Class for the purpose
Following is the Java Code.
PopupWin.java
public class PopupWin extends Activity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button bPopup = (Button)findViewById(R.id.openpopup);
bPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
Button bDismiss = (Button)popupView.findViewById(R.id.dismiss);
bDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(bPopup, 50, -30);
}});
}
}
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dim_back" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dim_back"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:src="#drawable/appetimg01" />
<Button
android:id="#+id/dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20sp"
android:text="Go Back"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/appetizerDes1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="0.40"
android:text="Tap the Image to view High Resolution Image."/>
<Button
android:id="#+id/openpopup"
android:layout_width="80sp"
android:layout_height="80sp"
android:layout_gravity="right"
android:layout_margin="40sp"
android:background="#drawable/custombutton"
android:scaleType="centerCrop" />
</LinearLayout>
</LinearLayout>
The background was appearing white but I wanted it transparent for which I created back_dim.xml in drawables and set it along with android:background="#drawable/back_dim".

Categories

Resources