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!
Related
The app I am working on takes JSONs from a server and has to interpret them. I have around 6-7 JSONs that are requested.
As such, this is bound to take some time.
This code runs whenever the user types in information to an EditText and clicks a button. After the search button is clicked, the app freezes and will not do anything until all the data is loaded.
I tried having the code like this:
Button button = (Button) findViewById(R.id.search_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(summonerNameET.getWindowToken(), 0);
final CollectUserData c = new CollectUserData();
c.setRegion(region);
c.setSummonerName(summonerNameET.getText().toString());
Runnable r = new Runnable() {
#Override
public void run() {
try {
c.setUpTheJSONs();
updateUI(c);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
};
r.run();
}
});
But it still freezes. Here is the code for setUpTheJSON();
public void setUpTheJSONs() throws IOException, JSONException, URISyntaxException {
jsonSummonerInfo = getJsonSummonerInfo();
if(canIContinue()) {
jsonSummonerStats = getJsonSummaryStats();
//jsonSummonerRankedStats = getJsonRankedStats();
jsonSummonerMatchHistory = getJsonMatchHistory();
jsonSummonerLeagueInfo = getJsonSummonerLeagueInfo();
jsonSummonerRecentGames = getJsonSummonerRecentGames();
}
}
And here is the code for unpdateUI(c);
public void updateUI(CollectUserData c) {
if (c.canIContinue()) {
String league = "";
try {
league = c.getMundaneCurrentLeague();
} catch (JSONException e) {
e.printStackTrace();
}
setLeague(tierIV, league);
try {
summonerNameTV.setText(c.getSummonerName());
} catch (JSONException e) {
e.printStackTrace();
}
try {
tierTV.setText(c.getTier());
} catch (JSONException e) {
e.printStackTrace();
}
try {
leaguePointsTV.setText(c.getLeaguePoints() + "");
} catch (JSONException e) {
e.printStackTrace();
}
try {
winsTV.setText("W: " + c.getWins());
} catch (JSONException e) {
e.printStackTrace();
}
slashTV.setText("/");
try {
lossesTV.setText("L: " + c.getLosses());
} catch (JSONException e) {
e.printStackTrace();
}
try {
lastSeasonRankTV.setText("Season 4: " + c.getRankOfLastSeason());
} catch (JSONException e) {
e.printStackTrace();
}
try {
gameModeGameOneTV.setText(c.getGameType(1));
} catch (JSONException e) {
e.printStackTrace();
}
try {
gameOneChampion.setBackground(c.getChampionPicturePlayed(1));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneKillsTV.setText(c.getKills(1) + "");
} catch (JSONException e) {
e.printStackTrace();
}
gameOneSlashOneTV.setText(" / ");
try {
gameOneDeathsTV.setText(c.getDeaths(1) + "");
} catch (JSONException e) {
e.printStackTrace();
}
gameOneSlashTwoTV.setText(" / ");
try {
gameOneAssistsTV.setText(c.getAssists(1) + "");
} catch (JSONException e) {
e.printStackTrace();
}
try {
gameOneGoldTV.setText(c.getGold(1) + " K");
} catch (JSONException e) {
e.printStackTrace();
}
try {
gameOneCSTV.setText(" " + c.getMinionsKilled(1) + " CS");
} catch (JSONException e) {
e.printStackTrace();
}
boolean gameOneWon = true;
try {
gameOneWon = c.isGameWon(1);
} catch (JSONException e) {
e.printStackTrace();
}
if (gameOneWon) {
gameOneTitleBar.setBackgroundColor(Color.parseColor("#d604c429"));
gameOne.setBackgroundColor(Color.parseColor("#4600FF06"));
}
if (!gameOneWon) {
gameOneTitleBar.setBackgroundColor(Color.parseColor("#d6ff0100"));
gameOne.setBackgroundColor(Color.parseColor("#4fff0100"));
}
try {
gameOneItemOneIV.setBackground(c.getItemFromMatchHistory(1, 0, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneItemTwoIV.setBackground(c.getItemFromMatchHistory(1, 1, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneItemThreeIV.setBackground(c.getItemFromMatchHistory(1, 2, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneItemFourIV.setBackground(c.getItemFromMatchHistory(1, 3, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneItemFiveIV.setBackground(c.getItemFromMatchHistory(1, 4, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
gameOneItemSixIV.setBackground(c.getItemFromMatchHistory(1, 5, getResources()));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Drawable[] tempDrawable = null;
try {
tempDrawable = c.getTeamPlayerChampionIcon(1, 100);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (tempDrawable != null) {
gameOneTeamOnePlayerOne.setBackground(tempDrawable[0]);
gameOneTeamOnePlayerTwo.setBackground(tempDrawable[1]);
gameOneTeamOnePlayerThree.setBackground(tempDrawable[2]);
gameOneTeamOnePlayerFour.setBackground(tempDrawable[3]);
gameOneTeamOnePlayerFive.setBackground(tempDrawable[4]);
}
Drawable[] tempDrawable2 = null;
try {
tempDrawable2 = c.getTeamPlayerChampionIcon(1, 200);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (tempDrawable != null) {
gameOneTeamTwoPlayerOne.setBackground(tempDrawable2[0]);
gameOneTeamTwoPlayerTwo.setBackground(tempDrawable2[1]);
gameOneTeamTwoPlayerThree.setBackground(tempDrawable2[2]);
gameOneTeamTwoPlayerFour.setBackground(tempDrawable2[3]);
gameOneTeamTwoPlayerFive.setBackground(tempDrawable2[4]);
}
String[] tempString = null;
try {
tempString = c.getTeamPlayerName(1, 100);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (tempString != null) {
gameOneTeamOnePlayerOneName.setText(tempString[0]);
gameOneTeamOnePlayerTwoName.setText(tempString[1]);
gameOneTeamOnePlayerThreeName.setText(tempString[2]);
gameOneTeamOnePlayerFourName.setText(tempString[3]);
gameOneTeamOnePlayerFiveName.setText(tempString[4]);
}
String[] tempString2 = null;
try {
tempString2 = c.getTeamPlayerName(1, 200);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (tempString2 != null) {
gameOneTeamTwoPlayerOneName.setText(tempString2[0]);
gameOneTeamTwoPlayerTwoName.setText(tempString2[1]);
gameOneTeamTwoPlayerThreeName.setText(tempString2[2]);
gameOneTeamTwoPlayerFourName.setText(tempString2[3]);
gameOneTeamTwoPlayerFiveName.setText(tempString2[4]);
}
}
}
Anyone know a fix for this?
Thanks ☺
You need to introduce multi threading. The r.run() command needs to be started by another thread.
http://developer.android.com/training/multiple-threads/index.html
You are running the Runnable in the UI Thread and this is why you are getting your UI blocked until it finished.
Try something like this:
Button button = (Button) findViewById(R.id.search_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(summonerNameET.getWindowToken(), 0);
final CollectUserData c = new CollectUserData();
c.setRegion(region);
c.setSummonerName(summonerNameET.getText().toString());
new Thread(new Runnable() {
#Override
public void run() {
try {
c.setUpTheJSONs();
updateUI(c);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}).start();
}
});
Put your downloading files in a separate thread. Use AsyncTask or Java Thread. Just simply.
class MyTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
//Show progress dialog, if u want
}
#Override
protected Void doInBackground(Void... params) {
//Do all your processing here!
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//Return some result to UI
}
}
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;
}
}
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();
}
I currently am designing an app which needs to connect to a device, write/read data, and close the connection reliably. Currently I have the write/read solid. My disconnect and then reconnect is terribly unreliable, and often actually crash the phone.. and sometimes Eclipse. I've been looking through numerous articles trying to figure it out and.. no luck..
****CONNECT FUNCTION**
public boolean connect()
{
ConfigData.getInstance();
BluetoothSocket tmp = null;
BluetoothDevice device = ConfigData.m_SharedBluetoothDevice;
Method m;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);//(BluetoothSocket)
m.invoke(device, 1);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
ConfigData.m_SharedBluetoothSocket = tmp;
try {
ConfigData.m_SharedBluetoothSocket.connect();
ConfigData.bIsBTConnected = true;
} catch (IOException e) {
try {
closeSocket();
m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e1) {
e.printStackTrace();
} catch (IllegalAccessException e1) {
e.printStackTrace();
} catch (InvocationTargetException e1) {
e.printStackTrace();
}
ConfigData.m_SharedBluetoothSocket = tmp;
try {
ConfigData.m_SharedBluetoothSocket.connect();
ConfigData.bIsBTConnected = true;
} catch (IOException e1) {
ConfigData.m_BluetoothException += e1.toString();
ConfigData.bIsBTConnected = false;
return false;
}
e.printStackTrace();
return true;
}
return true;
}
****Disconnect Function**
public void destroySocket()
{
try {
if(m_InStream != null)
{
m_InStream.close();
m_InStream = null;
}
if(m_OutStream != null)
{
m_OutStream.close();
m_OutStream = null;
}
if(ConfigData.m_SharedBluetoothSocket != null)
{
ConfigData.m_SharedBluetoothSocket.close();
ConfigData.m_SharedBluetoothSocket = null;
}
if(m_InStream == null && m_OutStream == null && ConfigData.m_SharedBluetoothSocket == null)
{
ConfigData.bIsBTConnected = false;
}
} catch (IOException e1) {
m_InStream = null;
m_OutStream = null;
ConfigData.m_SharedBluetoothSocket = null;
e1.printStackTrace();
}
}
So the disconnect is successful and returns everything null. The problem is that when I reconnect it blocks at the 2nd connect attempt and will either just sit there or completely crash the phone, causing several reboots.
Does anyone have any advice here? Its been extremely frustrating. Any help would be much appreciated!!
Thanks and Gig 'Em!
TxAg
What phone are you using? What OS? See this answer:
Disconnect a bluetooth socket in Android
The close is actually not working properly on some HTC 2.1update1 phones
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();
}