I would like to create a list of RadioButtons to click on a Preference. I did the radio buttons in this way:
<RadioGroup>
<Preference android:key="ex" android:summary="Example" android:title="Example"/>
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_male"
android:checked="true" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_female" />
</RadioGroup>
After, in the Java file
Preference ex;
ex = (Preference) this.findPreference("ex");
ex.setOnPreferenceClickListener(new OnPreferenceClickListener() {
});
After this I do not know how to do. You can post a code to click on the preference displays a list of radio buttons? Thank you very much
There is no out-of-the-box RadioGroup preference available. You would need to write one, by extending DialogPreference class.
Create a separate layout file (/res/layout/pref_radiogroup.xml) with the Dialog content.
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/radio_male" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_female" />
</RadioGroup>
</FrameLayout>
Create your RadioGroupPreference class which will extend DialogPreference.
public class RadioGroupPreference extends DialogPreference {
private RadioGroup radioGroup;
public RadioGroupPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.pref_radiogroup);
}
#Override
protected View onCreateDialogView() {
View root = super.onCreateDialogView();
radioGroup = (RadioGroup) root.findViewById(R.id.radioSex);
return root;
}
#Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
setSelectedValue(getPersistedString("Male"));
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (!positiveResult) {
return;
}
if (shouldPersist()) {
String valueToPersist = getSelectedValue();
if (callChangeListener(valueToPersist)) {
persistString(valueToPersist);
}
}
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}
private void setSelectedValue(String value) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
View view = radioGroup.getChildAt(i);
if (view instanceof RadioButton) {
RadioButton radioButton = (RadioButton) view;
if (radioButton.getText().equals(value)) {
radioButton.setChecked(true);
}
}
}
}
private String getSelectedValue() {
int radioButtonId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonId);
return (String) radioButton.getText();
}
}
Now you only have to add your custom preference to your preference screen.
<com.package.RadioGroupPreference
android:summary="Radio Group Preference Summary"
android:title="Radio Group Preference"
android:key="preference_key"
android:defaultValue="#string/radio_male" />
Related
I have a radio group with the choices "Yes," "No," and "Maybe". I want a function that will return which choice the user picked.
Here is my code
<RadioGroup
android:id="#+id/answerRadioGroup"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="#+id/submitButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/myTextView"
app:layout_constraintVertical_bias="0.26">
<RadioButton
android:id="#+id/yesRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Yes" />
<RadioButton
android:id="#+id/noRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="No" />
<RadioButton
android:id="#+id/maybeRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Maybe" />
</RadioGroup>
And here is the function I am trying to use:
private String checkGuess(){
guessRadioGroup = (RadioGroup) findViewById(R.id.guessRadioGroup);
int selectedId = guessRadioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedId);
String s = (String) radioButton.getText().toString();
return s;
}
This function seems to work when I replace String s = (String) radioButton.getText().toString(); with something simply like String s = "Whatever". The problem seems to be with using the getText() method.
This is what I use in MainActivity.java to display the text:
TextView tempTextView = (TextView) findViewById(R.id.tempTextView);
tempTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String abc = checkGuess();
tempTextView.setText("You chose: " + abc);
}
});
You should use CustomListener for radioButton
radioButton.setOnCheckedChangeListener(new CustomListener());
class CustomListener implements CompoundButton.OnCheckedChangeListener{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.rd1:
break;
}
}
}
You should use your radioGroup as parent for find radio button. And also you don't need cast as String while using toString().
private String checkGuess(){
guessRadioGroup = (RadioGroup) findViewById(R.id.guessRadioGroup);
int selectedId = guessRadioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) guessRadioGroup.findViewById(selectedId);
String s = radioButton.getText().toString();
return s;
}
Hope it will help you.
first of all, I'll explain the expected behavior of the App.
What does the App do?
When I run the App I see this view:
Now if you hit the "+" button you'll see this Dialog:
By clicking Add this entry will be added to a ListView. I added two items. This looks like this:
Expected Result
Now when I touch and hold the mic icon of the list items it should start recording audio through the device microphone and then save this recording with the name of the bold entry, that means e.g. the first recording will get the name A.3gp, the second one B.3gp and so on..
Actual Result (UPDATE 07.12.2017)
The current state is:
Every item in the list gets his "own" recording. That means if I touch and hold the microphone icon of the first item in the list, it is doing what it should do. The same goes for all other items on the list.
When I add the first item A, then touch its record icon, a file will be created with the name A.3gp (which is correct behavior).
When I add the second item B then do nothing and then add a third item C and touch the record icon for B, a file will be created with the name C.3gp (which is not a correct behavior, it should be B.3gp).
Now to the fun part.
Code (UPDATE 07.12.2017)
1. The Model
public class Word {
private String mForeignTranslation;
private String mDefaultTranslation;
private ImageView mRecordIconImageResourceId;
private MediaRecorder mMediaRecorder;
public Word(String foreignTranslation, String defaultTranslation, ImageView recordIconImageResourceId) {
this.mForeignTranslation = foreignTranslation;
this.mDefaultTranslation = defaultTranslation;
this.mRecordIconImageResourceId = recordIconImageResourceId;
}
public Word(String foreignTranslation, String defaultTranslation) {
this.mForeignTranslation = foreignTranslation;
this.mDefaultTranslation = defaultTranslation;
}
public String getDefaultTranslation() {
return mDefaultTranslation;
}
public String getForeignTranslation() {
return mForeignTranslation;
}
public ImageView getRecordIconImageResourceId() {
return mRecordIconImageResourceId;
}
public MediaRecorder getMediaRecorder() {
return mMediaRecorder;
}
public void setDefaultTranslation(String mDefaultTranslation) {
this.mDefaultTranslation = mDefaultTranslation;
}
public void setForeignTranslation(String mForeignTranslation) {
this.mForeignTranslation = mForeignTranslation;
}
public void setRecordIconImageResourceId(ImageView recordIconImageResourceId) {
this.mRecordIconImageResourceId = recordIconImageResourceId;
}
public void setMediaRecorder(MediaRecorder mMediaRecorder) {
this.mMediaRecorder = mMediaRecorder;
}
}
2. The Adapter
public class WordAdapter extends ArrayAdapter<Word> {
private ArrayList<Word> wordsArrayList = new ArrayList<>();
private MediaRecorder mediaRecorder = new MediaRecorder();
public WordAdapter(#NonNull Context context, ArrayList<Word> words) {
super(context, 0, words);
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.my_word_list_items,parent,false);
}
Word currentWord = getItem(position);
TextView foreignWord = listItemView.findViewById(R.id.myForeignWord);
foreignWord.setText(currentWord.getForeignTranslation());
TextView defaultWord = listItemView.findViewById(R.id.myDefaultWord);
defaultWord.setText(currentWord.getDefaultTranslation());
final ImageView recordIconImageView = listItemView.findViewById(R.id.recordIconImageView);
wordsArrayList = MyWordsActivity.getWordsArrayList();
recordIconImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN: {
recordIconImageView.setImageResource(R.drawable.mic_red);
startAudioRecording();
break;
}
case MotionEvent.ACTION_UP: {
recordIconImageView.setImageResource(R.drawable.mic_black);
stopAudioRecording();
break;
}
}
return true;
}
});
return listItemView;
}
private ArrayList<Word> getWordsArrayList() {
return wordsArrayList;
}
private void startAudioRecording() {
if (wordsArrayList != null) {
Log.i("ArrayListe", wordsArrayList.toArray().toString());
getMediaRecorderReady();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
Toast.makeText(this.getContext(), "Recording started", Toast.LENGTH_SHORT).show();
}
}
private void stopAudioRecording() {
try {
mediaRecorder.stop();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(this.getContext(), "Recording stopped", Toast.LENGTH_SHORT).show();
}
private void getMediaRecorderReady() {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"speakmylanguage"+"/"+MyWordsActivity.getForeignWord()+".3gp");
}
}
3. The Activity
public class MyWordsActivity extends AppCompatActivity {
private static String defaultWord, foreignWord;
private static ArrayList<Word> wordsArrayList = new ArrayList<>();
WordAdapter wordAdapter;
// Main Activity Views
TextView hintTextView;
ListView myWordsListView;
FloatingActionButton floatingButtonAddNewWord;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_words);
// Init Main Activity Views
hintTextView = findViewById(R.id.hintTextView);
myWordsListView = findViewById(R.id.myWordsList);
floatingButtonAddNewWord = findViewById(R.id.fabAddNewWord);
floatingButtonAddNewWord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog addNewWordsDialog = new Dialog(MyWordsActivity.this);
addNewWordsDialog.setContentView(R.layout.activity_add_new_words);
final EditText addForeignWordEditText = addNewWordsDialog.findViewById(R.id.addForeignWordEditText);
final EditText addDefaultWordEditText = addNewWordsDialog.findViewById(R.id.addDefaultWordEditText);
final Button addNewWordButton = addNewWordsDialog.findViewById(R.id.addNewWordButton);
addNewWordsDialog.show();
addNewWordButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!addDefaultWordEditText.getText().toString().equals("") &&
!addForeignWordEditText.getText().toString().equals("")) {
foreignWord = addForeignWordEditText.getText().toString();
defaultWord = addDefaultWordEditText.getText().toString();
wordsArrayList.add(new Word(foreignWord, defaultWord));
hintTextView.setVisibility(View.GONE);
addNewWordsDialog.dismiss();
} else {
Toast.makeText(MyWordsActivity.this, "Please enter two words", Toast.LENGTH_SHORT).show();
}
}
});
wordAdapter = new WordAdapter(MyWordsActivity.this, getWordsArrayList());
myWordsListView.setAdapter(wordAdapter);
}
});
}
public static String getDefaultWord() {
return defaultWord;
}
public static void setDefaultWord(String defaultWord) {
MyWordsActivity.defaultWord = defaultWord;
}
public static String getForeignWord() {
return foreignWord;
}
public static void setForeignWord(String foreignWord) {
MyWordsActivity.foreignWord = foreignWord;
}
public static ArrayList<Word> getWordsArrayList() {
return wordsArrayList;
}
}
4. The Layouts
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/myWordsListItems"
android:layout_width="match_parent"
android:layout_height="88dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:orientation="vertical">
<TextView
android:id="#+id/myForeignWord"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="24sp"
android:textStyle="bold"
tools:text="foreign word" />
<TextView
android:id="#+id/myDefaultWord"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="24sp"
tools:text="default word" />
</LinearLayout>
<ImageView
android:id="#+id/playIconImageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:clickable="true"
android:src="#drawable/play_icon" />
<ImageView
android:id="#+id/recordIconImageView"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="72dp"
android:clickable="true"
android:src="#drawable/mic_black" />
</RelativeLayout>
activity_my_words.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.yousef.mustafa.speakmylanguage.View.MyWordsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="#string/app_name"
app:titleTextColor="#color/colorWhite" />
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myWordsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:drawSelectorOnTop="true"
android:orientation="vertical"
tools:context=".View.MyWordsActivity" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabAddNewWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center"
android:layout_marginBottom="32dp"
android:tint="#color/colorWhite"
app:backgroundTint="#color/colorGrey"
app:srcCompat="#drawable/icon_add" />
<TextView
android:id="#+id/hintTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="155dp"
android:gravity="center"
android:text="#string/hint"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
activity_add_new_words.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorDialogBackground">
<LinearLayout
android:id="#+id/addNewWordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:layout_marginTop="12dp"
android:text="#string/add_word_title"
android:textColor="#color/colorBlack"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/addForeignWordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="4dp"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_weight="5"
android:gravity="center_horizontal"
android:hint="#string/enter_foreign_word"
android:inputType="textPersonName|textCapWords"
android:textSize="24sp" />
<EditText
android:id="#+id/addDefaultWordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:layout_weight="5"
android:gravity="center_horizontal"
android:hint="#string/enter_default_word"
android:inputType="textPersonName|textCapWords"
android:textSize="24sp" />
<Button
android:id="#+id/addNewWordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/button_add"
android:textAllCaps="false"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
I would appreciate any help. Thanks in advance.
In the startAudioRecording() method of the Activity, you set the output filename from the foreignWord field. The problem is that the foreignWord field is set to the most recently added foreign word each time the user submits a word pair in the Add Word dialog. Thus each time the user starts a recording, the name of the output file will be set to the last foreign word added.
Instead of using fields for recordingImageView, foreignWord, defaultWord, foreignWord, audioSavePath, and mediaRecorder, you should make them local variables, and pass them as parameters to your record(), startAudioRecording(), stopAudioRecording() methods with the following steps:
Just delete those fields from the beginning of the class so that you see some compilation errors where the fields were used.
Where you see an undefined variable on the left side of an assignment, make the variable local (Ctrl+V in Android Studio).
Where you see an undefined variable elsewhere, make the variable a parameter of the method (Ctrl+P).
It should work after those changes.
`
Kindly move this snippet :
recordingImageView.setOnTouchListener(new View.OnTouchListener()
{
#Override public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
{ recordingImageView.setImageResource(g startAudioRecording(position);
break;
}
case MotionEvent.ACTION_UP:
{
recordingImageView.setImageResource(R.drawable.mic_black);
stopAudioRecording();
break;
}
}
return true;
}
});
In Adapter , getView(...) Below accessing recordImageView.
And put startAudioRecording() , stopAudioRecording() in same adapter or all relevant method for recording is to be in Adapter.
So by this you will get click on in each individual image view. Previously when you are fetching it will only take instance of any one image view.
Now update code on "startAudioRecording()" as:
private void startAudioRecording()
{
if (checkPermission())
{
if (wordsArrayList != null)
{
audioSavePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + wordsArrayList.get(position) + ".3gp";
getMediaRecorderReady();
}
}
}
Now your second point, saving its name by any name. Will cover by above code.
Thanks and happy coding
Is there a way how to hide some buttons when i click on EditText and keybord shows?
I have this layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/InnerRelativeLayout">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="#+id/table">
<TableRow
android:id="#+id/tariffRow">
<TextView
android:layout_column="1"
android:text="Název"
android:padding="3dip" />
<EditText
android:id="#+id/tariffName"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
</ScrollView>
<LinearLayout android:id="#+id/InnerRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button android:id="#+id/okBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Ok"/>
<Button android:id="#+id/stornoBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Storno" />
</LinearLayout>
Now when the editText is clicked to type some value i need to hide that linearLayout android:id="#+id/InnerRelativeLayout" because otherwise it is still visible above the keyboard.
Need to set a FocusChangeListener to the EditText which triggers whenever focus changes on that EditText as follow:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// hide relative layout
} else {
// show relative layout
}
}
});
It is not actually that simple, here is my solution how I got it working.
First Need to create MyEditText.java
public class MyEditText extends EditText {
private KeyImeChange keyImeChangeListener;
public MyEditText(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public void setKeyImeChangeListener(KeyImeChange keyImeChange){
keyImeChangeListener = keyImeChange;
}
public interface KeyImeChange {
public boolean onKeyIme(int keyCode, KeyEvent keyEvent);
}
#Override
public boolean onKeyPreIme (int keyCode, KeyEvent keyEvent){
if(keyImeChangeListener != null){
return keyImeChangeListener.onKeyIme(keyCode, keyEvent);
}
return false;
}
}
Then layout.xml, change following
<EditText
android:id="#+id/tariffName"
android:gravity="right"
android:padding="3dip" />
Into
<!-- change yourapp -->
<com.yourapp.MyEditText
android:id="#+id/tariffName"
android:gravity="right"
android:padding="3dip" />
Add also android:focusable="true" to your LinearLayout
<LinearLayout android:id="#+id/InnerRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:focusable="true">
Then put into your activity like MainActivity.java,
import fi.hgs.apps.MyEditText.KeyImeChange;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
removeFocus();
MyEditText myEditText = (MyEditText) findViewById(R.id.tariffName);
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
showLinearLayout();
} else {
hideLinearLayout();
}
}
});
myEditText.setKeyImeChangeListener(new KeyImeChange() {
#Override
public boolean onKeyIme(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == event.getKeyCode()) {
removeFocus();
}
return false;
}
});
}
Add also these to do the actual job.
public void removeFocus() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.requestFocus();
}
public void showLinearLayout() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.setVisibility(View.VISIBLE);
}
public void hideLinearLayout() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.setVisibility(View.INVISIBLE);
}
I created a deletable EditText. Here is the layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<EditText
android:id="#+id/cacEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:paddingRight="35dp" />
<Button
android:id="#+id/cacClearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:background="#android:drawable/ic_input_delete"
android:visibility="invisible" />
</RelativeLayout>
The problem is that when I put more of these into one layout and rotate to landscape, they all suddenly have the same value. I suppose it is because the system restores the values by ID and each component consists of elements of the same IDs. How can I solve this problem?
More info:
deletable EditText (cz.kns.uome.component.CleanAndClearEditText) in a layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/cz.kns.uome"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<cz.kns.uome.component.CleanAndClearEditText
android:id="#+id/nameEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/contactPickerButton"
app:hint="#string/person_name"
app:type="textPersonName" />
<ImageButton
android:id="#+id/contactPickerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="#string/select_contact"
android:src="#drawable/ic_action_dropdown" />
</RelativeLayout>
<cz.kns.uome.component.CleanAndClearEditText
android:id="#+id/emailEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:hint="#string/person_email_opt"
app:type="textEmailAddress" />
<cz.kns.uome.component.CleanAndClearEditText
android:id="#+id/descriptionEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:hint="#string/description_opt"
app:type="text" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="#string/save" />
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="#string/cancel" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
java class
public class CleanAndClearEditText extends RelativeLayout {
private final EditText editText;
private final Button clearButton;
public CleanAndClearEditText(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.clean_and_clear_edit_text, this);
TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
R.styleable.ClearAndCleanEditText);
// ?????
// I have to somehow access that edittext here but static id does not work
editText = (EditText) findViewById(R.id.cacEditText);
editText.addTextChangedListener(textWatcher);
editText.setHint(typedArray.getString(R.styleable.ClearAndCleanEditText_hint));
String type = typedArray.getString(R.styleable.ClearAndCleanEditText_type);
if (type != null) {
editText.setInputType(InputTypes.get(type));
}
clearButton = (Button) findViewById(R.id.cacClearButton);
clearButton.setOnClickListener(clearButtonListener);
}
// rest ommited
}
I'm running on this issue, and I was able to "solve" this, by removing all views in the layout that contains the custom views, implement this:
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
// retain your data in the bundle then remove views
layout.removeAllViewsInLayout();
}
So you can get your data back from the bundle in the onCreate method.
See if this helps:
public class CleanAndClearEditText extends RelativeLayout {
private final EditText editText;
private final Button clearButton;
public CleanAndClearEditText(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.clean_and_clear_edit_text, this);
RelativeLayout rl = (RelativeLayout) getChildAt(0); // consider using a merge tag in R.layout.clean_and_clear_edit_text instead of the parent RelativeLayout
//clearButton = (Button) findViewById(R.id.cacClearButton);
clearButton = (Button) rl.getChildAt(1);
clearButton.setOnClickListener(null);
//editText = (EditText) findViewById(R.id.cacEditText);
editText = (EditText) rl.getChildAt(0);
}
#Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.stateToSave = editText.getText().toString();
return ss;
}
#Override
protected void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
editText.setText(ss.stateToSave);
}
static class SavedState extends BaseSavedState {
String stateToSave;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
stateToSave = in.readString();
}
#Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeString(stateToSave);
}
// required field that makes Parcelables from a Parcel
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}
and the modified R.layout.clean_and_clear_edit_text file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:paddingRight="35dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#android:drawable/ic_input_delete"
android:visibility="invisible" />
</RelativeLayout>
Edit:
The main part of the code is from the question I posted as a comment.
OK, I see you are inflating custom code and inserting it into a linearlayout. You should use a ListView and implement your ListAdapter. You can customize your ListItem to fit your needs.
That's the way I would do it.
All my other onClick methods work except the ones in which I have to inflate the layout to get the button! What am I doing wrong! Here is some code:
package com.games.think;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TableLayout;
public class Think extends Activity{
//What question are we on?
int question = 1;
//What level are we on?
String lvl ="1";
//Radio buttons we need to access them global
RadioButton lvl1;
RadioButton lvl2;
RadioButton lvl3;
RadioButton lvl4;
RadioButton lvl5;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//play
Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
playOnClick(v);
}
});
//level
Button level = (Button)findViewById(R.id.level);
level.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
levelOnClick(v);
}
});
//setLevel
LayoutInflater inflater = LayoutInflater.from(this);
TableLayout tbl = new TableLayout(this);
View playv = inflater.inflate(R.layout.level, null);
Button updateLevel = (Button) playv.findViewById(R.id.updateLevel);
tbl.addView(updateLevel);
updateLevel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateLevelOnClick(v);
}
});
View levelv = inflater.inflate(R.layout.play, null);
Button gotomenu = (Button) levelv.findViewById(R.id.tomenu);
gotomenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toMenuOnClick(v);
}
});
//Radio Buttons
lvl1 = (RadioButton) levelv.findViewById(R.id.lvl1);
lvl2 = (RadioButton) levelv.findViewById(R.id.lvl2);
lvl3 = (RadioButton) levelv.findViewById(R.id.lvl3);
lvl4 = (RadioButton) levelv.findViewById(R.id.lvl4);
lvl5 = (RadioButton) levelv.findViewById(R.id.lvl5);
//tomenu
lvl = getLevel();
if(lvl.equals("-1")) {
lvl=getLevel();
}
}
protected void toMenuOnClick(View v) {
setContentView(R.layout.main);
}
protected void updateLevelOnClick(View v) {
setContentView(R.layout.main);
}
protected void levelOnClick(View v) {
setContentView(R.layout.level);
if(lvl.equals("1")) {
lvl1.setChecked(true);
}
if(lvl.equals("2")) {
lvl2.setChecked(true);
}
if(lvl.equals("3")) {
lvl3.setChecked(true);
}
if(lvl.equals("4")) {
lvl4.setChecked(true);
}
if(lvl.equals("5")) {
lvl5.setChecked(true);
}
}
protected void playOnClick(View v) {
setContentView(R.layout.play);
setQuestion();
}
private String getLevel() {
String FILENAME = "think_level";
FileInputStream fis;
byte[] buffer = new byte[1000];
try {
fis = openFileInput(FILENAME);
} catch (FileNotFoundException e) {
setLevel("1");
return "-1";
}
try {
fis.read(buffer,0,1000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String level = buffer.toString();
return level;
}
private void setLevel(String _level) {
String FILENAME = "think_level";
String level = _level;
FileOutputStream fos;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(level.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void setQuestion() {
}
}
Here are my xml files:
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="#+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/imageView1" android:src="#drawable/think" android:scaleType="fitCenter"></ImageView>
<Button android:id="#+id/play" android:text="Play" android:layout_height="wrap_content" android:layout_width="wrap_content" ></Button>
<Button android:text="Level" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/level"></Button>
</TableLayout>
Here is my level.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="#+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:id="#+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="Level:" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</TableRow>
<TableRow android:id="#+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<RadioGroup android:id="#+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<RadioButton android:id="#+id/lvl1" android:text="Level 1" android:layout_height="wrap_content" android:checked="true" android:layout_width="wrap_content"></RadioButton>
<RadioButton android:id="#+id/lvl2" android:text="Level 2" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton>
<RadioButton android:id="#+id/lvl3" android:text="Level 3" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton>
<RadioButton android:id="#+id/lvl4" android:text="Level 4" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton>
<RadioButton android:id="#+id/lvl5" android:text="Level 5" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton>
</RadioGroup>
</TableRow>
<Button android:text="Set Level" android:id="#+id/updateLevel" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableLayout>
Here is play.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:id="#+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="TextView" android:id="#+id/question" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
</ScrollView>
<RadioGroup android:id="#+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" >
<RadioButton android:id="#+id/radioButton1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="RadioButton"></RadioButton>
<RadioButton android:id="#+id/radioButton2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="RadioButton"></RadioButton>
<RadioButton android:id="#+id/radioButton3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="RadioButton"></RadioButton>
</RadioGroup>
<Button android:id="#+id/go" android:text="Go" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
<Button android:id="#+id/tomenu" android:text="Back To Menu" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
</TableLayout>
The problem is that the view you are adding does not have a parent and therefore is not able to receive click events. Currently you're inflating the view using
View playv = inflater.inflate(R.layout.level, null);
You'll want to place a parent view in that second argument.
View playv = inflater.inflate(R.layout.level, parentView, false);
This will allow you to use a parent view but not actually attach it.
So the buttons are in your level and play layouts, and you're inflating those layouts, but it doesn't look like you're ever adding the layouts to the your main view/layout. Are you actually able to see those two layouts you are inflating?
inflation is async method, can you try to add the click listener after its inflated
//only inflate in oncreate / onresume
inflater.inflate(
R.layout.play, // resource
this, // root
true); //attach to root
//overide onFinishInflate and get the view here
protected void onFinishInflate() {
super.onFinishInflate();
Button gotomenu = (Button) levelv.findViewById(R.id.tomenu);
gotomenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toMenuOnClick(v);
}
});
}
There is no apparent reason why this should not work.
Any runtime created views must be added to your linearlayout(or any other parent layout)
using :
linearLayoutMain.addView(updateLevel);
Have you used this buttons in any of the linear layouts in your xml file i.e. does these buttons appear when you reach the corresponding activity?
You should be seeing the exception at tbl.addView(updateLevel);, because you are trying to add a view whose parent is already specified.
Are you not seeing this exception ?
If You want item value on item click just use .setTag(some value); and getTag();
RelativeLayout firstContactRlayout = (RelativeLayout) myLayout.findViewById(R.id.firstContactRlayout);
firstContactRlayout.setTag(number);
firstContactRlayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String gettags = (String) v.getTag();
Log.e("gettags", "--" + gettags);
}
});
Try this after tbl.addView(updateLevel); 0 of tbl.getChildAt(0) may change:
tbl.getChildAt(0).findViewById(R.id.updateLevel).setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
updateLevelOnClick(v);
}
});