I'm using broadcast receiver to show a dialog.So the flow of code is something like:
Step1 Getting the requestCode value
Step2 Based on this requestCode the broadCast receiver goes to if or else if or else part
Step3 If the value that i entered using some scanner into the EditText(i.e Scan) doesn't matches it shows a Toast "Item Not Available".
Step 4 Once "Item Not Available" toast comes I need the focus to be on EditText(i.e Scan) so that next time also i can put fresh value in that after clearing it.
So my question is "How can i give focus to the EditText" and set it to the EditText(i.e Scan).
For Reference I'm attaching the snap with code snippet and the layout.xml.Please have a look and drop your suggestions why the focus is going to the listview.
.java snippet
final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
loc = mspinner.getItemAtPosition(mspinner.getSelectedItemPosition())
.toString();
final String ItemNo;
final String Desc;
final String StockUnit;
final String PickSeq;
final String qtyCount;
final String qtyonHand;
final Button mok;
final Button mcancel;
final Button mplus;
final Button mminus;
final EditText medtQtyCount;
final EditText medtItem;
final EditText medtdesc;
final EditText medtuom;
final DatabaseHandler dbHandler;
final String[] UOM = null;
int requestCode;
LayoutInflater li = LayoutInflater.from(InventoryCount.this);
View promptsView = li.inflate(R.layout.quantityupdate, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
InventoryCount.this);
alertDialogBuilder.setView(promptsView);
//requestCode=Integer.parseInt(intent.getStringExtra("idx"));
requestCode=intent.getIntExtra("idx", -1);
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
dbHandler = new DatabaseHandler(InventoryCount.this);
medtuom = (EditText) promptsView.findViewById(R.id.edt_mseshipuom_mic);
mok = (Button) promptsView.findViewById(R.id.btn_mseshipOk_mic);
mcancel = (Button) promptsView.findViewById(R.id.btn_mseshipCancel_mic);
mplus = (Button) promptsView.findViewById(R.id.btn_mseshipIncr_mic);
mminus = (Button) promptsView.findViewById(R.id.btn_mseshipDecr_mic);
medtQtyCount = (EditText) promptsView
.findViewById(R.id.edt_shipShiped_mic);
medtdesc = (EditText) promptsView
.findViewById(R.id.edt_mseshipQtyOrd_mic);
medtItem = (EditText) promptsView
.findViewById(R.id.edt_mseshipItemNo_mic);
if (requestCode == 1) {
}
else if (requestCode == 0) {
// ItemNo
/*if (resultCode == RESULT_OK) {
Log.i("Scan resul format: ",
intent.getStringExtra("SCAN_RESULT_FORMAT"));
*/
String itNo = intent.getStringExtra("SCAN_RESULT");
dbhelper.getReadableDatabase();
MIC_Inventory mic_inventory = dbhelper.getMicInventoryDetails(
loc, itNo);
dbhelper.closeDatabase();
if (mic_inventory != null) {
loc = mspinner.getItemAtPosition(
mspinner.getSelectedItemPosition()).toString();
ItemNo = mic_inventory.getItemno();
Desc = mic_inventory.getItemdescription();
PickSeq = mic_inventory.getPickingseq();
StockUnit = mic_inventory.getStockunit();
qtyonHand = mic_inventory.getQoh();// This value gives
// QOHand
qtyCount = mic_inventory.getQc();
medtItem.setText(ItemNo);
medtdesc.setText(Desc);
medtQtyCount.setText(qtyCount);
medtuom.setText(StockUnit);
mplus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String a = medtQtyCount.getText().toString();
int b = Integer.parseInt(a);
b = b + 1;
a = a.valueOf(b);
medtQtyCount.setText(a);
}
});
mminus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int c = Integer.parseInt(medtQtyCount.getText()
.toString());
c = c - 1;
medtQtyCount.setText(new Integer(c).toString());
}
});
mok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*
* UOM[mspinnerUom.getSelectedItemPosition()] =
* medtQtyCount .getText().toString();
*/
MIC_UOMInternal mic_uom = new MIC_UOMInternal();
mic_uom.setLocation(loc);
mic_uom.setItemno(ItemNo);
String updatedqtyCount = medtQtyCount.getText()
.toString();
if (!qtyCount.equals(updatedqtyCount)) {
mic_uom.setQc(Double
.parseDouble(updatedqtyCount));
mic_uom.setUom(StockUnit);
MIC_Inventory mic_Inventory = new MIC_Inventory();
mic_Inventory.setItemdescription(Desc);
mic_Inventory.setItemno(ItemNo);
mic_Inventory.setLocation(loc);
mic_Inventory.setPickingseq(PickSeq);
mic_Inventory.setQc(updatedqtyCount);
mic_Inventory.setQoh(qtyonHand);
mic_Inventory.setStockunit(StockUnit);
dbHandler.getWritableDatabase();
String result = dbHandler
.insertIntoInternal(mic_uom);
if (result.equals("success")) {
result = dbHandler.updateMIC(mic_Inventory);
}
dbHandler.closeDatabase();
}
Intent i = new Intent(InventoryCount.this,
InventoryCount.class);
i.putExtra("et", 1);
i.putExtra("LOCATION", loc);
// i.putExtra("ID", ID);
startActivity(i);
// InventoryCount.this.finish();
}
});
mcancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.cancel();
}
});
// show it
alertDialog.show();
} else {
/*
* Toast.makeText(this, "Item not available",
* Toast.LENGTH_LONG).show();
*/
toastText.setText("Item not available");
Toast toast = new Toast(getBaseContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
lstView.setFocusable(false);
msearchtext.requestFocus();
/*InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(msearchtext, InputMethodManager.SHOW_IMPLICIT);*/
msearchtext.setText("");
}
else if (requestCode == 2) {
}
else
{
toastText.setText("Problem in Scanning");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
}
}
Layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/border_green"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/txt_InvTitle"
style="#style/pageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="#string/invTitle" />
<View
android:id="#+id/txt_InvView"
android:layout_width="match_parent"
android:layout_height="2dip"
android:layout_below="#+id/txt_InvTitle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#2E9AFE" />
<LinearLayout
android:id="#+id/invLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/txt_InvView"
android:layout_marginTop="16dp" >
<TextView
android:id="#+id/txtLoc"
style="#style/textRegular"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center"
android:text="#string/location" />
<Spinner
android:id="#+id/sploc"
style="#style/SpinnerItemAppTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:editable="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/invScanType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/invLocation"
android:layout_gravity="center"
android:layout_marginBottom="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="18dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/edt_Search_mic"
style="#style/EditTextAppTheme_Scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_weight=".15"
android:gravity="center"
android:hint="#string/scan" />
<RadioGroup
android:id="#+id/radioScanBasedOn_mic"
style="#style/RadioButtonAppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radioInum_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:button="#drawable/radiobutton_selector"
android:checked="true"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/itemno" />
<RadioButton
android:id="#+id/radioNum_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="#drawable/radiobutton_selector"
android:checked="false"
android:layout_marginRight="5dp"
android:layout_weight=".25"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/manfno" />
<RadioButton
android:id="#+id/radioUpc_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="#drawable/radiobutton_selector"
android:checked="false"
android:layout_marginRight="5dp"
android:layout_weight=".25"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/upc" />
</RadioGroup>
</LinearLayout>
<HorizontalScrollView
android:id="#+id/scroll_full_mic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/invScanType" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="25dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lay_fullTitle_mic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
style="#style/textRegular_list"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:text="#string/itemno"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:gravity="center|left"
android:text="#string/description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/pick_seq"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/qoh"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/qc"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/uom"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/lst_msefull_mic"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/ListViewAppTheme.White" >
</ListView>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/lay_PO_mic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="41dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:visibility="gone" >
<Button
android:id="#+id/btn_OrderLstImport_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_OrderLstExport_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_OrderLstExit_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
What about this:
lstView.post(new Runnable() {
#Override
public void run() {
msearchtext.requestFocus();
}
});
Related
I have an dialog that appears where the user can costumize how many points they get from each action. The dialog is based on a layout that is not the main_activity. the main activity is the layout that is used in setContentView in the onCreate method.
when I try to get the values I am only getting null, (because the layout where the EditText is is not set as the contentview.... maybe?)
How do I solve this problem so that I get the value of the edittext in the "settings" layout and can use that value in the MainActivity?
private int teamOnePoints=0;
private int teamTwoPoints=0;
private boolean teamOneField=true;
private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;
private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = (ToggleButton)
findViewById(R.id.field_team_toggle); //activates toggle button
toggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
//changes between the teams
teamOneField=false;
} else {
teamOneField=true;
}
}
});
FloatingActionButton fab = findViewById(R.id.points_edit_button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.settings, null);
builder.setView(v);
builder.setTitle(getResources().getString(R.string.edit_rules));
builder.setPositiveButton(getResources().getString(R.string.ok_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
applySettings();
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_button),
null);
builder.show();
}
private void applySettings(){
EditText newBurnedPoints = findViewById(R.id.burned_edit);
burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());
EditText newLyrePoints = findViewById(R.id.lyre_edit);
lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());
EditText newOneHandLyrePoints = findViewById(R.id.one_hand_lyre_edit);
oneHandLyrePoints=Integer.parseInt
(newOneHandLyrePoints.getText().toString());
EditText newHomeRunPoints = findViewById(R.id.home_run_edit);
homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());
EditText newRevPoints = findViewById(R.id.rev_edit);
revPoints=Integer.parseInt(newRevPoints.getText().toString());
EditText newTeamOneName = findViewById(R.id.name_one_edit);
teamOneName= newTeamOneName.getText().toString();
TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
setNewTeamOneName.setText(teamOneName);
EditText newTeamTwoName =findViewById(R.id.name_two_edit);
teamTwoName= newTeamTwoName.getText().toString();
TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
setNewTeamTwoName.setText(teamTwoName);
}
SETTINGS LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:id="#+id/settings_layout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/name_one_settings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/name_team_one"
style="#style/settings_headings"
/>
<TextView
android:id="#+id/name_two_settings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/name_team_two"
style="#style/settings_headings"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/name_one_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/team_one"
android:maxLength="20"
android:singleLine="true"
/>
<EditText
android:id="#+id/name_two_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/team_two"
android:maxLength="20"
android:singleLine="true"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/burned_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/burned_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lyre_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/lyre_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="3"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/one_hand_lyre_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/one_hand_lyre_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="5"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/home_run_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/home_run_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="5"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rev_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/rev_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
</LinearLayout>
MAIN LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<LinearLayout
android:id="#+id/team_names"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/name_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/team_one"
style="#style/Headings"
/>
<TextView
android:id="#+id/name_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/team_two"
style="#style/Headings"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/team_points"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/points_one"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:text="0"
style="#style/TeamPoints"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/field_team"/>
<ToggleButton
android:id="#+id/field_team_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="#string/team_one"
android:textOn="#string/team_two"
style="#style/buttons"/>
</LinearLayout>
<TextView
android:id="#+id/points_two"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:text="0"
style="#style/TeamPoints"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/events"
android:paddingTop="16dp"
style="#style/Headings"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
>
<Button
android:id="#+id/burned_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="burned"
android:text="#string/burned"
style="#style/buttons"/>
<Button
android:id="#+id/lyre_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="lyre"
android:text="#string/lyre"
style="#style/buttons"
/>
<Button
android:id="#+id/one_hand_lyre_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneHandLyre"
android:text="#string/oneHandLyre"
style="#style/buttons"
/>
<Button
android:id="#+id/homeRun_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="homeRun"
android:text="#string/homeRun"
style="#style/buttons"
/>
<Button
android:id="#+id/rev_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="rev"
android:text="#string/rev"
style="#style/buttons"
/>
</TableLayout>
<Button
android:id="#+id/reset_button"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginTop="16dp"
android:onClick="reset"
android:text="#string/reset"
style="#style/buttons"
/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/points_edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#android:drawable/ic_menu_edit"
android:layout_margin="16dp" />
</LinearLayout>
I would strongly recommend using SharedPreferences. This way, you can access the data even if you restart your application. If that does not work for you, you can use Intent.
Consider reading this StackOverflow post.
Using SharedPreferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.points), pointsPerActivity);
editor.commit();
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = 1;
// or you can store the value somewhere
// getResources().getInteger(R.integer.defaultPointsPerActivity);
int pointsPerActivity = sharedPref.getInt(pointsPerActivity, defaultValue);
pass view object in applySettings(v);
private int teamOnePoints=0;
private int teamTwoPoints=0;
private boolean teamOneField=true;
private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;
private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = (ToggleButton)
findViewById(R.id.field_team_toggle); //activates toggle button
toggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
//changes between the teams
teamOneField=false;
} else {
teamOneField=true;
}
}
});
FloatingActionButton fab = findViewById(R.id.points_edit_button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.settings, null);
builder.setView(v);
builder.setTitle(getResources().getString(R.string.edit_rules));
builder.setPositiveButton(getResources().getString(R.string.ok_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
applySettings(v);
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_button),
null);
builder.show();
}
private void applySettings(View view){
EditText newBurnedPoints = view.findViewById(R.id.burned_edit);
burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());
EditText newLyrePoints = view.findViewById(R.id.lyre_edit);
lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());
EditText newOneHandLyrePoints = view.findViewById(R.id.one_hand_lyre_edit);
oneHandLyrePoints=Integer.parseInt
(newOneHandLyrePoints.getText().toString());
EditText newHomeRunPoints = view.findViewById(R.id.home_run_edit);
homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());
EditText newRevPoints = view.findViewById(R.id.rev_edit);
revPoints=Integer.parseInt(newRevPoints.getText().toString());
EditText newTeamOneName = view.findViewById(R.id.name_one_edit);
teamOneName= newTeamOneName.getText().toString();
TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
setNewTeamOneName.setText(teamOneName);
EditText newTeamTwoName =view.findViewById(R.id.name_two_edit);
teamTwoName= newTeamTwoName.getText().toString();
TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
setNewTeamTwoName.setText(teamTwoName);
}
I have 2 textboxes with a users name and Id number in my main activity.
When clicking the edit button the user is sent to second activity where they can edit their name and Id number.
Then when they click Save, the edited input should override the written text in the two existing textviews in the first activity.
But when I click the Save button, my app crashes...
Furthermore, main activity has also a capture profile pic. feauture, but when in landscape mode, my captured image. disappear.
I'm totally new to Android and this is a school assignment with due on Sunday... Can someone please help me!!
This is my first activity's xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.solveigdoan.cameraactivity.SecondActivity2"
android:background="#F0FFFF">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="8"
android:id="#+id/EditName"
android:layout_marginTop="42dp"
android:hint="Solveig Mortensen"
android:layout_marginLeft="160dp"
android:background="#87CEEB"
android:textSize="20dp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/EditName"
android:text="#string/edit_name"
android:textSize="20dp"
android:textColor="#000000"
android:id="#+id/NameLabel" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="8"
android:id="#+id/EditID"
android:hint="123456"
android:layout_below="#+id/EditName"
android:layout_alignLeft="#+id/EditName"
android:layout_alignStart="#+id/EditName"
android:layout_marginTop="40dp"
android:background="#87CEEB"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/EditID"
android:text="#string/edit_id"
android:textSize="20dp"
android:textColor="#000000"
android:id="#+id/IdLabel" />
<CheckBox
android:id="#+id/Yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Y"
android:onClick="onCheckboxClicked"
android:layout_marginTop="27dp"
android:layout_below="#+id/AndrStatus"
/>
<CheckBox android:id="#+id/NO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/N"
android:onClick="onCheckboxClicked"
android:layout_marginTop="30dp"
android:layout_below="#+id/Yes"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:id="#+id/buttonCancel"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#000000"
android:background="#87CEEB" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/save_btn"
android:id="#+id/buttonSave"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#000000"
android:background="#87CEEB" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/android_developer"
android:id="#+id/AndrStatus"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/ID"
android:layout_alignStart="#+id/ID"
android:textSize="20dp"
android:textColor="#000000" />
</RelativeLayout>
AND second activity's xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.solveigdoan.cameraactivity.CameraActivity">
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#F0FFFF" />
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/profile_picture"
android:layout_centerHorizontal="true"
android:layout_marginLeft="40dp"
android:textColor="#000000" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="#string/take_photo"
android:layout_below="#+id/textview1"
android:background="#87CEEB"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:layout_below="#+id/button1"
android:background="#BCC6CC" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:ems="9"
android:id="#+id/Name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignLeft="#+id/ID"
android:layout_alignStart="#+id/ID"
android:layout_below="#+id/imageView1"
android:textSize="20dp"
android:background="#87CEEB"
android:textColor="#000000"
android:hint="Solveig Mortensen" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_alignBaseline="#id/Name"
android:text="#string/name"
android:textSize="20dp"
android:textColor="#000000"
android:id="#+id/NameTitle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="9"
android:id="#+id/ID"
android:layout_alignTop="#+id/Name"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
android:textSize="20dp"
android:background="#87CEEB"
android:textColor="#000000"
android:hint="123456" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_alignBaseline="#id/ID"
android:text="#string/id_nr"
android:textSize="20dp"
android:textColor="#000000" />
<Button
android:id ="#+id/push_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="#string/edit_btn"
android:background="#drawable/button_bg_round"
android:padding="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="12dp"
android:textColor="#000000" />
<TextView
android:id="#+id/tvView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="#+id/ID"
android:layout_marginTop="20dp" />
</RelativeLayout>
My main java (named CameraActivity):
public class CameraActivity extends Activity {
Button BtnTakePhoto;
ImageView imgTakenPhoto;
Button push_button;
TextView Name;
TextView ID;
private static final int CAMERA_REQUEST = 1888;
private static final int EDIT_REQUEST = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
push_button = (Button) findViewById(R.id.push_button);
push_button.setOnClickListener(new push_buttonClicker());
imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);
BtnTakePhoto = (Button) findViewById(R.id.button1);
BtnTakePhoto.setOnClickListener(new BtnTakePhotoClicker());
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imgTakenPhoto.setImageBitmap(thumbnail);
}
if (requestCode == EDIT_REQUEST) {
Bundle b = getIntent().getExtras();
Name = (TextView) findViewById(R.id.Name);
ID = (TextView) findViewById(R.id.ID);
Name.setText(b.getCharSequence("Name"));
ID.setText(b.getCharSequence("ID nr"));
}
}
class BtnTakePhotoClicker implements Button.OnClickListener
{
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}class push_buttonClicker implements Button.OnClickListener{
public void onClick(View v){
Intent Intent = new Intent(CameraActivity.this, SecondActivity2.class);
CameraActivity.this.startActivityForResult(Intent, EDIT_REQUEST);
}}}
AND my secondActivity.java:
public class SecondActivity2 extends AppCompatActivity {
TextView tvView;
EditText EditName;
EditText EditID;
Button buttonCancel;
Button buttonSave;
CheckBox checkBox;
TextView Name;
TextView ID;
private static final int RESULT_OK = 10;
//private static final int RESULT_CANCELED = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second2);
// CheckBox Yes = (CheckBox) findViewById(R.id.Yes);
// CheckBox NO = (CheckBox) findViewById(R.id.NO);
buttonSave = (Button) this.findViewById(R.id.buttonSave);
buttonCancel = (Button) findViewById(R.id.buttonCancel);
EditName = (EditText) findViewById(R.id.EditName);
EditID = (EditText) findViewById(R.id.EditID);
buttonSave.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent data = new Intent();
data.putExtra("name", EditName.getText().toString());
data.putExtra("ID", EditID.getText().toString());
setResult(RESULT_OK, data);
finish();
}
});}}
change your code inside onActivityResult under EDIT_REQUEST condition
String name = data.getStringExtra("name");
String ID = data.getStringExtra("ID");
Name.setText(name);
ID.setText(ID);
no need to create a Bundle there, it will solve your problem.
I have created a profile creation page, where user respond to a variety of questions. My issue is that when the application is loaded the layout is not display from the top. In the sense that you have to scroll up to the see the field on top. I think the issue may resolve around the edit text headline (etxheadline), where when the activity load, it seems to be focused into it.
Thanks in advance
Below is the layout code
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollProfile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/dark_texture_blue" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="797dp"
android:gravity="center"
android:orientation="vertical" >
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/profilePicturePreview"
android:layout_width="132dp"
android:layout_height="120dp"
android:layout_below="#+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:alpha="1" />
<Button
android:id="#+id/button2"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_above="#+id/etxtage"
android:layout_alignLeft="#+id/etxtname"
android:alpha="0.8"
android:background="#330099"
android:text="Upload from Facebook"
android:textColor="#ffffff"
android:textSize="17sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnPictureSelect"
android:layout_width="118dp"
android:layout_height="60dp"
android:layout_alignRight="#+id/etxtname"
android:layout_below="#+id/profilePicturePreview"
android:layout_marginRight="8dp"
android:alpha="0.8"
android:background="#000000"
android:onClick="pickPhoto"
android:text="Select photo from gallery"
android:textColor="#ffffff"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp"
android:text="Preferred Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:gravity="center"
android:text="Profile Creation"
android:textColor="#ffffff"
android:textSize="28sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Age"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<EditText
android:id="#+id/etxtage"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_below="#+id/btnPictureSelect"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:ems="10"
android:hint="Please type your age here"
android:inputType="number"
android:maxLength="2"
android:textAlignment="center"
android:textColor="#f2f2f2"
android:textSize="18dp" />
<EditText
android:id="#+id/etxtname"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView6"
android:layout_centerHorizontal="true"
android:ems="10"
android:enabled="true"
android:hint="Please type your name here"
android:inputType="textPersonName"
android:textColor="#ffffff"
android:textSize="18sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etxtname"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="Upload your Profile Picture"
android:textColor="#f2f2f2"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_toLeftOf="#+id/textView3" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textColor="#f2f2f2"
android:text="Male" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f2f2f2"
android:text="Female" />
</RadioGroup>
<SeekBar
android:id="#+id/seekBarDistance"
android:layout_width="250dp"
android:progress="50"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_below="#+id/textView12"
android:layout_marginTop="11dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/etxtheadline"
android:layout_marginTop="38dp"
android:text="I am a"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup
android:id="#+id/radioGroup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignTop="#+id/radioGroup2"
android:layout_marginTop="10dp" >
</RadioGroup>
<RadioGroup
android:id="#+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/etxtheadline"
android:layout_below="#+id/textView2" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male"
android:textColor="#f2f2f2" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:textColor="#f2f2f2" />
</RadioGroup>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignRight="#+id/radioGroup2"
android:text="Looking for"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold" />
<SeekBar
android:id="#+id/seekBarMinimumAge"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView7"
android:layout_marginTop="11dp"
android:progress="25" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etxtage"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Minimum Age Looking For"
android:textColor="#f2f2f2"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="serif" />
<EditText
android:id="#+id/etxtheadline"
android:layout_width="270dp"
android:layout_height="70dp"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/textView8"
android:hint="A quick description of yourself"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#f2f2f2"
android:textSize="18dp" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/seekBarDistanceValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tMinAge"
android:layout_below="#+id/seekBarDistance"
android:text="50"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f2f2f2"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioGroup1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Search Distance (100KM)"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/tMinAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/seekBarMinimumAge"
android:layout_centerHorizontal="true"
android:text="25"
android:textColor="#f2f2f2"
android:textSize="18sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tMaxAge"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Headline"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tMinAge"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Maximum Age Looking For"
android:textColor="#f2f2f2"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="serif" />
<SeekBar
android:id="#+id/seekBarMaximumAge"
android:layout_width="221dp"
android:progress="50"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_below="#+id/textView14"
android:layout_marginTop="11dp" />
<TextView
android:id="#+id/tMaxAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/seekBarMaximumAge"
android:layout_centerHorizontal="true"
android:text="50"
android:textColor="#f2f2f2"
android:textSize="18sp" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView16"
android:layout_centerHorizontal="true"
android:text="I agree to the terms and Conditions"
android:textColor="#D2D2D2"
android:textColorHint="#ffffff" />
<TextView
android:id="#+id/textView16"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_alignLeft="#+id/checkBox1"
android:layout_below="#+id/seekBarDistanceValue"
android:layout_marginTop="14dp"
android:gravity="center"
android:text="Click here to review the terms and conditions"
android:textColor="#99CCFF"
android:textSize="16sp" />
<Button
android:id="#+id/btnReset"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_marginBottom="7dp"
android:layout_below="#+id/checkBox1"
android:layout_toLeftOf="#+id/btnPictureSelect"
android:alpha="0.8"
android:background="#660000"
android:layout_marginTop="14dp"
android:text="Reset"
android:textColor="#ffffff"
android:textSize="17sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnConfirm"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_below="#+id/checkBox1"
android:layout_toRightOf="#+id/seekBarDistanceValue"
android:alpha="0.8"
android:layout_marginTop="14dp"
android:layout_marginBottom="7dp"
android:background="#330099"
android:text="Confirm"
android:textColor="#ffffff"
android:textSize="17sp"
android:textStyle="bold" />
</RelativeLayout>
Update
Activity code
public class ProfileCreation extends Activity {
private static final int RESULT_LOAD_IMAGE = 1;
FrameLayout layout;
Button save;
protected EditText mName;
protected EditText mAge;
protected EditText mHeadline;
protected Button mConfirm;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_creation);
ScrollView s = (ScrollView) findViewById(R.id.scrollProfile);
s.fullScroll(ScrollView.FOCUS_UP);
Parse.initialize(this, "SBNldSjuNL1F7IOj0OWpwenBWRF7rSkjkm8WFQJj", "DLvq1OqSo87kzEw5j1XNBPKJdXF7dxwRtfEPkxWJ");
mName = (EditText)findViewById(R.id.etxtname);
mAge = (EditText)findViewById(R.id.etxtage);
mHeadline = (EditText)findViewById(R.id.etxtheadline);
mConfirm = (Button)findViewById(R.id.btnConfirm);
mConfirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = mName.getText().toString();
String age = mAge.getText().toString();
String headline = mHeadline.getText().toString();
age = age.trim();
name = name.trim();
headline = headline.trim();
if (age.isEmpty() || name.isEmpty() || headline.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
builder.setMessage(R.string.signup_error_message)
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
// create the new user!
setProgressBarIndeterminateVisibility(true);
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put("name", name);
currentUser.put("age", age);
currentUser.put("headline", headline);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Success!
Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
});
save = (Button) findViewById(R.id.button2);
String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
if (!picturePath.equals("")) {
ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
SeekBar seekBarMinimum = (SeekBar) findViewById(R.id.seekBarMinimumAge);
final TextView txtMinimum = (TextView) findViewById(R.id.tMinAge);
seekBarMinimum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
txtMinimum.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
SeekBar seekBarMaximum = (SeekBar) findViewById(R.id.seekBarMaximumAge);
final TextView txtMaximum = (TextView) findViewById(R.id.tMaxAge);
seekBarMaximum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
txtMaximum.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// Locate the image in res >
Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Object image = null;
try {
String path = null;
image = readInFile(path);
} catch (Exception e) {
e.printStackTrace();
}
// Create the ParseFile
ParseFile file = new ParseFile("picturePath", (byte[]) image);
// Upload the image into Parse Cloud
file.saveInBackground();
// Create a New Class called "ImageUpload" in Parse
ParseObject imgupload = new ParseObject("Image");
// Create a column named "ImageName" and set the string
imgupload.put("Image", "picturePath");
// Create a column named "ImageFile" and insert the image
imgupload.put("ImageFile", file);
// Create the class and the columns
imgupload.saveInBackground();
// Show a simple toast message
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
private byte[] readInFile(String path) throws IOException {
// TODO Auto-generated method stub
byte[] data = null;
File file = new File(path);
InputStream input_stream = new BufferedInputStream(new FileInputStream(
file));
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
data = new byte[16384]; // 16K
int bytes_read;
while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, bytes_read);
}
input_stream.close();
return buffer.toByteArray();
}
}
You need to do it progmatically using the ScrollView instance.
You need to add an id in your ScrollView and use that instance somewhere in your activity/fragment class then scroll up the scrollview to the top
sample:
ScrollView s = (ScrollView) findViewById(R.id.yourid);
s.fullScroll(ScrollView.FOCUS_UP);
Do the following ,
ScrollView s = (ScrollView) findViewById(R.id.yourid);
s.scrollTo(0,s.getTop());
I have added a scrollview to my layout, however, when the activity loads, the scrollview is not viewed from the top but rather somewhere in middle, and where a user has to scroll up in order the see the above fields. I would prefer if the scrollview was set to top.
With support, I tried to achieve this programmingly, however, my attempts have been unsuccessful. In particular, I was advise to use the following code, but that hasn't been effective.
ScrollView s = (ScrollView) findViewById(R.id.scrollProfile);
s.scrollTo(0,s.getTop());
Below is the activity code
public class ProfileCreation extends Activity {
private static final int RESULT_LOAD_IMAGE = 1;
FrameLayout layout;
Button save;
protected EditText mName;
protected EditText mAge;
protected EditText mHeadline;
protected Button mConfirm;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_creation);
ScrollView s = (ScrollView) findViewById(R.id.scrollProfile);
s.scrollTo(0,s.getTop());
mName = (EditText)findViewById(R.id.etxtname);
mAge = (EditText)findViewById(R.id.etxtage);
mHeadline = (EditText)findViewById(R.id.etxtheadline);
mConfirm = (Button)findViewById(R.id.btnConfirm);
mConfirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = mName.getText().toString();
String age = mAge.getText().toString();
String headline = mHeadline.getText().toString();
age = age.trim();
name = name.trim();
headline = headline.trim();
if (age.isEmpty() || name.isEmpty() || headline.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
builder.setMessage(R.string.signup_error_message)
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
// create the new user!
setProgressBarIndeterminateVisibility(true);
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put("name", name);
currentUser.put("age", age);
currentUser.put("headline", headline);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Success!
Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
});
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
SeekBar seekBarMinimum = (SeekBar) findViewById(R.id.seekBarMinimumAge);
final TextView txtMinimum = (TextView) findViewById(R.id.tMinAge);
seekBarMinimum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
txtMinimum.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
SeekBar seekBarMaximum = (SeekBar) findViewById(R.id.seekBarMaximumAge);
final TextView txtMaximum = (TextView) findViewById(R.id.tMaxAge);
seekBarMaximum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
txtMaximum.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
private byte[] readInFile(String path) throws IOException {
// TODO Auto-generated method stub
byte[] data = null;
File file = new File(path);
InputStream input_stream = new BufferedInputStream(new FileInputStream(
file));
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
data = new byte[16384]; // 16K
int bytes_read;
while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, bytes_read);
}
input_stream.close();
return buffer.toByteArray();
}
}
Below is the layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollProfile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/dark_texture_blue" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="797dp"
android:gravity="center"
android:orientation="vertical" >
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/profilePicturePreview"
android:layout_width="132dp"
android:layout_height="120dp"
android:layout_below="#+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:alpha="1" />
<Button
android:id="#+id/bRemove"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_above="#+id/etxtage"
android:layout_alignLeft="#+id/etxtname"
android:alpha="0.8"
android:background="#330099"
android:text="Upload from Facebook"
android:textColor="#ffffff"
android:textSize="17sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnPictureSelect"
android:layout_width="118dp"
android:layout_height="60dp"
android:layout_alignRight="#+id/etxtname"
android:layout_below="#+id/profilePicturePreview"
android:layout_marginRight="8dp"
android:alpha="0.8"
android:background="#000000"
android:onClick="pickPhoto"
android:text="Select photo from gallery"
android:textColor="#ffffff"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp"
android:text="Preferred Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:gravity="center"
android:text="Profile Creation"
android:textColor="#ffffff"
android:textSize="28sp"
android:textStyle="bold"
android:typeface="serif" />
<EditText
android:id="#+id/etxtname"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView6"
android:layout_centerHorizontal="true"
android:ems="10"
android:enabled="true"
android:hint="Please type your name here"
android:inputType="textPersonName"
android:textColor="#ffffff"
android:textSize="18sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etxtname"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="Upload your Profile Picture"
android:textColor="#f2f2f2"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_toLeftOf="#+id/textView3" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textColor="#f2f2f2"
android:text="Male" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f2f2f2"
android:text="Female" />
</RadioGroup>
<SeekBar
android:id="#+id/seekBarDistance"
android:layout_width="250dp"
android:progress="50"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_below="#+id/textView12"
android:layout_marginTop="11dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/etxtheadline"
android:layout_marginTop="38dp"
android:text="I am a"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup
android:id="#+id/radioGroup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignTop="#+id/radioGroup2"
android:layout_marginTop="10dp" >
</RadioGroup>
<RadioGroup
android:id="#+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/etxtheadline"
android:layout_below="#+id/textView2" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male"
android:textColor="#f2f2f2" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:textColor="#f2f2f2" />
</RadioGroup>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignRight="#+id/radioGroup2"
android:text="Looking for"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold" />
<SeekBar
android:id="#+id/seekBarMinimumAge"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView7"
android:layout_marginTop="11dp"
android:progress="25" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etxtage"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Minimum Age Looking For"
android:textColor="#f2f2f2"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/seekBarDistanceValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tMinAge"
android:layout_below="#+id/seekBarDistance"
android:text="50"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f2f2f2"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioGroup1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Search Distance (100KM)"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/tMinAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/seekBarMinimumAge"
android:layout_centerHorizontal="true"
android:text="25"
android:textColor="#f2f2f2"
android:textSize="18sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tMaxAge"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Headline"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tMinAge"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Maximum Age Looking For"
android:textColor="#f2f2f2"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="serif" />
<SeekBar
android:id="#+id/seekBarMaximumAge"
android:layout_width="221dp"
android:progress="50"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_below="#+id/textView14"
android:layout_marginTop="11dp" />
<TextView
android:id="#+id/tMaxAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/seekBarMaximumAge"
android:layout_centerHorizontal="true"
android:text="50"
android:textColor="#f2f2f2"
android:textSize="18sp" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView16"
android:layout_centerHorizontal="true"
android:text="I agree to the terms and Conditions"
android:textColor="#D2D2D2"
android:textColorHint="#ffffff" />
<TextView
android:id="#+id/textView16"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_alignLeft="#+id/checkBox1"
android:layout_below="#+id/seekBarDistanceValue"
android:layout_marginTop="14dp"
android:gravity="center"
android:text="Click here to review the terms and conditions"
android:textColor="#99CCFF"
android:textSize="16sp" />
<Button
android:id="#+id/btnReset"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_marginBottom="7dp"
android:layout_below="#+id/checkBox1"
android:layout_toLeftOf="#+id/btnPictureSelect"
android:alpha="0.8"
android:background="#660000"
android:layout_marginTop="14dp"
android:text="Reset"
android:textColor="#ffffff"
android:textSize="17sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnConfirm"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_below="#+id/checkBox1"
android:layout_toRightOf="#+id/seekBarDistanceValue"
android:alpha="0.8"
android:layout_marginTop="14dp"
android:layout_marginBottom="7dp"
android:background="#330099"
android:text="Confirm"
android:textColor="#ffffff"
android:textSize="17sp"
android:textStyle="bold" />
<EditText
android:id="#+id/etxtheadline"
android:layout_width="270dp"
android:layout_height="70dp"
android:layout_below="#+id/textView8"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="A quick description of yourself"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#f2f2f2"
android:textSize="18dp" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/etxtage"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/btnPictureSelect"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Please type your age here"
android:inputType="number"
android:maxLength="2"
android:textAlignment="center"
android:textColor="#f2f2f2"
android:textSize="18dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bRemove"
android:layout_marginTop="17dp"
android:layout_toLeftOf="#+id/btnConfirm"
android:text="Age"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ADD8E6"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
</RelativeLayout>
</ScrollView>
Any help would be greatly appreciated.
Thanks in advance
change it like this, first add
android:id="#+id/main" to RelativeLayout:
so it will be :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="797dp"
android:id="#+id/main"
android:gravity="center"
android:orientation="vertical" >
and in onCreate
RelativeLayout v = (RelativeLayout) findViewById(R.id.main);
v.requestFocus();
I'm trying to make a custom dialog for my android application. I get null pointer exceptions when I attempt to access the fields from the 'ok' button method. It seems like my fields fade from existence as soon as the onclick listener fires. Is there a better way to do this?
addchars.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/scrollBox">
<RelativeLayout
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/addChars">
<AutoCompleteTextView android:layout_height="wrap_content" android:id="#+id/inCharName" android:hint="#string/txtCharacterNameBox" android:layout_below="#+id/textView1" android:layout_centerHorizontal="true" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</AutoCompleteTextView>
<TextView android:id="#+id/textView2" android:layout_height="wrap_content" android:gravity="center" android:layout_width="wrap_content" android:text="#string/txtHP" android:layout_below="#+id/inCharName" android:layout_alignParentLeft="true" android:layout_marginLeft="22dp"></TextView>
<TextView android:id="#+id/textView3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/txtTempHP" android:layout_below="#+id/inCharName" android:layout_alignParentRight="true" android:layout_marginRight="57dp"></TextView>
<EditText android:id="#+id/inHP" android:digits="-0123456789" android:minEms="3" android:gravity="center" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_below="#+id/textView2" android:layout_alignLeft="#+id/textView2">
</EditText>
<EditText android:id="#+id/inTempHP" android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_alignBaseline="#+id/inHP" android:layout_alignBottom="#+id/inHP" android:layout_alignLeft="#+id/textView3"></EditText>
<TextView android:id="#+id/TextView01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inHP" android:layout_alignLeft="#+id/inHP" android:text="#string/txtInitRoll"></TextView>
<TextView android:id="#+id/TextView02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inTempHP" android:layout_alignLeft="#+id/inTempHP" android:text="#string/txtInitBonus"></TextView>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView01" android:layout_alignLeft="#+id/TextView01" android:id="#+id/inInitRoll"></EditText>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView02" android:layout_alignLeft="#+id/TextView02" android:id="#+id/inInitBonus"></EditText>
<TextView android:id="#+id/TextView03" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/txtAC" android:layout_below="#+id/inInitRoll" android:layout_alignRight="#+id/textView2"></TextView>
<EditText android:id="#+id/inAC" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView03" android:layout_alignLeft="#+id/TextView03"></EditText>
<TextView android:id="#+id/TextView04" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="#+id/inAC" android:layout_alignLeft="#+id/inInitBonus" android:text="#string/txtFortitude"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView04" android:layout_alignLeft="#+id/TextView04" android:id="#+id/inFort"></EditText>
<TextView android:id="#+id/TextView06" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inFort" android:layout_alignLeft="#+id/inFort" android:text="#string/txtWill"></TextView>
<TextView android:id="#+id/TextView05" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inAC" android:layout_alignLeft="#+id/inAC" android:text="#string/txtRef"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView05" android:layout_alignLeft="#+id/inInitRoll" android:id="#+id/inReflex"></EditText>
<EditText android:id="#+id/inWill" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_above="#+id/chkRoll" android:layout_alignLeft="#+id/TextView06"></EditText>
<CheckBox android:text="#string/txtRollBox" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/chkRoll" android:layout_below="#+id/inReflex" android:layout_alignLeft="#+id/TextView05" android:layout_alignParentRight="true"></CheckBox>
</RelativeLayout>
</ScrollView>
And this is my alertdialog:
addBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//this block is to prep the layout for the alert dialog.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = inflater.inflate(R.layout.addchars, (ViewGroup) findViewById(R.id.scrollBox));
//taking this part out to try a better way.
new AlertDialog.Builder(initiative.this)
.setTitle("Add a Character")
.setView(addView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//ui controls
AutoCompleteTextView characterName = (AutoCompleteTextView) findViewById(R.id.inCharName);
EditText hp = (EditText) findViewById(R.id.inHP);
EditText tempHp = (EditText) findViewById(R.id.inTempHP);
EditText initRoll = (EditText) findViewById(R.id.inInitRoll);
EditText initBonus = (EditText) findViewById(R.id.inInitBonus);
EditText ac = (EditText) findViewById(R.id.inAC);
EditText fort = (EditText) findViewById(R.id.inFort);
EditText reflex = (EditText) findViewById(R.id.inReflex);
EditText will = (EditText) findViewById(R.id.inWill);
CheckBox rollInit = (CheckBox) findViewById(R.id.chkRoll);
//variables for various checks
entriesOk=false;
needToRoll=false;
if(characterName.getText().length() > 0)
{
holdCharacterName = characterName.getText().toString();
if(hp.getText().length() > 0)
{
holdHp = hp.getText().toString();
if(initBonus.getText().length() > 0)
{
holdInitBonus = initBonus.getText().toString();
if(ac.getText().length() > 0)
{
holdAc = ac.getText().toString();
if(fort.getText().length() > 0)
{
holdFort = fort.getText().toString();
if(reflex.getText().length() > 0)
{
holdReflex = reflex.getText().toString();
if(will.getText().length() > 0)
{
holdWill = will.getText().toString();
if(tempHp.getText().length() == 0)
{
holdTempHp = "0";
tempHp.setText("0");
}
else
{
holdTempHp = tempHp.getText().toString();
}
if(rollInit.isChecked())
{
entriesOk = true;
needToRoll = true;
}
else
{
if(initRoll.getText().length() > 0)
{
holdInitRoll = initRoll.getText().toString();
entriesOk = true;
}
}
}
}
}
}
}
}
}
btnLogic();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//do nothing
}
}).show();
addView.findViewById(R.id.inHP) instead of findViewById(R.id.inHP)
The problem is that you are calling addView.findViewById inside your onClick event, the onclick doesn't really happen until somebody clicks on it, I'll suggest that you make addView class member variable so that it can persist in memory. That'll solve your problem.