Reputation: 221
I'd like to that with the letters in my application.
I tried everything with TextViews and Buttons. I used GridView and GridLayout but it did not work. I can make the button disappear and the button's text appear. My main problem is when i click on the "a" letter first it appears in the second box because there is the correct place. But it shloud appear in the first box. So i can't make letters appear the order as i click. If anyone has an idea how to do that please response.
Here is my code:
int fillFactor = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String word = "SAMSUNG";
final LinearLayout linear = (LinearLayout)findViewById(R.id.linear);
for(int i=0;i<word.length();i++){
Button b = new Button(this);
b.setTag(word.charAt(i));
b.setLayoutParams(new LinearLayout.LayoutParams(48,48));
linear.addView(b);
}
for(int j=0;j<word.length();j++){
Button bn = new Button(this);
bn.setTag(word.charAt(j));
bn.setLayoutParams(new LinearLayout.LayoutParams(48,48));
linear.addView(bn);
bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(fillFactor < word.length())
fillFactor++;
Button view = null;
Button clickedview = view;
//find a correct button
Button leftButton = ((Button)linear.getChildAt(fillFactor));
if((boolean)leftButton.getTag().equals((String)clickedview.getTag()));
leftButton.setText((CharSequence) clickedview.getTag());
if(checkComplete()) moveToNextWordActivity();
else {
Toast.makeText(MainActivity.this, "Invalid character", Toast.LENGTH_LONG).show();
fillFactor--;
}
}
private boolean checkComplete() {
return fillFactor == word.length() - 1;
}
private void moveToNextWordActivity() {
}
});
}
} }
Upvotes: 0
Views: 150
Reputation:
my idea is like that:
make an Button
array of the letter of the word you need to find.
down forget to initialize all of the buttons on the onCreate()
.
in addition, make an int
array at the same size.
Button[] letters = new Button[size];
int[] arr = new int[size];
set all of the number in the array to 0;
for(int i = 0; i < size; i++)
{
arr[i] = 0;
}
now, every time the user press a letter, search for the first variable that equal to 0, lets call it i
.
change the text of the Button
in the array at i
to the letter you need and the int
at i
to 1.
@Override
public void onClick(View v)
{
for(int i = 0; i < size; i++)
{
if(arr[i] == 0)
{
arr[i] = 1;
letters[i].setText(view.getText().toString());
break;
}
}
if(arr[size-1] == 1)
{
test();
}
}
//i would suggest to check all of the array in case the user removed one of the middle letters but it will work this way too. i did it for the sake of simplicity
now, all of the letters in the word.
when the last letter is placed the function test()
will be called.
this function will get the letter and turn in into a word(String
):
public void test()
{
String word = "";
for(int i = 0; i < size; i++)
{
word += letters[i].getText().toString();
}
if(word == requested_word)
{
doSomething(); // winning notification or what you need to do
}
}
requested_word
is the word you need to find.
please keep in mind that you can make it better and this is the basic concept. alse, i didn't use ide to check the code and i might have few typoes.
good night and keep coding ;)
Upvotes: 1
Reputation: 607
I'm not completely sure if i got your point, but if you try to use the buttons at the button of your screen as a kind of one way keybord (every button can only be pressed ones and the letters appear in the same ordering like the button clicks) then i would implement it like this:
INVISIBLE
If you want to have an advanced and clean solution, I would recommend to create a CompoundView for the word that is typed as well as one for the "OneWayKeyboard". If you want to learn how you can create your on views in Android have a look at the following tutorial by Lars Vogel: http://www.vogella.com/tutorials/AndroidCustomViews/article.html
Upvotes: 0
Reputation: 15821
For example you may have 'SAMSUNG' word.
Initialize a parent which contains all Buttons
String word = "SAMSUNG";
LinearLayout linear = new LinearLayout(context);// or use findViewById
// make linear layout horizontal
add fill buttons :
for(int i=0;i<word.length();i++){
Button b = new Button(context);
b.setTag(word.charAt(i));
b.setLayoutParams(new LinearLayout.LayoutParams(48,48));
linear.addView(b);
}
Keyboard buttons add in the same way : set in tag related character.
init a fillFactor value :
int fillFactor = -1;
When you click on any button on KEYBOARD:
OnClick(...) {
if(fillFactor < word.length())
fillFactor++;
Button clickedView = view;
//find a correct button
Button leftButton = ((Button)linear.getChildAt(fillFactor));
if((String)leftButton.getTag().equals((String)clickedView.getTag())
leftButton.setText(clickedView.getTag());
if(checkComplete()) moveToNextWordActivity();
else {
showErrorToast("Invalid character");
fillFactor--;
}
}
public boolean checkComplete() {
return fillFactor == word.length - 1;
}
Upvotes: 0