I have a layout with a spinner and a lot of textboxes and buttons.
I need to make appear some elements of the layout when one of the options of the spinner is selected and to make disappear when other is selected. ----> PROBLEM SOLVED!!!
But also, I need to deactivate some elements of the layout. I mean that they have to be visible, but the user can't press buttons or edit edittext's
Yes, you can.
Assuming you have a handle to your layout you can do smth like this:
yourLayoutToShow.setVisibility(View.VISIBLE)
or
yourLayoutToHide.setVisibility(View.GONE)
UPDATE:
To get a handle to your layout (the one you want to show/hide dynamically) you need to do smth like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
LinearLayout viewToShowOrHide =
(LinearLayout) findViewById(R.id.view_to_show_or_hide);
}
You can disable a control by doing ...
myControl.setEnabled(false);
Related
Well, I'm making a game that uses a class that extends from SurfaceView to represent the game screen, but it works with buttons, so I'm defining the "buttons panel" on xml and then adding the "GameView" to the LinearLayout, that contains all the stuff, programatically and bringing the "buttons panel" to front, just like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout rl1 = (LinearLayout)findViewById(R.id.rl1);
rl1.addView(new GameView(this));
SquareLayout sq1 = (SquareLayout)findViewById(R.id.square);
//the control buttons are in another custom layout called "SquareLayout"
sq1.bringToFront();
}
The problem here is that i want the layout that contains the game buttons(the SquareLayout) to fill the half of the height of the RelativeLayout(the activity is set to landscape), i get this morphing the RelativeLayout to a vertical LinearLayout, adding another SquareLayout, and setting both weight's properties to 1, and it's like this:
The problem now is that "bringToFront();" method does not work on LinearLayout, so I can't modify the z of the buttons panel after adding the gameview, so my question is if there's a way to make the buttons panel just like is on the image on a RelativeLayout or a similar method like "bringToFront()" that works on LinearLayout.
Try to invoke the method bringChildToFront(View) on your LinearLayout http://developer.android.com/reference/android/view/ViewGroup.html#bringChildToFront(android.view.View). The parameter will be the view you want to display on top of its siblings.
I want to be able to display text (a bar if you will) at the bottom of my application dynamically to indicate if the the application is online or not (an endless issue for users currently). I don't need for this to have any action, but I do need it to be able to control it displaying or not based on them toggling between online and offline mode. So the split action bar is not what I am looking for.
Not enough reputation points to post an example. Doh. Here is the link:
Ugly example, look at bottom
Something simple is fine - I am good with XML based or dynamic (though I will also need to hide and show it dynamically).
Thanks!
Dynamic solution. Define your method that the activity will use to hide / display your bottom TextView (text bar?).
In your XML:
<TextView
android:id="#+id/text_bar"
...
...
android:visibility="gone"/>
In your activity:
public MyActivity extends Activity{
private TextView mTextBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_xml);
...
...
mTextBar = (TextView) findViewById(R.id.text_bar);
}
public void updateVisibility(boolean visible){
int visibility = (visible) ? View.VISIBLE: View.GONE;
mTextBar.setVisibility(visibility);
}
}
Initially, the TextView (your text bar) has it's visibiity set to gone. Dynamically, when you want to show it call the updateVisibility(true) method to show it, and updateVisibility(false) to hide it.
Since I'm not sure how you are handling the check for the network status (in queries, in the onCreate, etc). I don't know what you intended to do for the TextView. If you need advice on how to position the TextView on the bottom of the screen I can provide example code for that as well.
I'm trying to create a RadioGroup with RadioButtons that have more than text only.
The image below will make things easier to understand.
I want the RadioButton to be selected when the user clicks somewhere in the layout around it.
I've tried do do somthing lik this:
<RadioGroup>
<RelativeLayout>
<RadioButton>
<...other stuff>
</RelativeLayout>
</RadioGroup>
But that didn't work.
Does somebody know how to easily get the requested layout and behaviour? Thanks
EDIT:
I forgot to mention that I don't want to use a list. I want all items to be visible because there are maximum three. Also, the container is a ScrollView
As Der Golem said you can use ListView and set it to be in a single choice mode: list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
But if you need only few layouts you can register Click listener to your layouts and call onClick events on RadioButtons:
bigLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
radioButton.performClick();
}
});
Hope it will help
I would like to change the background of all the Buttons in a View.
android:background="#drawable/button_red"
And i would like to do this in an OnClick() event.
android:onClick="ChangeCouleur"
I would like to do this in a foreach loop, but i am not sure how to do this.
For example:
for( b in ... )
if (b.getid()!=idofthebutton)
b.setbackgroud(button_red)
Thanks for any help!
Put all your Buttons in an Array of Buttons, then cycle to it an change the background.
Button button1 = (Button)this.findViewById(...);
Button button2 = (Button)this.findViewById(...);
Button button3 = (Button)this.findViewById(...);
Button[] buttons={button1, button2, button3};
for (Button currentButton : buttons) {
currentButton.setBackgroundResource(R.drawable.my_new_background);
}
One thing you could do is subclass Button, then make all the buttons in you app instances of your new class. That way, if you decide that you want to adjust the color or change something else, you only have to do it once, and it will change all the buttons in your app. Here's a question that should give you some pointers on how to do that.
For changing the background of the clicked button
public void changeColor(View v) {
v.setBackground(btn_red);
}
You will have to get a reference to all the Buttons. Add them to an ArrayList or something similar. In your ChangeCouleur method use a loop to iterate through all the buttons changing the color of each one.
Create .XML file in your “res/drawable/” and use selector attribute in it.And use different images for the button.Please refer this link.
http://www.mkyong.com/android/android-imagebutton-selector-example/
This will clear your idea. :)
want to make an Android app that starts with a main layout and when you push a button (called stateButton) that is in this layout the layout changes to a main2 layout containing another button (called boton2), and when you push this one you get back to the first main.
I want to do this in the same activity without creating or starting another one.
Here I show you part of the code:
public class NuevoshActivity extends Activity
implements SensorEventListener, OnClickListener {
private Button stateButton;
private Button boton2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.stateButton = (Button) this.findViewById(R.id.boton);
this.boton2 = (Button) this.findViewById(R.id.boton2);
stateButton.setOnClickListener(this);
boton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==stateButton) {
setContentView(R.layout.main2);
}
else if(v==boton2) {
setContentView(R.layout.main);
}
}
}
The mains only have some images, text views and the buttons.
But I've some troubles. Can't it just be as simple as that or what am I missing or what is wrong?
When you use findViewById, you are actually trying to find a view inside the layout you specified by the setContentView. So using setContentView again and again might bring problems when you are trying to check for buttons.
Instead of using a setContentView, I would add the 2 layouts for the screen as child's for a view-flipper which only shows one child at a time. And you can specify the index of which child to show. The benefit of using a view flipper is that you can easily specify a 'in' and 'out' animation for the view if you need an animation when you switch between views. This is a lot cleaner method then recalling setContentView again and again.
The FrameLayout handles this wonderfully... Use this with the <include... contstruct to load multiple other layouts, then you can switch back and forth between them by using setvisibility(View.VISIBLE); and setVisibility(View.INVISIBLE); on the individual layouts.
For example:
Main XML including two other layouts:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="#+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<include android:id="#+id/buildinvoice_step1_layout" layout="#layout/buildinvoice_step1" android:layout_width="fill_parent" android:layout_height="fill_parent"></include>
<include android:id="#+id/buildinvoice_step2_layout" android:layout_width="fill_parent" layout="#layout/buildinvoice_step2" android:layout_height="fill_parent"></include>
</FrameLayout>
Code to switch between layouts:
findViewById(R.id.buildinvoice_step1_layout).setVisibility(View.VISIBLE);
findViewById(R.id.buildinvoice_step2_layout).setVisibility(View.INVISIBLE);
You will also need to set the visibility of the individual layouts when the activity starts (or in XML) otherwise you will see them both - one on top of the other.
Your boton2 button will be NULL because the definition of the button is in main2.xml.
The only views you will be able to find are the views which are defined in main.xml.
Thanks!!! All the info was usefull to understand a lot of things and as C0deAttack commented I've got troubles with the button on the main2. What I've done is to set View.VISIBLE and View.GONE to the TextViews and Buttons that I wanted in each layout. Thank you very much.