; 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

Using Toolbar in android in LollyPop and Pre-Lollypop Devices - Part 2



1) toolbar.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/md_blue_500_primary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
</android.support.v7.widget.Toolbar>


2) activity_main.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" >

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

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Name"
android:inputType="textPersonName"
android:textColor="@color/md_black">
<requestFocus />
</EditText>

</LinearLayout>


3)  MainActivity.java


package com.pratap.materialdesign;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;

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

toolbar = (Toolbar) findViewById(R.id.toolbar);

toolbar.setTitle("Material Design");
//toolbar.setSubtitle("AppCompat");
//toolbar.setTitleTextColor(Color.RED);

setSupportActionBar(toolbar);

}

@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) {
return true;
}
return super.onOptionsItemSelected(item);
}
}




4) values/styles.xml

<resources>
    <!-- Base application theme using AppCompatv21 Library -->
<style name="AppBaseTheme" parent="Theme.AppCompat">

<!-- your app branding color for the app bar -->
<item name="colorPrimary">@color/md_blue_500_primary</item>

<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">@color/md_blue_700</item>

<!-- Change the Background color of the window in the activity -->
<item name="android:windowBackground">@color/md_white</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">@color/md_red_200</item>

<!-- Chnaging the toolbar text color -->
<!-- <item name="android:textColorPrimary">@color/md_red_500_primary</item> -->

<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme"></style>

</resources>

5) values-21/styles.xml

<resources>

<style name="AppTheme" parent="AppBaseTheme">
<!-- API 21 theme customizations can go here. -->

<item name="android:navigationBarColor">@color/md_cyan_500_primary</item>
</style>

</resources>


6)  AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pratap.materialdesign"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>



7) Build and Run the Code


ScreenShot:





Apply Material Design for LollyPop and Pre-Lolly Devices using AppCompat v7 Library -Part 1


Step 1:

Extend Your Activty Class with AppCompatActivity
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
package com.pratap.materialdesign;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity{

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

@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) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Step 2:

Modify the theme in styles.xml file with AppCompat Theme in values folder

values/styles.xml

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<resources>

<!-- Base application theme using AppCompatv21 Library -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<!-- your app branding color for the app bar -->
<item name="colorPrimary">@color/md_blue_500_primary</item>

<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">@color/md_blue_700</item>

<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">@color/md_red_200</item>
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme"></style>

</resources>

Step 3:

Create a new folder named values-21 under res folder and create a styles.xml for specific to lollypop device.

values-21/styles.xml


1
2
3
4
5
6
7
<resources>

<style name="AppTheme" parent="AppBaseTheme">
<!-- API 21 theme customizations can go here. -->
</style>

</resources>


Step 4:

Update the AndroidManifest.xml file with Application theme.

AndroidManifest.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
27
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pratap.materialdesign"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


Step 5:

Run the application in device or Emulator


ScreenShots





Creating Input Dialog Box with Xml Layout File



Creating Input Dialog Box with Xml Layout File


Call this Method in your Actvity


private void OpenCategroyDialogBox() {

LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.addnewcategory, null);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Add New Category");
alert.setView(promptView);

final EditText input = (EditText) promptView
.findViewById(R.id.etCategory);

input.requestFocus();
input.setHint("Enter Category");
input.setTextColor(Color.BLACK);

alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String newCategoryName = input.getText().toString();

// Do something with value!

if (newCategoryName.equals("")) {
input.setError("Name Required");
OpenCategroyDialogBox();
} else {

Toast.makeText(getApplicationContext(),
"Ok Clicked", Toast.LENGTH_SHORT).show();


}
}



});

alert.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
      Toast.makeText(getApplicationContext(),
"Ok Clicked", Toast.LENGTH_SHORT).show();
} }); // create an alert dialog AlertDialog alert1 = alert.create(); alert1.show(); }



addnewcategory.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:orientation="vertical" >

<EditText
android:id="@+id/etCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="20dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="20dp"
android:inputType="textCapSentences" >

<requestFocus />
</EditText>

</LinearLayout>


ScreenShot



Dynamic Controls Creation in Android with validation of Child Views



Previous post with out validation

http://android-pratap.blogspot.in/2014/11/dynamic-controls-creation-in-android.html



DynamicFieldsActivity.java


package com.pratap.allsampleprojects;

import android.animation.LayoutTransition;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
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);

final 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 name = (String) (etText.getText().toString());

String age = (String) (etAge.getText().toString());

String gender = (String) (radioSexButton.getText()
.toString());

etText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable edt) {
if (etText.getText().length() > 0) {
etText.setError(null);
}
}

@Override
public void beforeTextChanged(CharSequence s,
int start, int count, int after) {
// TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub

}
});

if (etText.getText().toString().length() == 0) {

etText.setError("Enter Name");

} else {

}

if (age.equals("")) {

etAge.setError("Enter age");

}

if (gender.equals("")) {

radioSexButton.setError("Enter Gender");

}

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();
}
});
}
}


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






UPTET news