Posts

Showing posts with the label Android Layout

Change Linear Layout Top Margin Programmatically Android

Answer : layout = (LinearLayout) findViewById(R.id.layoutbtnlinear_aboutme); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)layout.getLayoutParams(); params.setMargins(0, 50, 0, 0); layout.setLayoutParams(params); LayaoutParams usually create confusion while setting margin because of their parent layout... So this MarginLayoutParams is very useful which works with all layouts. Java Code MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); params.width = 200; //Ths value 200 is in px... Please convert in DP params.leftMargin = 100; params.topMargin = 200; Kotlin code val params: MarginLayoutParams = view!!.layoutParams as MarginLayoutParams params.width = 200 params.leftMargin = 100 params.topMargin = 200 This updates the top margin without the need to update the other margin values. LinearLayout layout = (LinearLayout) findViewById(R.id.your_linear_layout); LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)...

Android Shape With Gradient Border And Shadow

Image
Answer : Here you go Create your layout <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#c8c0c0" android:orientation="vertical"> <LinearLayout android:layout_width="120dp" android:layout_height="120dp" android:layout_centerInParent="true" android:background="@drawable/my_rectangle"> </LinearLayout> </RelativeLayout> Create my_rectangle.xml file inside drawable folder <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <gradient ...

Change TextInputEditText Hint Color

Answer : The fix for me was to set the hint and the textColor on TextInputLayout, rather than on TextInputEditText as described here: https://material.io/develop/android/components/text-input-layout/ <android.support.design.widget.TextInputLayout android:id="@+id/passwordTextInputLayout" android:hint="@string/add_nitrogen_template_name_label" android:textColorHint="@color/warm_grey"> <android.support.design.widget.TextInputEditText android:textAppearance="@style/TextAppearance.Regular" android:textColor="@color/white"/> </android.support.design.widget.TextInputLayout> have you tried also these attributes for your AppTheme ? <item name="colorControlNormal">@color/primary</item> <item name="colorControlHighlight">@color/warm_grey</item> <item name="colorControlActivated">@color/warm_g...

Android Floating View (over Other Views)

Answer : A FrameLayout allows you to have a view overlapping another view. I'm not sure it makes sense to have them with only one child view, as you have in your example. Try having a FrameLayout at the highest level, with your "static" view as the first child element, and the floating menu as the second child. The developer documents have a good overview the layout types, it might help you get started.

Android Toolbar Center Title And Custom Font

Image
Answer : To use a custom title in your Toolbar all you need to do is remember is that Toolbar is just a fancy ViewGroup so you can add a custom title like so: <android.support.v7.widget.Toolbar android:id="@+id/toolbar_top" android:layout_height="wrap_content" android:layout_width="match_parent" android:minHeight="?attr/actionBarSize" android:background="@color/action_bar_bkgnd" app:theme="@style/ToolBarTheme" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Toolbar Title" android:layout_gravity="center" android:id="@+id/toolbar_title" /> </android.support.v7.widget.Toolbar> This means that you can style the TextView however you would like because it's just a regular TextView . So in your activity you can access the title...

Android: Set View Style Programmatically

Answer : Technically you can apply styles programmatically, with custom views anyway: private MyRelativeLayout extends RelativeLayout { public MyRelativeLayout(Context context) { super(context, null, R.style.LightStyle); } } The one argument constructor is the one used when you instantiate views programmatically. So chain this constructor to the super that takes a style parameter. RelativeLayout someLayout = new MyRelativeLayout(new ContextThemeWrapper(this,R.style.RadioButton)); Or as @Dori pointed out simply: RelativeLayout someLayout = new RelativeLayout(new ContextThemeWrapper(activity,R.style.LightStyle)); Now in Kotlin: class MyRelativeLayout @JvmOverloads constructor( context: Context, attributeSet: AttributeSet? = null, defStyleAttr: Int = R.style.LightStyle, ) : RelativeLayout(context, attributeSet, defStyleAttr) or val rl = RelativeLayout(ContextThemeWrapper(activity, R.style.LightStyle)) What worked for me: Button b = new Button(new ...

Android WebView Not Loading URL

Answer : Did you added the internet permission in your manifest file ? if not add the following line. <uses-permission android:name="android.permission.INTERNET"/> hope this will help you. EDIT Use the below lines. public class WebViewDemo extends Activity { private WebView webView; Activity activity ; private ProgressDialog progDailog; @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); activity = this; progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true); progDailog.setCancelable(false); webView = (WebView) findViewById(R.id.webview_compontent); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setLoadWithOverviewMode(t...

Android Image Scale Animation Relative To Center Point

Answer : 50% is center of animated view. 50%p is center of parent <scale android:fromXScale="1.0" android:toXScale="1.2" android:fromYScale="1.0" android:toYScale="1.2" android:pivotX="50%" android:pivotY="50%" android:duration="175"/> The answer provided by @stevanveltema and @JiangQi are perfect but if you want scaling using code, then you can use my answer. // first 0f, 1f mean scaling from X-axis to X-axis, meaning scaling from 0-100% // first 0f, 1f mean scaling from Y-axis to Y-axis, meaning scaling from 0-100% // The two 0.5f mean animation will start from 50% of X-axis & 50% of Y-axis, i.e. from center ScaleAnimation fade_in = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); fade_in.setDuration(1000); // animation duration in milliseconds fade_in.setFillAfter(true); // If fillAfter is true, the tr...

Add Ripple Effect To My Button With Button Background Color?

Answer : Here is another drawable xml for those who want to add all together gradient background, corner radius and ripple effect: <?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorPrimaryDark"> <item android:id="@android:id/mask"> <shape android:shape="rectangle"> <solid android:color="@color/colorPrimaryDark" /> <corners android:radius="@dimen/button_radius_large" /> </shape> </item> <item android:id="@android:id/background"> <shape android:shape="rectangle"> <gradient android:angle="90" android:endColor="@color/colorPrimaryLight" android:startColor="@color/colorPrimary" ...

Android Button Drawable Tint

Image
Answer : You can achieve coloring the drawableleft on a button with this method: Step 1: Create a drawable resource file with bitmap as parent element as shown below and name it as ic_action_landscape.xml under the drawable folder <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@android:drawable/ic_btn_speak_now" android:tint="@color/white" /> Step 2: Create your Button control in your layout as below <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="@color/md_light_green_500" android:drawableLeft="@drawable/ic_action_landscape" android:drawablePadding="8dp" android:fontFamily="sans-serif" android:gravity="left|center_vertical" android:stateListAn...

Android Studio Suddenly Cannot Resolve Symbol R

Answer : Most of the time it is due to a bad XML file. XML files can be layout files, value files, or the Manifest file. Please check your xml files and try to rebuild the project. Sometimes cleaning the project and rebuilding it also works. In addition, make sure you do not have a drawable with an invalid name. I had a drawable with a numeric filename and that didn't sit well with Android so it failed to compile R.java. Downgrade Your Gradle Plugin Version No amount of cleaning, rebuilding and restarting would do the trick for me. The only thing that did the trick was downgrade our Gradle version from 3.4.0-alpha02 to 3.2.1 . So, instead of: dependencies { classpath 'com.android.tools.build:gradle:3.4.0-alpha02' } We used: dependencies { classpath 'com.android.tools.build:gradle:3.2.1' } After making that change and then doing a Gradle sync, everything worked. Not sure if it's related to the alpha release or to that version or if ...

Android Mirror Vector Drawable

Image
Answer : For those who Use ImageView or TextView or EditText Scale works perfectly. Use android:scaleX="-1" //To flip horizontally or android:scaleY="-1" //To flip vertically OR Try android:rotationX="180" // for horizontal android:rotationY="180" // for vertical OR Simply rotation="180" for vertical android:rotation="180" // for vertical Edit: Additional If you want to flip/mirror icons/drawable when changing language RTL/LTR ("Right To Left"/"Left To Right"), there is a nice way of doing so in android vector drawable just check the ckeckbox Enable auto mirroring for RTL layout . => Right Click on drawable folder => New => Vector Asset => Select drawable => check the Checkbox . I am using AndroidStudio 3.0.1 in Windows 10 . well, there is no need to create another vector image, you can do it with one single vector image just make sure you do the following st...