I'm developing a Dialog, which should shrink to its content, so, I want to get a behaviour like wrap_content in a common view but for a normal Dialog.
That's what I want to show in a Dialog Window
In a Dialog Window should look like this
but this is what I actually get
Could you say me what I do wrong?
I thank you in Advance.
That's my code
...
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.advanced_options);
dialog.setTitle(titleId);
// Stuff referred to builder
AlertDialog.Builder builder ...
...
int type = WindowManager.LayoutParams.TYPE_INPUT_METHOD;
WindowManager.LayoutParams w_layout_params = new WindowManager.LayoutParams(type);
dialog.getWindow().setAttributes(w_layout_params );
...
builder.create();
dialog.show();
advanced_options.xml Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLayout_advancedOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_advanced_options"
android:divider="?android:listSeparatorTextViewStyle"
android:showDividers="" >
<TextView
android:id="#+id/speed_limit"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="#string/speed_limit"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:textSize="14dp" />
<TextView
android:id="#+id/speed_limit_alert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/speed_limit"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="#string/speed_limit_alert"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<TextView
android:id="#+id/percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/speed_limit_alert"
android:layout_toRightOf="#+id/speed_limit_alert_edit_text"
android:text="#string/percentage_symbol"
android:textColor="#color/black" />
<TextView
android:id="#+id/minimum_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/speed_limit_alert"
android:layout_below="#+id/speed_limit_alert_edit_text"
android:layout_marginTop="15dp"
android:text="#string/minimum_speed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<EditText
android:id="#+id/minimum_speed_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minimum_speed"
android:layout_alignBottom="#+id/minimum_speed"
android:layout_alignLeft="#+id/speed_limit_alert_edit_text"
android:ems="3"
android:inputType="number"
android:textColor="#color/black"
android:text="6" />
<TextView
android:id="#+id/minimum_speed_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/minimum_speed"
android:layout_alignLeft="#+id/percent"
android:text="#string/metric_speed_unit"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<TextView
android:id="#+id/maximum_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/minimum_speed"
android:layout_below="#+id/minimum_speed_edit_text"
android:layout_marginTop="15dp"
android:text="#string/maximum_speed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black"/>
<EditText
android:id="#+id/maximum_speed_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/maximum_speed"
android:layout_alignBottom="#+id/maximum_speed"
android:layout_alignLeft="#+id/minimum_speed_edit_text"
android:ems="3"
android:inputType="number"
android:textColor="#color/black"
android:text="18" />
<TextView
android:id="#+id/maximum_speed_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/maximum_speed"
android:layout_toRightOf="#+id/maximum_speed_edit_text"
android:text="#string/metric_speed_unit"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black"/>
<TextView
android:id="#+id/mobile_device_performance"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/maximum_speed_edit_text"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:text="#string/mobile_device_performance"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/maximum_number_objects"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/maximum_speed"
android:layout_below="#+id/mobile_device_performance"
android:layout_marginTop="20dp"
android:text="#string/maximum_objects_in_view"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<EditText
android:id="#+id/maximum_number_objects_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/maximum_number_objects"
android:layout_alignBottom="#+id/maximum_number_objects"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/maximum_number_objects"
android:ems="3"
android:inputType="number"
android:text="450"
android:textColor="#color/black" />
<EditText
android:id="#+id/speed_limit_alert_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/percent"
android:layout_alignBottom="#+id/percent"
android:layout_alignLeft="#+id/maximum_number_objects_edit_text"
android:ems="3"
android:inputType="number"
android:text="42"
android:textColor="#color/black" >
<requestFocus />
</EditText>
<View
android:id="#+id/void_view"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/maximum_speed_unit"
android:layout_below="#+id/maximum_number_objects" />
Change RelativeView's width to match_parent
What you're looking for is match_parent's behavior.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLayout_advancedOptions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_advanced_options"
android:divider="?android:listSeparatorTextViewStyle"
android:showDividers="" >
Note: wrap_content in android specifies the view to use as little space as needed and that is the behavior you're getting.
I could adjust the Dialog (NOT AlertDialog!) to my custom Layout but I couldn't* set either the Title or the Buttons so that I got an Exception cause of setting content before requestFeature, then I decided to custom my Dialog and including the Buttons and title in my Custom Layout.
*I have researched and it's possible to overcome that if we define the Dialog in OnCreate other even in OnCreateDialog but I have an extra Class just for static Dialog Methods and was no solution for me.
In Addition I post what it was useful for me, perhaps someone is also useful.
Now my custom dialog looks like this
Here the code for whom wants to test it.
/**
* #param c
* the Context
* #return the about dialog
*/
public static void getAdvancedOptions(final Activity activity) {
Log.i("TAG", "Dialogs::getAdvancedOptions:: 0");
//settings = session.getSettings();
// Creation of a Dialog with Frame and title Characteristics
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.advanced_options_expanded);
// Here we control the validity of "edit_text" Fields
speed_limit_alert = (EditText)dialog.findViewById(R.id.speed_limit_alert_edit_text);
minimum_speed = (EditText)dialog.findViewById(R.id.minimum_speed_edit_text);
maximum_speed = (EditText)dialog.findViewById(R.id.maximum_speed_edit_text);
max_objects_in_view = (EditText)dialog.findViewById(R.id.maximum_number_objects_edit_text);
//showAdvancedOptions("getAdvancedOptions 1::");
// First we set the "SharedPreferences"-saved values on EditText Fields
performEditText(speed_limit_alert,Constants.MIN_PERCENTAGE, Constants.MAX_PERCENTAGE,"speed_limit_alert");
performEditText(minimum_speed, "","", "minimum_speed");
performEditText(maximum_speed, "","","maximum_speed");
performEditText(max_objects_in_view, "","","max_objects_in_view");
//showAdvancedOptions("getAdvancedOptions 4::");
ok_button = (Button)dialog.findViewById(R.id.ok_button);
cancel_button = (Button)dialog.findViewById(R.id.cancel_button);
// Definition of "OK" Button for the Dialog
ok_button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Log.i("TAG", "Dialogs::getAdvancedOptions::onClick");
if ( dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
});
cancel_button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Log.i("TAG", "Dialogs::getAdvancedOptions::onClick");
if ( dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
});
dialog.show();
}
private static void performEditText(EditText edit_text, final String min_value, final String max_value, final String id) {
String edit_text_value = edit_text.getText().toString();
Log.i("TAG", "Dialogs::performEditText:: id: "+ id +" edit_text_value: "+edit_text_value+ " (min_value,max_value)=("+min_value+","+max_value+")");
TextWatcher textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
int i = 0;
Log.i("TAG","Dialogs::performEditText::afterTextChanged:id: "+ id +" - 0 : s: "+s.toString());
int length = s.length();
if ( length == 0 )
i = 0;
else if ( length < 3 ) {
if ( s.charAt(0) == '0')
s.delete(1, length );
i = Integer.parseInt(s.toString());
Log.i("TAG","Dialogs::performEditText::afterTextChanged:id: "+ id +" - 1a : s: "+s+", i:"+i);
} else {
//String hundred = "100";
String s_value = s.toString();
Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 1b : s: "+s+", i:"+i);
if ( ( min_value != null ) && ( min_value.length() != 0 ) && ( max_value != null) && ( max_value.length() != 0 )) {
if ( !s_value.equalsIgnoreCase(max_value))
s.delete(2, length );
}
i = Integer.parseInt(s.toString());
Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 2b : i: "+i);
}
if (i >= 0 && i <= 100) {
Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 3 : (i >= 0 && i <= 100): i: "+i);
//speed_limit_alert.setText(s); // This ensures 0-100 value for speed_limit_alert
}
Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 4 : i: "+i);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.i("TAG","Dialogs::performEditText::beforeTextChanged: id: "+ id +" s: "+s);
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.i("TAG","Dialogs::performEditText::onTextChanged: id: "+ id +" s: "+s);
}
};
edit_text.addTextChangedListener(textWatcher);
}
private static void showAdvancedOptions(String entryPoint) {
String s_l_a = speed_limit_alert.getText().toString();
Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: s_l_a: "+s_l_a);
String mi_s = minimum_speed.getText().toString();
Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: mi_s: "+mi_s);
String ma_s = maximum_speed.getText().toString();
Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: ma_s: "+ma_s);
String m_o_i_v = max_objects_in_view.getText().toString();
Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: m_o_i_v: "+m_o_i_v);
}
And the layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_advanced_options"
android:divider="?android:listSeparatorTextViewStyle"
android:showDividers="middle" >
<RelativeLayout
android:id="#+id/relLayout_advancedOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:divider="?android:listSeparatorTextViewStyle"
android:orientation="vertical"
android:showDividers="" >
<TextView
android:id="#+id/speed_limit"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/void_view_0"
android:layout_marginTop="10dp"
android:text="#string/speed_limit"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:textSize="14dp" />
<TextView
android:id="#+id/speed_limit_alert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/speed_limit"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="#string/speed_limit_alert"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<TextView
android:id="#+id/percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/speed_limit_alert"
android:layout_toRightOf="#+id/speed_limit_alert_edit_text"
android:text="#string/percentage_symbol"
android:textColor="#color/black" />
<TextView
android:id="#+id/minimum_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/speed_limit_alert"
android:layout_below="#+id/speed_limit_alert_edit_text"
android:layout_marginTop="15dp"
android:text="#string/minimum_speed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<EditText
android:id="#+id/minimum_speed_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minimum_speed"
android:layout_alignBottom="#+id/minimum_speed"
android:layout_alignLeft="#+id/speed_limit_alert_edit_text"
android:ems="3"
android:inputType="number"
android:text="6"
android:textColor="#color/black" />
<TextView
android:id="#+id/minimum_speed_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/minimum_speed"
android:layout_alignLeft="#+id/percent"
android:paddingRight="5dp"
android:text="#string/metric_speed_unit"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<TextView
android:id="#+id/maximum_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/minimum_speed"
android:layout_below="#+id/minimum_speed_edit_text"
android:layout_marginTop="15dp"
android:text="#string/maximum_speed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<EditText
android:id="#+id/maximum_speed_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/maximum_speed"
android:layout_alignBottom="#+id/maximum_speed"
android:layout_alignLeft="#+id/minimum_speed_edit_text"
android:ems="3"
android:inputType="number"
android:text="18"
android:textColor="#color/black" />
<TextView
android:id="#+id/maximum_speed_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/maximum_speed"
android:layout_alignRight="#+id/minimum_speed_unit"
android:layout_toRightOf="#+id/maximum_speed_edit_text"
android:paddingRight="5dp"
android:text="#string/metric_speed_unit"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<TextView
android:id="#+id/mobile_device_performance"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/maximum_speed_edit_text"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:text="#string/mobile_device_performance"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/maximum_number_objects"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/maximum_speed"
android:layout_below="#+id/mobile_device_performance"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="#string/maximum_objects_in_view"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<EditText
android:id="#+id/maximum_number_objects_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/maximum_number_objects"
android:layout_alignBottom="#+id/maximum_number_objects"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/maximum_number_objects"
android:ems="3"
android:inputType="number"
android:text="450"
android:textColor="#color/black" />
<EditText
android:id="#+id/speed_limit_alert_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/percent"
android:layout_alignBottom="#+id/percent"
android:layout_alignLeft="#+id/maximum_number_objects_edit_text"
android:digits="0123456789"
android:ems="3"
android:inputType="number"
android:text="42"
android:textColor="#color/black" >
<requestFocus />
</EditText>
<View
android:id="#+id/void_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/lin_layout_buttons"
android:layout_below="#+id/maximum_number_objects"
android:background="#color/black" />
<TextView
android:id="#+id/title_advanced_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/minimum_speed_unit"
android:drawableLeft="#drawable/ic_launcher_48"
android:drawableRight="#drawable/ic_action_settings_48"
android:gravity="center|center_vertical"
android:paddingLeft="5dp"
android:text="Advanced Options"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/blue" />
<View
android:id="#+id/void_view_0"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/title_advanced_options"
android:layout_below="#+id/title_advanced_options"
android:background="#color/black" />
<LinearLayout
android:id="#+id/lin_layout_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/minimum_speed_unit"
android:layout_alignTop="#+id/void_view"
android:weightSum="3" >
<Button
android:id="#+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/maximum_number_objects"
android:layout_alignRight="#+id/speed_limit_alert"
android:layout_below="#+id/maximum_number_objects_edit_text"
android:layout_weight="1.5"
android:text="OK" />
<Button
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:layout_weight="1.5"
android:text="Cancel" />
</LinearLayout>
</RelativeLayout>
Related
I am using this library to create Double RangeBar in android. Now in this rangebar i want that if left pin overlaps the right pin it must return to its previous position instead of overlapping and same for right pin. This is same as used in flipkart to filter price ranges.(Flipkart uses Range bar like this). I want to implement same property like that. How can i implement this property. Please help me..
Thanks in advance.
This is my Activity and xml
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.range_bar);
min = (TextView)findViewById(R.id.min);
sign_rupees = (TextView)findViewById(R.id.sign_rupees);
sign_rupees2 = (TextView)findViewById(R.id.sign_rupees2);
selected_min = (TextView)findViewById(R.id.selected_min);
selected_max = (TextView)findViewById(R.id.selected_max);
rangeBar = (RangeBar)findViewById(R.id.rangebar3);
rangeBar.setOnRangeBarChangeListener(this);
}
#Override
public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex, int rightPinIndex, String leftPinValue, String rightPinValue) {
int diff = rightPinIndex-leftPinIndex;
if (diff == 0){
rangeBar.setRangePinsByIndices(leftPinIndex-1,rightPinIndex+1);
rangeBar.setEnabled(true);
}else{
rangeBar.setEnabled(true);
if (leftPinIndex == 0){
min.setText("Min");
min.setVisibility(View.VISIBLE);
sign_rupees.setVisibility(View.INVISIBLE);
selected_min.setVisibility(View.INVISIBLE);
}else{
min.setVisibility(View.GONE);
sign_rupees.setVisibility(View.VISIBLE);
selected_min.setVisibility(View.VISIBLE);
selected_min.setText(String.valueOf(leftPinIndex*500));
}
if (rightPinIndex == 6){
selected_max.setText("2500+");
}else{
selected_max.setText(String.valueOf(rightPinIndex*500));
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rangebar1"
android:layout_marginBottom="10dp">
<com.appyvet.rangebar.RangeBar
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/rangebar3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:tickStart="0"
custom:tickInterval="1"
custom:tickEnd="6"
custom:pinRadius="20dp"
custom:pinMinFont="5sp"
custom:textColor="#android:color/transparent"
custom:selectorSize="5dp"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/left_linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/rangebar1">
<TextView
android:id="#+id/min_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Min Price"
android:textSize="12sp"
android:textColor="#color/grey_dark"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Min"
android:textColor="#000"
android:layout_marginLeft="10dp"
android:layout_below="#+id/min_price"
android:visibility="visible"
android:textSize="20sp"/>
<TextView
android:id="#+id/sign_rupees"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rupees_symbol"
android:textColor="#000"
android:layout_marginLeft="10dp"
android:layout_below="#+id/min_price"
android:visibility="invisible"
android:textSize="20sp"/>
<TextView
android:id="#+id/selected_min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="250"
android:textColor="#000"
android:layout_marginLeft="5dp"
android:layout_below="#+id/min_price"
android:layout_toRightOf="#+id/sign_rupees"
android:visibility="invisible"
android:textSize="20sp"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/right_linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_below="#+id/rangebar1">
<TextView
android:id="#+id/max_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max Price"
android:textColor="#color/grey_dark"
android:textSize="12sp"
android:layout_alignRight="#+id/rl"/>
<RelativeLayout
android:id="#+id/rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/max_price"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/sign_rupees2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rupees_symbol"
android:textColor="#000"
android:textSize="20sp"/>
<TextView
android:id="#+id/selected_max"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2500+"
android:textColor="#000"
android:textSize="20sp"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
</RelativeLayout>
I haven't used this library , but looking at their code what you can try is :
#Override
public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex, int rightPinIndex, String leftPinValue, String rightPinValue) {
int diff = rightPinIndex-leftPinIndex;
if (diff == 0){
rangeBar.setRangePinsByIndices(leftPinIndex-1,rightPinIndex+1);
rangeBar.setEnabled(true);
}else{
rangeBar.setEnabled(true);
if (leftPinIndex == 0){
min.setText("Min");
min.setVisibility(View.VISIBLE);
sign_rupees.setVisibility(View.INVISIBLE);
selected_min.setVisibility(View.INVISIBLE);
}else{
min.setVisibility(View.GONE);
sign_rupees.setVisibility(View.VISIBLE);
selected_min.setVisibility(View.VISIBLE);
selected_min.setText(String.valueOf(leftPinIndex*500));
}
if (rightPinIndex == 6){
selected_max.setText("2500+");
}else{
selected_max.setText(String.valueOf(rightPinIndex*500));
}
}
if(rightPinIndex <= leftPinIndex) {
rangebar.setRangePinsByIndices(leftIntIndex-5, rightIntIndex+5); // This +-5 value can be anything
}
}
I am having a fragment and in that fragment there is an add button which triggers a dialog when clicked . In that dialog I am having two edittext and two buttons. On click of save button dialog is dismissed. Now problem is that when I click on save button edit text keyboard closes and another keyboard pop up.(Edit text keyboard type is number while that popup after is alphabetical).I want to close this keyboard therefore I tried configChanges and this method
public static void hideKeyboard(Context ctx) {
InputMethodManager inputManager = (InputMethodManager) ctx
.getSystemService(Context.INPUT_METHOD_SERVICE);
// check if no view has focus:
View v = ((Activity) ctx).getCurrentFocus();
if (v == null)
return;
inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
But none of this method works and I am also not having any edit text in fragment from which dialog appears.
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lin_add_dns">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="ADD DNS"
android:gravity="center"
android:background="#drawable/add_dns_title"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="20sp"
android:id="#+id/txt_add_dns"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="DNS NAME"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:layout_marginRight="20dp"
android:id="#+id/et_name"
android:layout_gravity="center"
android:textColorHint="#999999"
android:textColor="#000"
android:focusableInTouchMode="true"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:hint="IP Address"
android:inputType="number"
android:id="#+id/et_ip"
android:layout_gravity="center"
android:digits="0123456789."
android:textColorHint="#999999"
android:textColor="#000"
android:maxLength="15"
android:focusableInTouchMode="true"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
android:text="Submit"
android:background="#drawable/textview_click"
android:textColor="#ffffff"
android:id="#+id/btn_submit"/>
</LinearLayout>
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#ccc"
android:focusableInTouchMode="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:id="#+id/TopHeader"
android:text="Current WIFI Info"
android:paddingRight="15dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_weight=".50"
android:paddingRight="5dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity"
android:id="#+id/WiFiConnectLeft"
android:text="Wifi Name :"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity"
android:id="#+id/DefaultGatewayLeft"
android:text="Default Gateway :"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity"
android:id="#+id/DNS1Left"
android:text="Primary DNS :"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity"
android:id="#+id/DNS2Left"
android:text="Secondary DNS :"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity"
android:id="#+id/CurrentIPLeft"
android:text="IP Address :"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight=".50"
android:paddingLeft="5dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:id="#+id/WiFiConnectedText"
android:textAlignment="gravity"
android:text=""/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DefaultGatewayText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DNS1Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DNS2Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/CurrentIPText"/>
</LinearLayout>
</LinearLayout>
-<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHANGE DNS"
android:layout_gravity="center"
android:textAlignment="gravity"
android:id="#+id/MiddleHeaderText"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:textColor="#000"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/tv_title"
android:orientation="horizontal"
android:weightSum="3"
android:layout_marginTop="10dp"
android:id="#+id/lin_primary"
android:gravity="center_vertical"
android:paddingRight="15dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/primary"
android:textStyle="bold"
android:textColor="#000"
android:id="#+id/PrimaryDNSText"
android:gravity="center"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:textSize="13sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1.2"
android:background="#000"
android:textColor="#ffffff"
android:inputType="numberDecimal"
android:layout_marginRight="10dp"
android:maxLength="16"
android:id="#+id/EDITDNS1"
android:gravity="center"
android:text=""/>
<Button
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.8"
android:text="BROWSE"
android:gravity="center"
android:background="#drawable/textview_click"
android:textColor="#ffffff"
android:id="#+id/BrowseDNS1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/lin_primary"
android:orientation="horizontal"
android:weightSum="3"
android:layout_marginTop="10dp"
android:id="#+id/lin_secondary"
android:gravity="center_vertical"
android:paddingRight="15dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/secondary"
android:textColor="#000"
android:id="#+id/SecondaryDNSText"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:textSize="12sp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1.2"
android:background="#000"
android:textColor="#ffffff"
android:inputType="numberDecimal"
android:layout_marginRight="10dp"
android:maxLength="16"
android:id="#+id/EDITDNS2"
android:gravity="center"
android:text=""
/>
<Button
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.8"
android:text="BROWSE"
android:gravity="center"
android:background="#drawable/textview_click"
android:textColor="#ffffff"
android:id="#+id/BrowseDNS2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="3">
<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:text="ADD DNS"
android:id="#+id/btn_add"
android:textColor="#ffffff"
android:background="#drawable/textview_click"
android:gravity="center"
android:layout_marginLeft="15dp"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:id="#+id/UpdateDNS"
android:text="Update"
android:background="#drawable/textview_click"
android:textColor="#ffffff"
android:layout_marginLeft="20dp"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:id="#+id/ResetWifi"
android:text="RESET"
android:background="#drawable/textview_click"
android:textColor="#ffffff"
android:layout_marginLeft="20dp"
android:layout_marginRight="15dp"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Dialog java code
addDns.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
android.support.v7.app.AlertDialog.Builder builder;
final android.support.v7.app.AlertDialog alertDialog;
TextView txt_add_dns;
Button btn_submit;
final EditText et_name, et_dns;
LayoutInflater inflater = getActivity().getLayoutInflater();
final View layout = inflater.inflate(R.layout.add_dns_dialog, (ViewGroup) getActivity().findViewById(R.id.lin_add_dns));
builder = new android.support.v7.app.AlertDialog.Builder(ctx);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg);
txt_add_dns = (TextView) layout.findViewById(R.id.txt_add_dns);
et_name = (EditText) layout.findViewById(R.id.et_name);
et_dns = (EditText) layout.findViewById(R.id.et_ip);
Fonts.setHelveticaFont(ctx, txt_add_dns);
btn_submit = (Button) layout.findViewById(R.id.btn_submit);
Fonts.setHelveticaFont(ctx, btn_submit);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
if (!resultingTxt.matches("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
return "";
} else {
String[] splits = resultingTxt.split("\\.");
for (int i = 0; i < splits.length; i++) {
if (Integer.valueOf(splits[i]) > 255) {
return "";
}
}
}
}
return null;
}
};
et_dns.setFilters(filters);
btn_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fonts.setHelveticaFont(ctx, et_name);
Fonts.setHelveticaFont(ctx, et_dns);
et_dns.setRawInputType(InputType.TYPE_CLASS_NUMBER);
if (!et_name.getText().toString().equalsIgnoreCase("") && !et_dns.getText().toString().equalsIgnoreCase("")) {
String name = et_name.getText().toString();
String dns = et_dns.getText().toString();
IPAddressValidator iptester = new IPAddressValidator();
boolean valid = iptester.validate(dns);
if (valid) {
dataBaseHelper.addUserDns(name, dns);
// dnsListAdapter.notifyDataSetChanged();
alertDialog.dismiss();
hideKeyboard(getActivity());
} else {
Toast.makeText(ctx, "Invalid IP Address", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(ctx, "Please enter IP Address and DNS Name", Toast.LENGTH_LONG).show();
}
}
});
}
});
What's the issue here??
**EDIT:**After checking for each edit text individually I found that the keyboard from first editext didn't closes itself.So I added focus change listener and when it loses focus I called hide Keyboard.
I debugged line by line and everything works perfectly, debugger also enters hide keyboard method but that code is not able to hide keyboard. Is there any other method for hiding keyboard in fragments??
Is this problem with context that is passed in hide keyboard method ??Can we get context of an alert dialog in android because when I debug it showing context of Main Activity as I am passing getActivity() as parameter .Is this the issue??
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
create this methos infragment and call this method where u want to hide keyboard in your fragment.
Hiding Keyboard using method doesn't work for me. I added the code directly in submit button and that worked for me. Here is what I added
InputMethodManager im = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(et_name.getWindowToken(), 0);
InputMethodManager im1 = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
im1.hideSoftInputFromWindow(et_dns.getWindowToken(), 0);
Here et_dns and et_name are my edit text
Hi Iam inflating a layout into another one. But it is overwriting the existing layout. Please do let me know to inflate a layout into another layout if anybody knows. When the plus button is pressed it should inflate a layout.
My 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:orientation="vertical" >
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="14dp"
android:text="Daily Call Report"
android:textColor="#008000"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="253dp"
android:text="Date"
android:textColor="#008000"
android:textSize="35dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/dayworkcategory"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="10dp"
android:text="Day Work Plan:"
android:textSize="15dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="101dp"
android:layout_toRightOf="#+id/spinner1" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/spinner2"
android:layout_alignLeft="#+id/spinner2"
android:text="Category:"
android:textSize="15dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/placeworkstation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/dayworkcategory"
android:layout_marginTop="12dp"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/textView5"
android:ems="10"
android:hint="STATION NAME" >
</AutoCompleteTextView>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_marginLeft="27dp"
android:layout_toRightOf="#+id/textView4"
android:text="Actual Working :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="16dp"
android:text="Place of work(as per TP) :"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/placeworkstation"
android:layout_marginLeft="28dp"
android:text="Stockists Visited :"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/stocklist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView6"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Stocklist Name" >
</AutoCompleteTextView>
<Spinner
android:id="#+id/spinner3"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignBottom="#+id/autoCompleteTextView2"
android:layout_marginLeft="33dp"
android:layout_toRightOf="#+id/autoCompleteTextView2" />
<EditText
android:id="#+id/editText1"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/autoCompleteTextView2"
android:layout_alignBottom="#+id/autoCompleteTextView2"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/spinner3"
android:ems="10"
android:hint="Qty" />
<EditText
android:id="#+id/editText2"
android:layout_width="145dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/autoCompleteTextView2"
android:layout_marginLeft="31dp"
android:layout_toRightOf="#+id/editText1"
android:ems="10"
android:hint="Remarks" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_marginLeft="13dp"
android:layout_toRightOf="#+id/editText2"
android:text="+" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/chemistlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView7"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView3"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Chemist Name" />
<EditText
android:id="#+id/editText4"
android:layout_width="145dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button2"
android:layout_marginRight="63dp"
android:ems="10"
android:hint="Remarks" >
</EditText>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignBottom="#+id/editText4"
android:layout_marginLeft="472dp"
android:layout_toRightOf="#+id/autoCompleteTextView3"
android:text="+" />
<EditText
android:id="#+id/editText3"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/editText4"
android:ems="10"
android:hint="Qty" />
<Spinner
android:id="#+id/spinner4"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/button2"
android:layout_marginRight="19dp"
android:layout_toLeftOf="#+id/editText3" />
</RelativeLayout>
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/chemistlist"
android:layout_marginTop="12dp"
android:text="Travel Expenses Detail :"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/travelexpenses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView8"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView4"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="21dp"
android:ems="10"
android:hint="FROM" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="43dp"
android:layout_alignBaseline="#+id/editText6"
android:layout_alignBottom="#+id/editText6"
android:layout_alignLeft="#+id/editText6"
android:layout_marginLeft="125dp"
android:text="+" />
<Spinner
android:id="#+id/spinner5"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignBottom="#+id/autoCompleteTextView5"
android:layout_marginLeft="13dp"
android:layout_toRightOf="#+id/autoCompleteTextView5" />
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView5"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/autoCompleteTextView4"
android:layout_alignBottom="#+id/autoCompleteTextView4"
android:layout_marginLeft="37dp"
android:layout_toRightOf="#+id/autoCompleteTextView4"
android:ems="10"
android:hint="TO" >
</AutoCompleteTextView>
<EditText
android:id="#+id/editText6"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText5"
android:layout_alignBottom="#+id/editText5"
android:layout_marginLeft="42dp"
android:layout_toRightOf="#+id/editText5"
android:ems="10"
android:hint="Km" />
<EditText
android:id="#+id/editText5"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/spinner5"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/spinner5"
android:ems="10"
android:hint="AMOUNT" >
</EditText>
</RelativeLayout>
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_below="#+id/travelexpenses"
android:layout_marginTop="14dp"
android:text="Miscelleneous Expenses Detail :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/chemistlist"
android:layout_centerVertical="true"
android:text="Chemists Visited :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView9"
android:layout_alignBottom="#+id/textView9"
android:layout_alignRight="#+id/travelexpenses"
android:layout_marginRight="34dp"
android:text="Daily Allowance Expenses Detail :"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/miscelleneous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView9"
android:orientation="vertical" >
<EditText
android:id="#+id/editText7"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="21dp"
android:ems="10"
android:hint="Amount" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/editText7"
android:layout_marginLeft="276dp"
android:layout_toRightOf="#+id/editText7"
android:text="+" />
<EditText
android:id="#+id/editText8"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignTop="#+id/button4"
android:layout_marginLeft="40dp"
android:layout_toRightOf="#+id/editText7"
android:ems="10"
android:hint="Remarks" />
</RelativeLayout>
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignBottom="#+id/miscelleneous"
android:layout_alignLeft="#+id/textView10"
android:layout_marginBottom="10dp"
android:layout_marginLeft="35dp"
android:text="Total(INR):"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignBaseline="#+id/textView11"
android:layout_alignBottom="#+id/textView11"
android:layout_marginLeft="23dp"
android:layout_toRightOf="#+id/textView11"
android:text="TOTAL"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/dayworkcategory"
android:layout_marginRight="33dp"
android:text="UPGRADE DRAFT" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/placeworkstation"
android:text="SUBMIT LATER" />
</RelativeLayout>
My code for inflating the layout:
package abts.medismo.e_detailing;
import abts.medismo.e_detailing.Model.Spinmodel;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
public class DCR extends Activity {
Spinner spinworkplan, spincategory, spinstockproduct, spinchemistproduct,
spintravelmode;
Spinner autostation, autostock, autochemist;
String dbcatid, dbcatname, strcatname, spcatid, spcatname, strcatid;
RelativeLayout stocklist;
int i = 1;
int count = 0;
View rowView;
private View mExclusiveEmptyView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dcrdummo);
spinworkplan = (Spinner) findViewById(R.id.spinner1);
spincategory = (Spinner) findViewById(R.id.spinner2);
spinstockproduct = (Spinner) findViewById(R.id.spinner3);
spinchemistproduct = (Spinner) findViewById(R.id.spinner4);
spintravelmode = (Spinner) findViewById(R.id.spinner5);
stocklist = (RelativeLayout) findViewById(R.id.stocklist);
DatabaseHandler dbh = new DatabaseHandler(DCR.this);
SQLiteDatabase db = dbh.getWritableDatabase();
final ArrayAdapter<Spinmodel> adapterWorkPlan = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
adapterWorkPlan
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterWorkPlan.add(new Spinmodel("Doctor Visit", "Doctor Visit"));
adapterWorkPlan.add(new Spinmodel("Transit", "Transit"));
adapterWorkPlan.add(new Spinmodel("Meeting", "Meeting"));
adapterWorkPlan.add(new Spinmodel("Strike", "Strike"));
adapterWorkPlan.add(new Spinmodel("Head Quarters", "Head Quarters"));
adapterWorkPlan.add(new Spinmodel("Sales Closing", "Sales Closing"));
adapterWorkPlan.add(new Spinmodel("Others", "Others"));
spinworkplan.setAdapter(adapterWorkPlan);
final ArrayAdapter<Spinmodel> adapterCategory = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
String strquery1 = "SELECT * FROM category";
Cursor cursor = db.rawQuery(strquery1, null);
if (cursor.getCount() != 0) {
cursor.moveToFirst();
do {
dbcatid += "," + cursor.getString(0);
dbcatname += "," + cursor.getString(1);
} while (cursor.moveToNext());
}
System.out.println("dbCatid-->>" + dbcatid);
System.out.println("dbCatName-->>" + dbcatname);
if (dbcatname != null) {
int pos = dbcatname.indexOf(",");
int pos1 = dbcatid.indexOf(",");
if (pos == pos1) {
strcatid = dbcatid.substring(pos1 + 1);
strcatname = dbcatname.substring(pos + 1);
}
String delimiter = "\\,";
String[] catid = strcatid.split(delimiter);
String[] catname = strcatname.split(delimiter);
for (int i = 0; i < catid.length; i++) {
spcatid = catid[i];
spcatname = catname[i];
adapterCategory.add(new Spinmodel(spcatname, spcatid));
}
spincategory.setAdapter(adapterCategory);
}
dbcatid = null;
dbcatname = null;
final ArrayAdapter<Spinmodel> adapterStockProduct = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
String strproductquery = "SELECT * FROM product";
Cursor productcursor = db.rawQuery(strproductquery, null);
if (productcursor.getCount() != 0) {
productcursor.moveToFirst();
do {
dbcatid += "," + productcursor.getString(0);
dbcatname += "," + productcursor.getString(1);
} while (productcursor.moveToNext());
}
if (dbcatname != null) {
int pos = dbcatname.indexOf(",");
int pos1 = dbcatid.indexOf(",");
if (pos == pos1) {
strcatid = dbcatid.substring(pos1 + 1);
strcatname = dbcatname.substring(pos + 1);
}
String delimiter = "\\,";
String[] catid = strcatid.split(delimiter);
String[] catname = strcatname.split(delimiter);
for (int i = 0; i < catid.length; i++) {
spcatid = catid[i];
spcatname = catname[i];
adapterStockProduct.add(new Spinmodel(spcatname, spcatid));
}
spinstockproduct.setAdapter(adapterStockProduct);
spinchemistproduct.setAdapter(adapterStockProduct);
}
final ArrayAdapter<Spinmodel> adaptertravelmode = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
adaptertravelmode
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adaptertravelmode.add(new Spinmodel("TwoWheeler", "TwoWheeler"));
adaptertravelmode.add(new Spinmodel("Bus", "Bus"));
adaptertravelmode.add(new Spinmodel("Car", "Car"));
adaptertravelmode.add(new Spinmodel("Train", "Train"));
adaptertravelmode.add(new Spinmodel("Airlines", "Airlines"));
spintravelmode.setAdapter(adaptertravelmode);
}
public void onAddNewClicked(View v) {
count++;
System.out.println("value of i: " + i);
inflateEditRow(count);
v.setVisibility(View.VISIBLE);
System.out.println("value of count " + count);
}
private void inflateEditRow(int id) {
i = id - 1;
for (int j = i; j < id; j++) {
System.out.println("value of j " + j);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.dummaa, null);
mExclusiveEmptyView = rowView;
rowView.setId(id);
stocklist.addView(rowView);
// stocklist.addView(rowView, stocklist.getChildCount());
}
}
public void onDeleteClicked(View v) {
stocklist.removeView((View) v.getParent());
}
}
I got a bit lost in your code, but if I understood right you may need to have the main view as a parameter in rowView = inflater.inflate(R.layout.dummaa, null); instead of passing null.
Here is my Java file for my app that I am working on. The in logcat it is saying there is an error in the initvar() method in this line "length1et = (EditText) findViewById(R.id.etlength1);" I know this is because there is not a variable in the edittext box and I have had this problem before and just put 0's there to start out. Is there a simple command to have nothing there and it will just understand to automatically put in a 0 for the value or is the only way to write some code to treat it as a zero? Below the java code is the xml if that is necessary. In a previous app I have just had it read the edittext box and if nothing is there, set the value to 0 before it does anything else. For the record I know I don't have a button to call the calculate() method I just haven't put that in yet.
package com.example.constructioncalculator;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class Paint extends Activity {
int numwin, numdoor;
double areadoor, areawin, lengthft, lengthin, widthft, widthin, heightft,
heightin, areawall, areaceil, paintcon, paintneeded;
EditText length1et, length2et, width1et, width2et, height1et, height2et,
paintconet, paintneededet;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.paintdisp);
initvar();
calculate();
output();
}
private void initvar() {
// TODO Auto-generated method stub
numwin = 0;
numdoor = 0;
areawin = 20;
areadoor = 19.5;
lengthft = 0;
lengthin = 0;
widthft = 0;
widthin = 0;
heightft = 0;
heightin = 0;
areawall = 0;
areaceil = 0;
paintcon = 0;
paintneeded = 0;
length1et = (EditText) findViewById(R.id.etlength1);
length2et = (EditText) findViewById(R.id.etlength2);
width1et = (EditText) findViewById(R.id.etwidth1);
width2et = (EditText) findViewById(R.id.etwidth2);
height1et = (EditText) findViewById(R.id.etheight1);
height2et = (EditText) findViewById(R.id.etheight2);
paintconet = (EditText) findViewById(R.id.etpaintcon);
paintneededet = (EditText) findViewById(R.id.etpaintneeded);
}
private void calculate() {
// TODO Auto-generated method stub
lengthft = Double.parseDouble(length1et.getText().toString());
lengthin = Double.parseDouble(length2et.getText().toString());
widthft = Double.parseDouble(width1et.getText().toString());
widthin = Double.parseDouble(width2et.getText().toString());
heightft = Double.parseDouble(height1et.getText().toString());
heightin = Double.parseDouble(height2et.getText().toString());
paintcon = Double.parseDouble(paintconet.getText().toString());
areaceil = (lengthft + lengthin / 12) * (widthft + widthin / 12);
areawall = areaceil * (heightft + heightin / 12) - (numwin * areawin)
- (numdoor * areadoor);
paintneeded = (areawall / paintcon) * 1.1;
}
private void output() {
// TODO Auto-generated method stub
paintneededet.setText(String.valueOf(paintneeded));
}
}
*********Here is my xml file...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Paint Coverage Estimator"
android:textSize="30dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="#string/paintinst" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvlength"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Length"
android:textSize="20dp" />
<TextView
android:id="#+id/tvwidth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Width"
android:textSize="20dp" />
<TextView
android:id="#+id/tvheight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Height"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/etlength1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="feet"
android:text="1"
android:textSize="10dp" />
<EditText
android:id="#+id/etlength2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="inches"
android:text="1"
android:textSize="10dp" />
<EditText
android:id="#+id/etwidth1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="feet"
android:text="1"
android:textSize="10dp" />
<EditText
android:id="#+id/etwidth2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="inches"
android:text="1"
android:textSize="10dp" />
<EditText
android:id="#+id/etheight1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="feet"
android:text="1"
android:textSize="10dp" />
<EditText
android:id="#+id/etheight2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:hint="inches"
android:text="1"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="approx paint coverage (ft^2)?" />
<EditText
android:id="#+id/etpaintcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="300" >
<requestFocus />
</EditText>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Paint needed of your buckets" />
<EditText
android:id="#+id/etpaintneeded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Try this:
lengthft = Double.parseDouble(length1et.getText().toString()+0);
But be aware, if the user type in a invalid number and click on the button you haven't implemented yet, your app will crash. So you should probably add a check to all the EditText's before you call calculate().
Or even better, add a function to do it for you:
lengthft = getDouble(length1et);
public double getDouble(EditText et) {
if (!et.getText().equals(null)) {
return Double.parseDouble(et.getText().toString()); //convert your string into integer
} else {
return 0; // or what you want to return if string is Null
}
}
I have an android activity that when I launch sometimes (about 1 in 4 times) it only draws quarter or half the screen before showing it. When I change the orientation or press a button the screen draws properly.
I'm just using TextViews, Buttons, Fonts - no drawing or anything like that.
All of my code for initialising is in the onCreate(). In this method I'm loading a text file, of about 40 lines long, and also getting a shared preference. Could this cause a delay so that it can't draw the intent?
Thanks in advance if anyone has seen anything similar.
EDIT - I tried commenting out the loading of the word list, but it didn't fix the problem.
EDIT 2 - I didn't manage to resolve the problem, but managed to improve it by adding a timer right at the end of the onCreate. I set a low refresh rate and in the tick changed the text of one of the controls. This then seemed to redraw the screen and get rid of the black portions of the screen.
Here is the activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blue_abstract_background" >
<RelativeLayout
android:id="#+id/relativeScore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/ScoreLabel"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="10dip"
android:text="SCORE:"
android:textSize="18dp" />
/>
<TextView
android:id="#+id/ScoreText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ScoreLabel"
android:padding="5dip"
android:text="0"
android:textSize="18dp" />
<TextView
android:id="#+id/GameTimerText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:minWidth="25dp"
android:text="0"
android:textSize="18dp" />
<TextView
android:id="#+id/GameTimerLabel"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/GameTimerText"
android:padding="10dip"
android:text="TIMER:"
android:textSize="18dp" />
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeHighScore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/relativeScore" >
<TextView
android:id="#+id/HighScoreLabel"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dip"
android:text="HIGH SCORE:"
android:textSize="16dp" />
<TextView
android:id="#+id/HighScoreText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/HighScoreLabel"
android:padding="10dip"
android:text="0"
android:textSize="16dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeHighScore"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearMissing"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_gravity="center"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/yellow_text" />
<TextView
android:id="#+id/MissingWordText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:text="MSSNG WRD"
android:textSize="22dp"
android:typeface="normal" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/yellow_text" />
<TableLayout
android:id="#+id/buttonsTableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="5dp" >
<Button
android:id="#+id/aVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/a"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
<Button
android:id="#+id/eVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/e"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="#+id/iVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:background="#drawable/blue_vowel_buttoni"
android:text="#string/i"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
<Button
android:id="#+id/oVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/o"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
<Button
android:id="#+id/uVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/u"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/TimeToStartText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="24dp" />
<Button
android:id="#+id/startButton"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="#drawable/blue_button_menu"
android:gravity="center"
android:padding="10dip"
android:text="START!"
android:textSize="28dp" />
</LinearLayout>
</RelativeLayout>
And the OnCreate() and a few methods:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
isNewWord = true;
m_gameScore = 0;
m_category = getCategoryFromExtras();
// loadWordList(m_category);
initialiseTextViewField();
loadHighScoreAndDifficulty();
setFonts();
setButtons();
setStartButton();
enableDisableButtons(false);
}
private void loadHighScoreAndDifficulty() {
//setting preferences
SharedPreferences prefs = this.getSharedPreferences(m_category, Context.MODE_PRIVATE);
int score = prefs.getInt(m_category, 0); //0 is the default value
m_highScore = score;
m_highScoreText.setText(String.valueOf(m_highScore));
prefs = this.getSharedPreferences(DIFFICULTY, Context.MODE_PRIVATE);
m_difficulty = prefs.getString(DIFFICULTY, NRML_DIFFICULTY);
}
private void initialiseTextViewField() {
m_startButton = (Button) findViewById(R.id.startButton);
m_missingWordText = (TextView) findViewById(R.id.MissingWordText);
m_gameScoreText = (TextView) findViewById(R.id.ScoreText);
m_gameScoreLabel = (TextView) findViewById(R.id.ScoreLabel);
m_gameTimerText = (TextView) findViewById(R.id.GameTimerText);
m_gameTimerLabel = (TextView) findViewById(R.id.GameTimerLabel);
m_timeToStartText = (TextView) findViewById(R.id.TimeToStartText);
m_highScoreLabel = (TextView) findViewById(R.id.HighScoreLabel);
m_highScoreText = (TextView) findViewById(R.id.HighScoreText);
}
private String getCategoryFromExtras() {
String category = "";
Bundle extras = getIntent().getExtras();
if (extras != null) {
category = extras.getString("category");
}
return category;
}
private void enableDisableButtons(boolean enable) {
Button button = (Button) findViewById(R.id.aVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.eVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.iVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.oVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.uVowelButton);
button.setEnabled(enable);
}
private void setStartButton() {
m_startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
m_gameScore = 0;
updateScore();
m_startButton.setVisibility(View.GONE);
m_timeToStartText.setVisibility(View.VISIBLE);
startPreTimer();
}
});
}