Android, how to create layout from code - android

I have a problem. In my app I have an Activity where I need to create x Button, then add to all this Button an ActionListener that start another activity.
In details. I have a Database table where I store x name.
In my Activity I need to have a Button for each name stored in the db and to add to each button an actionListener so when you click it it start another activity (which is the same for all buttons) with a putExtra String (which is unique for each button).
I thought that I can get all the name via an AsyncTask. But I can't figure out how to add the Buttons to the basic layout and add the Action Listener to them.
Anyone can help?
P.S. The "putExtra" String is the name I got from the db. So I also thought to get them in String[] array. Then create a Button[] array (don't know if it's possible) and create a new Button() foreach element of the String[] array. Then foreach element in the Button[] array I would putExtra the relative index String. Like Button[0] have as a putExtra the String[0], Button[1] the String[1] and so on.

The solution is a RecyclerView layout with the listener that you can find at this thread

Related

How to get user inputs from multiple fragments added to single activity

As shown in image, I have added the same fragment 5 times in activity by performing on click operation on ADD FRAGMENT button:
Now I want to get all user entered data from those 5 edittext on click of GET DATA button. Is this possible?
(Both buttons is in Main Activity)
First we need to traverse to the dynamic edit text linear layout and then only we can count how many edit texts are avaliable then read the values of corresponding edit text :)
//step:1
LinearLayout layout = (LinearLayout)findViewById(R.id.LinearDynamicEditTextID);// change as your //dynamic EditTexts' parent Linear Layout id.
//step:2
for(int i=0;i<layout.getChildCount();i++)// Count how many children avaliable
{
View v = (View)layout.getChildAt(i);// get the child view
if (v instanceof EditText)// verifying whether the child is EditText for secure.
{
EditText editText = (EditText)layout.getChildAt(i);//thats it.
Log.w("Edit Text "+i+" Value" , editText.getText());
}
}
Now I want to get all user entered data from those 5 edittext on click
of GET DATA button. Is this possible?
Answer: Yes, this is possible.
How?
Solution: You can make public variables in the activity and you can access those variables from the fragment which you are adding. On click of GET DATA button, you can read those variables set by the different fragment and show them in the same Activity or in different Fragment.

Android Studio - How to create a back button on randomly selected strings?

I have an array of strings that I am displaying in my activity using a random number every time a button called Next is clicked. How do I create another button called Back, that simply displays the previously or last generated strings in the order they were randomly generated?. So that instead of clicking next to generate another string, I can click Back at any time to view all the previous strings one by one and then click Next again to continue the random selection.
Create a array list of String like
ArrayList<String> arraylist= new ArrayList<String>();
after that using add function add the strings that you are displaying in your activity using a random number every time a button called Next is clicked.
arraylist.add("your_randomly_generated_string");
now your randomly generated string will stored in arraylist one by one
This will store the string now how to retrieve?
following steps may help you not sure ...
While adding the randomly generated string into arraylist initialize the int variable with zero and increment its value after each adding...
public int indexvalue=0;
on your Onclick of NEXT button add two lines
arraylist.add("your_randomly_generated_string");
indexvalue++;
And now on the Onclick of BACK Button Use indexvalue to retrieve...
String data = arraylist.get(indexvalue);
indexvalue--;
here Decremented the indexvalue to retrieve the previous string..
hope you'll understand what im trying to say little bit of lengthy explanation ...

How to display database contents in a new activity at onclick of a button?

thing is, if i click a button GETDATA - then a new activity starts new.java and i want it to display the contents of my database.
in this new.java i have strings name and id. and i have lined database and this class, such that name and id have the corresponding details that i want to display.
now the ques is, how to display them ?
if i have a
EditText ta;
ta=findViewBy(R.id.edittext1);
ta.setText(name);
should this work ?
it isnt working. also, i dont want user to change the contents and want to make this filed un-editable.
should i use
TextView tf;
ta=findViewBy(R.id.textview1);
tf.setText(name);
?
even this isnt happening.
what is the procedure for this ?
it is done by using append.
TextView ta;
ta=(TextEdit)findViewById(Ri.id.textView1);
String a=getData(1); // goto database, id is 1
ta.append(a);

Updating Text View with a list of values in android app widget

I have an app widget that does some stuff in AsyncTask's doInBackground() method and the final steps are to get the list of store names, that looks like this:
Store storeObject=store.getStores().getItems().get(0).getStore();
String name= storeObject.getName();
//name of the store
return name;
Right now I am getting the name at 0th position.
Now I want all the names of the stores (that are 27 in my code being fetched) in my widget text view one by one when you click a next button.
So how should I pass this name in my remote views back in onPostExecute() of AsyncTask.
updateViews.setTextViewText(R.id.text_view, name);
updateViews.setOnClickPendingIntent(R.id.next, pendingIntent);
Where should I use a for loop? in doInBackground or onPostExcecute or both? How can I use loop in remoteviews and update it? Please please help. I am a newbie.
I got it. Its a list so I generated an index that's randomly updated, no need of loop.

How can Dynamic Add and Remove View In android ? And this is also Store even if I closed the Application

In my Application I want to Add and Remove View (like Button, or Textview, Checkbox ) by Coding (Programming ).
In Details:
I have One EditText and One Add Button. User Enter Anything in EditText and Press the Add Button then this one is added in bellow LinearLayout, and whether User click on his/her added Button it will going to next LinearLayout.
I get sucess upto this.
but when user click the button in second LinearLayout then it will come back on first Linearlayout. I am getting error Here, i don't know where I made a Mistake.
And I also facing Problem about how can I Store this all. Like User Add 5 Button and closed the application and whenever he/she came back to application I need whatever he/she previously added.
Here is what i done.
http://dynamicandroidview.blogspot.com/2011/12/how-to-add-view-in-android-by-coding.html
Try to create a database table with minimum 2 columns in your case it will be id and buttonText.
Now when user clicks on the add button it will save text to the database and will create the button dynamically below any buttons which are already created before or as a new button.
Now in your onCreate method get the count of text thats stored in database.Some thing like the following code:
DB getData = DB.getInstance();
getData.open(this);
ArrayList<TextHolder> getList = new ArrayList<TextHolder>();
getList = getData.getAllTextFromGeT();
getData.close();
x = genList.size();
Here x will be the number/count of elements that are already stored in the database.Now you can another int say i and using this i and x in the for loop you can create buttons dynamically.
Inside the loop you can do something like the following to get text for all the buttons that are being created:
TextHolder firstOne = getList.get(i);
String text = firstOne.getText();
You will also need class with getters and setters method in order to convert DB elements into objects.Like in the above code getText() is our getter method which is getting elements from database and returning it here.
here text will be the text of the button.
So every-time users starts the application he will see all the buttons that he created when he ran the application before and newly added button will appear on the spot and also will be stored in the database for future retrieval.
Remember we are just storing text of the button and assigning it unique id which helps us to create the buttons.Hope this helps

Categories

Resources