activity_main.xml
-------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentsexample.One"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentsexample.Two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
-------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentsexample.One"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentsexample.Two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
first.xml
----------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#996699">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First fragment"
android:layout_gravity="center"/>
<EditText
android:id="@+id/idText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Id"
android:textColorHint="#110F10">
<requestFocus />
</EditText>
<EditText
android:id="@+id/pwdText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Enter Password"
android:textColorHint="#110F10"/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
two.xml
-------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#996600">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Fragment"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/idView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/pwdView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear" />
</LinearLayout>
MainActivity.java
---------------------
package com.example.fragmentsexample;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
One.java
--------------
package com.example.fragmentsexample;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class One extends Fragment{
EditText ed1,ed2;
Button btn;
String str1,str2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup viewGroup=(ViewGroup) inflater.inflate(R.layout.first, container,false);
ed1=(EditText) viewGroup.findViewById(R.id.idText);
ed2=(EditText) viewGroup.findViewById(R.id.pwdText);
btn=(Button) viewGroup.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
str1=ed1.getText().toString();
str2=ed2.getText().toString();
SharedPreferences prefs=getActivity().getSharedPreferences("myprefs",0 );
SharedPreferences.Editor prefsEditor=prefs.edit();
prefsEditor.putString("ID", str1);
prefsEditor.putString("PWD", str2);
prefsEditor.commit();
Two fg2=new Two();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// fragmentTransaction.replace(R.id.fragment2, fg2);
fragmentTransaction.add(R.id.fragment2, fg2);
fragmentTransaction.commit();
}
});
return viewGroup;
}
}
Two.java
----------------
package com.example.fragmentsexample;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.TextView;
public class Two extends Fragment {
TextView tv1,tv2;
String line1,line2;
Button btn2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup viewGroup=(ViewGroup) inflater.inflate(R.layout.two, container,false);
tv1=(TextView) viewGroup.findViewById(R.id.idView);
tv2=(TextView) viewGroup.findViewById(R.id.pwdView);
SharedPreferences prefs1=getActivity().getSharedPreferences("myprefs",0 );
line1=prefs1.getString("ID", null);
line2=prefs1.getString("PWD", null);
tv1.setText(line1);
tv2.setText(line2);
btn2=(Button) viewGroup.findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv1.setText(null);
tv2.setText(null);
}
});
return viewGroup;
}
}
No comments:
Post a Comment