I have this button that should change its appearance when I click the button from
state1 to state2 and vice versa.
the heart is 2 different drawables (#drawable/ic_fav_dish_color & #drawable/ic_fav_dish_grey) and the text is 2 different strings (#string/dish_faved & #string/dish_not_faved)
I made the button in xml with that code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/fav_dish_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_fav_dish_color"
android:gravity="start|center_vertical"
android:text="#string/dish_faved"
android:textAlignment="center"
android:layout_margin="8dp"/>
</LinearLayout>
you can use this , you should have two images one that is fill and other is
not
final Button btn = (Button)(findViewById(R.id.fav_dish_button));
final Drawable drawable = getResources().getDrawable(R.drawable.your_fill_heart_image_name);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null);
}
});
You should make a click listener on your button like below:
Button mButton=(Button)findViewById(R.id.fav_dish_button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mButton.setText(getResources().getString(R.string.dish_not_faved););
mButton.setBackgroundResource(R.drawable.ic_fav_dish_grey);
}
});
Update:
Try below code if you want to change drawable left:
// Left, top, right, bottom drawables
Drawable[] drawables = mButton.getCompoundDrawables();
// get left drawable.
Drawable leftCompoundDrawable = drawables[0];
// get desired drawable.
Drawable img = getContext().getResources().getDrawable(R.drawable.ic_fav_dish_grey);
// set image size (don't change the size values)
img.setBounds(leftCompoundDrawable.getBounds());
// set new drawable
mButton.setCompoundDrawables(img, null, null, null);
Related
I have the list of images in horizontal view in android. If I click particular Image of an Image view have to highlight on border color programmtically.
How can I do that for highlighted border of an Image View.
Thanks in Advance.
I want to display like this border of an ImageView when user clicked a single image of an ImageView.
CODE
horizontalimage=(LinearLayout)findViewById(R.id.linearimage);
// final RelativeLayout r1=(RelativeLayout)findViewById(R.id.relative_border);
// frame=(FrameLayout)findViewById(R.id.framelayout
if(multipleimage.length()>0) {
for (int j = 0;j<multipleimage.length();j++)
{
pimages=multipleimage.getJSONObject(j);
JSONObject oneimage=multipleimage.getJSONObject(0);
ii= new ImageView(singleshooppingcart.this);
multipleimages=(ImageView)findViewById(R.id.singleimage);
ii.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
LinearLayout.LayoutParams image = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
image.width=100;
image.height=1
image.setMargins(5,0,0,0);
final String multimgs=pimages.getString("original_res");
String oneimg=oneimage.getString("original_res");
String[] img2 = multimgs.split("\\.");
String imagone=productpath + alertObj.getString("seller_id")+ '/' + img2[0] + '(' + '1' + '0' + '0' + ')' + '.' + img2[1];
String singleiamges=productpath + alertObj.getString("seller_id")+ '/' + oneimg;
// displayimages=productpath + alertObj.getString("seller_id")+ '/' + multimgs[];
YelloPage.imageLoader.displayImage(imagone, ii, options);
YelloPage.imageLoader.displayImage(singleiamges, multipleimages, options);
ii.setLayoutParams(image);
// ii.setBackgroundResource(R.drawable.imgviewpress);
// ii.setBackground(R.drawable.imgviewpress);
/* GradientDrawable gd = new GradientDrawable();
gd.setColor(R.drawable.imageviewhighlight); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
// gd.setStroke(1, 0xFF000000);
ii.setBackgroundDrawable(gd);*/
horizontalimage.addView(ii);
ii.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable highlight = getResources().getDrawable( R.drawable.imgviewpress);
ii.setBackground(highlight);
int indexOfImage = horizontalimage.indexOfChild(view);
String img1=String.valueOf(indexOfImage);
// Toast.makeText(getApplicationContext(),img1,Toast.LENGTH_LONG).show();
try {
images = multipleimage.getJSONObject(Integer.parseInt(img1)).getString(String.valueOf("original_res"));
} catch (JSONException e) {
e.printStackTrace();
}
// Toast.makeText(getApplicationContext(),images,Toast.LENGTH_LONG).show();
// multipleimages.setImageResource(indexOfImage);
try
YelloPage.imageLoader.displayImage(productpath + alertObj.getString("seller_id")+"/"+images, multipleimages, options);
} catch (JSONException e) {
e.printStackTrace();
}
// String img1=String.valueOf(indexOfImage);
// YelloPage.imageLoader.displayImage(displayimages[indexOfImage], multipleimages, options);
}
});
Create a drawable file, in the drawable folder.
highlight.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="0dp"/>
<solid android:color="#android:color/transparent"/>
<stroke android:color="#FFC830"
android:width="3dp"/>
</shape>
ImageView in your activity
<ImageView
android:id="#+id/iv_test"
android:layout_width="150dp"
android:layout_height="150dp"
android:padding="3dp"
android:src="#drawable/yourImageName"
android:tint="#color/colorAccent"/>
Here padding is important.
Inside activity code
imv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawable highlight = getResources().getDrawable( R.drawable.highlight);
imv.setBackground(highlight);
//
//Do your other stuff here.
//
}
});
If you want to remove the background, use this code :
imv.setBackground(null);
In the activity xml, the padding attribute is important because the highlight background will take the same size of the imageview. So if there is any image in the imageview we will not be able to see the background/highlight. So the padding attribute pushes the image a bit inwards, letting the highlight to be seen.
Output
UPDATE
Inside you code implement View.OnClickListener.
And change your onClick() method
#Override
public void onClick(View v) {
Drawable highlight = getResources().getDrawable( R.drawable.highlight );
for (int j=0;j<ii.length;j++)
{
if (ii[j].getBackground()!=null) {
ii[j].setBackground(null);
}
}
v.setBackground(highlight);
//
//Do what you want to do when clicking on the imageview
//
}
And right now you are using
ii=new ImageView(singleshooppingcart.this);
make it an array and use it like this
ii[i]=new ImageView(singleshooppingcart.this);
Change this
ii.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable highlight = getResources().getDrawable(R.drawable.imgviewpress);
ii.setBackground(highlight);
to
ii[i].setOnClickListener(this);
Here i is the looping variable.
This way you will have objects for all your imageViews. and all these imageviews will have a ClickEvent which we have set.
put image in a RelativeLayout and add a margin of 1dp and make invisible on start.
Then
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
relativeLayout.SetVisibility(View.VISIBLE)
}
});
Firstly your imageView keep into relative layout. e.g.
<RelativeLayout
android:id="#+id/relative_border"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="38dp"
android:background="#FFFFFF"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
then add the following java code. Something like this
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_border);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
layout.setBackgroundColor(0xFF00FF00);
}
});
Try to use GradientDrawable
GradientDrawable gd = new GradientDrawable();
gd.setColor(0xFF00FF00); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);
Imageview imgs= (Imageview )findViewById(R.id.imgviw);
imgs.setBackgroundDrawable(gd);
Create a custom Image view with linear layout and image with some padding
basically change the color of the layout this will provide border for image view
On click use this,
customImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//use either v.getchild(position) or v.findViewById(R.id.borderlayout)
LinearLayout borderlayout = v.getChild(0);
// change the border color or visibility you like to do
}
});
I am trying to create an android app. I have two buttons next and back in my android app. I want when i click on next button its open same activity with different background. Next time i again click next new background image. And on press on back button its show previous image. And if no previous image its shows menu on press. Similarly if background with last image its hide next button. I have no idea how to do this.
I have tried this:
#Override
public void onCreate(Bundle savedInstanceState)
{
onCreate(savedInstanceState);
back = (Button) findViewById(R.id.back);
next = (Button) findViewById(R.id.next);
back.setOnClickListener(this);
next.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.back)
{
startActivty(new Intent(this,));
}
else if(v.getId()==R.id.next)
{
startActivity(newIntent(this,));
}
}
Xml:
<?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:background="#drawable/back">
<Button
android:id="#+id/back"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="25dp"
android:background="#drawable/ques"
android:text="Back" />
<Button
android:id="#+id/next"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/back2"
android:layout_alignBottom="#+id/back2"
android:layout_alignParentRight="true"
android:background="#drawable/ques"
android:text="Next" />
</RelativeLayout>
In layout as you can see i am using image back for background. I want when i click next new background image then next and so on.
But i dont know how to start same activity with differene backgroud.
Don't start new Activity just change the background:
Keep an array of background resources in your activity like:
int[] backgroundResId;
and one int variable to store current background index:
int currentIndex=0;
now inside your onCreate initialize this array with resource id's of all the backgrounds drawables:
backgroundResId=new int[]{R.drawable.a,R.drawable.b,R.drawable.c};
changeBackground()
create function changeBackground in activity:
private void changeBackground(){
findViewById(R.id.root_layout).setBackgroundResource(backgroundResId[currentIndex]);
}
Now onClick of next button increase currentIndex:
currentIndex++;
if(current<=backgroundResId.length){
changeBackground();
}else{
// setVisibility of next button to invisible
}
onBackButton Click
currentIndex--;
if(current>=0){
changeBackground();
//// setVisibility of next button to visible
}else{
//show menu
}
Make an images array and post your data to the next activity:
Intent intent = getIntent();
intent.putExtra("background", imageIdInTheImageArray);
startActivity(intent);
//finish();
and in your onCreate function :
Bundle b = getIntent().getExtras();
if (b != null) {
int background = b.getInt("background");
//set your background
}
You can add an ImageView in your xml file.
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
you can change background using this
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setBackgroundResource(resId);
Try this..
Global:
int[] backgrounds = new int[]{ images in drawable as int array };
int count = 0;
Button back,next;
RelativeLayout img_backn_lay;
JAVA:
setContentView(R.layout.activity_main);
back = (Button) findViewById(R.id.back);
next = (Button) findViewById(R.id.next);
back.setOnClickListener(this);
next.setOnClickListener(this);
img_backn_lay = (RelativeLayout) findViewById(R.id.main_lay);
img_backn_lay.setBackgroundResource(backgrounds[count]);
count += 1;
ClickListener:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.next)
{
if(backgrounds.length != count){
img_backn_lay.setBackgroundResource(backgrounds[count]);
count += 1;
}else{
Toast.makeText(MainActivity.this, "No images", Toast.LENGTH_LONG).show();
}
}
else if(v.getId()==R.id.back)
{
if(count != 0){
img_backn_lay.setBackgroundResource(backgrounds[count]);
count -= 1;
}else{
Toast.makeText(MainActivity.this, "No images", Toast.LENGTH_LONG).show();
}
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_lay"
android:layout_width="match_parent"
android:layout_height="match_parent" >
I have an ImageView that it's a background with an arrow. And I have another ImageView that it's the same background but with another different arrow.
I want to show one of the ImageView, and when I press a button, it shows the another ImageView and the first one. I.e. overlap them.
Is there any function to overlap 2 ImageView without use canvas or another?
Thank you
XML
<LinearLayout android:name="myView">
<ImageView android:name="rightArrow"
android:src"#+id/drawable/image1">
</ImageView>
<ImageView android:name="leftArrow"
android:src"#+id/drawable/image2">
</ImageView>
<Button android:name="myButton">
</Button>
</LinearLayout>
Code:
Button button= (Button) findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (imageView1.getVisibility == View.INVISIBLE ) {
imageView1.setVisibility(View.VISIBLE) ;
imageView2.setVisibility(View.INVISIBLE);
} else {
imageView1.setVisibility(View.INVISIBLE) ;
imageView2.setVisibility(View.VISIBLE);
}
}
});
You can call onclick :
if (view1.getVisibility == View.INVISIBLE ) {
view1.setVisibility(View.VISIBLE) ;
view2.setVisibility(View.INVISIBLE);
} else {
view1.setVisibility(View.INVISIBLE) ;
view2.setVisibility(View.VISIBLE);
}
I don't think there is a function to overlap directly...
XML
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton1"
android:src="#drawable/image1"
android:onClick="buttonClick"
/>
JAVA
--------------------
public void buttonClick(View v)
{
Button aButton = (Button)v;
aButton.setBackgroundResource(R.drawable.image2);
}
Here's what I've tried so far with no luck...
I want to be able to click the button and change the image to image2, there's also going to be other images i'll change it to based off of other variables. I'm just really stuck.. I'll continue looking at other questions and if I find an answer I'll post it here.
Your buttonClick() needs fixing:
public void buttonClick(View v)
{
ImageButton aButton = (ImageButton)v;
aButton.setImageResource(R.drawable.image2);
}
the View is an ImageButton, not a Button. The src attribute is updated via setImageResource, not setBackgroundResource.
In my project, there are a number of images having back and forward images and all images having a common layout. On clicking back and next buttons new images should be displayed.
private int imageCounter = 0;
private ImageView imageView;
private int[] imageArray = {R.drawable.image_w_lbl_0, R.drawable.image_w_lbl_1,};
private int index_count = 0;
#Override
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.frame0);//this one is the common parent layout for all imageviews
super.onCreate(savedInstanceState);
imageView = new ImageView(this);
ImageButton next = (ImageButton) findViewById(R.id.next);
ImageButton back = (ImageButton) findViewById(R.id.back);
next.setOnClickListener(this);
back.setOnClickListener(this);
//show the default image
this.loadImage(imageArray[0]);
}
#Override
public void onClick(View v)
{
int imagePath = 0;
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.next:
if(imageCounter < 25)
{
imagePath = imageArray[index_count];
}
imageCounter++;
break;
case R.id.back:
if(imageCounter > 0)
{
//imagePath =
}
imageCounter--;
break;
}
this.loadImage(imagePath);
}
private void loadImage(int imagePath)
{
imageView.setImageResource(imagePath);
}
I am able to see my layout only in the output having back and forward buttons with the black background not an image from my drawable folder.
XML Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ImageButton android:id="#+id/back" android:src="#drawable/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"/>
<ImageButton
android:id="#+id/next"
android:src="#drawable/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" />
</LinearLayout>
int array of images from resources (i.e. res/drawable-mdpi):
Integer[] imageArray = { "R.drawable.img1", "R.drawable.img2" }
...
imageView.setImageResource(imageArray[0]); // displays img1
Not very clear what you are asking, but do you want to do something like this:
imagePath=R.drawable.image_wo_lbl_0;
You can store image path in this way, if you actually want to do this.
imageView.setImageResource(R.drawable.img1)
Easiest way - Can be consider the below code
We can take advantage of Imageview setImageResource , refer below code for the same.
put image in the drawable like the below order
image_1.png, image_2.png, etc.
int imagePosition = 1;
int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", getPackageName());
imageview.setImageResource(resId);
Use ContextCompat to get drawable(Image in your case). This is one of the new ways to set image to your image view.
imageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
R.drawable.your_image));
You can use ContextCompat to get colors as well.
I use it in my apps and it works properly.