Hi i have created two fragments on single layout.
In layout-land :
fragments need to display horizontally.
In layout-port :
fragments need to display vertically.
Now i have to run the app on portrait mode means displaying the fragment vertically.
now i have to rotate these to landscape mode means displaying the fragments vertically only.
At the same time i have to run the app on landscape mode means displaying the fragment horizontally.now i have to rotate these to portrait mode means displaying the fragment horizontally.
But i wish to display the output like:
i have to run the app on portrait mode means displaying the fragment vertically.
now i have to rotate these to landscape mode means displaying the fragments horizontally.
At the same time i have to run the app on landscape mode means displaying the fragment horizontally.now i have to rotate these to portrait mode means displaying the fragment vertically.
How can i do ???
Why am getting the result like above.please give me any suggestions .
EDIT:
layout-land : fragment.xml:
<?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"
android:orientation="vertical"
android:id="#+id/_listDetails"
>
<ImageView
android:id="#+id/refresh"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#+id/pos"
android:src="#drawable/ref_off" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/layout"
android:background="#414042"
android:orientation="horizontal" >
<fragment
android:id="#+id/activity_main"
android:name="com.notch.notchbolly.MainActivity"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="0.83"
/>
<View
android:layout_width="2dp"
android:layout_height="fill_parent"
android:background="#FFFFFF"
/>
<fragment
android:id="#+id/subcate"
android:name="com.notch.notchbolly.SubCate"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
layout-port: fragment.xml:
<?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"
android:orientation="vertical"
android:id="#+id/_listDetails"
>
<ImageView
android:id="#+id/refresh"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#+id/pos"
android:src="#drawable/ref_off" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/layout"
android:background="#414042"
android:id="#+id/orien"
android:orientation="vertical"
>
<fragment
android:id="#+id/activity_main"
android:name="com.notch.notchbolly.MainActivity"
android:layout_width="fill_parent"
android:layout_height="200dp"
/>
<View
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#FF0000"
/>
<fragment
android:id="#+id/subcate"
android:name="com.notch.notchbolly.SubCate"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</RelativeLayout>
This is my AndroidListFragmentActivity:
public class AndroidListFragmentActivity extends FragmentActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment);
ImageView refresh = (ImageView) findViewById(R.id.refresh);
refresh.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent in = new Intent(AndroidListFragmentActivity.this, AndroidListFragmentActivity.class);
startActivity(in);
}
});
}
#Override
public void onConfigurationChanged ( Configuration newConfig )
{
super.onConfigurationChanged(newConfig);
}
}
Remove the android:configChanges="orientation" from the activity declaration in manifest file.
Related
In my activity IndividualActivity i have set the contextview of individual.xml now i also want to add another sub_individual.xml in the activity without removing, replacing or hiding the individual.xml so that they remain together.
public class IndividualActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.individual);
...
}
}
individual.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IndividualActivity"
android:background="#drawable/default_wall" >
<ListView
android:id="#+id/listx"
android:layout_width="fill_parent"
android:layout_height="380dp"
android:dividerHeight="1dp"/>
...
sub_individual.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="40dip"
android:layout_marginLeft="0dp"
android:background="#323331"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="5dip"
android:text="custom title bar" >
<ImageView
android:id="#+id/bk_btn"
android:layout_width="35dip"
android:layout_height="35dip"
android:background="#DDD" />
...
This functionality is supported by using Fragments. See http://developer.android.com/guide/components/fragments.html
You can use this from xml
Use "include" tag in your first xml and add second xml in this tag.
I'd like to achieve such a layout, where user got 2 control panels. He is able to switch from first to second by pressing button and vice versa.
Already have tried to use LayoutInflater, however, without success :/
The main reason, why doing it with 2 different layouts is, that buttons will be almost on the same position, so i'd like to prevent all that mess in one layout and create 2 separate control panel layouts.
Here are my layouts:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/control_panel_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5">
<!-- Here comes including layouts-->
</RelativeLayout>
</LinearLayout>
control_panel_1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/control_panel1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_action1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_selector"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btn_action2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_selector2"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btn_action1"
android:layout_marginRight="50dp"/>
<Button
android:id="#+id/btn_action3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_selector3"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/btn_action1"
android:layout_marginLeft="50dp" />
</RelativeLayout>
control_panel_2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/control_panel1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_action3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_selector4"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btn_action4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_selector5"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btn_action3"
android:layout_marginTop="20dp"
android:layout_marginRight="60dp"/>
</RelativeLayout>
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_root);
RelativeLayout controlPanelLayout = (RelativeLayout) findViewById(R.id.control_panel_layout);
//Inflate first control panel on activity start
LayoutInflater layoutInflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View controlPanel1 = layoutInflater.inflate(R.layout.control_panel_1.xml);
controlPanelLayout.addView(controlPanel1)
}
EDIT:
As shown in the image, let's say activity starts with Screen1 and once user press Btn1, Screen2 appears...as you can see, only control panel has been switched.
however, it won't inflate that layout at start of application...
Thanks in advance for any suggestions, hints...
Inflate your control panel in onCreateView() and when handling button click (to change panel). The code should be somewhat like this:
private void inflateYourPanel(int panelLayoutID, ViewGroup placeholder) {
placeholder.removeAllViews();
View view = LayoutInflater.from(mActivity).inflate(panelLayoutID, placeholder, false);
//find your views and set values and listeners
placeholder.addView(view);
}
Edit: placeholder may be any layout (control_panel_layout) inflated when starting activity etc
Still may be you'd better look at Fragments - it may fit your purpose better and provide more scalability
I created a menuView.xml layout to be in all of the layouts of my activity. This layout has one column on each border and a title bar like this:
ComposeView http://img845.imageshack.us/img845/2121/d6zp.png
I insert this layout in the other layouts this way:
<!-- Show menu -->
<com.example.MenuView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
But if one of the layouts has full screen view, part of this view gets covered by the MenuView, so...
How could I tell to this view to adapt its size to the blank space inside the MenuView to not get covered by it?
UPDATE -- full XML included
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/degradado">
<!-- Show menu -->
<com.example.MenuView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:id="#+id/Left_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" >
//Here go buttons, views, etc...
</RelativeLayout>
<RelativeLayout
android:id="#+id/Right_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" >
//Here go buttons, views, etc...
</RelativeLayout>
What happens here is that these 2 Relative layouts get covered by the MenuView (The darkest gre borders and the top black bar), and the ideal way would be that these 2 layouts get fitted to the blank space (the clearest gray).
I can solve this setting margin sizes to the Relative layouts to fit inside of it, but i know this is not the best way to do it, so I don't know if there is another way.
I think the best way to solve your issue is with inheritance.
If you define an Activity that can be used as a template for all your fleshed out Activitys to add their content to.
I don't know what you custom menu is 'made of' but as a simple example:
Create a basic Activity with code:
public class ActivityWithMenu extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_with_menu_layout);
}
}
and xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityWithMenu" >
<TextView
android:layout_width="match_parent"
android:layout_height="20dip"
android:background="#ff000000"
android:textColor="#ffffffff"
android:text="Main Menu Title Bar"
android:id="#+id/mainmenutitle" />
<LinearLayout
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_below="#+id/mainmenutitle"
android:layout_alignParentLeft="true"
android:background="#ff999999"
android:orientation="vertical"
android:id="#+id/lefthandmenu" />
<LinearLayout
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_below="#+id/mainmenutitle"
android:layout_alignParentRight="true"
android:background="#ff999999"
android:orientation="vertical"
android:id="#+id/righthandmenu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/righthandmenu"
android:layout_toRightOf="#+id/lefthandmenu"
android:layout_below="#+id/mainmenutitle"
android:orientation="vertical"
android:id="#+id/activitycontent"
/>
</RelativeLayout>
Then create your xml for a specific Activity, in this case a simple 'Hello World' layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00ff00"
android:text="Hello World!"
/>
</LinearLayout>
</ScrollView>
But now when you write the code for this Activity, extend 'ActivityWithMenu' instead of the Activity class direct and inflate this xml layout as follows:
public class Activity1 extends ActivityWithMenu
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
LinearLayout ll = (LinearLayout)findViewById(R.id.activitycontent);
ScrollView sv = (ScrollView)this.getLayoutInflater().inflate(R.layout.activity1_layout, ll, false);
ll.addView(sv);
}
}
I have added the code for making the Activity fullscreen here instead of in the parent ActivityWithMenu class assuming that you wouldn't want them all displayed that way but you could move it into the parent class if appropriate.
Hope this helps.
there's a thing that is really making me crazy. The layout of the screen isnt changing from land to portrait correctly.
I have two folders in my layout folder to handle the orientation thing and .xml files in it, like this:
layout\main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="#+id/menu"
android:name="br.trebew.odonto.MenuData"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="#+id/princ"
android:name="br.trebew.odonto.FragList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
and the other:
layout-large-land\main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="290dp"
android:layout_height="fill_parent">
<fragment
android:id="#+id/menu"
android:name="br.trebew.odonto.MenuData"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="#+id/princ"
android:name="br.trebew.odonto.FragList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<fragment
android:id="#+id/fdetalhe"
android:name="br.trebew.odonto.DetalheFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
My main java class is OdontO.java, which is:
public class OdontO extends FragmentActivity implements OnDateChangeListener, OnAgendaItemClickListener {
FragList fList;
MenuData mData;
DetalheFragment frag_detalhe;
private boolean detalhesInLine;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("LayoutOr", "Entrou no onCreate OdontO");
mData = (MenuData) getSupportFragmentManager().findFragmentById(R.id.menu);
fList = (FragList) getSupportFragmentManager().findFragmentById(R.id.princ);
frag_detalhe = (DetalheFragment) getSupportFragmentManager().findFragmentById(R.id.fdetalhe);
detalhesInLine = (frag_detalhe != null &&
(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE));
if (detalhesInLine) {
fList.enablePersistentSelection();
}
else if (frag_detalhe != null) {
frag_detalhe.getView().setVisibility(View.GONE);
}
}
If screen is portrait than rotating it changes correctlly.
The problem is the reverse. If the screen is land than rotation has no change on the layout. Infact it calls the onCreate method correctly, but the layout doesnt changes to portrait! =/
Anyone would know why?
The problem may be that your portrait is in a folder for normal-sized screens and your landscape is in a folder for large-size screens. Maybe you could change layout-large-land to just layout-land (if your device has a normal screen) or change layout to layout-large (if your device has a large screen) and see it if does anything. (Note: even if it has a large screen, putting it in layout and layout-land should still work).
I have an activity in android which I set up a button under a SurfaceView. And the result looks like this:
And I'm satisfied with it except one thing, the writing on the button is astray and must be setup correctly.
For this I used animations like this:
takePhoto = (Button) findViewById(R.id.btnPhoto);
takePhoto.setText(t);
RotateAnimation ranim = (RotateAnimation)AnimationUtils.loadAnimation(this, R.anim.myanim);
ranim.setFillAfter(true);
takePhoto.setAnimation(ranim);
WHERE the res/anim/myanim looks like:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="0" />
But the result is a little bit disappointing cause the whole looks like:
The text is ok, but the dimensions are reduced.
What I want is to keep the text correctly and the initial size.How could I do that???
It has nothing to do with the button's properties in the xml file cause I haven't changed them when loading the animation....Ideas?Thanks...
EDIT: xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
>
<SurfaceView android:layout_width="450dip"
android:layout_height="fill_parent" android:id="#+id/surface_camera" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_toRightOf="#id/surface_camera"
android:layout_height="fill_parent"
>
<Button android:layout_width="680dip"
android:layout_height="680dip"
android:id="#+id/btnPhoto"
android:layout_alignParentBottom="true"
android:text="Take Photo"
android:layout_gravity="bottom|right" />
</RelativeLayout>
</RelativeLayout>
IMPORTANT:
In the cases described below...my activity is in `LANDSCAPE` mode and the position of the phone is portrait.Another issue is that when I turn my phone in landscape mode(and here I mean the position of the phone, cause the activity is still landscape) the button doesn't go at the bottom of the phone is stays there.No matter how I turn the phone the button is in the same position!!!!
You should read this
Make two layout folders like follows ...
In both of folders your layout file name should be same.
In layout-port folder your layout file should look like this ...
android:layout_toRightOf="#id/surface_camera"
replaced by ...
android:layout_Below="#id/surface_camera"
as follows ...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
>
<SurfaceView android:layout_width="450dip"
android:layout_height="fill_parent" android:id="#+id/surface_camera" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_toBelow="#id/surface_camera"
android:layout_height="fill_parent"
>
<Button android:layout_width="680dip"
android:layout_height="680dip"
android:id="#+id/btnPhoto"
android:layout_alignParentBottom="true"
android:text="Take Photo"
android:layout_gravity="bottom|right" />
</RelativeLayout>
</RelativeLayout>
Even this will considered as bad practice but you can also handle this manually, try this ..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
int orientation = getResources().getConfiguration().orientation;
if(orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.main1);
}
else {
setContentView(R.layout.main2);
}
}
Also If you are doing this way No need to make two layout folders, but needs two layout files main1 and main2 in same folder
Yet another option ...
In manifest ...
<activity android:name=".ActivityName" android:configChanges="keyboardHidden|orientation">
+
In your Activity ...
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if(orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.main2);
}
else {
setContentView(R.layout.main);
}
}
Try this XML instead:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="fill_parent">
<SurfaceView android:id="#+id/surface_camera"
android:layout_width="450dip"
android:layout_height="fill_parent" />
<FrameLayout android:layout_width="fill_parent"
android:layout_toRightOf="#id/surface_camera"
android:layout_height="fill_parent">
<Button android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView android:id="#+id/btnPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Take Photo" />
</FrameLayout>
</RelativeLayout>
You will rotate the Text not the button.