Menus

Wednesday, 8 February 2017

Add a row to the table


Adding Table rows Dynamically in Android

Display Data in GridView in Android

How to use TextView to display GridView item text in Android ?


Output Form like this





Solutions:

Layout file like:


<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.root.mymysqlapp.Table2Activity">


<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3d455b"
android:layout_alignParentLeft="true" >

<HorizontalScrollView
    android:id="@+id/hscrll1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_gravity="center"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TableLayout
            android:id="@+id/table_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" >
        </TableLayout>
    </RelativeLayout>
</HorizontalScrollView>
</ScrollView>


</RelativeLayout>


Code like this :

Create an init() function and point the table layout. Then create the needed rows and columns. Call init function in your onCreate method:



package com.example.root.mymysqlapp;



import android.graphics.Color;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Gravity;

import android.widget.TableLayout;

import android.widget.TableRow;

import android.widget.TextView;

public class Table2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table2);


        init();

    }


    public void init() {

        TableLayout stk = (TableLayout) findViewById(R.id.table_main);
        TableRow tbrow0 = new TableRow(this);

        TextView tv0 = new TextView(this);
        tv0.setText(" Sl.No ");
        tv0.setTextColor(Color.WHITE);
        tbrow0.addView(tv0);

        TextView tv1 = new TextView(this);
        tv1.setText(" Student Name ");
        tv1.setTextColor(Color.WHITE);
        tbrow0.addView(tv1);

        TextView tv2 = new TextView(this);
        tv2.setText(" Class ");
        tv2.setTextColor(Color.WHITE);
        tbrow0.addView(tv2);

        TextView tv3 = new TextView(this);
        tv3.setText(" Total Fee ");
        tv3.setTextColor(Color.WHITE);
        tbrow0.addView(tv3);

        stk.addView(tbrow0);

        for (int i = 1; i <= 25; i++) {
            TableRow tbrow = new TableRow(this);

            TextView t1v = new TextView(this);
            t1v.setText(""+i);
            t1v.setTextColor(Color.WHITE);
            t1v.setGravity(Gravity.CENTER);
            tbrow.addView(t1v);

            TextView t2v = new TextView(this);
            t2v.setText("Student " + i);
            t2v.setTextColor(Color.WHITE);
            t2v.setGravity(Gravity.CENTER);
            tbrow.addView(t2v);

            TextView t3v = new TextView(this);
            t3v.setText("1-A");
            t3v.setTextColor(Color.WHITE);
            t3v.setGravity(Gravity.CENTER);
            tbrow.addView(t3v);

            TextView t4v = new TextView(this);
            t4v.setText("1500.00");
            t4v.setTextColor(Color.WHITE);
            t4v.setGravity(Gravity.CENTER);
            tbrow.addView(t4v);

            stk.addView(tbrow);
        }

    }

}

Above application expline working of TableLayout .





How add and delete rows to table layout in java programically


How to delete table row in table layout in android


Layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/table"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

</TableLayout>
Activity:
public class TableLayoutActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_layout);
    final TableLayout tableLayout = (TableLayout) findViewById(R.id.table);

    for (int i = 0; i < 5; i++) {
        // Creation row
        final TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

        // Creation textView
        final TextView text = new TextView(this);
        text.setText("Test" + i);
        text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

        // Creation  button
        final Button button = new Button(this);
        button.setText("Delete");
        button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TableRow parent = (TableRow) v.getParent();
                tableLayout.removeView(parent);
            }
        });

        tableRow.addView(text);
        tableRow.addView(button);

        tableLayout.addView(tableRow);
    }

}
}


Inserting table row beginning of table layout in java programmatically


tbl.addView(View child, int index)

child contains rows object and index contains for add row position in table layout.

Example

tbl.addView(row,0);  // added first row
tbl.addView(row,1);  // added second row
.
.
tbl.addView(row);  // added row in end of table layout.



Dynamically add imagebutton programatically android


ImageButton b1 = new ImageButton(this);
b1.setImageResource(R.drawable.delete);
b1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final TableRow parent = (TableRow) v.getParent();
        stk.removeView(parent);
    }
});

tbrow.addView(b1);




How do I get values from a dynamically created android TableRow?



getChildAt to get a hold of the two TextViews and get the data:

tr.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
      TableRow t = (TableRow) view;
      TextView firstTextView = (TextView) t.getChildAt(0);
      TextView secondTextView = (TextView) t.getChildAt(1);
      String firstText = firstTextView.getText().toString();
      String secondText = secondTextView.getText().toString();
   }
});


Solution for the get values of selected row on clicked button in table row.


ImageButton b1 = new ImageButton(EntriesActivity.this);
b1.setImageResource(R.drawable.delete);
b1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
b1.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {
        final TableRow parent = (TableRow) v.getParent();

        //code for remove concerned row
        //stk.removeView(parent);
        TextView items = (TextView) parent.getChildAt(7);

        Toast.makeText(getApplicationContext(), items.getText().toString() ,Toast.LENGTH_SHORT).show();


    }
});
row.addView(b1);



How to hide a particular column in Android TableRow



Try to Use setVisibility(View.GONE); 
t3v.setVisibility(View.GONE);

TextView id = new TextView(EntriesActivity.this);
id.setText(content.get("id").toString());
id.setTextColor(Color.WHITE);
id.setVisibility(View.GONE);
row.addView(id);




Get total RowCount for TableLayout in Android



Through this code you can get the total rowCount for TableLayout
TableLayoutName.getChildCount();

Examples

TableLayout tb = (TableLayout) findViewById(R.id.table_main);

if(tb.getChildCount()>1){

.....Code Here.....

}



Adding space between columns of a TableLayout


 Programetically  add

  TextView count = new TextView(RptsalesreportdisplayActivity.this);
  count.setText(obj.get("count").toString());
  count.setTextColor(Color.WHITE);
  count.setPadding(0,0,30,0);
  row.addView(count);


<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="0"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:gravity="center"
        android:text="col1" />   ...........


Set tablelayout row background color programmatically


tbrow0.setBackgroundResource(R.color.cgreen);

Wednesday, 1 February 2017

Update data From Other Table


Update one MySQL table with values from another



My customer table looks like:



And the customer_rate table looks like:



I want to update prize in customer with the id from customer_rate based on prize 
Action : customer.prize = customer.qty * customer_rate.prize
The updated table will hopefully look like:


I have a query that works

UPDATE customerINNER JOIN customer_rate ON (customer.cus_id = customer_rate.id)SET customer.prize = customer.qty * customer_rate.prize



add where condition 

UPDATE customerINNER JOIN customer_rate ON (customer.cus_id = customer_rate.id)SET customer.prize = customer.qty * customer_rate.prize where customer.id = 1




Wednesday, 4 January 2017

Android Radio Group and Radio Button Operations


What is Radio Group ?

A RadioGroup class is used for set of radio buttons.

If we check one radio button that belongs to a radio group, it automatically unchecks any previously checked radio button within the same group.

Example of using Radio Group

Create the layout of the Activity


<RadioGroup
    android:id="@+id/rg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/rad1"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/rad2"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
       android:id="@+id/rad3"
        android:checked="false" />

</RadioGroup>


* Radio Group orientation as two types are vertical and horizontal

Android:placing the radio buttons horizontally


android:orientation="horizontal"

Android:placing the radio buttons vertically

android:orientation="vertical"

Code the Activity

At this point we will show how we can handle the change of a radio button, that belongs to a RadioGroup.

package com.example.root.calendar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class EntriesActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private Button btnSaveall;
    private RadioButton rad1, rad2, rad3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entries);


        radioGroup = (RadioGroup) findViewById(R.id.rg);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

// find which radio button is selected

if(checkedId == R.id.rad1) { Toast.makeText(getApplicationContext(), "choice: Radio1 Selected",Toast.LENGTH_SHORT).show(); } else if(checkedId == R.id.rad2) { Toast.makeText(getApplicationContext(), "choice: Radio2 Selected",Toast.LENGTH_SHORT).show(); } else if(checkedId == R.id.rad3) { Toast.makeText(getApplicationContext(), "choice: Radio3 Selected",Toast.LENGTH_SHORT).show(); } } }); rad1 = (RadioButton) findViewById(R.id.rad1); rad2 = (RadioButton) findViewById(R.id.rad2); rad3 = (RadioButton) findViewById(R.id.rad3); btnSaveall = (Button)findViewById(R.id.btnSaveall); btnSaveall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int selectedId = radioGroup.getCheckedRadioButtonId();

// find which radioButton is checked by id

if(selectedId ==rad1.getId()) { Toast.makeText(getApplicationContext(),"Radio 1 Selected" ,Toast.LENGTH_SHORT).show(); } else if(selectedId == rad2.getId()) { Toast.makeText(getApplicationContext(),"Radio 2 Selected" ,Toast.LENGTH_SHORT).show(); } else if(selectedId == rad3.getId()) { Toast.makeText(getApplicationContext(),"Radio 3 Selected" ,Toast.LENGTH_SHORT).show(); } } }); } }

Now let’s have a look at the code above. When a checked radio button is changed in its group, OnCheckedChangeListener is invoked in order to handle this situation. The onCheckedChanged() method of this interface, includes the unique id of the radio button that was selected and caused the invoke of the callback.
In this example we will show you another way of selecting the choice information (for example when a button is pressed). This can be done through getCheckedRadioButtonId(), which is a public function of RadioGroup class. This method returns the unique id of the radio button that is chosen from the group. You can have a look at the code to see how you can handle both situations.
Of course Android system provides us a more dynamic way of changing and handling the attributes of the application views. As a prerequisite is to map every view with the unique id component of the XML. This can be done viafindViewById() method.



Radio Buttons Set checked state through code


You can use either radioButton.setChecked(true); or radiobutton.setSelected(true);

private RadioButton radA; //declare 
.
.
.
rad1 = (RadioButton) findViewById(R.id.rad1);
rad1.setChecked(true);



Hiding a RadioButton in Android


you can hide the particular radio button this way
RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio);
myRadioButton.setVisibility(View.INVISIBLE);
or if you use View Gone menas radio button hide with sapce
RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio);
myRadioButton.setVisibility(View.GONE);

Hiding a radio group

RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_quiz_answers);
myRadioGroup.setVisibility(View.Visible);


Hi use like this.
RadioButton myRadioButton = (RadioButton) findViewById(R.id.my_radio_button_id);
myRadioButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            myRadioButton.setVisibility(View.INVISIBLE);
        }
    });
OR
  <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" 
        android:visibility="invisible"/>

radioGroup1.setVisibility(View.GONE);
radioGroup2.setVisibility(View.GONE);
radioGroup3.setVisibility(View.GONE);
radioGroup2.setVisibility(View.VISIBLE);


How to set the background colour of a RadioButton?


        <RadioGroup
            android:id="@+id/eligibility"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:orientation="horizontal"
            android:layout_weight=".5" >

            <RadioButton
                android:id="@+id/eligibilityYes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:background="@drawable/checkbox_background"
                android:text="yes" />

            <RadioButton
                android:id="@+id/eligibilityNo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/checkbox_background"
                android:text="No" />
        </RadioGroup>

your checkbox_background.xml file put image in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false" android:drawable="@drawable/radio_rad" />
    <item android:state_checked="true" android:drawable="@drawable/radio_green" />

</selector>

Hi use like this:

Text color  - android:textColor="@color/white"
Backgroud - android:background="@color/red"


try this...
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
            if(radioButton.getText().equals("yes")) {
                radioButton.setBackgroundColor(Color.GREEN);
            } else {
                radioButton.setBackgroundColor(Color.RED);
            }
        }
    });



How to check if a radiobutton is checked in a radiogroup in Android?


If you want to check on just one RadioButton you can use the isChecked function
if(radioButton.isChecked())
{
  // is checked    
}
else
{
  // not checked
}
and if you have a RadioGroup you can use
if (radioGroup.getCheckedRadioButtonId() == -1)
{
  // no radio buttons are checked
}
else
{
  // one of the radio buttons is checked
}