Open top menu

How to Convert Upper Case Every First Letter of word in string in Android ?

We will explain two way.

First.

the method used only 3 words.
.................................................................................................................................................................


String sentence="you are good";
String  ls = Character.toString(sString.charAt(0)).toUpperCase()+sString.substring(1);
String[] splitvalue=ls.split(" ");
String s1=splitvalue[0];
String s2=splitvalue[1];
String  s = Character.toString(s2.charAt(0)).toUpperCase()+s2.substring(1);
if(splitvalue.length==3){
    String s3=splitvalue[2];
    String  ss = Character.toString(s3.charAt(0)).toUpperCase()+s3.substring(1);
      countdetails=s1+" "+s+" "+ss;
}else {
    countdetails = s1 + " " + s;
}

...................................................................................................................................................................

Second

The best way is second method.
...................................................................................................................................................................
String str1 = MainActivity.ButtonName.toLowerCase();
final StringBuilder result = new StringBuilder(str1.length());
String[] words = str1.split("\\s");
for(int i=0,l=words.length;i<l;++i) {
    if(i>0) result.append(" ");
    result.append(Character.toUpperCase(words[i].charAt(0)))
            .append(words[i].substring(1));

}
countdetails=result.toString();
Read more

 





HorizontalListViewDemo air_listview= (HorizontalListViewDemo)findViewById(R.id.listcountry);

Card_Button adapter = new Card_Button(MainActivity.this, arrayCardName);
air_listview.setAdapter(adapter);
air_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        for (int i = 0; i < air_listview.getChildCount(); i++) {
            if (position == i) {
                air_listview.getChildAt(i).setBackgroundColor(Color.parseColor("#f5833c"));
            } else {
                air_listview.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
            }
        }
Read more


Example – How to implement Horizontal Scroll in GridView?


In this blog we will explain how to implement Horizontal GridView with selector  in android. The selector keep GridView index position,  …explain step by step
Step 1: Implement selector xml class inside drawable folder.

selector_gridindex.xml

…………………………………………………………………………………………………………….....

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <
item android:state_enabled="true"
       
android:state_pressed="true" android:drawable="@drawable/background_selected" />
    <
item android:state_enabled="false"
       
android:state_focused="false" android:drawable="@drawable/background_selected" />
    <
item android:state_enabled="true"
       
android:state_selected="true" android:drawable="@drawable/background_selected" />

</
selector>
…………………………………………………………………………………………………...................

Step 2: implement color gradient xml class and set in selector_gridindex.xml class.
Background_selected.xml
……………………………………………………………………………………………………………….
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle" >



    <gradient

        android:angle="90"

        android:centerColor="#50ff0000"

        android:endColor="#50ff0000"

        android:startColor="#50ff0000" />



</shape>

……………………………………………………………………………………………………………….


Step 3: Implement activity_main .xml class

activity_main.xml.

………………………………………………………………………………………………………………
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

     >





    <HorizontalScrollView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:scrollbars="none"

        android:id="@+id/title_horizontalScrollView"

        android:layout_margin="1dp"

        android:fillViewport="false">



        <LinearLayout

            android:orientation="horizontal"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            >

            <GridView

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:id="@+id/listview"

                android:drawSelectorOnTop="true"

                android:gravity="center"

                android:listSelector="@drawable/selector_gridindex "

                android:scrollbars="horizontal"></GridView>



        </LinearLayout>

    </HorizontalScrollView>





</LinearLayout>
……………………………………………………………………………………………………………….
Step 4: implement gridview adapter class inside src folder.
 

GridViewImageAdapter.java

……………………………………………………………………………………………………………….
 

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;



import java.util.ArrayList;



   

public class GridViewImageAdapter extends BaseAdapter {

    private Context mContext;

    private final ArrayList<String> web;





    public GridViewImageAdapter(Context c, ArrayList<String> web ) {

        mContext = c;

        this.web = web;

    }



    @Override

    public int getCount() {

        // TODO Auto-generated method stub

        return web.size();

    }



    @Override

    public Object getItem(int position) {

        // TODO Auto-generated method stub

        return web.get(position);

    }



    @Override

    public long getItemId(int position) {

        // TODO Auto-generated method stub

        return position;

    }



    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        // TODO Auto-generated method stub

        //View grid;

        final ViewHolder holder;

        LayoutInflater inflater = (LayoutInflater) mContext

                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);



        if (convertView == null) {

            convertView = inflater.inflate(R.layout.card_background, null);



        }

        holder = new ViewHolder();

        holder.loungeName = (TextView) convertView.findViewById(R.id.text_items);

        holder.loungeName.setText(web.get(position));

        return convertView;

    }

    static class ViewHolder

    {



        TextView loungeName ;

    }

}

……………………………………………………………………………………………………………….

Step 5:implement MainActivity.java

……………………………………………………………………………………………………………….
 



import android.app.Activity;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Color;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.DisplayMetrics;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.GridLayout;

import android.widget.GridView;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.Toast;



import java.util.ArrayList;



public class MainActivity extends Activity {

    @Override

    protected void attachBaseContext(Context newBase) {

        super.attachBaseContext(newBase);

    }

    int width;

    GridView listview;

    ArrayList value=new ArrayList();

    @Override

    public void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        for (int i = 0; i < 20; i++) {

            value.add("SARVESH Kaushik" + i);

        }



        int noofcoloum=value.size();

        listview = (GridView)findViewById(R.id.listview);

        // (item layout width)*(Array Length)*(Add item main Layout Margin)

        int totalWidth =(100*noofcoloum)*2;

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(totalWidth, LinearLayout.LayoutParams.MATCH_PARENT);

        listview.setLayoutParams(params);

        listview.setNumColumns(noofcoloum);

        GridViewImageAdapter adapter = new GridViewImageAdapter(MainActivity.this, value);

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getApplicationContext(),position+"",Toast.LENGTH_LONG).show();

            }

        });



    }

}


……………………………………………………………………………………………………………….



Read more

 

 Example- How to Show Plaintext as an EditText in Android?

 style="@android:style/Widget.DeviceDefault.EditText"

 .......................................................................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical">

    <
TextView
       
android:id="@+id/textView55"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:text="New Text show"
       
android:textSize="35dp"
       
android:textStyle="bold" />

    <
TextView
       
android:id="@+id/textView5"
       
style="@android:style/Widget.DeviceDefault.EditText"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="100dp"
       
android:layout_gravity="center_horizontal"
       
android:text="New Text show"
       
android:textSize="35dp"
       
android:textStyle="bold" />
</
LinearLayout>
.............................................................................................





Read more

 

Example - How To Filter ListView With ArrayAdapter, TextWatcher in Android?


In this blog we will implement ListView with Filter. As filter all text filed via EditText TexWatcher with adapter. you have to search individual filed  one by one.show This Picture.






activity_main.xml

...................................................................................................................................................................

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@drawable/register_cover">

    <LinearLayout

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:background="#15d32b"

        android:layout_marginRight="5dp"

        android:layout_marginLeft="5dp">

        <LinearLayout

            android:orientation="horizontal"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:gravity="center"

            >







            <TextView

                android:layout_width="fill_parent"

                android:gravity="center"

                android:padding="2dp"

                android:textSize="18dp"

                android:textStyle="bold"

                android:layout_weight="1"

                android:textColor="#ffffff"

                android:layout_height="wrap_content"

                android:text="Student Details"

                android:id="@+id/textView51"

                android:layout_marginTop="2dp"

                />



        </LinearLayout>







        <LinearLayout

            android:orientation="horizontal"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:gravity="center"

            >

            <LinearLayout

                android:orientation="vertical"

                android:layout_width="0dp"

                android:layout_weight=".7"

                android:gravity="center"

                android:layout_margin="4dp"

                android:background="#ffffff"

                android:layout_height="wrap_content">



                <EditText

                    android:layout_width="fill_parent"

                    android:layout_height="35dp"

                    android:drawableRight="@drawable/search_icon"

                    android:id="@+id/searchViewtext" />

            </LinearLayout>



            <LinearLayout

                android:orientation="horizontal"

                android:layout_width="wrap_content"

                android:gravity="center"

                android:layout_margin="4dp"

                android:layout_height="wrap_content">



                <Button

                    android:layout_width="100dp"

                    android:layout_height="30dp"

                    android:layout_margin="2dp"

                    android:background="@android:color/transparent"

                    android:id="@+id/searchbtn" />





            </LinearLayout>





        </LinearLayout>





    </LinearLayout>

    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="match_parent"



        android:padding="3dp"

        android:layout_marginRight="5dp"

        android:layout_marginLeft="5dp"

        android:background="#2e52d3"

        android:layout_height="wrap_content">



        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Name"

            android:textSize="13dp"

            android:textStyle="bold"

            android:gravity="center_vertical"

            android:textColor="#ffffff"

            android:layout_weight="1.5"

            android:id="@+id/textView53"

            android:layout_gravity="center_horizontal" />



        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Roll No."

            android:textColor="#ffffff"

            android:textSize="13dp"

            android:gravity="center"

            android:textStyle="bold"

            android:layout_weight="1"

            android:layout_gravity="center_horizontal" />

        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Sections"

            android:gravity="center"

            android:textColor="#ffffff"

            android:textSize="13dp"

            android:textStyle="bold"

            android:layout_weight="1"

            android:layout_gravity="center_horizontal" />

        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Status"

            android:gravity="center"

            android:textColor="#ffffff"

            android:textSize="13dp"

            android:textStyle="bold"

            android:layout_weight="1"

            android:layout_gravity="center_horizontal" />



    </LinearLayout>

    <ListView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/listViewforSeat" />



</LinearLayout>






...................................................................................................................................................................

special_list_item.xml

...................................................................................................................................................................
 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent">

    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="match_parent"

        android:padding="3dp"

        android:layout_marginRight="5dp"

        android:layout_marginLeft="5dp"

        android:layout_height="wrap_content"

        >



        <TextView

            android:id="@+id/textName"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_gravity="center_horizontal"

            android:layout_weight="1.5"

            android:gravity="center_vertical"

            android:paddingBottom="5dp"

            android:paddingTop="5dp"

            android:text="Name"

            android:textSize="13dp" />



        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="RollNo."

            android:textSize="13dp"

            android:gravity="center"

            android:layout_weight="1"

            android:id="@+id/textRoll"

            android:paddingBottom="5dp"

            android:paddingTop="5dp"

            android:layout_gravity="center_horizontal" />



        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Section"

            android:gravity="center"

            android:textSize="13dp"

            android:layout_weight="1"

            android:id="@+id/textSection"

            android:paddingBottom="5dp"

            android:paddingTop="5dp"

            android:layout_gravity="center_horizontal" />



        <TextView

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:text="Status"

            android:gravity="center"

            android:textSize="13dp"

            android:layout_weight="1"

            android:paddingBottom="5dp"

            android:paddingTop="5dp"

            android:id="@+id/textstatus"

            android:layout_gravity="center_horizontal" />





    </LinearLayout>



</LinearLayout>
 



...................................................................................................................................................................


SeatDetails_Adapter.java

...................................................................................................................................................................
package com.example.sarvesh.testproject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Locale;


public class SeatDetails_Adapter extends BaseAdapter {
    private Context context;

    ArrayList myList = new ArrayList();
    private ArrayList<Special_SeatItem> arraylist;
    int resorce;

    public SeatDetails_Adapter(Context context, ArrayList<Special_SeatItem> myList) {
        this.context = context;
        this.myList = myList;
        this.arraylist = new ArrayList<Special_SeatItem>();
        this.arraylist.addAll(myList);

    }

   /* public SeatDetails_Adapter(MainActivity mainActivity, ArrayList<SeatDetail_Item> seatInfo) {   } */

    @Override    public int getCount() {
        return myList.size();
    }

    @Override    public Special_SeatItem getItem(int position) {

        return (Special_SeatItem) myList.get(position);
    }

    @Override    public long getItemId(int position) {
        return 0;
    }

    @Override    public View getView(int position, View convertView, ViewGroup parent) {
        Special_SeatItem speciitem = getItem(position);
        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.special_list_item, null);
        }
        holder = new ViewHolder();
        try {


            holder.StuName = (TextView) convertView.findViewById(R.id.textName);
            holder.StuName.setText(speciitem.getName());

            holder.StuRollNo = (TextView) convertView.findViewById(R.id.textRoll);
            holder.StuRollNo.setText(speciitem.getRollNo());

            holder.StuSection = (TextView) convertView.findViewById(R.id.textSection);
            holder.StuSection.setText(speciitem.getSection());

            holder.StuStatus = (TextView) convertView.findViewById(R.id.textstatus);
            holder.StuStatus.setText(speciitem.getStatus());


        } catch (Exception e) {
            e.printStackTrace();
        }
        convertView.setTag(holder);
        return convertView;
    }

    static class ViewHolder {

        TextView StuName, StuRollNo, StuSection, StuStatus;
    }


    // Filter Class    public void filter(String charText) {

        charText = charText.toLowerCase(Locale.getDefault());
        myList.clear();
        if (charText.length() == 0) {
            myList.addAll(arraylist);
        } else {
            for (Special_SeatItem sp : arraylist) {
                if (sp.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
                    myList.add(sp);
                } else if (sp.getRollNo().toLowerCase(Locale.getDefault()).contains(charText)) {
                    myList.add(sp);
                } else if (sp.getSection().toLowerCase(Locale.getDefault()).contains(charText)) {
                    myList.add(sp);
                } else if (sp.getStatus().toLowerCase(Locale.getDefault()).contains(charText)) {
                    myList.add(sp);
                }


            }
        }
        notifyDataSetChanged();
    }
}

...................................................................................................................................................................



MainActivity.java

...................................................................................................................................................................
package com.example.sarvesh.testproject;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Locale;


public class MainActivity extends  Activity {

    ArrayList<String> Name,rollno,Sections,Status;
    ArrayList<Special_SeatItem> Studendarraylist=new ArrayList<Special_SeatItem>();

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

        Name=new ArrayList<String>();
        Name.add("Mohan Kumar");
        Name.add("Rahul Saini");
        Name.add("Mona Kumari");
        Name.add("Sanjay Gupta");
        Name.add("Meenu Sharma");

        rollno=new ArrayList<String>();
        rollno.add("05678");
        rollno.add("05698");
        rollno.add("05679");
        rollno.add("07678");
        rollno.add("08378");

        Sections=new ArrayList<String>();
        Sections.add("B2");
        Sections.add("C2");
        Sections.add("D2");
        Sections.add("B2");
        Sections.add("B1");

        Status=new ArrayList<String>();
        Status.add("I");
        Status.add("2nd");
        Status.add("3rd");
        Status.add("2nd");
        Status.add("I");
        for (int i = 0; i < 5; i++) {
            Special_SeatItem studentdetails = new Special_SeatItem();
            studentdetails.setName(Name.get(i));
            studentdetails.setRollNo(rollno.get(i));
            studentdetails.setSection(Sections.get(i));
            studentdetails.setStatus(Status.get(i));
            Studendarraylist.add(studentdetails);
        }

        ListView listviewStu=(ListView)findViewById(R.id.listViewforSeat);
        final SeatDetails_Adapter adapter=new SeatDetails_Adapter(this,Studendarraylist);
        listviewStu.setAdapter(adapter);

        final EditText filter=(EditText)findViewById(R.id.searchViewtext);
        filter.addTextChangedListener(new TextWatcher() {

            @Override            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                String text = filter.getText().toString().toLowerCase(Locale.getDefault());
                //listAdapter.getffilter().filter(text);                adapter.filter(text);
            }

            @Override            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
                // TODO Auto-generated method stub            }

            @Override            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
                // TODO Auto-generated method stub            }
        });
    }

}


...................................................................................................................................................................
















Read more