0

i want to learn how to create an android app, and i'm confused at :

1.first time launching my app will display viewpagers img_btn ice, img_btn jelly. and when img_buttons clicked will display which image chosen in profil.xml, is there any solution do it in 1 xml or i must create 2 xml for each button?

2.if user have choosed img_button, main_activity will change to profil.xml, so for the second time launch will show profil.xml. how can i do that?

main_activity.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#000000"
>
<TextView
    android:text="Choose char"
    android:textSize="25dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#1e00ff"/>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_ViewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"/>

page1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Halaman 2"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal"
    android:textSize="54dp"
    android:textColor="#0aedff"/>
<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton"
    android:background="#00000000"
    android:src="@drawable/btn_info"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_vertical|center"/>

page1.java

import android.support.v4.app.*;
import android.view.*;
import android.os.*;

public class page1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState)
{
    View v=inflater.inflate(R.layout.halaman_1, container,false);
    return v;
}
}

page2.xml and page2.java same as page1 English isn’t my first language, so please excuse any mistakes.

deckyazmi
  • 3
  • 5

1 Answers1

0

I am not sure what Fragment you extended. import android.support.v4.app.Fragment; or import android.app.Fragment; The below example assume you use support.v4

public void replaceFragment(Fragment fragment, String title) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, fragment)
                .addToBackStack(title)
                .commit();
    }

    public void addFragment(Fragment fragment, String title) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.content_frame, fragment, title)
                .addToBackStack(title)
                .commit();
    }

you better has 2 xml (eg. page1.xml, page2.xml) and two classes extend Fragment, you may use the above function to first add a fragment, subsequently use replace when you open new fragments. Notice there is a R.id.content_frame in the functions given. That is from your main_activity.xlm, make a linear_layout name it content_frame, which will hold the open fragment. You then put a button on page1.xml, put an ImageView inside page2.xml, when the button on page1 has been clicked. use replaceFragment to open page2.xml

kggoh
  • 742
  • 1
  • 10
  • 24