Reputation: 35
I have a MainActivity with activity_main Layout. in activity_main layout i have 4 Fragments. in one of these fragments i have 2 text box and 1 button. its a login form. when i click on this button i want to know what is that text inside textbox. in MainActivity i defined Textbox with findviewbyid. but when i click on button i cant get text inside text view.
what should i do?
activity_main :
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.decoration.MainActivity"
tools:ignore="MergeRootFrame"
android:background="#FFFFFF"
android:orientation="horizontal"
android:focusableInTouchMode="true">
<TableRow android:layout_weight="1"
android:gravity="top">
<fragment
android:id="@+id/UpLeft"
android:name="com.example.decoration.UpLeftFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left" />
<fragment
android:id="@+id/UpRight"
android:name="com.example.decoration.UpRightFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right" />
</TableRow>
<TableRow android:layout_weight="1"
android:gravity="bottom" >
<fragment
android:id="@+id/DownLeft"
android:name="com.example.decoration.DownLeftFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left" />
<fragment
android:id="@+id/DownRight"
android:name="com.example.decoration.DownRightFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right" />
</TableRow>
down_left_fragment :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="#761F01"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:scaleType="fitXY"
android:src="@drawable/bottomleft" />
<EditText
android:id="@+id/Usernme"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@drawable/txtedges"
android:gravity="center"
android:hint="Username"
android:inputType="text"
android:singleLine="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="3dp"/>
<EditText
android:id="@+id/Password"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@drawable/txtedges"
android:gravity="center"
android:hint="Password"
android:inputType="textPassword"
android:singleLine="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="3dp"/>
<Button
android:id="@+id/SignInBtn"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@drawable/btnedges"
android:text="SignIn"
android:textColor="#FFFFFF" />
<CheckBox
android:id="@+id/Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:checked="true"
android:gravity="right|end"
android:text="Save"
android:textColor="#F7BA00" />
DownLeftFragment.java :
public class DownLeftFragment extends Fragment{
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.down_left_fragment, container, false);
}
}
MainActivity :
public class MainActivity extends Activity {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText a = (EditText) findViewById (R.id.Username);
Button Login = (Button) findViewById(R.id.SignInBtn);
Login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
StrictMode.setThreadPolicy(policy);
String n = a.getText().toString();
login(n);
}
});
public void login(String a)
{...
}
Upvotes: 1
Views: 134
Reputation: 12933
Don't get a new reference of the EditText
inside your onClick()
, because it will be a different object as the one where the user has done the input.
In addition, as ElDuderino suggested, if you have a layout for a Fragment get the instance of the Views with findViewById(..)
in your Fragment. This way if the View isn't needed anymore it can be gargabe collected along with the Fragment. Otherwise it will be alive as long the Activity is.
Edit:
Try this in your Fragment:
public class DownLeftFragment extends Fragment {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View root = inflater.inflate(R.layout.down_left_fragment, container, false);
EditText edit = (EditText) findViewById (R.id.Username);
Button login = (Button) findViewById(R.id.SignInBtn);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StrictMode.setThreadPolicy(policy);
String n = edit.getText().toString();
login(n);
}
});
return root;
}
public void login(String a) {
...
}
}
Your MainActivity looks like this now:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This way all VIews belong to the Fragment which inflates the specific layout.
Upvotes: 1