‏إظهار الرسائل ذات التسميات Android code sample: general info. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات Android code sample: general info. إظهار كافة الرسائل

الثلاثاء، 1 سبتمبر 2015

RecyclerView + CardView example - list system properties using System.getProperties()

My former example show how to "Retrieve system properties using System.getProperties()", here we are going to list the Properties on RecyclerView + CardView, similar to another example "RecyclerView + CardView example with ImageView - list available Drawable".


To use RecyclerView + CardView, we have to "Add Support Libraries of RecyclerView, CardView to Android Studio Project".

layout/layout_cardview.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="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="5sp"
card_view:cardElevation="5sp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp" />

<TextView
android:id="@+id/item_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp" />

</LinearLayout>

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


com.blogspot.android_er.androidproperties.MyRecyclerViewAdapter.java
package com.blogspot.android_er.androidproperties;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyRecyclerViewAdapter extends
RecyclerView.Adapter<MyRecyclerViewAdapter.ItemHolder>{

private List<String> itemsName;
private List<String> itemsValue;
private LayoutInflater layoutInflater;
private Context context;

public MyRecyclerViewAdapter(Context context){
this.context = context;
layoutInflater = LayoutInflater.from(context);
itemsName = new ArrayList<String>();
itemsValue = new ArrayList<String>();
}

@Override
public ItemHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
CardView itemCardView =
(CardView)layoutInflater.inflate(R.layout.layout_cardview, viewGroup, false);
return new ItemHolder(itemCardView, this);
}

@Override
public void onBindViewHolder(ItemHolder itemHolder, int i) {
itemHolder.setItemName(itemsName.get(i));
String value = itemsValue.get(i);
itemHolder.setItemValue(String.valueOf(value));
}

@Override
public int getItemCount() {
return itemsName.size();
}

public void add(int location, String iName, String iValue){
itemsName.add(location, iName);
itemsValue.add(location, iValue);
notifyItemInserted(location);
}

public void remove(int location){
if(location >= itemsName.size())
return;

itemsName.remove(location);
itemsValue.remove(location);
notifyItemRemoved(location);
}

public static class ItemHolder extends RecyclerView.ViewHolder{

private MyRecyclerViewAdapter parent;
private CardView cardView;
TextView textItemName;
TextView textItemValue;

public ItemHolder(CardView cView, MyRecyclerViewAdapter parent) {
super(cView);
cardView = cView;
this.parent = parent;
textItemName = (TextView) cardView.findViewById(R.id.item_name);
textItemValue = (TextView) cardView.findViewById(R.id.item_value);
}

public void setItemName(CharSequence name){
textItemName.setText(name);
}

public CharSequence getItemName(){
return textItemName.getText();
}

public void setItemValue(CharSequence val){
textItemValue.setText(val);
}

public CharSequence getItemValue(){
return textItemValue.getText();
}
}
}


layout/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"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<android.support.v7.widget.RecyclerView
android:id="@+id/myrecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>


com.blogspot.android_er.androidproperties.MainActivity.java
package com.blogspot.android_er.androidproperties;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.Enumeration;
import java.util.Properties;

public class MainActivity extends AppCompatActivity {

private RecyclerView myRecyclerView;
LinearLayoutManager linearLayoutManager;
private MyRecyclerViewAdapter myRecyclerViewAdapter;

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

myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);

linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
myRecyclerViewAdapter = new MyRecyclerViewAdapter(this);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerView.setLayoutManager(linearLayoutManager);

prepareItems();
}

private void prepareItems(){

Properties properties = System.getProperties();
Enumeration<String> prop =
(Enumeration<String>) properties.propertyNames();

while(prop.hasMoreElements()){
String propName = prop.nextElement();

myRecyclerViewAdapter.add(
myRecyclerViewAdapter.getItemCount(),
propName,
System.getProperty(propName));
}
}
}


Tested on Nexus 7 running Android 5.1.1

Tested on Android Emulator running Android 6

download filesDownload the files (Android Studio Format) .

~ More step-by-step examples of RecyclerView + CardView.

الأحد، 30 أغسطس 2015

Retrieve system properties using System.getProperties()

getProperties() method of java.lang.System returns the system properties. Here is a example to retrieve system properties using System.getProperties().


package com.blogspot.android_er.androidsystemproperties;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.Enumeration;
import java.util.Properties;

public class MainActivity extends AppCompatActivity {

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

TextView info = (TextView)findViewById(R.id.info);

Properties properties = System.getProperties();
info.append(properties.toString());
info.append("\n----------\n\n");

Enumeration<String> prop =
(Enumeration<String>) properties.propertyNames();

while(prop.hasMoreElements()){
String propName = prop.nextElement();
info.append(propName + " : \n" +
System.getProperty(propName) + "\n\n");

}
}

}


<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:padding="10dp"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>

</LinearLayout>


الأربعاء، 19 أغسطس 2015

List all available drawable and its value

Example to list all available Drawable and its value:


package com.example.androidlistdrawable;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {

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

TextView info = (TextView)findViewById(R.id.info);

Field[] fieldDrawables = android.R.drawable.class.getFields();
for(int i=0; i<fieldDrawables.length; i++){
Field field = fieldDrawables[i];
info.append("R.drawable." + field.getName() + " :\n");
info.append(field.toString() + "\n");

try {
int value = (int) field.get(fieldDrawables);
info.append("value = " + value + "\n");
} catch (IllegalAccessException e) {
e.printStackTrace();
}

info.append("\n");

}
}

}


<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:padding="10dp"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>



Next:
- RecyclerView + CardView example - list available Drawable