Professional Documents
Culture Documents
Agments
Agments
A fragment has its own layout and its own behaviour with its own life cycle
callbacks.
You can add or remove fragments in an activity while the activity is running.
You can combine multiple fragments in a single activity to build a multi-pane
UI.
A fragment can be used in multiple activities.
Fragment life cycle is closely related to the life cycle of its host activity which
means when the activity is paused, all the fragments available in the activity
will also be stopped.
A fragment can implement a behaviour that has no user interface component.
Fragments were added to the Android API in Honeycomb version of Android
which API version 11.
You create fragments by extending Fragment class and You can insert a
fragment into your activity layout by declaring the fragment in the activity's
layout file, as a <fragment> element.
We can show only a single activity on the screen at one given point in time. So
we were not able to divide device screen and control different parts separately.
But with the introduction of fragment we got more flexibility and removed the
limitation of having a single activity on the screen at a time.
Now we can have a single activity but each activity can comprise of multiple
fragments which will have their own layout, events and complete life cycle.
Android fragments have their own life cycle very similar to an android activity. This
section briefs different stages of its life cycle.
Fragment lifecycle
Here is the list of methods which you can to override in your fragment class −
The requestWindowFeature(Window.FEATURE_NO_TITLE)
method of Activity used to hide the title which will be coded before the
setContentView method.
Let's see the full code to hide the title bar in android.
activity_main.xml
</android.support.constraint.ConstraintLayout>
Activity class
package first.javatpoint.com.hidetitlebar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
}
}
Output: Hiding the Title Only
Syntax:
<activity android:name="package_name.Your_ActivityName"
android:screenOrientation="orirntation_type">
</activity>
Example:
android:screenOrientation="portrait">
</activity>
<activity android:name=".SecondActivity"
android:screenOrientation="landscape">
</activity>
Value Description
unspecified It is the default value. In such case, system chooses the orientation.
activity_main.xml
package example.javatpoint.com.screenorientation;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
}
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}
activity_second.xml: activity_second.xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="180dp"
android:text="this is landscape orientation"
android:textSize="22dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
package example.javatpoint.com.screenorientation;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
AndroidManifest.xml:
Output: