I am having a problem in changing images rapidly on click. I have made an image button I want to change the image when the button is clicked and back to the previous image. I have searched a lot but cant find the solution.
public class Buttonone extends Activity {
ImageButton btonImage;
boolean isPressed = true;
MediaPlayer mp;
boolean isPlay = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.button_one);
btonImage = (ImageButton) findViewById(R.id.btonImage);
btonImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playSound();toggleImage();
}
});
}
private void toggleImage() {
// TODO Auto-generated method stub
btonImage.setImageResource(R.drawable.image2);
}
}
this is the layout xml for image button
<ImageButton
android:id="#+id/btonImage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="#null"
android:background="#080808"
android:src="#drawable/image1" />
Set the src to a drawable like this
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/imgButtonBack"/>
And create a file named imgButtonBack.xml in drawable folder and set the following code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:drawable/presence_video_away"
android:state_pressed="true" />
<item android:drawable="#android:drawable/presence_audio_busy"
android:state_focused="true" />
<item android:drawable="#android:drawable/presence_audio_busy" />
</selector>
Change the image as per your need
setContentView(R.layout.button_one);
This is your problem. You should set the entire layout and handle click event !
Related
getSupportActionBar().setLogo(R.drawable.addicon);
The code above seems to place my Icon in the Centre . I would like to place to the far right and make it clickable as well . At the moment it the image for the icon is in the project files as a drawable I have not included it in any xml files.
You can set android:layoutDirection="rtl" in your toolbar xml for place it to right and
set below code for clickable :
getSupportActionbar().setDisplayHomeAsUpEnabled(true);
and :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
There are 2 approaches for doing that.
Option Menus
Creating custom toolbar.
With Option Menus
create main.xml under res/menu.
<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="com.ioa.MainActivity" >
<item
android:id="#+id/action_addition"
android:icon="#drawable/addicon"
android:orderInCategory="100"
android:title="Addition"
app:showAsAction="always"/>
</menu>
and inside your activity create menu like,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
you can perform Action on particular menu item.
#Override
public boolean onOptionsItemSelected(MenuItem Item) {
if (Item.getItemId() == R.id.action_addition) {
// perform action
}
return super.onOptionsItemSelected(paramMenuItem);
}
With custom toolbar
Create your ToolBar like this,
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/add"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="4dp"
android:clickable="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
and now you can perform click event of particular ImageView.
ImageView add = (ImageView) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do work.
}
});
happy Coding.
You Need 3 Things:
A Custom Toolbar Layout
Include the toolbar in your activity
Set the Logo to the toolbar
Create new Resource Layout:
include_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/colorPrimaryDark"
android:id="#+id/include_toolbar"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Change Your Activity File: MainActivity.java
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar)findViewById(R.id.include_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.mipmap_ic_launcher);
}
}
And here's the styles.xml:
<!-- language: xml-->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
</resources>
Hi all I have one problem
I am trying to move action bar app icon,title,menu or... from left to right
and I am using SherlockActionBar with setCustomView see codes
my activity
private void BiftorSetUpActionBar() {
// TODO Auto-generated method stub
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
View actionbarview = getLayoutInflater().inflate(R.layout.biftor_action_bar, null);
View app_actionbar_bg =(View) actionbarview.findViewById(R.id.app_actionbar_bg);
app_actionbar_bg.setBackgroundResource(R.drawable.list_item_bg_normal);
BiftorApptitle=(TextView) actionbarview.findViewById(R.id.app_name);
BiftorAppIcon=(ImageView) actionbarview.findViewById(R.id.app_icon);
BiftorAppIcon.setClickable(true);
BiftorAppMenu=(ImageView) actionbarview.findViewById(R.id.app_menu);
BiftorAppIcon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!mDrawerLayout.isDrawerOpen(Gravity.RIGHT))
mDrawerLayout.openDrawer(Gravity.RIGHT);
else
mDrawerLayout.closeDrawer(Gravity.RIGHT);
}
});
BiftorAppMenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
openOptionsMenu();
}
});
getSupportActionBar().setCustomView(actionbarview);
}
my action bar layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/app_actionbar_bg"
android:background="#drawable/list_item_bg_normal"
>
<ImageView
android:id="#+id/app_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/app_menu"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffeeeeee"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/app_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/app_icon"
android:layout_alignParentLeft="true"
android:src="#drawable/abs__ic_menu_moreoverflow_holo_dark" />
</RelativeLayout>
all things is OK but I have one problem,when my app launch the original (default) android action bar will load for 2-3 seconds then my custom action bar will show.
I don't want it,I want remove the original completely.
look at this video you will get what I am saying
This video
I am waiting.
Thanks All
edit 1:
I changed some things by searching on Google and this site but still first default action bar first then load my custom action bar
private void BiftorSetUpActionBar() {
// TODO Auto-generated method stub
//getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
View actionbarview = getLayoutInflater().inflate(R.layout.biftor_action_bar, null);
View app_actionbar_bg =(View) actionbarview.findViewById(R.id.app_actionbar_bg);
app_actionbar_bg.setBackgroundResource(R.drawable.list_item_bg_normal);
.
.
.
.Skip....
.
.
.
getSupportActionBar().setCustomView(actionbarview);
getSupportActionBar().setDisplayShowCustomEnabled(true);}
and changed the style.xml
<style name="BiftorAppTheme" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="background">#drawable/list_item_bg_normal</item>
<item name="android:background">#drawable/list_item_bg_normal</item>
</style>
but again nothings :-(
In a fragment you can do this:
ActionBar actionbar = ((MainActivity)requireActivity()).getSupportActionBar();
actionbar.setDisplayOptions(actionbar.DISPLAY_SHOW_CUSTOM);
actionbar.setCustomView(R.layout.app_bar_custom_layout);
I have layer, it is transparent. I can click on button only, I want to click through layer.
Here's my Manifest:
<activity android:name=".Test" android:theme="#style/Theme.Transparent"/>
Here's Transparent theme style:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Here's my Activity code:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// v I can touch through app, but cant click on button
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
// ^ I can touch through app, but cant click on button
Button xclose = (Button) findViewById(R.id.buttonx);
xclose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
}
}
Here's my test.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<Button android:id="#+id/buttonx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="X" />
</RelativeLayout>
Try this code and set click listener on the linear layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll">
<Button android:id="#+id/buttonx" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:text="X" />
</LinearLayout>
</RelativeLayout>
You already do the following here to set the click listener for the X button.
Button xclose = (Button) findViewById(R.id.buttonx);
xclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
Now all you need to do is the same thing but for the Layout which you have created that is transparent. So that when you click the layout in the area that your play button is, it listens to the click, rather than the button, as the button is out of reach.
I am having a problem with one of my applications. I have written a basic application menu which has custom buttons linking to other activity's. My problem is that it seems my application is not registering my button clicks when I am testing it. I do not receive any errors or crashes upon clicks, the application just acts as if the click never happened. Could anyone provide me with any help with this problem, I have provided some code bellow.
Main Menu code:
public void initialize(){
Film = (Button) findViewById(R.id.bFilm);
}
#Override
public void onClick(View view) {
try{
switch(view.getId()){
case R.id.bFilm:
Intent film = new Intent(this, Film.class);
startActivity(film);
break;
Layout Code:
<Button
android:id="#+id/bFilm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="110dp"
android:background="#drawable/film"
android:textSize="20sp" />
Custom XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/actor_1"></item>
<item android:state_focused="true" android:drawable="#drawable/actor_1"></item>
<item android:drawable="#drawable/actor_1"></item>
</selector>
Within your initialize(), add after findViewById the following:
Film.setOnClickListener(onClick);
Try this
public void initialize(){
Film = (Button) findViewById(R.id.bFilm);
Film.setOnClickListener(this);
}
add
android:onclick="onClick"
in
<Button
android:id="#+id/bFilm"
android:onclick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="110dp"
android:background="#drawable/film"
android:textSize="20sp" />
it will work
EDIT
However
onClick attribute is not defined for API 3 and less. (Android <= 1.5)
It works since API 4 (Android 1.6)
If you want compatibility you can use:
findViewById(R.id.myButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do stuff
}
});
So I'm new to android development...How can I create an image that acts like button, so when I press that image, image starts a specific activity.
So I want this to show as image:
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="33dp"
android:text="Button" />
Create ImageButton as:
In main.xml:
<ImageButton android:id="#+id/ib"
android:src="#drawable/bookmark" <-- SET BUTTON IMAGE HERE -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
In Code part:
ImageButton ib=(ImageButton)findViewById(R.id.ib);
ib.setOnClickListener(ibLis);
}
private OnClickListener ibLis=new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//START YOUR ACTIVITY HERE AS
Intent intent = new Intent(YOUR_CURRENT_ACTIVITY.this,NextActivity.class);
startActivity(intent, 0);
}
};
EDIT:
and second option if you want to create an image like button using Button View then Create an Custom button as:
First put your all images like for pressed,focused and default in res/drawable folder and then add an newbtn.xml in drawable/newbtn.xml as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
Finally in button XML set android:background as :
<Button
android:id ="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textColor="#ffffffff"
android:background="#drawable/newbtn" <-- get button background to selector -->
/>
See this tutorial for Creating Custom Button with images
Creating custom, fancy buttons in Android
With the ImageView element, attaching a click listener to it.
The XML:
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myPic"
/>
The code:
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ThisActivity.this, MyOtherActivity.class);
startActivity(intent);
}
});
You also could use a ImageButton (in the same way). It makes almost no difference. You can see more details here. Difference between a clickable ImageView and ImageButton
It's an overcomplication to use "setOnClickListener()". Instead, use the 'onClick' property in XML:
<ImageButton
android:id="#+id/button19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:onClick="yourCallback"
android:src="#drawable/your_image"
/>
public void yourCallback(View view)
{
...
}