I want to make a new activity, like to confirm what radio-button is checked.
I have 2 radio-groups
Each group has 10 radio-buttons at least
I want to take whatever radio-button is checked value to the next activity
and for sure just one radio-button can be selected from every radio-group
Here is my xml code and I want the java code
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="391dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Where Are You" />
<RadioGroup
android:id="#+id/radioGroup0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="it" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eng" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="there" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="it" />
<RadioButton
android:id="#+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eng" />
<RadioButton
android:id="#+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="there" />
<RadioButton
android:id="#+id/radio6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="it" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Where Do You Want To Go" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" >
<RadioButton
android:id="#+id/radio12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="it" />
<RadioButton
android:id="#+id/radio13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eng" />
<RadioButton
android:id="#+id/radio14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="there" />
<RadioButton
android:id="#+id/radio15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="it" />
<RadioButton
android:id="#+id/radio16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eng" />
<RadioButton
android:id="#+id/radio17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="there" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
</ScrollView>
I have deleted some of the radio-buttons so it doesn't get long
Please note I am a beginner.
First, bind the RadioGroup:
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
Then get the position of the button that was checked:
int checked = rg.getCheckedRadioButtonId();
Then create the intent of your next activity:
Intent intent = new Intent(this,nextActivity.class);
Then put the information you want to pass on the intent:
intent.putExtra("checked",checked);
Then start the next activity:
startActivity(intent);
On the next activity you recover that info like this:
Intent intent = getIntent();
int checked = intent.getIntExtra("checked");
try that;
public void onCreate(Bundle savedInstanceState) {
enter code here
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
String selectedRadioValue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId() )).getText().toString();
RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
String selectedRadioValue2 =((RadioButton)findViewById(rg.getCheckedRadioButtonId() )).getText().toString();
Button btn = (Button) findViewById(R.id.buttonName);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//enter code here for your control and
Intent intent = new Intent(getApplicationContext(), your.class);
intent.putExtra("radioGroup1Selected", selectedRadioValue);
intent.putExtra("radioGroup2Selected", selectedRadioValue2);
startActivity(intent);
}
});
}
get another activity :
Intent intent = getIntent();
String selectedRadioValue = intent.getStringExtra("selectedRadioValue");
String selectedRadioValue2 = intent.getStringExtra("selectedRadioValue2");
Related
I'm an Android beginner and I have a little problem.
I have 2 radioButtons titled "Yes" and "No", 4 editTexts which are disabled and a button titled "Reset All".
Now, when I select the "No" radioButton, editTexts 1 and 2 become enabled and 1 gains focus. This is the desired behaviour. But When I select "Reset All" and again repeat the procedure, only editText 2 gets enabled and the "No" radioButton is still unchecked.
Following is my code:
MainActivity.java
package com.example.chris.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
EditText e1;
EditText e2;
EditText e3;
EditText e4;
RadioButton yes;
RadioButton no;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText)findViewById(R.id.e1);
e2 = (EditText)findViewById(R.id.e2);
e3 = (EditText)findViewById(R.id.e3);
e4 = (EditText)findViewById(R.id.e4);
yes = (RadioButton)findViewById(R.id.yes);
no = (RadioButton)findViewById(R.id.no);
}
public void onClickreset(View v){
yes.setChecked(false);
no.setChecked(false);
e1.setEnabled(false);
e2.setEnabled(false);
e3.setEnabled(false);
e1.setText("");
e2.setText("");
e3.setText("");
e4.setText("");
};
public void onRadioButtonClicked(View v)
{
boolean checked = ((RadioButton) v).isChecked();
switch(v.getId()){
case R.id.yes:
if(checked)
e1.setEnabled(false);
e2.setEnabled(false);
e3.setEnabled(true);
e3.requestFocus();
break;
case R.id.no:
if(checked)
e1.setEnabled(true);
e2.setEnabled(true);
e2.requestFocus();
e2.clearFocus();
e3.setEnabled(false);
break;
}
}
}
activity_main.xml
<?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:id="#+id/LL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.chris.myapplication.MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*">
<!-- Row 1 Starts From Here -->
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:checked="false"
android:onClick="onRadioButtonClicked"
android:text="yes" />
<RadioButton
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:checked="false"
android:onClick="onRadioButtonClicked"
android:text="no" />
</RadioGroup>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="1:"
android:textColor="#000000" />
<EditText
android:id="#+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="2:"
android:textColor="#000000" />
<EditText
android:id="#+id/e2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="3:"
android:textColor="#000000" />
<EditText
android:id="#+id/e3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/t4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="4:"
android:textColor="#000000" />
<EditText
android:id="#+id/e4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:enabled="false"
android:inputType="numberDecimal"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="30dp" />
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_span="3"
android:gravity="center"
android:onClick="onClickreset"
android:text="Reset"
android:textAllCaps="false"
android:textColor="#000000"
android:textStyle="normal" />
</TableLayout>
</LinearLayout>
It's better to use RadioGroup.OnCheckedChangeListener, first of all, get rid of android:onClick attributes and assign an id to RadioGroup in XML:
<RadioGroup
android:id="#+id/yes_no_radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:checked="false"
android:text="yes" />
<RadioButton
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:checked="false"
android:text="no" />
</RadioGroup>
Then make your Activity implements RadioGroup.OnCheckedChangeListener and move there code from onRadioButtonClicked(View v) method:
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
//Some activity code
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.yes: //Not needed to
e1.setEnabled(false); //which RadioButton clicked
e2.setEnabled(false);
e3.setEnabled(true);
e3.requestFocus();
break;
case R.id.no:
e1.setEnabled(true);
e2.setEnabled(true);
e2.requestFocus();
e2.clearFocus();
e3.setEnabled(false);
break;
}
}
}
Then, assign OnCheckedChangeListener with RadioGroup:
yesNoGroup = findViewById(R.id.radio_group);
yesNoGroup.setOnCheckedChangeListener(this);
And change:
yes.setChecked(false);
no.setChecked(false);
To
yesNoGroup.clearCheck();
i have problem with radiogroup and radiobutton that always sending a same value
the xml code look like this
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/radiogender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioPria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:button="#drawable/bg_selector_radiobutton"
android:checked="true"
android:text="Male"
android:textColor="#color/color_text_font_login"
android:textSize="14sp" />
<RadioButton
android:id="#+id/radioWanita"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/bg_selector_radiobutton"
android:text="Female"
android:textColor="#color/color_text_font_login"
android:textSize="14sp" />
</RadioGroup>
</LinearLayout>
and the snippet code is
jeniskelamin = (RadioGroup) findViewById(R.id.radiogender);
if (jeniskelamin.getCheckedRadioButtonId() == R.id.radioPria) {
gender = "male";
} else if (jeniskelamin.getCheckedRadioButtonId() == R.id.radioWanita) {
gender = "female";
} else gender = null;
no matter which radiobutton i choose the value always sending "male"
help out here
thanks for your help in advance!
Why am I getting two RadioButtons selected?
I want only one RadioButton selected at a time.
Here is the layout:
<RadioGroup
android:orientation="vertical"
android:id="#+id/radio_gender"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checkedButton="#+id/radio_female"
android:layout_alignRight="#+id/Button01"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/button1" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio_female"
android:text="#string/radio_female" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio_male"
android:text="#string/radio_male" />
and the code:
public void radioButtonClicked(View view) {
// Check that the button is now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.radio_female:
if (checked)
userGender = "female";
break;
case R.id.radio_male:
if (checked)
userGender = "male";
break;
}
}
You need to put the RadioButton inside the RadioGroup. Change your XML like this:
<RadioGroup
android:orientation="vertical"
android:id="#+id/radio_gender"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checkedButton="#+id/radio_female"
android:layout_alignRight="#+id/Button01"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/button1">
<RadioButton
android:onClick="radioButtonClicked"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio_female"
android:text="#string/radio_female" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio_male"
android:text="#string/radio_male" />
</RadioGroup>
Close RadioGroup tag
<RadioGroup
android:orientation="vertical"
android:id="#+id/radio_gender"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checkedButton="#+id/radio_female"
android:layout_alignRight="#+id/Button01"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/button1" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio_female"
android:text="#string/radio_female" />
<RadioButton
android:onClick="radioButtonClicked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio_male"
android:text="#string/radio_male" />
</RadioGroup>
I want to add radio buttons such that only one of them is selected at a time among 4 buttons and I want to place them as:
RadioButton1 RadioButton2
RadioButton3 RadioButton4
I am trying the following code but 1&2 forms a grp and 3&4 forms a different grp and there are two value selected a time. Can anyone check it and share the correct way to do it?
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/apple" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/rdogrp_main"
android:orientation="vertical"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rdogrp1"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"
android:textColor="#000000"
/>
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="BlueBerry"
android:textColor="#000000"
/>
</RadioGroup>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rdogrp2"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple "
android:textColor="#000000"
/>
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Santra :P"
android:textColor="#000000"
/>
</RadioGroup>
</RadioGroup>
</LinearLayout>
Ok I find a solution for you:
I know it is not the best solution but it works! :)
Just copy the xml file and activity code bellow:
Activity:
package com.example.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RadioButton;
public class MainActivity extends Activity {
private static String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RadioButton radiobutton1 = (RadioButton) findViewById(R.id.RadioButton1);
final RadioButton radiobutton2 = (RadioButton) findViewById(R.id.RadioButton2);
final RadioButton radiobutton3 = (RadioButton) findViewById(R.id.RadioButton3);
final RadioButton radiobutton4 = (RadioButton) findViewById(R.id.RadioButton4);
radiobutton1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton1.setChecked(true);
radiobutton2.setChecked(false);
radiobutton3.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton2.setChecked(true);
radiobutton1.setChecked(false);
radiobutton3.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton3.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton3.setChecked(true);
radiobutton1.setChecked(false);
radiobutton2.setChecked(false);
radiobutton4.setChecked(false);
return true;
}
});
radiobutton4.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
radiobutton4.setChecked(true);
radiobutton1.setChecked(false);
radiobutton2.setChecked(false);
radiobutton3.setChecked(false);
return true;
}
});
}
}
activity_main.xml :
<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"
tools:context=".MainActivity" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FFDDDDDD" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioGroup
android:id="#+id/rdogrp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="BlueBerry"
android:textColor="#000000" />
</RadioGroup>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioGroup
android:id="#+id/rdogrp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple "
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Santra :P"
android:textColor="#000000" />
</RadioGroup>
</TableRow>
</TableLayout>
</RelativeLayout>
For that you need to create only one RadioGroup and add four different RadioButton.
And for adding UI refer this url : Manage Layout Inside a Horizontal Oriented Radiogroup .
I hope this will help you.
Try Like this..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/rdogrp_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="India"
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton2"
android:layout_width="74dp"
android:layout_height="wrap_content"
android:layout_weight="0.57"
android:text="Austraila"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Srilanka "
android:textColor="#000000" />
<RadioButton
android:id="#+id/RadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.48"
android:text="SouthAfrica"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
Let me know if you solve the problem...
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();
}
});
}