Pagination with out using buttons in android - android

How to do Pagination in Android? I have tried with few pagination and most of them use buttons. Is there any separate feature in Android other than using buttons. I need page number in bottom, as how we use to get in our Google page..

you can try this example
ListView Pagination Ex-2
Here the logic is very simple. just add the page numbers dynamically to the footer and load list items based on the number clicked
private void Btnfooter()
{
int val = TOTAL_LIST_ITEMS%NUM_ITEMS_PAGE;
val = val==0?0:1;
noOfBtns=TOTAL_LIST_ITEMS/NUM_ITEMS_PAGE+val;
LinearLayout ll = (LinearLayout)findViewById(R.id.btnLay);
btns =new Button[noOfBtns];
for(int i=0;i<noOfBtns;i++)
{
btns[i] = new Button(this);
btns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));
btns[i].setText(""+(i+1));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(btns[i], lp);
final int j = i;
btns[j].setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
loadList(j);
CheckBtnBackGroud(j);
}
});
}
}

Related

How to add buttons dynamically?

I want to add buttons dynamically. I am adding multiple buttons dynamically but I want to add the buttons in following pattern:
[BUTTON1] [BUTTON2]
[BUTTON3] [BUTTON4]
[BUTTON5] [BUTTON6]
That means I want to add only 2 buttons in a row that is dynamically.
I have tried many options.
one of them is:
LinearLayout ll = (LinearLayout) findViewById(R.id.buttonlayout);
Button[][] buttonArray = new Button[count][count];
TableLayout table = new TableLayout(this);
for (int row = 0; row < count; row++) {
TableRow currentRow = new TableRow(this);
for (int button = 0; button < row; button++) {
Button currentButton = new Button(this);
// you could initialize them here
currentButton.setOnClickListener(this);
// you can store them
buttonArray[row][button] = currentButton;
// and you have to add them to the TableRow
currentRow.addView(currentButton);
}
// a new row has been constructed -> add to table
table.addView(currentRow);
}
and finally takes that new table and add it to your layout. ll.addView(table);
Note: count of button could be random.
How can I do that?
Use vertical LinearLayout in XML. Then programmatically create horizontal LinearLayout and add buttons in horizontal layout. For each line, create and add new horizontal layout.
XML:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonlayout">
</LinearLayout>
ACTIVITY:
public class dynamicButtons extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int numberOfRows = 3;
int numberOfButtonsPerRow = 2;
int buttonIdNumber = 0;
final LinearLayout verticalLayout= LinearLayout)findViewById(R.id.buttonlayout);
for(int i=0;i<numberOfRows;i++){
LinearLayout newLine = new LinearLayout(this);
newLine.setLayoutParams(params);
newLine.setOrientation(LinearLayout.HORIZONTAL);
for(int j=0;j<numberOfButtonsPerRow;j++){
Button button=new Button(this);
// You can set button parameters here:
button.setWidth(20);
button.setId(buttonIdNumber);
button.setLayoutParams(params);
button.setText("Button Name");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent is = new Intent(getApplicationContext(), someOtherApplication.class);
is.putExtra("buttonVariable", buttonIdNumber);
startActivity(is);
}
});
newLine.addView(button);
buttonIdNumber++;
}
verticalLayout.addView(newLine);
}
}
}
try this code:
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
for (int i=0; i<2; i++) { //number of rows
TableRow tr = new TableRow(this);
for (int j=0; j<2; j++) { //number of columns
Button b = new Button (this);
b.setText("Button:"+i+j);
b.setTextSize(10.0f);
b.setOnClickListener(this);
tr.addView(b, 30,30);
}
layout.addView(tr);
}
As your number of button is random u can use:
int total = 20; //random number of buttons
int column = 3; //specify the column number
int row = total / column;
now use the column and row value to dynamically show buttons
Do something like this:
LinearLayout ll_Main = new LinearLayout(getActivity());
LinearLayout ll_Row01 = new LinearLayout(getActivity());
LinearLayout ll_Row02 = new LinearLayout(getActivity());
ll_Main.setOrientation(LinearLayout.VERTICAL);
ll_Row01.setOrientation(LinearLayout.HORIZONTAL);
ll_Row02.setOrientation(LinearLayout.HORIZONTAL);
final Button button01 = new Button(getActivity());
final Button button02 = new Button(getActivity());
final Button button03 = new Button(getActivity());
final Button button04 = new Button(getActivity());
ll_Row01.addView(button01);
ll_Row01.addView(button02);
ll_Row02.addView(button03);
ll_Row02.addView(button04);
ll_Main.addView(ll_Row01);
ll_Main.addView(ll_Row02);
button04.setVisibility(View.INVISIBLE);
button04.setVisibility(View.VISIBLE);
Create a listview/recyclerview with custom item that hold 2 button as you mentioned in your question, than populate that listView with the buttons (inside the adapter if item index % 2 == 0 it will take the left position otherwise the right position).

How can get dynamic button text from another dynamic button OnClickListener event

I want get second dynamic button text from another dynamic button OnClickListener event:
Here is define some dynamic buutons:
LinearLayout lv=(LinearLayout)findViewById(R.id.lv);
for (int k = 1; k <= str[0].length(); k++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
btnTopEn = new Button(this);
btnTopEn.setId(k);
final int id_ = btnTopEn.getId();
btnTopEn.setText(" ");
lv.addView(btnTopEn, params);
btnTopEn = ((Button) findViewById(id_));
final Button finalBtnT = btnTopEn;
btnTopEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finalBtnT.setText("");
}
});
}
Now I want text of second button from OnClickListener Event:
TableLayout layout = (TableLayout)findViewById(R.id.TableL);
String stt="RZCEADHPTAUJTSFR";
int l=0;
for (int f=0; f<=1; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=7; c++) {
btnCEn = new Button (this);
String ss=(String.valueOf(stt.charAt(l)));
btnCEn.setText(ss);;
final Button finalBtnB = btnCEn;
btnCEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int m=2;
btnTopEn = ((Button) findViewById(m));
final Button finalBtnT = btnTopEn;
if (finalBtnT.getText().equals("")) {
String stGetText=finalBtnB.getText().toString();
finalBtnT.setText(stGetText);
break;
}
}
}
});
TableRow.LayoutParams lp = new TableRow.LayoutParams(100,100);
tr.addView(btnCEn, lp);
}
layout.addView(tr);
}
I wrote some code in OnClickListener event but none happen!
What is the value of str in the first loop ?
Also you are setting the text to a space .
btnTopEn.setText(" ");
And while checking you check for empty :
if (finalBtnT.getText().equals("")){
}
Try changing to
if (finalBtnT.getText().toString().trim().equals("")){
}

Adding onClick to buttons that are created programmatically

A TextView and Button are programmatically created and added to a pre-existing vertical layout making it look like a vertical list of views. These views are only created based off the user entering data into an edittext and that data being saved into an ArrayList.
How can I add an onClick function to my 'trash' buttons that are programmatically created that would allow them to remove the view they are associated with.
public static ArrayList<String> deckNameArray = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
for(int i = 0; i < deckNameArray.size(); i++)
{
LinearLayout layout = new LinearLayout(this);
if ((i % 2) == 0) {
layout.setBackgroundColor(Color.CYAN);
} else {
layout.setBackgroundColor(Color.WHITE);
}
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setPadding(10, 5, 10, 5);
layout.setWeightSum(5);
mainLayout.addView(layout);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 4f);
TextView deckName = new TextView(this);
deckName.setText(deckNameArray.get(i));
deckName.setTextColor(Color.BLACK);
deckName.setTextSize(18);
deckName.setGravity(Gravity.CENTER_VERTICAL);
deckName.setLayoutParams(textViewParams);
layout.addView(deckName);
LinearLayout.LayoutParams imageButtonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
ImageButton remove = new ImageButton(this);
remove.setImageResource(R.mipmap.trash);
if ((i % 2) == 0) {
remove.setBackgroundColor(Color.CYAN);
} else {
remove.setBackgroundColor(Color.WHITE);
}
remove.setLayoutParams(imageButtonParams);
layout.addView(remove);
}
remove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
See http://developer.android.com/reference/android/widget/Button.html
You can set a click listener on any View, using code similar to the sample below:
View myView;
myView.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
// TODO your logic here
}
}

Add ImageButton dynamically from Database not displayed

I'm here today for my soundboard app ! son to make it simple i have a G_Son object which is the controller of the "Son" model. i get the list of sound from my database (everything is fine until here) but then when I dynamically try to create ImageButtons and add them on my activity (manageLayout() ), I have absolutely nothing appearing on my activity ! not even a single button. So if you have any Idea, or want to give me a hand, I'm aware of any suggestion
private G_Son gson;
private OurNiceSoundPlayer soundPlayer;
private List<Son> sons;
RelativeLayout gameBoard;
private Son selectedSound;
private View.OnClickListener mSoundOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlayer.setSound(sons.get(Integer.parseInt(v.getTag().toString())));
Log.i("Board", "Clicked on ImgButton ->" + v.getTag());
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_board);
gson = G_Son.getInstance();
sons = gson.getSons(getApplicationContext());
soundPlayer = new OurNiceSoundPlayer(getApplicationContext());
gameBoard = (RelativeLayout) findViewById(R.id.soundboard);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);
manageLayout();
gameBoard.invalidate();
}
private void manageLayout() {
if (sons.size()>0)
{
int rawNbr = (int) Math.ceil((double) sons.size() / 3);
int currentSon = 0;
Son displayed = sons.get(currentSon);
for (int i = 0; i < rawNbr; i++)
{
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 3; j++)
{
ImageButton btnTag = new ImageButton(this);
if (displayed.getIsPerso()) {
File imgFile = new File(displayed.getPathImage());
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
btnTag.setImageBitmap(myBitmap);
} else {
btnTag.setImageResource(R.drawable.defaultimage);
}
}
else
{
int id = getResources().getIdentifier("com.example.m.sbst:drawable/" + displayed.getPathImage(), null, null);
btnTag.setImageResource(id);
}
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setOnClickListener(mSoundOnClickListener);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setTag(currentSon);
btnTag.setId(i);
row.addView(btnTag);
currentSon++;
if (currentSon>=sons.size())
{
break;
}
else
{
displayed = sons.get(currentSon);
}
}
gameBoard.addView(row);
}
}
}
Forgot to mention a size, moreover, after months of learning android, I should advice people to use a grid view to do so, it allows with custom layout to do event better than creating your own layout with dynamic image buttons

Approach to get values of a dynamically generated textView

I successfully created a dynamic TextView and a Button now when ever the button was clicked the value of a TextView changes.
But the problem is I have a final "submit button" outside a loop which should get the INDIVIDUAL values of each TextView and I cant think of a way how to do it can someone pls give me an approach for this thanks!. pls be nice..
code
Cursor citem= sdb.rawQuery("SELECT * FROM ITEM INNER JOIN CATEGORY ON item.categoryid = category.id where category.categoryname='"+fcat+"'", null);
ScrollView scrollView= new ScrollView(this);
LinearLayout mainLayout= new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
Button border = new Button(this);
border.setId(Integer.parseInt(cuser.getString(cuser.getColumnIndex("id"))));;
border.setText("ORDER");
while (citem.moveToNext())
{
byte[] blob =citem.getBlob(citem.getColumnIndex("itemimage"));
int id = Integer.parseInt(citem.getString(citem.getColumnIndex("id")));
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setTag(id);
int i;
ImageView iv = new ImageView(this);
iv.setId(id);
iv.setImageBitmap(dh.getPhoto(blob));
final TextView txtquantity = new TextView(this);
txtquantity.setId(id);
txtquantity.setText("0");
txtquantity.setTextSize(20);
final TextView txtprice = new TextView(this);
txtprice.setId(id);
txtprice.setText(citem.getString(citem.getColumnIndex("itemprice")));
txtprice.setTextSize(30);
ImageButton btn1 = new ImageButton(this);
btn1.setId(id);
final int id_ = btn1.getId();
btn1.setImageResource(R.drawable.ic_launcher);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int i = 0;
i=Integer.parseInt((String) txtquantity.getText())+1;
txtquantity.setText(String.valueOf(i));
totalprice.setText(String.valueOf(Integer.parseInt(totalprice.getText().toString())+(Integer.parseInt(txtprice.getText().toString())*1)));
}
});
ImageButton btn2 = new ImageButton(this);
btn2.setId(id);
final int id2_ = btn2.getId();
btn2.setImageResource(R.drawable.ic_launcher);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(Integer.parseInt((String)txtquantity.getText())<=0)
{
}
else
{
int i=0;
i= Integer.parseInt((String) txtquantity.getText())-1;
txtquantity.setText(String.valueOf(i));
totalprice.setText(String.valueOf(Integer.parseInt(totalprice.getText().toString())-(Integer.parseInt(txtprice.getText().toString())*1)));
}
}
});
linearLayout.addView(iv);
linearLayout.addView(txtprice);
linearLayout.addView(btn1);
linearLayout.addView(txtquantity);
linearLayout.addView(btn2);
mainLayout.addView(linearLayout);
}
mainLayout.addView(totalprice);
mainLayout.addView(border);
scrollView.addView(mainLayout);
setContentView(scrollView);
Not quite sure what problem you're having? If it's just reading all the TextViews you created in the loop, then you should just keep a list, and send it off for processing when you submit...
List<TextView> tv_list = new ArrayList<TextView>();
while(...){
//In loop..add your tv's
TextView some_tv = new TextView()
tv_list.add(some_tv);
...
}
//In the submit, send them off for processing...
private void process_tvs(List<TextView> tv_list){
for(TextView tv:tv_list){
//Assuming your tv's have numbers...
int val = Integer.valueOf(tv.getText().toString());
//do something....
}
}

Categories

Resources