; paras[X - 1].parentNode.insertBefore(ad1, paras[X]); } if (paras.length > X + 4) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 3].parentNode.insertBefore(ad2, paras[X + 4]); } if (isMobile && paras.length > X + 8) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 7].parentNode.insertBefore(ad3, paras[X + 8]); } });

Advertisement

Dynamic Controls Creation in Android


DynamicFieldsActivity.java

import android.animation.LayoutTransition;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class DynamicFieldsActivity extends Activity {

EditText textIn;
Button buttonAdd;
LinearLayout container;
Button buttonShowAll;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamicfields);
textIn = (EditText) findViewById(R.id.textin);
buttonAdd = (Button) findViewById(R.id.add);
container = (LinearLayout) findViewById(R.id.container);

buttonAdd.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {


int val=Integer.parseInt(textIn.getText().toString());


for (int c = 1; c <=val; c++) {


LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(
R.layout.dynamicrow, null);

container.addView(addView, 0);


}


}
});

LayoutTransition transition = new LayoutTransition();
container.setLayoutTransition(transition);

buttonShowAll = (Button) findViewById(R.id.showall);
buttonShowAll.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

String showallPrompt = "";

int childCount = container.getChildCount();
showallPrompt += "childCount: " + childCount + "\n\n";

for (int c = 0; c < childCount; c++) {
View childView = container.getChildAt(c);


EditText etText = (EditText) (childView
.findViewById(R.id.etname));


EditText etAge = (EditText) (childView
.findViewById(R.id.etAge));


RadioGroup radioSexGroup = (RadioGroup) childView
.findViewById(R.id.radioSex);

int selectedId = radioSexGroup.getCheckedRadioButtonId();

RadioButton radioSexButton = (RadioButton) childView.findViewById(selectedId);

String childTextViewText="";

childTextViewText += (String) (etText.getText().toString());

childTextViewText +=(String)(etAge.getText().toString());


childTextViewText +=(String)(radioSexButton.getText().toString());

showallPrompt += c + ": " + childTextViewText + "\n";
}

Toast.makeText(DynamicFieldsActivity.this, showallPrompt,
Toast.LENGTH_LONG).show();
}
});
}
}



dynamicfields.xml


<LinearLayout 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:orientation="vertical"
android:layout_margin="10dp"
android:padding="5dp" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add" />

<EditText
android:id="@+id/textin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/add"
android:inputType="number" />
</RelativeLayout>

<Button
android:id="@+id/showall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Data in Fieldsl" />

<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>

</LinearLayout>


dynamicrow.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
android:padding="3dp"
>

<EditText
android:id="@+id/etname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="Name"
android:inputType="textPersonName" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/etAge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Age"
android:inputType="number" >
</EditText>

<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp">

<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="M"
/>

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F"
/>
</RadioGroup>

</LinearLayout>





ScreenShots






Recycler view with OnClick Implementation with AppCompat




In the Previous Post, We have create list of items with Recyclerview using appcompat.

Now, we have implemented onclick event for each list item..


Clicking on the item , shows you a small toast with data.


I have used the implementation in this project from the following link.
// Implementation of onItemClick RecycleView
// http://stackoverflow.com/questions/24471109/recyclerview-onclick/26196831#26196831


Please see the update Source code 



Recyclerview with Cards example in Android with AppCompat (V7)


I have created a sample application to show RecyclerView and CardView in android with Latest Appcompat Library (V7 Library).
Step 1:
Import three Libary Project to the Eclipse
1) AppCompat 
2) RecyclerView
3) CardView
from the folder android-sdk\extras\android\support\v7
Step 2 :
Create a new Project/download the Source from below, 
and add the three Libary Projects to the MainProject as a Library Projects.

CardViewActivity.java

package com.pratap.cardviews1;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class CardViewActivity extends ActionBarActivity {

private Toolbar toolbar;

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);

}

String[] myDataset = { "Alpha", "Beta", "CupCake", "Donut", "Eclair",
"Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwitch",
"JellyBean", "KitKat", "LollyPop" };

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

// getSupportActionBar().setIcon(R.drawable.ic_launcher);

// getSupportActionBar().setTitle("Android Versions");

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);

// specify an adapter (see also next example)
mAdapter = new CardViewDataAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Settings Clicked",
Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.action_search) {
Toast.makeText(getApplicationContext(), "Search Clicked",
Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}


CardViewDataAdapter.java

package com.pratap.cardviews1;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {
public String[] mDataset;



// Provide a suitable constructor (depends on the kind of dataset)
public CardViewDataAdapter(String[] myDataset) {
mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.cardview_row, null);

// create ViewHolder

ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData

viewHolder.tvtinfo_text.setText(mDataset[position].toString());

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}

// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView tvtinfo_text;

public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
tvtinfo_text = (TextView) itemLayoutView
.findViewById(R.id.info_text);

}
}

}


toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>

activity_main.xml

<?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:background="#c9c9c9"
android:orientation="vertical" >

<!-- A RecyclerView with some commonly used attributes -->

<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />

<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:scrollbars="vertical" />

</LinearLayout>


cardview_row.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:selectableItemBackground"  >

<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="24sp" />
</RelativeLayout>

</android.support.v7.widget.CardView>


Link   Source code





ScreenShots




How to create a bottom bar in android




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

<-- <ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottombar"
android:fastScrollEnabled="true" >
</ListView> -->



<LinearLayout
android:id="@+id/bottombar"
style="?android:buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:divider="?android:dividerVertical"
android:dividerPadding="8dp"
android:gravity="center_horizontal"
android:showDividers="middle" >

<Button
android:id="@+id/events_list_view_organizing_button"
style="?android:buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="Cancel" >
</Button>

<Button
android:id="@+id/events_list_view_attending_button"
style="?android:buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="Done" >
</Button>
</LinearLayout>

</RelativeLayout>

ScreenShot



Launching Google Maps Directions via an intent on Android



Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?saddr=17.428323,78.412567&daddr=28.6454414,77.0907573"));
intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(intent);




Android Example With ViewPager for Swiping Left to Right and Viceversa

ViewPager With Fragment in Android

Android Example With ViewPager for Swiping Left to Right and Viceversa

The Project Contains Swiping of Contact from Left to Right and ViceVersa.

MainActivity.java

public class MainActivity extends ActionBarActivity {

private static final int NUMBER_OF_PAGES = 10;

private ViewPager mViewPager;
private DataFragmentPagerAdapter mMyFragmentPagerAdapter;

ArrayList<Contact> contactsList;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);

contactsList = new ArrayList<Contact>();

prepareData();


mMyFragmentPagerAdapter = new DataFragmentPagerAdapter(
getSupportFragmentManager(), contactsList);
mViewPager.setAdapter(mMyFragmentPagerAdapter);
}

private void prepareData() {

contactsList.add(new Contact("pratap1", "5121390971"));
contactsList.add(new Contact("pratap2", "5121390972"));
contactsList.add(new Contact("pratap3", "5121390973"));
contactsList.add(new Contact("pratap4", "5121390974"));
contactsList.add(new Contact("pratap5", "5121390975"));
contactsList.add(new Contact("pratap6", "5121390976"));
contactsList.add(new Contact("pratap7", "5121390977"));

}


}


PageFragment .java

public class PageFragment extends Fragment {
 
    public static PageFragment newInstance(Contact singleContact) {

        PageFragment pageFragment = new PageFragment();
        Bundle bundle = new Bundle();
      /*  bundle.putString("name", singleContact.getName());
        bundle.putString("phone", singleContact.getPhone());
        pageFragment.setArguments(bundle);*/
     
        bundle.putSerializable("contact", singleContact);
        pageFragment.setArguments(bundle);
     
        return pageFragment;
    }
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         
        View view = inflater.inflate(R.layout.fragment, container, false);
        final TextView textView1 = (TextView) view.findViewById(R.id.textView1);
        final TextView textView2 = (TextView) view.findViewById(R.id.textView2);
     
        Contact cont= (Contact) getArguments().getSerializable("contact");
     
        textView1.setText(cont.getName());
        textView2.setText(cont.getPhone());
     
        Button btnClick=(Button) view.findViewById(R.id.btnClick);
     
        btnClick.setOnClickListener(new  OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(getActivity(), "Clciked "+textView1.getText().toString(), Toast.LENGTH_SHORT).show();

}
});
     
     
        return view;
    }
}


DataFragmentPagerAdapter.java

public  class DataFragmentPagerAdapter extends FragmentPagerAdapter {

ArrayList<Contact> users;

public DataFragmentPagerAdapter(FragmentManager fm, ArrayList<Contact> usersList) {
super(fm);
this.users=usersList;
}

@Override
public Fragment getItem(int index) {

return PageFragment.newInstance(users.get(index));
}

@Override
public int getCount() {

return users.size();
}
}


Contact.java

public class Contact implements Serializable{

private String name;

public Contact(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}

private String phone;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.pratap.androidviewpager.MainActivity"
     >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />

</RelativeLayout>

fragment.xml

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

    <Button
        android:id="@+id/btnClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Click ME" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Medium Text"
        android:layout_margin="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />
 
</RelativeLayout>


Screenshots 




Download Source code for the entire project

UPTET news