Thursday, 22 January 2015

How to change Background color by selecting color from Listview

activitymain.xml file::>







<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:background="@drawable/bg"
    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.listviewexample.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:entries="@array/color" >
    </ListView>

</RelativeLayout>



Activity_second.xml file::>



<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.listviewexample.Second"
    android:id="@+id/rel1"
   
     >

</RelativeLayout>














Java File code 

MainACtivity.java


package com.example.listviewexample;



import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {
  ListView l1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<String> color=new ArrayList<String>();
        color.add("YELLOW");
        color.add("BLUE");
        color.add("RED");
        color.add("BLACK");
        color.add("GREEN");
       
       
        ArrayAdapter<String> ad=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, color);
       
    l1=(ListView) findViewById(R.id.listView1);
    l1.setAdapter(ad);
     l1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            String col;
            col= arg0.getItemAtPosition(arg2).toString();
            Intent i=new Intent(getApplicationContext(),Second.class);
            i.putExtra("color", col);
            startActivity(i);
       
        }
    });
   
    }

   
}










SecondActivity.java




package com.example.listviewexample;

import android.R.integer;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Second extends ActionBarActivity {
   TextView txt1;
   RelativeLayout r1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
         r1= (RelativeLayout) findViewById(R.id.rel1);
        Intent i= getIntent();
        String colr=i.getStringExtra("color");
   
     
      //txt1.setText("YOUR SELECTED COLOR IS "+colr);
     // int colc= Integer.parseInt(colr);
   
   
     // r1.setBackgroundColor(Color.BLACK);
     if(colr.equals("BLACK"))
      {
          r1.setBackgroundColor(Color.BLACK);
      }
      else if(colr.equals("BLUE"))
      {
          r1.setBackgroundColor(Color.BLUE);

      }
      else if(colr.equals("YELLOW"))
      {
          r1.setBackgroundColor(Color.YELLOW);

      }
      else if(colr.equals("GREEN"))
      {
          r1.setBackgroundColor(Color.GREEN);

      }
      else if(colr.equals("RED"))
      {
          r1.setBackgroundColor(Color.RED);

      }
      else
      {
          r1.setBackgroundColor(Color.CYAN);
         
      }
 
    }

   
}








Simple example of Polymorphisam in java

interface a{
   
    String CompanyName=null;
    public void set();
    public void get();
}

class b{
   
    public static void display()
    {
        System.out.println("THIS IS CLASS B");
    }
}
public class PolymorphisomExample extends b implements a{

    public static void main(String Dx[]){
       
        PolymorphisomExample PlEx=new PolymorphisomExample();
        b b1=new b();
        b.display();
       
    }

    @Override
    public void set() {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void get() {
        // TODO Auto-generated method stub
       
    }
}

how to create Interface example in java

interface cars{
    String CarName=null;
    int CarModelNo=0;
    public void display();
   
}
public class InterFaceExample implements cars{
     
    String  CarName="Maruti";
    @Override
    public void display() {
        // TODO Auto-generated method stub
        System.out.println("Car Name :"+CarName);
    }
    public static void main(String Dx[])
    {
       
        InterFaceExample IntEx=new InterFaceExample();
        IntEx.display();
    }

}

Simple Encapsulation example in java

class test
{
  String Name=null;
  int age;
  String occupation=null;
  public void set(String name,int age1,String occ)
  {
      Name=name;
      age=age1;
      occupation=occ;
  }
  public void get()
  {
   
      System.out.println("-------YOUR PERSONAL INFORMATION------");
      System.out.println("NAME :"+Name);
      System.out.println("AGE :"+age);
      System.out.println("OCCUPATION :"+occupation);
     
  }

}
public class EncapsulationExample extends test{
    public static void main(String Dx[])
    {
        test t1=new test();
        t1.set("Dx Panchal",23,"TRAINEE");
        t1.get();
       
    }

}

How to create abstract class in java

abstract class NewCars{
    String CarsName=null;
    public void display(String CN)
    {
        this.CarsName=CN;
        System.out.println("CarName :"+CarsName);
    }
   
}
public class AbstractionExample extends NewCars{

    public static void main(String Dx[])
    {
        AbstractionExample absEx=new AbstractionExample();
       
        absEx.display("New i30");
       
    }
}

How to use TreeSet in java

import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;


public class TreeSetExample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TreeSet<String> name=new TreeSet<>();
        name.add("Dx");
        name.add("Ravi");
        name.add("mitul");
        name.add("Dhaval");
        name.add("Bittu");
       
        Iterator<String> it=name.iterator();
        while(it.hasNext())
        {
            System.out.println(it.next());
        }
    }

}

How to use simple String array in java

import java.util.*;
public class SimpleArray {

    public static void main(String[] Dx) {
        // TODO Auto-generated method stub
       
        String[] name = new String[5];
        name[0]="Dx ";
        name[1]="RAvi ";
        name[2]="Mitul";
        name[3]="Brijesh";
        name[4]="Dhaval";
       
       
        for(int i=0;i<5;i++)
        {
        System.out.println(" "+name[i]);

        }
    }
}

How to change Background color by selecting color from Listview

activitymain.xml file::> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xm...