Shuffle Button with position Android - android

I have ten buttons in my xml layout i want to exchange the position of the 10 buttons randomly. I tried two methods to achieve it.
Method 1: I changed the background resource of the button but only the image changed. Not the button position on the layout. Here is my workings for method 1.
objects = new ArrayList<Integer>();
objects.add(R.drawable.num_one);
objects.add(R.drawable.num_eight);
objects.add(R.drawable.num_six);
objects.add(R.drawable.num_five);
objects.add(R.drawable.num_nine);
objects.add(R.drawable.num_four);
objects.add(R.drawable.num_two);
objects.add(R.drawable.num_three);
objects.add(R.drawable.num_zero);
objects.add(R.drawable.num_seven);
// Shuffle the collection
Collections.shuffle(objects);
List<Button> buttons = new ArrayList<Button>();
buttons.add((Button)findViewById(R.id.name));
buttons.add((Button)findViewById(R.id.boutton_eight));
buttons.add((Button)findViewById(R.id.boutton_six));
buttons.add((Button)findViewById(R.id.boutton_five));
buttons.add((Button)findViewById(R.id.time));
buttons.add((Button)findViewById(R.id.boutton_four));
buttons.add((Button)findViewById(R.id.boutton_two));
buttons.add((Button)findViewById(R.id.search_box));
buttons.add((Button)findViewById(R.id.boutton_zero));
buttons.add((Button)findViewById(R.id.boutton_seven));
for (int i = 0; i < objects.size(); i++) {
buttons.get(i).setBackgroundResource(objects.get(i));
}
In Method two: I shuffled the button directly but as the widget are already set in the xml layout. AddView () give an error message that the view is already set. Here is my working for the second solution.
ll = (LinearLayout)findViewById(R.id.llShuffleBox);
//create another two linear layouts which will host 5 buttons horizontally
top_compte = (LinearLayout)findViewById(R.id.top_compte);
bottom_calculator= (LinearLayout)findViewById(R.id.bottom_calculator);
buttons.add((Button)findViewById(R.id.name));
buttons.add((Button)findViewById(R.id.boutton_eight));
buttons.add((Button)findViewById(R.id.boutton_six));
buttons.add((Button)findViewById(R.id.boutton_five));
buttons.add((Button)findViewById(R.id.time));
buttons.add((Button)findViewById(R.id.boutton_four));
buttons.add((Button)findViewById(R.id.boutton_two));
buttons.add((Button)findViewById(R.id.search_box));
buttons.add((Button)findViewById(R.id.boutton_zero));
buttons.add((Button)findViewById(R.id.boutton_seven));
Collections.shuffle(buttons);
for (int i=0;i<5;i++) {
top_compte.addView(buttons.get(i));
}
//add remaining 5 to second layout
for (int i=5;i<10;i++){
bottom_calculator.addView(buttons.get(i));
}
// ll.addView(top_compte);
ll.addView(bottom_calculator);
My question is how can i exchanged the position of the buttons on my layout. Note button 0 to 4 is in a horizontal linear layout and below it is the second horizontal linear layout containing button 5 to 9. Also ll is the Main LinearLayout that contain the two horizontal layout.

You can try creating the Buttons dynamically(in code). So, you'll have a LinearLayout(vertical) and two LinearLayouts(horizontal) within it. Keep your xml file as:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llShuffleBox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/top_compte"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_calculator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
Now, for the activity code:
ll = (LinearLayout)findViewById(R.id.llShuffleBox);
//create another two linear layouts which will host 5 buttons horizontally
top_compte = (LinearLayout)findViewById(R.id.top_compte);
bottom_calculator= (LinearLayout)findViewById(R.id.bottom_calculator);
// Create an ArrayList to hold the Button objects that we will create
ArrayList<Button> buttonList = new ArrayList<Button>();
// Create the Buttons, set their text as numeral value of the index variable
for (int i = 0; i < 10; i++) {
Button b = new Button(this);
b.setText("" + (i+1));
b.setGravity(Gravity.CENTER_HORIZONTAL);
buttonList.add(b);
}
// Shuffle
Collections.shuffle(buttonList);
for (int i = 0; i < 10; i++) {
// Add the first five Buttons to top_compte
// Add the last five Buttons to bottom_calculator
if (i < 5) {
top_compte.addView(buttonList.get(i));
} else {
bottom_calculator.addView(buttonList.get(i));
}
}
Edit 1:
Declare and initialize this global variable:
// Global variable
int id = 1;
Add the following method to your activity. It will generate an id that is not in use elsewhere in your view tree.
// Generates and returns a valid id that's not in use
public int generateUniqueId(){
View v = findViewById(id);
while (v != null){
v = findViewById(++id);
}
return id++;
}
Change the code to set ids to buttons:
ll = (LinearLayout)findViewById(R.id.llShuffleBox);
//create another two linear layouts which will host 5 buttons horizontally
top_compte = (LinearLayout)findViewById(R.id.top_compte);
bottom_calculator= (LinearLayout)findViewById(R.id.bottom_calculator);
// Create an ArrayList to hold the Button objects that we will create
ArrayList<Button> buttonList = new ArrayList<Button>();
// Create the Buttons, set their text as numeral value of the index variable
for (int i = 0; i < 10; i++) {
Button b = new Button(this);
b.setText("" + (i+1));
b.setGravity(Gravity.CENTER_HORIZONTAL);
b.setId(generateUniqueId()); // Set an id to Button
buttonList.add(b);
}
// Shuffle
Collections.shuffle(buttonList);
for (int i = 0; i < 10; i++) {
// Add the first five Buttons to top_compte
// Add the last five Buttons to bottom_calculator
if (i < 5) {
top_compte.addView(buttonList.get(i));
} else {
bottom_calculator.addView(buttonList.get(i));
}
}
You can retrieve the ids using the getId() method. If you do not want to use the generateUniqueId() method, you can try replacing the line b.setId(generateUniqueId()); with b.setId(i + 1);. This will set the button ids from 1 to 10. But, it can be problematic if some view in your view hierarchy has the same id as any other.
Edit 2:
Here is how you can set the OnClickListeners to the Buttons you create dynamically:
for (int i = 0; i < 10; i++) {
final Button b = new Button(this);
b.setText("" + (i+1));
b.setGravity(Gravity.CENTER_HORIZONTAL);
b.setId(i + 1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dealWithButtonClick(b);
}
});
bList.add(b);
}
And here is one possible implementation of dealWithButtonClick(Button) method. You can change it to suit your needs:
public void dealWithButtonClick(Button b) {
switch(b.getId()) {
case 1:
b.setBackground(getResources().getDrawable(R.drawable.some_drawable_1));
break;
case 2:
b.setBackground(getResources().getDrawable(R.drawable.some_drawable_2));
break;
........
........
........
case 10:
b.setBackground(getResources().getDrawable(R.drawable.some_drawable_10));
break;
default:
b.setBackground(getResources().getDrawable(R.drawable.some_drawable_for_when_no_cases_were_matched));
break; }
}

I had a same problem (shuffle buttons) and i come up with an easy solution
I just used an array of integer numbers of buttons ids and simple shufle. In my case was that enough. It's simple and u get the same result.
Integer[] a = { 0x7f090049, 0x7f09004b, 0x7f09004c, 0x7f090048};
Collections.shuffle(Arrays.asList(a));
button1 = (Button)findViewById(a[0]);
button2 = (Button)findViewById(a[1]);
button3 = (Button)findViewById(a[2]);
button4 = (Button)findViewById(a[3]);
I hope this will help.
Regards !!
(srry for my broken english)

Related

Find child of a LinearLayout inside a LinearLayout

I have a LinearLayout ("ll") that is already created in xml and the app dynamically creates another LinearLayout inside of it and creates an EditText and a Button inside of that view. The button makes the whole LinearLayout destroy itself along with the EditText and Button inside it (the whole system is a player name entering activity). Anyway, I am trying to find a way to get the text from all of the EditTexts. I have tried using a for loop on "ll" and using ll.getChildAt() but I can't seem to use .getChildAt() on whatever ll.getChildAt() generates because getChildAt() generates a "View" not a "LinearLayout." I basically just need a way to search two children in, rather than just one. Also, if there is just a better way I should be doing this, let me know. I'm open to suggestions.
Here's my code if it will help:
NewGameCreate.java
public class NewGameCreate extends Activity {
int numOfPlayers = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game_create);
}
public void newPlayer(View view) {
numOfPlayers++;
final LinearLayout ll = findViewById(R.id.playerView);
final LinearLayout llNew = new LinearLayout(getApplicationContext());
llNew.setOrientation(LinearLayout.HORIZONTAL);
llNew.setId(numOfPlayers);
ll.addView(llNew);
EditText newName = new EditText(this);
newName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
newName.setHint("Enter Player Name");
newName.setId(numOfPlayers);
newName.setWidth(0);
llNew.addView(newName);
final Button delete = new Button(this);
delete.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
delete.setText("Delete");
delete.setId(numOfPlayers);
delete.setWidth(0);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = delete.getId();
ll.removeViewInLayout(findViewById(id));
Drawable back = ll.getBackground();
ll.setBackgroundColor(00000000);
ll.setBackground(back);
ll.invalidate();
}
});
llNew.addView(delete);
}
public void startGame(View view){
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for(int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = ll.getChildAt(loop);
}
}
}
I think I found the answer to it. You need to change a little bit of code in the startGame() method I m providing the code for startGame below.
public void startGame(View view) {
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for (int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = (LinearLayout) ll.getChildAt(loop);
for (int j = 0; j < inner.getChildCount(); j++) {
if (inner.getChildAt(j) instanceof EditText) {
EditText textET = (EditText) inner.getChildAt(j);
Log.d("TAG",textET.getText().toString());
}
}
}
}
In the above code you were able to get the first child only but as you have added a linearLayout with orientation Horizontal in a parent LinearLayout with orientation Vertical, you have written code for the child of parent layout i.e playerView. I have modified the code to get the elements of the child Linear layout and Log prints all the text from the EditText.
Hope that helps!!

How I can save a linear layout and all its inner views and use it later android

I have one activity and inside it I have a Linear layout (lets name it the "main linear layout") and I am adding dynamically created view inside it (text views, linear layouts, edit texts, ...etc).
In the bottom of the screen there are two button (next and back).
If the user clicked on next button I should save the current "main linear layout " in a list for example and then I am generating a new views and I add it inside the "main linear layout" then .
And if the user clicked on back button I should restore the "main linear layout" and show all its views.
I don't know how I can do it.
I hope my description is clear :)
Take a look at onSaveInstance for preserving the view on the main screen when leaving to a different activity or screen orientation.
You can also try and use the buttons to hide the layouts using Visibility instead of trying to navigate through them.
This may help you. but concrete code need you write it yourself.
public class TestActivity extends Activity{
Stack<LinearLayout> layouts = new Stack<>();
FrameLayout container;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btnPre = new Button(this);
Button btnNext = new Button(this);
btnPre.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
container.addView(layouts.pop());
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout linearLayout = (LinearLayout) container.getChildAt(0);
layouts.push(linearLayout);
container.removeAllViews();
container.addView(createNextView());
}
});
}
private LinearLayout createNextView(){
LinearLayout linearLayout = new LinearLayout(this);
return linearLayout;
}
}
Thank you all for your comments.
I haven't test Cyrus answer. I will post my answer as well in case someone has the same problem in Xamarin.Android since I am using it.
I fixed it like this:
I added this in next button click event and previous button click event:
AddQuestionsLinearLayoutToPagesLinearLayoutsList();
And the AddQuestionsLinearLayoutToPagesLinearLayoutsList() body is, of course you can change it the way you want:
private void AddQuestionsLinearLayoutToPagesLinearLayoutsList()
{
//Create a new linear layout to add views into it and saved it in _mPagesLinearLayouts list at index _mPageIndex
LinearLayout savedLinearLayout = new LinearLayout(this);
//List of _mQuestionsLinearLayout views
List<View> views = new List<View>();
//Add all _mQuestionsLinearLayout to views list
for (int i = 0; i < _mQuestionsLinearLayout.ChildCount; i++)
{
views.Add(_mQuestionsLinearLayout.GetChildAt(i));
}
//Remove views from main questions linear layout
_mQuestionsLinearLayout.RemoveAllViews();
//Add all views from the views list to savedLinearLayout
for (int i = 0; i < views.Count; i++)
{
savedLinearLayout.AddView(views[i]);
}
//Add savedLinearLayout to _mPagesLinearLayouts list at index _mPageIndex
_mPagesLinearLayouts.Insert(_mPageIndex, savedLinearLayout);
}
In creating views method:
if(NeedToCreateViews())
{
//create views here and add it to _mQuestionsLinearLayout
}
//Get all saved views and add it to _mQuestionsLinearLayout
else
{
//List of _mPagesLinearLayouts[_mPageIndex] LinearLayout views
List<View> views = new List<View>();
//Add all _mPagesLinearLayouts[_mPageIndex] LinearLayout to views list
for (int i = 0; i < _mPagesLinearLayouts[_mPageIndex].ChildCount; i++)
{
views.Add(_mPagesLinearLayouts[_mPageIndex].GetChildAt(i));
}
//Remove all views from _mPagesLinearLayouts[_mPageIndex] linear layout
_mPagesLinearLayouts[_mPageIndex].RemoveAllViews();
//Remove the linear layout at index _mPageIndex
_mPagesLinearLayouts.RemoveAt(_mPageIndex);
//Add all views from views list to _mQuestionsLinearLayout
for (int i = 0; i < views.Count; i++)
{
_mQuestionsLinearLayout.AddView(views[i]);
}
}

Different On-clicks for programmatically added buttons

I was looking through some topics but couldn't find exact answer or at least couldn't get it right. What happens in the code is that i create one button for each row in my database and each button is supposed to have an OnClick that sends us to another activity along with some values(each button is supposed to have different value) but in the end it seems like i get the same value for all of my buttons which makes me think that it only creates 1 view for all of the buttons.
Cursor przepis = bazaUzytkownikow.rawQuery("SELECT * FROM przepisy", null);
int liczba_wierszy = przepis.getCount();
przepis.moveToPosition(0);
for (int i = 0; i < (liczba_wierszy/4)+1; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++) {
if((przepis.moveToPosition((i*4)+j)!=false))
{
nrPrzepisu=(i*4)+j;
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(115, 60));
btnTag.setText(przepis.getString(przepis.getColumnIndex("nazwa")));
btnTag.setTextSize(10);
btnTag.setId(j + 1 + (i * 4));
btnTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View t) {
// TODO Auto-generated method stub
Intent IdzPrzepis = new Intent(getApplicationContext(), DodajPrzepis.class);
IdzPrzepis.putExtra("ID_uzytkownika", ID_uzytkownika);
IdzPrzepis.putExtra("nr_Przepisu", nrPrzepisu);
startActivity(IdzPrzepis);
}
});
row.addView(btnTag);
}
}
layout.addView(row);
}
To make the code more clear for you - bazaUzytkownikow is my database, liczba_wierszy is the number of the rows that i got. I move the cursor to the beginning since it's where i want to start and i proceed to "cut" my data using 2 loops. I am aiming for 4 buttons in 1 row.
The part that i think doesn't work is the OnClick method where i want my button to switch activity and send nrPrzepisu which is basically adding a connection between my button and proper row in the database (In the other activity i want to set text, reading rows from database depends on which button you click).
I checked the other activity and it seems to be reading same nrPrzepisu everytime which usually equals the last value of nrPrzepisu=(i*4)+j when loops finish and it made me think that i somehow need to make different views for each button.
you are passing the same object to each onClick then changing that object with the next iteration. In the end all the onClicks have the same nrPzepisu object and it is returning the value which is whatever is last in this example.
int nrPrzepisu = (i*4) + j;
This way you aren't passing the same object into all the onClicks.

OnClick button ,create a layer in the same form. Android

I m very new to Android.
I want to create a dynamic OnClick button functionality.
OnClick of "+" above , it should create a other layer , like this below.
My confusion , my entire design UI is in layout.xml.
How we can we include another layer in our UI on OnClick of "+" button.
Any input would be helpful.
Thanks !!!
You could do this programatically. XML is for static layouts.
Excuse my pseudo Android:
private LinearLayout root;
public void onCreate(Bundle b){
LinearLayout root = new LinearLayout(this);
root.addChild(createGlucoseReadingView());
setContentView(root);
}
private View createGlucoseReadingView() {
LinearLayout glucoseRoot = new LinearLayout(this);
glucoseRoot.addChild(new TextView(this));
return glucoseRoot;
}
public void onPlusClick(View button){
root.addChild(createGlucoseReadingView());
}
Something along those lines, I've obviosuly left out formatting and adding the layout params to the views, but you get the idea.
In your XML have one Vertical Linear Layout to add and remove EditTexts at runtime, Here I have shown you code I have used in my demos. To handle and maintain the usage.
Onclick of your Add and Minus button click
public void onClick(View view) {
super.onClick(view);
switch (view.getId()) {
case R.id.btnadd:
createTextview(counter);
counter++;
if (counter > 3) {
btnAdd.setVisibility(View.GONE);
btnRemove.setVisibility(View.VISIBLE);
}
break;
case R.id.btnremove:
removeView(counter);
txtoption[counter - 1] = null;
counter--;
if (counter < 3) {
btnAdd.setVisibility(View.VISIBLE);
btnRemove.setVisibility(View.GONE);
}
break;
}
}
Functions to create and remove view
private void createTextview(int index) {
txtoption[index] = new EditText(this);
txtoption[index].setSingleLine(true);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
param.bottomMargin = 10;
txtoption[index].setLayoutParams(param);
txtoption[index].setBackgroundResource(R.drawable.textbox);
txtoption[index].setTypeface(ttfDroidSherif);
lnpolloptions.addView(txtoption[index]);
}
private void removeView(int index) {
lnpolloptions.removeView(txtoption[index - 1]);
}
Your vertical LinearLayout to contain all the edittext childs
LinearLayout lnpolloptions = (LinearLayout) findViewById(R.id.lnpolloptions);
Arrays of edittext to be created of removed at runtime
private EditText[] txtoption = new EditText[4];
Onclick of submit to get value from each textbox
int length = txtoption.length;
for (int i = 0; i < length; i++) {
if (txtoption[i] != null) {
Log.i("Value",""+txtoption[i].getText());
}
}

Dynamic Add Buttons in LinearLayout, Button click event Issue

I've developed one Android application which uses a HorizontalScrollView, and the HorizontalScrollView has one child as a LinearLayout.
Now I want to add buttons on LinearLayout at Runtime means dynamically.
I added the button successfully, But the problem is that my button click event does not work in Android.
ArrayList listClassItems = objCompany.getListClassItems();
Button[] btnCategory = new Button[listClassItems.size()];
for(int i=0;i<listClassItems.size();i++)
{
System.out.println("OTHER_CLASS LENGTH : " + listClassItems.size());
System.out.println("CLASS ID : " + listClassItems.get(i).getClassId());
System.out.println("CLASS NAME : " + listClassItems.get(i).getClassName());
btnCategory[i] = new Button(myContext);
btnCategory[i].setId(i);
btnCategory[i].setTag(listClassItems.get(i).getClassId());
btnCategory[i].setText(listClassItems.get(i).getClassName());
btnCategory[i].setClickable(true);
btnCategory[i].setPadding(10,10,10,10);
LayoutParams layParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
if(i!=0 || i!=listClassItems.size()-1)
{
layParams.leftMargin = 10;
layParams.rightMargin = 10;
}
tabRowBottom.addView(btnCategory,layParams);
btnCategory[i].setOnClickListener(null);
tabRowBottom.addView(btnCategory[i]);
btnCategory[i].setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(myContext, "=== Button CLICKED ===",Toast.LENGTH_SHORT).show();
btnCategory.setBackgroundColor(Color.BLACK);
}
});
}
Instead of an array, create a list of buttons, you can create buttons and set their id, tags and onclicklistenners like this and add them to the button list:
buttonList = new ArrayList<Button>();
for (int i=0;i<5;i++){
Button button = new Button(getApplicationContext());
button.setOnClickListener(customListenner);
button.setId(i);
button.setTag(i);
myLayout.addView(button);
buttonList.add(button);
}
and when you need to use the button again, just call with their id or tags from the list.
If you need different listenners, you can control them by using the unique tag check in if function and declare another action.
This is the method that I always use when I create dynamic views programmatically.

Categories

Resources