I want to create the instruction, so when user press the next button I want to set to the
next layout page. I tried setContentView(R.layout.aboutus); but it looks like to set the new
layout to particular page.
Layout:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#dddddd">
<LinearLayout android:gravity="center" android:background = "#drawable/aboutus"
android:orientation="vertical" android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
code :
public void onItemClick(AdapterView<?> parent, View view, int position,
long lg) {
switch (position) {
case 4:
setContentView(R.layout.aboutus);
}
}
So, I want to know how to go to next layout without new activity and can be back to previous page. Thank you.
You should use a ViewFlipper.
Get a reference to the ViewFlipper:
ViewFlipper myViewFlipper = (ViewFlipper) findViewById(R.id.my_viewFlipper);
You then inflate each page as a view:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.your_layout_id, null);
And then add it to the ViewFlipper:
myViewFlipper.addView( view );
Then when the user hits the button for next page you call this method:
myViewFlipper.showNext();
Or if you want the previous page:
myViewFlipper.showPrevious();
Use a ViewSwitcher or ViewFlipper.
You can try to place the same page in your layout and set all it's component to have Visibility.GONE set, and when you press the button, to set all your visible widgets to GONE and others to Visible. Or you can try a dynamically inflate procedure ( to inflate your views ) since you can use separate layout to inflate views.
Check this for more information about inflate
http://developer.android.com/reference/android/view/LayoutInflater.html
May be you could do this:
If you have two different layouts and one wanna each one at one time.
First get the id's of each layout by findviewbyid method.
then use
firstlayout.setVisibility(View.VISIBLE).
secondlayout.setVisibility(View.GONE);
for showing first layout.
When you press button, in its click listener make
firstlayout.setVisibility(View.GONE).
secondlayout.setVisibility(View.VISIBLE);
it works but the layouts should be seperate.
Hope it helps.
Related
I inflate the same layout multiple times and add them to the same container view. Here is what I do:
layout_inflated.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</LinearLayout>
I inflate this layout and add to another view like the following:
for(int i = 0; i < 3; i++) {
final View rowLayout = layoutInflater.inflate(R.layout.layout_inflated, null, false);
rowLayout.setTag(i);
containerView.addView(rowLayout);
}
Then, suppose I enter the numbers "111", "222" and "333" to the first, second and third rows' edittexts. Then I check the RadioButton on 3rd row, and leave 1st and 2nd rows' RadioButtons unchecked. Then I move to next fragment and this fragment is put into backstack. And then I move back to this fragment, and it is popped out of backstack. At this point, all 3 RadioButtons become checked, and all edittexts have "333" in their boxes. That is, all rows have the third row's state.
But, if I do not inflate rows from the same xml and create rows like the following, then everything works fine:
for(int i = 0; i < 3; i++) {
LinearLayout rowLayout = new LinearLayout(context);
rowLayout.setOrientation(HORIZONTAL);
RadioButton radioButton = new RadioButton(context);
EditText edittext = new EditText(context);
rowLayout.addView(radioButton);
rowLayout.addView(editText);
containerView.addView(rowLayout);
}
I suspect that when I inflate the same xml multiple times and add them to the same parent view, since all of them have same id's, something happens and Android cannot differentiate them. My real row layout is much more complex and cannot create it dynamically, I have to inflate it from xml. So, can anyone explain why this is happening and how I can avoid it?
Edit: When I debug, I see that onTextChanged() and onCheckedChanged() methods are called automatically when fragment is popped out of backstack.
Thanks.
I think your problem is the same element's id in one view. According to documentation
View IDs need not be unique throughout the tree, but it is good practice to ensure that they are at least unique within the part of the tree you are searching.
And as I know findViewById() method will return the first occurrence of a View with the provided id. It can be reason why system redraw your views with values from only last view then the activity return from backstack.
So just remove android:id from xml file. If you need them use setId(int id).
I want to know, there is a linearlayout and use that with setContentView function. Also there is a spinner inside of linearlayout. What I want to do is, create a new layout inside of /res/layout folder and add it into layout that I set with setContentView.
Is there anyway or I need to do that programmatically?
EDIT:
I think I couldn't tell.
I have 2 two layouts(ready). I use the first layout with setContentView.For example, there is a buton and if user click that button, I want to add second layout bottom of first layout when application running.
Easiest you do that with include in the xml of your main layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/second" />
</LinearLayout>
It´s also possible to do it programmatically, but this way I think it is more clear.
Edit:
To do this programmatically, put this code in listener of the first button.
RelativeLayout view = (RelativeLayout) findViewById(R.id.RelativeLayout1);
Button b = new Button(getApplicationContext());
b.setText("Click me too!");
view.addView(b);
Instead of creating a button (or whatever you want) you can also inflate a premade layout.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.second, null);
view.addView(v);
I don't think you can change the res folder programmatically. You need to add any layout programmatically only.
Edited:
Get the 2nd layout's instance using findViewById and use setVisibility method to control the layout's visibility.
I have a problem button visibility. I have 2 button from titlebar.One of them edit, one of them done. First I want to see just edit button and when i clicked edit button, edit button visibility will be false and done button visibility true.
I get their id from xml and when i click one of them i want to change visibility but edit.setVisibility(); it doesnt work.What is wrong?I can see edit button.I want to change buton visibility programmatically.
Can anybody have any idea?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitle = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
edit=(Button)findViewById(R.id.edit);
done=(Button)findViewById(R.id.done);
edit.setVisibility(View.INVISIBLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.main);
if ( customTitle ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.main);
}
main.xml:
<?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">
<Button android:id="#+id/edit"
android:layout_width="57px"
android:layout_height="wrap_content"
android:text="edit"/>
<Button android:id="#+id/done"
android:layout_width="57px"
android:layout_height="wrap_content"
android:text="done"/>
</LinearLayout>
First, you're missing the android:orientation parameter in your LinearLayout.
Second, if you want to change between edit and done you can do this:
edit.setVisibility(View.GONE);
done.setVisibiluty(View.VISIBLE);
and the opposite to change to edit button again.. With View.INVISIBLE the button will not show but still use the space where it was.
The problem is that setFeatureInt just sets the resource ID for the title, which will cause a new inflation of the layout resource, which will be placed in a system FrameLayout called id/title_container. This can be inspected using the Hierarchy Viewer in eclipse.
Essentially, you end up with two instances of the main layout. One set as the content view (below the title) and the other set as the title. When you call findViewById, it will only look in the content view for any views matching the ID. This means that the edit and done buttons you retrieve are the ones in the content view.
If you want to access the buttons in the title area, you can use
View v = getWindow().getDecorView();
edit=(Button)v.findViewById(R.id.edit);
done=(Button)v.findViewById(R.id.done);
edit.setVisibility(View.INVISIBLE);
This will search through the whole view structure of the window, not just the content view, thus solving your problem.
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.
My issue is that I have a main screen, and I would like to dynamically spawn a view under it with a button click, then slide the main view off the screen revealing the view below it. I've accomplished this, but I feel like there's got to be a better way. The way I've done it is very limited in that you can't just spawn views over and over again under the main.
My main XML file looks like this:
<?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"
>
<RelativeLayout
android:id="#+id/subpage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</RelativeLayout>
<RelativeLayout
android:id="#+id/homescreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/homebg"
>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
I've deleted some unnecessary stuff. This is what's important. Notice the first child of the main layout is a relative layout with the id "subpage." As it is I use java to inflate another layout into the subpage layout when a button is clicked then I animate the "homescreen" layout off the screen. It seems like I shouldn't have to have the subpage declared in advance though. I guess my question is, is there a way to dynamically declare a new child layout underneath an existing layout?
=======================================================================
Edit: Part 2 of question
I'm trying to use addView and the app crashes. This is the code I use to try to add a view and inflate my xml into it. In the code below subview is a ViewGroup because as I understand it you can only inflate into ViewGroups, not regular views. Also 'activity' is defined at the top of the class as 'private Activity activity = this'. Any ideas what could be causing the crash?
btnHelp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
subView = (ViewGroup)new View(getApplicationContext());
mainScreen.addView(subView,1);
LayoutInflater inflater = activity.getLayoutInflater();
inflater.inflate(R.layout.help, subView);
}
});
=======================================================================
Edit: Part 3 of question
So one more issue. Everything works great as far as inflating and sliding off. However, the view that is inflated has a button in it. I'm trying to assign a listener to that button, but it doesn't seem to work. I'm doing it by adding the listener to the button after the layout inflater is called in the btnHelp I've been working on. Here's the code:
btnHelp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LayoutInflater inflater = activity.getLayoutInflater();
mainScreen.addView(inflater.inflate(R.layout.help, null),0);
homeScrn.startAnimation(slideLeftOut);
btnBackHome = (ImageView)findViewById(R.id.backMenuBtn);
btnBackHome.setOnClickListener(goHome);
}
});
goHome is a handler I've defined below this as such:
private OnClickListener goHome = new OnClickListener(){
public void onClick(View v) {
Log.d("ClickEvent: ","btnBackHome Clicked");
homeScrn.startAnimation(slideRightIn);
}
};
When I click the button referenced by btnBackHome it doesn't do anything. I'm just not sure if it's because the listener isn't actually being assigned, something is keeping the button from actually being clicked, or something else.
Call addView() on the RelativeLayout to add children to it, where the children are either inflated (getLayoutInflater().inflate()) or constructed directly in Java.
Also, you might consider using a ViewFlipper, considering that it does what you're seeking (animated transition from child to child, with only one child visible at a time in the steady state), perhaps with less code.
The default animation when starting a new Activity is a sliding animation.. why not just separate your "homescreen" and "subpage" into 2 different XML files and 2 Activities?