android-widget

How to add views dynamically to a RelativeLayout already declared in the xml layout?

心不动则不痛 提交于 2019-12-17 07:31:08
问题 I declared a RelativeLayout in a xml layout file. Now I want to add Views from code to the existing Layout. I added a Button dynamically to this existing layout as below through code: rLayout = (RelativeLayout)findViewById(R.id.rlayout); LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); Button tv1 = new Button(this); tv1.setText("Hello"); tv1.setLayoutParams(lprams); tv1.setId(1); rLayout.addView(tv1); Now I need to add another Button to the right of

Android: Change Tab Text Color Programmatically

删除回忆录丶 提交于 2019-12-17 07:30:55
问题 I've a TabHost like this: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabhost" android:background="@drawable/tabs_bg"> <LinearLayout android:id="@+id/LinearLayout01" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_height="wrap

how to play audio file in android

╄→гoц情女王★ 提交于 2019-12-17 07:17:20
问题 I have a mp3 file in my android mobile, lets it's a xyz.mp3 somewhere in my sdcard. How to play it through my application? 回答1: Simply you can use MediaPlayer and play the audio file. Check out this nice example for playing Audio: public void audioPlayer(String path, String fileName){ //set up MediaPlayer MediaPlayer mp = new MediaPlayer(); try { mp.setDataSource(path + File.separator + fileName); mp.prepare(); mp.start(); } catch (Exception e) { e.printStackTrace(); } } 回答2: @Niranjan, If

Dynamically creating Buttons and setting onClickListener

旧城冷巷雨未停 提交于 2019-12-17 07:11:59
问题 I have problem with handling dynamically created Buttons on Android. I'm creating N buttons and I have to do the same method when button is clicked but I have to know which button is clicked. for (int i = 0; i < NO_BUTTONS; i++){ Button btn = new Button(this); btn.setId(2000+i); ... btn.setOnClickListener((OnClickListener) this); buttonList.addView(btn); list.add(btn); Cucurrently I'm adding ID to every button and I'm using the method below to see which button was clicked. (line btn.setId

Android: Cloning a drawable in order to make a StateListDrawable with filters

别说谁变了你拦得住时间么 提交于 2019-12-17 07:04:21
问题 I'm trying to make a general framework function that makes any Drawable become highlighted when pressed/focused/selected/etc . My function takes a Drawable and returns a StateListDrawable, where the default state is the Drawable itself, and the state for android.R.attr.state_pressed is the same drawable, just with a filter applied using setColorFilter . My problem is that I can't clone the drawable and make a separate instance of it with the filter applied. Here is what I'm trying to achieve:

Incorrect Coordinates From getLocationOnScreen/getLocationInWindow

笑着哭i 提交于 2019-12-17 06:32:21
问题 A call to getLocationOnScreen() or getLocationInWindow() both give me a top/Y coordinate that is about ~30px (status/notifications bar's height) too far down. The left/X coordinate is dead on. As I hinted above, I believe the difference is because of the status/notification bar... I could be wrong. I think I can solve this if I can determine the size of the notification bar but, I'm having trouble doing just that. Any help would be greatly appreciated. 回答1: I ended up solving this issue by

Add padding on view programmatically

对着背影说爱祢 提交于 2019-12-17 05:34:11
问题 I am deveoping Android v2.2 app. I have a fragment . In the onCreateView(...) callback of my fragment class, I inflate an layout to the fragment like below: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.login, null); return view; } The above inflated layout file is (login.xml): <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Add padding on view programmatically

做~自己de王妃 提交于 2019-12-17 05:33:06
问题 I am deveoping Android v2.2 app. I have a fragment . In the onCreateView(...) callback of my fragment class, I inflate an layout to the fragment like below: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.login, null); return view; } The above inflated layout file is (login.xml): <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Heterogeneous GridLayout

孤人 提交于 2019-12-17 03:54:21
问题 UPDATE - my working solution: https://stackoverflow.com/a/21233824/902172 I am trying to implement the layout below: I guess GridLayout is suitable for my needs but after 2 hours of struggle I couldn't create even a similar layout.. The layout is resizing itself wrongly , it exceeds the screen of the phone and it also does not span the specified rows and columns. Here I selected a button so you can see how it exceeds the boundaries: and here is the associated xml code: https://gist.github.com

Heterogeneous GridLayout

久未见 提交于 2019-12-17 03:54:09
问题 UPDATE - my working solution: https://stackoverflow.com/a/21233824/902172 I am trying to implement the layout below: I guess GridLayout is suitable for my needs but after 2 hours of struggle I couldn't create even a similar layout.. The layout is resizing itself wrongly , it exceeds the screen of the phone and it also does not span the specified rows and columns. Here I selected a button so you can see how it exceeds the boundaries: and here is the associated xml code: https://gist.github.com