Fragment demo
- 2. <FrameLayout
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=".Fragment3">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="KKPK"
android:textSize="20sp"
android:textStyle="bold"
android:background="@color/teal_200"
/>
</FrameLayout>
MainActivity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/fr1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.fragmentdemo.Fragment1"/>
<fragment
android:id="@+id/fr2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.fragmentdemo.Fragment2"/>
<fragment
android:id="@+id/fr3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.fragmentdemo.Fragment3"/>
</LinearLayout>
- 6. setContentView(R.layout.activity_main);
b1=findViewById(R.id.button1);
b2=findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment( new KKFragment());
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment( new PKFragment());
}
});
}
private void loadFragment(Fragment fragment) {
FragmentManager fragmentManager=
getFragmentManager();
FragmentTransaction ft=
getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame1,fragment);
ft.commit();
}
}
ImageButton:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/imgb"
android:layout_width="wrap_content"
- 12. Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ch1=findViewById(R.id.chb1);
ch2=findViewById(R.id.chb2);
ch3=findViewById(R.id.chb3);
ch4=findViewById(R.id.chb4);
tv1=findViewById(R.id.textView1);
tv2=findViewById(R.id.textView2);
submit=findViewById(R.id.bt1);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int amount=0;
StringBuffer sb= new StringBuffer();
sb.append("You have Ordered : ");
if(ch1.isChecked()){
sb.append("n Burger + $ 50" );
amount+=50;
}
if(ch2.isChecked()){
sb.append("n Pizza + $ 50" );
amount+=50;
}
if(ch3.isChecked()){
sb.append("n Tea + $ 10" );