Skip to content

Instantly share code, notes, and snippets.

View JuniiiSays's full-sized avatar
🏠
Working from home

Junaid Iqbal JuniiiSays

🏠
Working from home
View GitHub Profile
ArrayList<String> words = new ArrayList<String>();
words.add("One");
//Find the rootView of Numbers activity
LinearLayout rootView = findViewById(R.id.rootView);
//Create a TextView programmatically
TextView wordView = new TextView(this);
//Show first element of the ArrayList ib that TextView
wordView.setText(words.get(0));
//Attach TextView with the rootView
@JuniiiSays
JuniiiSays / readFromStream.java
Created September 1, 2022 04:47
This method will get a InputStream as input and return a String of response
/**
* Convert the {@link InputStream} into a String which contains the
* whole JSON response from the server.
*/
private static String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null){
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()){
// TODO: Do sothing here if the user is Connected to the INTERNET
} else {
// TODO: Do sothing here if the user is not connected to the INTERNET
}
@JuniiiSays
JuniiiSays / CopyTextToClipbpoard.java
Last active September 12, 2022 15:45
Copy Text to Clip Board in Android
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
// Make sure you have imported android.content.ClipboardManager and NOT android.text.ClipboardManager. Latter is deprecated.
@JuniiiSays
JuniiiSays / ShareText.Java
Created September 12, 2022 15:10
Android: Share plain text using intent (to all messaging apps)
Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
txtIntent .setType("text/plain");
txtIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
txtIntent .putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(txtIntent ,"Share"));
@JuniiiSays
JuniiiSays / shareText.java
Created September 29, 2022 16:50
How to share data through Intent?
String textThatYouWantToShare = "Hello There!";
String mimeType = "text/plain";
String title = "Learning How to Share";
ShareCompat.IntentBuilder
.from(this)
.setType(mimeType)
.setChooserTitle(title)
.setText(textThatYouWantToShare)
.startChooser();