Android TabHost Border Line [duplicate] - android

Android 2.2 i.e API Level 8 has tabStripEnabled="true" for TabWidget
how to achieve the same in Older versions of Android?

private void SetupTabs(TabHost tabHost) {
LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
TabWidget tw = (TabWidget) ll.getChildAt(0);
Field mBottomLeftStrip;
Field mBottomRightStrip;
try {
mBottomLeftStrip = tw.getClass().getDeclaredField("mBottomLeftStrip");
mBottomRightStrip = tw.getClass().getDeclaredField("mBottomRightStrip");
if (!mBottomLeftStrip.isAccessible()) {
mBottomLeftStrip.setAccessible(true);
}
if (!mBottomRightStrip.isAccessible()) {
mBottomRightStrip.setAccessible(true);
}
mBottomLeftStrip.set(tw, getResources().getDrawable(R.drawable.blank));
mBottomRightStrip.set(tw, getResources().getDrawable(R.drawable.blank));// blank is the name of the image in drawable folder
}
catch (java.lang.NoSuchFieldException e) {
// possibly 2.2
try {
Method stripEnabled = tw.getClass().getDeclaredMethod("setStripEnabled", boolean.class);
stripEnabled.invoke(tw, false);
}
catch (Exception e1) {
e1.printStackTrace();
}
}
catch (Exception e) {}
}

I made it so:
try {
Method setStripEnabled = tabWidget.getClass().getDeclaredMethod(
"setStripEnabled", boolean.class);
setStripEnabled.invoke(tabWidget, true);
Method setLeftStripDrawable = tabWidget.getClass()
.getDeclaredMethod("setLeftStripDrawable", int.class);
setLeftStripDrawable.invoke(tabWidget, R.drawable.tab_line);
Method setRightStripDrawable = tabWidget.getClass()
.getDeclaredMethod("setRightStripDrawable", int.class);
setRightStripDrawable.invoke(tabWidget, R.drawable.tab_line);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}

Related

NumberPicker Restrictions on non-SDK interfaces

I'm trying to create a custom NumberPicker like this.
Class<?> numberPickerClass = null;
try {
numberPickerClass = Class.forName("android.widget.NumberPicker");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Field selectionDivider = null;
try {
if (numberPickerClass != null) {
selectionDivider = numberPickerClass.getDeclaredField("mSelectionDivider");
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
if (selectionDivider == null) {
return;
}
try {
selectionDivider.setAccessible(true);
selectionDivider.set(this, null);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
However, starting from API29, Android is restricting access on non-SDK interfaces. They are suggesting using set/getDividerHeight instead. But I'm super new to Android and I'm not sure how to update my code. Can anyone kindly help? Thanks!

How to create a hidden WiFi hotspot programmatically?

I want to create a hidden WiFi hotspot programmatically with an android device,but I can't find an answer with [developer.android.google.cn][1]
[1]: http:///https://developer.android.google.cn/reference/android/net/wifi/WifiConfiguration. just now I saw a property named hiddenSSID, I tried to change it's value(true/false),but it did not work,could anybody help me?
private void stratWifiAp(String mSSID, String mPasswd) {
Method method1 = null;
try {
method1 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = mSSID;
netConfig.preSharedKey = mPasswd;
netConfig.hiddenSSID = true;
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(4);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
method1.invoke(mWifiManager, netConfig, true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
Try to modify your code to be something like this:
private void stratWifiAp(String mSSID, String mPasswd) {
Method method1 = null;
try {
//通过反射机制打开热点
method1 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = mSSID;
netConfig.preSharedKey = mPasswd;
netConfig.hiddenSSID = true;
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(4);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
method1.invoke(mWifiManager, netConfig, true);
return netConfig;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
//return netConfig; or try to add here IF ONLY YOU ADDED THE ABOVE ONE!
}

How to change TimePicker lines color in Appcompat theme?

How to change TimePicker lines color between choosed number in Appcompat theme? The lines are blue, but i need orange lines.
I use TimePickerDialog with ContextThemeWrapper.
TimePickerDialog timePicker = new TimePickerDialog(
new ContextThemeWrapper(getActivity(), R.style.timePicker),
this, hour, minute, DateFormat.is24HourFormat(getActivity()));
and style
<style name="timePicker">
<item name="android:divider">#drawable/cab_background_top_play</item>
</style>
but the line have other id maybe. I'm not sure to use drawable or color only.
Thanks in advance
I solved this with own Timepicker class which extends android Timepicker class. I inspired by post stackoverflow.com/questions/20148671/android-how-to-change-the-color-of-the-datepicker-divider/20291416#20291416 which posted mohammad rababah in comment below my ask.
Here is styled timepicker class:
public class StyledTimePicker extends TimePicker {
public StyledTimePicker(Context context, AttributeSet attrs) {
super(context, attrs);
Class<?> internalRID = null;
try {
internalRID = Class.forName("com.android.internal.R$id");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Field month = null;
try {
month = internalRID.getField("hour");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
NumberPicker npMonth = null;
try {
npMonth = (NumberPicker) findViewById(month.getInt(null));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Field day = null;
try {
day = internalRID.getField("minute");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
NumberPicker npDay = null;
try {
npDay = (NumberPicker) findViewById(day.getInt(null));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Field year = null;
try {
year = internalRID.getField("amPm");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
NumberPicker npYear = null;
try {
npYear = (NumberPicker) findViewById(year.getInt(null));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Class<?> numberPickerClass = null;
try {
numberPickerClass = Class.forName("android.widget.NumberPicker");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Field selectionDivider = null;
try {
selectionDivider = numberPickerClass.getDeclaredField("mSelectionDivider");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
try {
selectionDivider.setAccessible(true);
selectionDivider.set(npMonth, getResources().getDrawable(
R.color.apptheme_color));
selectionDivider.set(npDay, getResources().getDrawable(
R.color.apptheme_color));
selectionDivider.set(npYear, getResources().getDrawable(
R.color.apptheme_color));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
You can change color apptheme_color only.
Thanks to mohammad rababah
Expand res folder in your application and expand values folder. Then create themes.xml file on values folder. then replace all code in themes.xml file with below code.
<style name="MYTheme" parent="#android:style/Theme">
<item name="android:divider">#drawable/dialog_divider</item>
</style>
Then open your AndroidManifest.xml file. and find android:theme and replcae with android:theme="#style/MYTheme"

Android getOnTouchListener with API version >= 15

I need to know if there has been set an OnTouchListener to a view.
Following code works fine below api level 15.
public static boolean isSetOnTouchListener_v8(View v) {
try {
Class<View> clazz = (Class<View>) Class.forName("android.view.View");
Field f = clazz.getDeclaredField("mOnTouchListener");
f.setAccessible(true);
if (f.get(v) == null) {
return false;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return true;
}
Now I need the same method for api level 15 and above.
I tried the following code, but it's not working. Does anyone have any suggestions?
public static boolean isSetOnTouchListener_v15(View v) {
try {
Object listenerInfo = getListenerInfo(v);
if (listenerInfo != null) {
Class<? extends Object> clazz = listenerInfo.getClass();
Field f = clazz.getDeclaredField("mOnTouchListener");
f.setAccessible(true);
if (f.get(listenerInfo) == null) return false;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return true;
}
private static Object getListenerInfo(View v) {
Field f = null;
try {
f = View.class.getDeclaredField("mListenerInfo");
f.setAccessible(true);
return (Object) f.get(v);
} catch (Exception e) {
return null;
}
}

tabStripEnabled for TabWidget in Older API's

Android 2.2 i.e API Level 8 has tabStripEnabled="true" for TabWidget
how to achieve the same in Older versions of Android?
private void SetupTabs(TabHost tabHost) {
LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
TabWidget tw = (TabWidget) ll.getChildAt(0);
Field mBottomLeftStrip;
Field mBottomRightStrip;
try {
mBottomLeftStrip = tw.getClass().getDeclaredField("mBottomLeftStrip");
mBottomRightStrip = tw.getClass().getDeclaredField("mBottomRightStrip");
if (!mBottomLeftStrip.isAccessible()) {
mBottomLeftStrip.setAccessible(true);
}
if (!mBottomRightStrip.isAccessible()) {
mBottomRightStrip.setAccessible(true);
}
mBottomLeftStrip.set(tw, getResources().getDrawable(R.drawable.blank));
mBottomRightStrip.set(tw, getResources().getDrawable(R.drawable.blank));// blank is the name of the image in drawable folder
}
catch (java.lang.NoSuchFieldException e) {
// possibly 2.2
try {
Method stripEnabled = tw.getClass().getDeclaredMethod("setStripEnabled", boolean.class);
stripEnabled.invoke(tw, false);
}
catch (Exception e1) {
e1.printStackTrace();
}
}
catch (Exception e) {}
}
I made it so:
try {
Method setStripEnabled = tabWidget.getClass().getDeclaredMethod(
"setStripEnabled", boolean.class);
setStripEnabled.invoke(tabWidget, true);
Method setLeftStripDrawable = tabWidget.getClass()
.getDeclaredMethod("setLeftStripDrawable", int.class);
setLeftStripDrawable.invoke(tabWidget, R.drawable.tab_line);
Method setRightStripDrawable = tabWidget.getClass()
.getDeclaredMethod("setRightStripDrawable", int.class);
setRightStripDrawable.invoke(tabWidget, R.drawable.tab_line);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}

Categories

Resources