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"
Related
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
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.
I'm creating a Soundboard app which uses buttons currently to make different sounds. However I want to change the buttons to image buttons, having tried this I get the following error:
Unexpected Cast to ImageButton: Layout tag was imageView.
I've tried looking around the internet but can't find a fix. The code in the java class looks like this.
public class SecondActivity extends AppCompatActivity {
Button b,b2;
MediaPlayer nice,burp,fancy;
ImageButton IMG;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
b = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button3);
nice = MediaPlayer.create(this, R.raw.nice);
burp = MediaPlayer.create(this, R.raw.burp);
fancy = MediaPlayer.create(this, R.raw.fancy);
configureImageButton();
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
nice.start();
}
}
);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
fancy.start();
}
}
);
}
private void configureImageButton() {
ImageButton IMG = (ImageButton) findViewById(R.id.IMG);
IMG.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
burp.start();
}
}
);
}
The XML code looks like this:
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.oliver.olliessoundofmusic.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nice"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fancy"
android:id="#+id/button3"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/IMG"
android:background="#drawable/robert"
android:layout_toEndOf="#+id/button3" />
</RelativeLayout>
I just want imageButtons to work with action listeners the same way a normal button would, to make the app more visually appealing.
Thanks in advance.
In your xml change ImageView tag to ImageButton
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nice"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fancy"
android:id="#+id/button3"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/button" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/IMG"
android:background="#android:color/transparent"
android:src="#drawable/robert"
android:layout_toEndOf="#+id/button3" />
Please notice that I made the background to a transparent color, and made the drawable to be in src property
Cast this to an ImageView instead, then since that is what you have in the XML
ImageButton IMG = (ImageButton) findViewById(R.id.IMG);
The OnClickListener will still work as expected.
Also, beware of variable shadowing. You'd want line this instead.
IMG = (ImageButton) findViewById(R.id.IMG);
Declare IMG as ImageView in you activity or use imagebutton widget in the layout file.
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);
}
};
I have a simple vertical LinearLayout in which there is a button on the top and a Listview below it. What I want is that when the button is pressed, it hides (using View.GONE) and the empty space generated by it is filled by ListView. But after all the efforts, I have not been able to implement it.
I tried invalidate() and onDraw(), I tried giving layout_weight=1 to the ListView, I tried forceLayout(), requestLayout() methods as well but none of them worked.
The strange thing is that if there is EditText or TextView or any other component instead of ListView, its working fine i.e. Edittext etc. go and grab the empty space generated by hiding the Button. But in case of ListView , its not happening.
This thing is perfectly working in my emulator
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new ProgressTask().execute();
btn1.setVisibility(8);
}
})
XML code
<?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:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ListView
android:id="#+id/itemlist"
android:layout_below="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
check this......
public class TestingActivity extends ListActivity {
/** Called when the activity is first created. */
Button checkBtn;
String[] temp={"item 1","item 2","item 3"};
/** Called when the activity is first created. */
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getListView().setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, temp));
checkBtn = (Button) findViewById(R.id.button1);
checkBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
checkBtn.setVisibility(View.GONE);
}});
} }
xml is:
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check connetion" />
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollingCache="false" >
</ListView>
</LinearLayout>