Month: January 2017
Android Highlight selected item in a ListView
res/values/strings.xml
<resources> <string name="app_name">014</string> <string-array name="countries"> <item>Iran</item> <item>Iraq</item> <item>India</item> <item>Italy</item> <item>United Kingdom</item> <item>United States</item> <item>Israel</item> <item>Kuwait</item> </string-array> </resources>
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="w3_camo_gray">#9495a5</color> <color name="Lemon_Yellow">#ffbb00</color> </resources>
res/layout/listview_countries_custom_layout.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"> <TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewListViewCountriesItem" android:gravity="center" android:paddingBottom="15px" android:textStyle="bold" android:textSize="18dp" android:background="@drawable/listview_countries_selector"/> </LinearLayout>
res/drawable/listview_countries_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@color/w3_camo_gray" ></item> <item android:state_selected="true" android:drawable="@color/Lemon_Yellow" ></item> </selector>
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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="iterator.ir.a014.MainActivity"> <ListView android:id="@+id/listviewCountries" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
MainActivity.java
ListView listViewCountries; String[] countries; ArrayAdapter<String> listViewArrayAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countries=getResources().getStringArray(R.array.countries); listViewCountries= (ListView) findViewById(R.id.listviewCountries); listViewArrayAdapter=new ArrayAdapter<String>(getBaseContext(), R.layout.listview_countries_custom_layout, R.id.textViewListViewCountriesItem,countries); listViewCountries.setAdapter(listViewArrayAdapter); listViewCountries.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast toast=Toast.makeText(getBaseContext(),parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT); toast.show(); view.setSelected(true); } }); }
References
https://github.com/mhdr/AndroidSamples/tree/master/014
Android ListView with own layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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="iterator.ir.a013.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/listViewCountries" /> </RelativeLayout>
listview_custom_layout.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"> <TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewListItem" android:gravity="center" android:textSize="18dp" android:textColor="@color/w3_highway_blue" android:paddingBottom="5dp"/> </LinearLayout>
res/values/strings.xml
<resources> <string name="app_name">013</string> <string-array name="countries"> <item>Iran</item> <item>Iraq</item> <item>India</item> <item>Italy</item> <item>Brazil</item> <item>France</item> <item>United Kingdom</item> <item>Pakistan</item> <item>Oman</item> </string-array> </resources>
MainActivity.java
ListView listViewCountries; ArrayAdapter<String> arrayAdapter; String[] countries; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countries=getResources().getStringArray(R.array.countries); listViewCountries= (ListView) findViewById(R.id.listViewCountries); arrayAdapter=new ArrayAdapter<String>(this, R.layout.listview_custom_layout, R.id.textViewListItem,countries); listViewCountries.setAdapter(arrayAdapter); listViewCountries.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast toast=Toast.makeText(getBaseContext(),parent.getItemAtPosition(position).toString(),Toast.LENGTH_LONG); toast.show(); } }); }
References
https://github.com/mhdr/AndroidSamples/tree/master/013
Android Working with ListView
activity_main.xml
<ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/listViewCountries" />
res/values/strings.xml
<resources> <string name="app_name">012</string> <string-array name="countries"> <item>Iran</item> <item>India</item> <item>France</item> <item>Italy</item> <item>Iraq</item> </string-array> </resources>
MainActivity.java
ListView listViewCountries; ArrayAdapter<String> arrayAdapter; String[] countries; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listViewCountries= (ListView) findViewById(R.id.listViewCountries); countries=getResources().getStringArray(R.array.countries); arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countries); listViewCountries.setAdapter(arrayAdapter); listViewCountries.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast toast=Toast.makeText(getBaseContext(),parent.getItemAtPosition(position).toString(),Toast.LENGTH_LONG); toast.show(); } }); }
References
https://github.com/mhdr/AndroidSamples/tree/master/012
Android Working with Spinner
activity_main.xml
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView" android:layout_toEndOf="@+id/textView" android:layout_marginLeft="30dp" android:layout_marginStart="30dp" android:id="@+id/spinnerCountries" />
Spinner spinner; ArrayAdapter<String> spinnerAdapter; String[] countries; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner= (Spinner) findViewById(R.id.spinnerCountries); countries=getResources().getStringArray(R.array.countries); spinnerAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,countries); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerAdapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast toast=Toast.makeText(getBaseContext(),parent.getSelectedItem().toString(),Toast.LENGTH_LONG); toast.show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); }
References
https://github.com/mhdr/AndroidSamples/tree/master/011
Android Working with Toggle Button
<ToggleButton android:textOn="Bluetooth On" android:textOff="Bluetooth Off" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/toggleButtonBluetooth" /> <Switch android:textOff="Wifi Off" android:textOn="Wifi On" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="54dp" android:id="@+id/switchWifi" android:layout_below="@+id/toggleButtonBluetooth" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:text="Wifi" />
MainActivity.java
ToggleButton toggleButtonBluetooth; Switch switchWifi; TextView textViewWifi; TextView textViewBluetooth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toggleButtonBluetooth= (ToggleButton) findViewById(R.id.toggleButtonBluetooth); switchWifi= (Switch) findViewById(R.id.switchWifi); textViewBluetooth= (TextView) findViewById(R.id.textViewBluetooth); textViewWifi= (TextView) findViewById(R.id.textViewWifi); textViewBluetooth.setText(""); textViewWifi.setText(""); switchWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { textViewWifi.setText("Wifi is On"); } else { textViewWifi.setText("Wifi is Off"); } } }); switchWifi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // both are correct } }); toggleButtonBluetooth.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Boolean isChecked=((ToggleButton) v).isChecked(); if (isChecked) { textViewBluetooth.setText("Bluetooth is On"); } else { textViewBluetooth.setText("Bluetooth is Off"); } } }); }
References
https://www.youtube.com/watch?v=RnpIY4UK87E&index=15&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk
https://github.com/mhdr/AndroidSamples/tree/master/010
List The Open Ports And The Process That Owns Them on Linux
sudo lsof -i sudo netstat -lptu sudo netstat -tulpn
References
https://www.cyberciti.biz/tips/linux-display-open-ports-owner.html