Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Friday, 4 November 2016

Tab Layout Example in Android

In this example, we will demonstrate the use of tab layout. Each tab contains a simple message. we include 3 tabs in this example.

How to create tab layout in android. Let's see the example

- Create new project
- To set 3 tab in layout create 3 blank activity in this example.

File Name : MainActivity.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.tabbarexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.app.TabActivity;
import android.content.Intent;
import android.widget.TabHost;

public class MainActivity extends TabActivity {

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

  TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;
 
        intent = new Intent().setClass(this, Tab1.class);
        spec = tabHost.newTabSpec("Tab 1").setIndicator("Tab 1")
                      .setContent(intent);
        tabHost.addTab(spec);
 
        intent = new Intent().setClass(this, Tab2.class);
        spec = tabHost.newTabSpec("Tab 2").setIndicator("Tab 2")
                      .setContent(intent);
        tabHost.addTab(spec);
 
        intent = new Intent().setClass(this, Tab3.class);
        spec = tabHost.newTabSpec("Tab 3").setIndicator("Tab 3")
                      .setContent(intent);
        tabHost.addTab(spec);
 
    
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

 
}

File name : activity_main.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:id="@+id/linearLayout1"
        android:layout_height="match_parent"
        android:orientation="vertical">
 
        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs">
        </TabWidget>
 
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent">
        </FrameLayout>
    </LinearLayout>
</TabHost>

  • Tab1 Activity Code
File Name : Tab1.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.tabbarexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Tab1 extends Activity {

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

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.tab1, menu);
  return true;
 }

}

File Name : tab1.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<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"
    tools:context=".Tab1" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="30dp"
        android:text="TAB 1 SCREEN" />

</RelativeLayout>


  • Tab2 Activity Code

File Name : Tab2.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.tabbarexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Tab2 extends Activity {

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

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.tab2, menu);
  return true;
 }

}

File Name : tab2.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<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"
    tools:context=".Tab2" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="30dp"
        android:text="TAB 2 SCREEN" />

</RelativeLayout>

  • Tab3 Activity Code
File Name : Tab3.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.tabbarexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Tab3 extends Activity {

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

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.tab3, menu);
  return true;
 }

}

File Name : tab3.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<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"
    tools:context=".Tab3" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="30dp"
        android:text="TAB 3 SCREEN" />

</RelativeLayout>



Output :

  

Tuesday, 1 November 2016

Capture div into image using html2canvas in php

In this example, this scripts will allow you to take screenshot of the div directly on the user browser.

In div, what you write content in this tag it will be convert in the image and can save image of div as a screenshot. If you put image in div tag, then we can also take screenshot of image what we mention between div tag.

 So, How it is possible programmatically ? Let's see the example :-




File Name : index.php


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="js/html2canvas.js"></script>
<script type="text/javascript" src="js/jquery.plugin.html2canvas.js"></script>

<h2>html2canvas With JavaScript and PHP</h2>

<form method="POST" enctype="multipart/form-data" action="save.php" id="form1">
 <input type="hidden" name="img_value" id="img_value" value="" />
</form>
<table>
 <tr>
  <td valign="top" style="padding: 10px;">
            <b>Div:</b>
  </td>
  <td>
   <div id="target" style="border: 1px solid #CCC;padding: 5px;margin: 5px;">
    <div>
    <p>PHP is a server-side scripting language designed primarily for web development but is also used as a general-purpose programming language.</p>
    </div>
   </div>
  </td>
 </tr>
 <tr>
  <td colspan="2">
   <table width="100%">
    <tr>
     <td>
      <input type="submit" value="Screenshot" onclick="capture();" />
     </td>
    </tr>
   </table>
  </td>
 </tr>
 
</table>
<script type="text/javascript">
 function capture() {
  $('#target').html2canvas({
   onrendered: function (canvas) {
                //Set hidden field's value to image data
    $('#img_value').val(canvas.toDataURL("image/png"));
                //Submit the form1
    document.getElementById("form1").submit();
   }
  });
 }
</script>

File Name : save.php


1
2
3
4
5
6
7
8
9
<?php
$filterData=substr($_POST['img_value'], strpos($_POST['img_value'], ",")+1); //Get the base-64 string from data
$decodeData=base64_decode($filterData); //Decode the string
file_put_contents('screenshot.png', $decodeData); //Save the image
?>
<h2>Save the image and show to user</h2>
<?php
echo '<img src="'.$_POST['img_value'].'" />';
?>

Output:








  • If you want to put image in div , then just change this code in index,php file. See this code and change it.


1
2
3
4
5
<div id="target" style="border: 1px solid #CCC;padding: 5px;margin: 5px;">
    <div>
    <p>PHP is a server-side scripting language designed primarily for web development but is also used as a general-purpose programming language.</p>
    </div>
</div>

  • Put this below code instead of above code lines

1
2
3
4
<div id="target" style="border: 1px solid #CCC;padding: 5px;margin: 5px;">
    <div>
    <img src = "a.png"/> //image which is available in the main folder where code file is available
</div>

Download File : Click Here

Saturday, 29 October 2016

How to store image in database in Php

In this example, we are learning to how to store image in database using Php.

First of all, create a folder of images name where your code file available. After create a database with two column of id and name.

We are storing just path of the image and give id of the image in database.

Let's see the example that how it works :

file name : index.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<HTML>
<BODY>
<FORM METHOD="POST" ACTION="image_upload.PHP"  enctype="multipart/form-data">
 <pre>
  Example of Image Upload<br>
  ID   : <INPUT TYPE='TEXT' NAME='ID'> <br/>
  Image:<input name="uploadedfile" type="file" /> <br/>
  <INPUT TYPE="SUBMIT" VALUE="Insert"> <INPUT TYPE="RESET" VALUE="CLEAR">
 </pre>
</FORM>

</BODY>
</HTML>


file name : image_upload.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
 $id=$_POST['ID'];
 $conn=mysql_connect("localhost","root","");
 mysql_select_db("demo");

 $fn=basename($_FILES['uploadedfile']['name']);
 $target_path = "./images/";

 $target_path = $target_path.$fn; 

 $tfn=$_FILES['uploadedfile']['tmp_name'];

 
  $x=move_uploaded_file($tfn,$target_path);

 if($x) 
 {
  echo "<img src='images/".basename($_FILES['uploadedfile']['name'])."'</img> ";
    echo "<br>The file <u>[".basename($_FILES['uploadedfile']['name'])."</u>] has been uploaded<br>";
 }
 else
 {
     echo "There was an error uploading the file, please try again!";
  die;
 }

 $insert="insert into img values($id,'$fn')";

 mysql_query($insert) or die("INSERT ".mysql_error());
 mysql_query("commit") or die("commit fail ".mysql_error());
 
 echo "<br>Record Successfully Added"; 

?>
<a href="index.php">Back to Home</a>


Output :

 

Note :
database name : demo
table name : img
field name : id (varchar) , name (varchar)

Thursday, 27 October 2016

Delete selected row record from database in php

In this example , we are learning about how to delete selected row record from database in php.

Here, we are fetching record from database. If database not available then create a database and add records. then put link of delete record in each row.

Here, I create a database login_tbl  which have 3 column of id,name,molbile.

From first file , If you delete 1st record then id of the first record pass by query string on second page and delete selected record.

Let's see the example :

File name : index.php



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
 $servername= "localhost";
 $username = "root";
 $password="";
 $dbname="demo";
 $conn = new mysqli($servername,$username,$password,$dbname);  
?>
<FORM METHOD="POST" enctype="multipart/form-data">  
  <?php
   $sql = "SELECT *FROM login_tbl";
   $result = $conn->query($sql);

   echo "<table border='1' style='width:50%'><tr><th>ID</th><th>Name</th><th>Mobile</th><th>Delete</th></tr>";
   if ($result->num_rows > 0)
   {
     // output data of each row
     while($row = $result->fetch_assoc())
     {
      echo "<br><tr><td align='center'>".$row["id"]." </td><td align='center'>".$row["name"]."</td><td align='center'>".$row["mobile"]."</td><td align='center'><a href='delete.php?id=".$row["id"]."'>Delete</td><br>";
     }
   }
   else
   {
     echo "No data available";
   }
   echo "</table>";
  ?>
</FORM>


File name : delete.php



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
 $id= $_GET['id'];
 $cn=mysql_connect("localhost","root","");
 mysql_select_db("demo");
 $s1="delete from login_tbl where id=".$id;

 mysql_query($s1) or die("INSERT ".mysql_error());
 mysql_query("commit") or die("commit fail ".mysql_error());
 
 header("Location: index.php");
?>

Output :


  • Before delete record



  • After delete record


Wednesday, 26 October 2016

How to use Color Dialog Box in C#

In this example, we are learning that how to use color dialog box control in c#.

First of all , we need to add Color Dialog Control in the form. In this example, one multi line textbox and two buttons were added in the form.



After adding the control, we are learning that how to change background color of textbox using color dialog box and also change font color of textbox.

When you click on the button color dialog box will be open and then you choose color. After selecting color, color will be change of textbox background color and font color.

Let's see the example :



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void background_color_Click(object sender, EventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            colorDlg.AllowFullOpen = true;
            colorDlg.AnyColor = true;
            colorDlg.SolidColorOnly = false;
            colorDlg.Color = Color.Red;
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.BackColor = colorDlg.Color;
            }
        }

        private void font_color_Click(object sender, EventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            colorDlg.AllowFullOpen = true;
            colorDlg.AnyColor = true;
            colorDlg.SolidColorOnly = false;
            colorDlg.Color = Color.Red;
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.ForeColor = colorDlg.Color;
            }
        }
    }
}

Output:

ColorDialogBox1

ColorDialogBox

Tuesday, 25 October 2016

How to encrypt and decrypt password in php

In this example , we are learning how to encrypt and decrypt password in php.   

Php Provide md5() function to encrypt password. Using md5() function we can store data in encrypted format and can secure password using md5().

Let's see the Example :-

File Name : index.php


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
$password = "Hello";
$encrypt = md5($password);
echo "Your password before encrypted is : ".$password."<br/>";
echo "Encrypted password [HELLO] : ".$encrypt."<br>";
if($encrypt == md5($password))
{
 echo "Your Decrypt password : ".$password;
}
?>

Output :



Saturday, 22 October 2016

Add text from textbox in listbox Column in C#

In this example, We are learning how to add items in listbox from textbox on button click.

Let's see the example :

File Name : Form1.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Item_Add_ListBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            item_listbox.Items.Add(txt_item_add.Text);
            txt_item_add.Text = "";
            
            //item_listbox = name of listbox
            //txt_item_add = name of textbox


        }
    }
}

Output :

Listbox add item

ListBox Item add



Like us on Facebook

Site Visitor

Powered by Blogger.