Changing background image on button click android app development - android

I am trying to change the background image of an activity
on button click, but not being able to do so. Can you guys tell me how can I can do that?
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try
{
Activity activity = Selection.this;
//Drawable db = Drawable..createFromPath(R.drawable.love1);
// The below line gives error because setbackgroundImage
// is supposed to take a drawable object as an argument.
// Now what to do?
activity.findViewById(android.R.id.content)
.setBackground(Drawable.createFromPath("res/love1.jpg"));
// What to do to correct the above line?
mySong = MediaPlayer.create(Selection.this, R.raw.song1);
mySong.start();
}
catch(Exception ee){
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(ee.getMessage());
}
}
});
I tried with Color.RED using setBackgroundColor but that too is not working.
PS: I have not changed anything in the xml file for accomplishing this.

I think the easiest way is to use layout in your xml file for your activity.. for example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayoutID"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_img1" >
and then change it from your Activity when you want e.g. after the button click:
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linearLayoutID);
mLinearLayout.setBackgroundResource(R.drawable.background_img2);

Change setBackground(Drawable) to setBackgroundResource(R.drawable.filename)

LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.LayoutID);
mLinearLayout.setBackgroundResource(R.drawable.image_name);

Related

Button to hide a TextView

I've been searching for a solution for this for a while but cannot seem to get one working. There are one or two on here about this subject but I can't seem to get them going. I'm also a novice in Android and while I've been on and off playing with it for a few years, I still understand next to nothing about what I'm writing.
Basically I've got a TextView and a button. Ideally I'd like to put some text in the TextView, press a button it's gone, press the button again and it's back.
I've narrowed it down to needing to understand what findViewById(R.id.button2) does but honestly I'm a bit lost.
I've added my button code but apologies that this is such a noob question
public void onClick(Button v){
TextView t1 = (TextView)findViewById(R.id.editText);
v.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t1 = (TextView)findViewById(R.id.TextView);
v.setVisibility(View.GONE);
}
});
}
Your code has a couple of issues. I'm not going to give you the code because that won't really help you learn. Instead I'll explain things and let you try to figure it out or come back with more explicit questions.
You know that xml file you set using setContentView? Some of the tags in it had a property android:id="xxxx". That xxxx is the id of that view, its used so you can find that view in your code. The findViewById function walks through all the views on screen and finds a view with that id and returns it. That gives you a reference to the view so you can change it. For example, you can set its visibility, set its background color, or set an OnClickListener.
So to have a button toggle the visibility of another view, you need to be able to do the following things:
1)Find the view who's visibility you want to change
2)Figure out what its visibility currently is
3)Figure out what you want it to be (the opposite of what it currently is
4)Set that visibility
You need to write a function that does all that. Then you need to do this
1)Find the button you want to use to change the visibility
2)Tell it to call your function when its pressed.
Figure out how to do each of those steps individually, and you should be able to put it together. Good luck.
findViewById(R.id.button2) finds the view with the id button2.
You can check inside onClick whether t1 is visible or not (t1.setVisibility(View.GONE); not v.setVisibility(View.GONE);), and toggle between View.GONE and View.VISIBLE.
Remember that your findViewById() should have a real id. They are normally set on the activity_name.xml.
You are using a onClick inside a onClick. Personally I recommend setting the listener manually with setOnClickListener.
There's a lot of work for you, start with these tutorials. Keep trying and try to understand what you are doing.
Look like you need a toogle button feature, here is a piece of code.
Important: you must pay heed to #GabeSechan and #SkyDriver2500 answers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//your other code
Button button = (Button) findViewById(R.id.button2);
final TextView t1 = (TextView) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
t1.setVisibility(t1.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
}
I'm not sure if the code will help you now. But just in case, here it is
final boolean[] isTvVisible = {false};
final TextView t1 = (TextView)findViewById(R.id.editText);
t1.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isTvVisible[0]) {
t1.setVisibility(View.GONE);
isTvVisible[0] = false;
} else {
t1.setVisibility(View.VISIBLE);
isTvVisible[0] = true;
}
}
});

Android realtime add button

I need a button that, when clicked, would add a TextView below it. Also, the TextView cannot be made invisible and appear on click in this case, it must add the TextView. Was looking for this and couldn't find anything that suits my needs.
Set a listener on the button and for each click, create a new TextView and add it to the layout.
final LinearLayout theLayout = (LinearLayout)findViewById(R.id.thelayout);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
tv.setTextColor(Color.BLACK);
theLayout.addView(tv);
}
});
Update: Added text color above

Design a layout like a dialog comes from the bottom side in android

I want to design layout which contains one button which is on the top side of the screen when we click on the button it should open a layout which comes from the bottom side like a keypad.
But no idea how to do this?
please share your ideas to do this.
Screen shot of layout:
when i click on that drop down button , another layout will come from download with a size of virtual keypad.
Thanks,
Ammu
Try SlidingDrawer as it is used Example and for more about that is obviously Android Docs
Animation can help you to do this:
private void initPopup()
{
final TransparentPanel popup = (TransparentPanel) findViewById(R.id.popup_window);
// Start out with the popup initially hidden.
popup.setVisibility(View.GONE);
animShow = AnimationUtils.loadAnimation( this, R.anim.popup_show);
animHide = AnimationUtils.loadAnimation( this, R.anim.popup_hide);
final Button showButton = (Button) findViewById(R.id.show_popup_button);
final Button hideButton = (Button) findViewById(R.id.hide_popup_button);
showButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
popup.setVisibility(View.VISIBLE);
popup.startAnimation( animShow );
showButton.setEnabled(false);
hideButton.setEnabled(true);
}});
hideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
popup.startAnimation( animHide );
showButton.setEnabled(true);
hideButton.setEnabled(false);
popup.setVisibility(View.GONE);
}});
final TextView locationName = (TextView) findViewById(R.id.location_name);
final TextView locationDescription = (TextView) findViewById(R.id.location_description);
locationName.setText("Animated Popup");
locationDescription.setText("Animated popup is created by Arun nu solla mattaen"
+ " Transparent layout is used on this example, and animation xml is also used"
+ " on this example. Have a Good day guys.");
}
See this example
Hope this helps you.

How to use Dialog OnClick Listner in Android?

I am new to Android development. Please excuse me If my question is very simple.
I have tried to create a button on my Android Layout view using XML. Now within the Activity class I am trying to get the button and add a on click listner to it. This is working fine without any issues.
On similar lines on the button click i explained previously I have a dialog being popped up. In this dialog I have a ImageButton. On click of this Image button I am trying to set a on click listner using the below code.
The Activity on create is as below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button button = (Button) findViewById(R.id.btnAdd);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final Button btnAdd = (Button) findViewById(R.id.btnAdd);
if(v==btnAdd) {
dialog = new Dialog(this);
dialog.setContentView(R.layout.add_dialog);
dialog.setTitle("Test Title.");
dialog.setCancelable(true);
dialog.show();
final ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
try {
Log.i("Log","1");
button.setOnClickListener(this);
Log.i("Log","2");
}
catch(Exception e)
{
Log.i("Log","3");
dialog.dismiss();
//Dialog d = new Dialog(this);
//d.setTitle("test.");
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
Toast.makeText(this,e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
Log.i("Log","4");
//d.show();
Log.i("Log","5");
}
}
}
In the above I get the Log in this sequence. 1,3,4,5. I dont get the 2. In the toast i get message of blank, blank followed by java.lang.Nullexception.
But this when executed gives me a force close pop up. Please advice how to do this. Or is there any workaround for the same? I need to have a dialog box to come on a button click, and then within the dialog I need to have more than one option of buttons. For each of buttons in the dialog I need to perform different activities. Any kind of help or advice is appreciable. Thank you in advance for your time and help.
Most probably you are trying to retrieve the button from the Activity class. It returns null because this method will only retrieve resources attached to the Activity (by using the method setContentView).
You have two options:
You can inflate the dialog layout using a LayoutInflater
If you are extending the Dialog class, add the listener inside that class instead.
Edit after the update:
As I said above, the problem is:
final ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
because imageButton1 is not part of the layout in the activity. Just replace it by:
final ImageButton button = (ImageButton) dialog.findViewById(R.id.imageButton1);

Get reference to Views in my Android Activity

I have a LinearLayout comprising of a few Buttons and I add this to my activity in the onCreate(..) method with setContentView(R.layout.myscreen). No surprises so far.
How do I get a reference to an iterator to these buttons? I'd like to add listeners to them but I'd rather not directly reference the Button's using their android:id.
Similar questions have been asked here and here but they don't quite answer my question.
Try something like this provide an id root_layout in xml to LinearLayout
LinearLayout mLayout = (LinearLayout) findViewById(R.id.root_layout);
for(int i = 0; i < mLayout.getChildCount(); i++)
{
Button mButton = (Button) mLayout.getChildAt(i);
mButton.setOnClickListener(this);
}
Where mLayout is object of you Linear Layout and Your activity must implements OnClickListener and here goes general listener
#Override
public void onClick(View v)
{
Button mButton = (Button)v;
String buttonText = mButton.getText().toString();
}
NOTE: For this to work properly you Linear Layout must only contains button no other views
You should take a look at my answer here.
In short. I'd assign the buttons a listener by setting the onClick attribute in the XML layout on each Button.
Inside of your Activity you'll need a public method like the one below which basically is what you want to do in your listener.
public void myFancyMethod(View v) {
// do something interesting here
}
If you want to go for accessing other elements you may try following syntax:
<ElementClass> <referencevariable> = (<ElementClass>) findViewById(R.id.<id_of_the_element>);
For Example:
TextView textView= (TextView) findViewById(R.id.t1); //I used t1 to refer my textview in the Layout.
This might work.
Then you can use these views with their inbuilt methods to perform as many as work you want.

Categories

Resources