Setting style for dynamically added buttons - java

I'm trying to set the style to a dynamically added buttons in an activity but the buttons are still with the default stlye.
Here is my activity
package com.sample.Learn_German_Quiz;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import static android.R.interpolator.linear;
public class LessonChooserActivity extends AppCompatActivity {
private GrammarBank grammarBank = new GrammarBank();
public String getSelectedLesson() {
return selectedLesson;
}
public void setSelectedLesson(String selectedLesson) {
this.selectedLesson = selectedLesson;
}
private String selectedLesson;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson_chooser);
grammarBank.initLessons(getApplicationContext());
setupButton();
}
private void setupButton() {
Context newContext = new ContextThemeWrapper(getBaseContext(), R.style.button_learn);
for (int i = 0; i <= 5; i++) {
LinearLayout layout = (LinearLayout) findViewById(R.id.linearTest);
layout.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(newContext);
//Button btn = new Button(this);
btn.setText(grammarBank.getLessonTitle(i));
layout.addView(btn);
}
}
}
and the layout file for the activity
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="#dimen/fab_margin"
android:id="#+id/linearTest">
<Button
style="#style/button_learn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ButtonTest"
android:ems="10"
android:text="Test"
android:textColor="#ffff"
android:padding="16dp"
android:onClick="onClick"
/>
</LinearLayout>
and the style I want to give to my buttons:
<style name="button_learn" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:background">#drawable/button_rounded</item>
<item name="android:layout_margin">20dp</item>
</style>
button_rounded.xml file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="50dp"></corners>
<solid android:color="#4F535F"></solid>
</shape>
</item>
</selector>
They are showing like this:
and I want them like this:

Try this one
LinearLayout layout = (LinearLayout) findViewById(R.id.linearTest);
layout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i <= 5; i++) {
Button btn= new Button(this, null,R.style.button_learn);
btn.setText(grammarBank.getLessonTitle(i));
layout.addView(btn);
}

Have you tried using another Button constructor that takes also int defStyleAttr? If you pass R.style.button_learn as defStyleAttr then it should work.

Related

Add more than one element to set content view

i have a app set up which loads a website into the app which works fine, when i try to add another element to the content view the app crashes without a error, How can i add more than one element to my set content view? ive tried it with frame layout and more but they all dont seem to work.
Here is my code
package com.example.fiercepcs;
import android.content.Context;
import android.os.Bundle;
import com.example.fiercepcs.databinding.ActivityMainBinding;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.app.Application;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout screenLayout = new LinearLayout(this);
screenLayout.setOrientation(LinearLayout.VERTICAL);
Button button = (Button) findViewById(R.id.button);
WebView myWebView = new WebView(getApplicationContext());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.fiercepc.co.uk");
setContentView(myWebView);
setContentView(button);
}
}
my activity main xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.FiercePcs.AppBarOverlay" />
<include
android:id="#+id/include"
layout="#layout/content_main" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:translationZ="-5dp">
</WebView>
<Button
android:id="#+id/button"
android:layout_width="312dp"
android:layout_height="179dp"
android:scaleX="1"
android:scaleY="1"
android:text="#string/button"
android:visibility="visible"
app:layout_anchor="#+id/include"
app:layout_anchorGravity="center" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
your app crashes because your are trying to set button view and webView as the root layout, don't do that, inflate or bind the root of your layout with ActivityMainBinding
do it like this :
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindnig = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
LinearLayout screenLayout = new LinearLayout(this);
screenLayout.setOrientation(LinearLayout.VERTICAL);
Button button = (Button) findViewById(R.id.button);
WebView myWebView = new WebView(getApplicationContext());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.fiercepc.co.uk");
}
}

Lottie Animation not working with setExitSharedElementCallback android

I am working on Android with a Lottie animation. I implemented a shared transition on Lottie from one activity to another activity. But this is not Working. What am I doing wrong in my setExitSharedElementCallback method?
MainActivity.java
package com.example.trans;
import android.app.ActivityOptions;
import android.app.SharedElementCallback;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.airbnb.lottie.LottieAnimationView;
public class MainActivity extends AppCompatActivity {
LottieAnimationView lottieAnimationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lottieAnimationView = findViewById(R.id.animation_logo);
setExitSharedElementCallback(new SharedElementCallback() {
#Override
public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, RectF screenBounds) {
int bitmapWidth = Math.round(screenBounds.width());
int bitmapHeight = Math.round(screenBounds.height());
Bitmap bitmap = null;
if (bitmapWidth > 0 && bitmapHeight > 0) {
Matrix matrix = new Matrix();
matrix.set(viewToGlobalMatrix);
matrix.postTranslate(-screenBounds.left, -screenBounds.top);
bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.concat(matrix);
sharedElement.draw(canvas);
}
return bitmap;
}
});
findViewById(R.id.start_animation).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, lottieAnimationView, "simple_activity_transition");
startActivity(intent, options.toBundle());
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context=".MainActivity">
<Button
android:id="#+id/start_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/animation_logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="click Me" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/animation_logo"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:transitionName="simple_activity_transition"
app:lottie_autoPlay="true"
app:lottie_loop="false"
app:lottie_rawRes="#raw/dm"
app:lottie_renderMode="hardware"
app:lottie_speed="1.2"
tools:ignore="MissingConstraints" />
</RelativeLayout>
activityMain2.java
package com.example.trans;
import androidx.appcompat.app.AppCompatActivity;
import android.app.SharedElementCallback;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import com.airbnb.lottie.LottieAnimationView;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setEnterSharedElementCallback(new SharedElementCallback() {
#Override
public View onCreateSnapshotView(Context context, Parcelable snapshot) {
View view = new View(context);
view.setBackground(new BitmapDrawable((Bitmap) snapshot));
return view;
}
#Override
public void onSharedElementStart(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
ImageView sharedElement = findViewById(R.id.animation_logo);
for (int i = 0; i < sharedElements.size(); i++) {
if (sharedElements.get(i) == sharedElement) {
View snapshot = sharedElementSnapshots.get(i);
Drawable snapshotDrawable = snapshot.getBackground();
sharedElement.setBackground(snapshotDrawable);
sharedElement.setImageAlpha(0);
forceSharedElementLayout();
break;
}
}
}
private void forceSharedElementLayout() {
ImageView sharedElement = findViewById(R.id.animation_logo);
int widthSpec = View.MeasureSpec.makeMeasureSpec(sharedElement.getWidth(),
View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(sharedElement.getHeight(),
View.MeasureSpec.EXACTLY);
int left = sharedElement.getLeft();
int top = sharedElement.getTop();
int right = sharedElement.getRight();
int bottom = sharedElement.getBottom();
sharedElement.measure(widthSpec, heightSpec);
sharedElement.layout(left, top, right, bottom);
}
#Override
public void onSharedElementEnd(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
ImageView sharedElement = findViewById(R.id.animation_logo);
sharedElement.setBackground(null);
sharedElement.setImageAlpha(255);
}
});
}
}
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#000"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/animation_logo"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:transitionName="simple_activity_transition"
app:lottie_autoPlay="true"
app:lottie_loop="false"
app:lottie_rawRes="#raw/dm"
app:lottie_renderMode="hardware"
app:lottie_speed="1.2" />
</RelativeLayout>
Styles
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/explode</item>
</style>

How to make a fixed BottomNavigationView with title below, no shifted navigation

I am having difficulties in making fixed BottomNavigationView like in the Photo below: OriginalPhoto.png
Here is my own project's BottomNavigationView: MyPhoto.png
Also, I want the title below icons visible all the time, not just when clicked. How can I do this??
Note: I have read all stackoverflow articles and followed other external links but no tangible results have gained. Please help me to figure this subtle problem out. Would be better if only with XML to solve this problem, rather than java code.
Here is my source code for activity_menu.xml, btw, it is not activity_main.xml because i have used main activity for my login page. This activity is after login page: `
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="datasite.com.konnex.Menu"
android:background="#ffffff">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3fc0ea">
<ImageButton
android:layout_width="120dp"
android:layout_height="38dp"
android:background="#drawable/lg1"
android:layout_marginLeft="130dp"
android:layout_marginStart="130dp"/>
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginStart="90dp"/>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2">
<FrameLayout
android:layout_width="191dp"
android:layout_height="150dp"
android:id="#+id/fm_cases"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/cases"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="35dp"
android:id="#+id/img_cases" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cases"
android:layout_marginLeft="70dp"
android:layout_marginStart="70dp"
android:layout_marginTop="128dp"
android:textSize="18sp"
android:textColor="#424242"/>
<TextView
android:layout_width="22dp"
android:layout_height="wrap_content"
android:text="#string/string_1"
android:textColor="#FFFFFF"
android:textSize="17sp"
android:layout_marginLeft="122dp"
android:layout_marginStart="122dp"
android:layout_marginTop="35dp"
android:background="#E91E63"
android:id="#+id/notif_cases"/>
</FrameLayout>
</GridLayout>
</RelativeLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fffafa"
app:menu="#menu/navigation"
app:itemIconTint="#color/dark"
app:itemTextColor="#color/dark"
android:animateLayoutChanges="false"
android:splitMotionEvents="false"
android:fitsSystemWindows="true"/>
</LinearLayout>`
Here is the source code for menu.java. it is also after main.java activity which is for login used.
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class Menu extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_about:
return true;
case R.id.nav_location:
return true;
case R.id.nav_phone:
return true;
case R.id.nav_home:
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
FrameLayout fml = (FrameLayout) findViewById(R.id.fm_cases);
fml.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Menu.this, Cases.class);
startActivity(i);
}
});
}
}
Thank you in advance!!! I really need your help Guys)
My Navigation.xml:
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_about" />
<item
android:id="#+id/nav_location"
android:icon="#drawable/nav_location"
android:title="#string/title_location"
/>
<item
android:id="#+id/nav_phone"
android:icon="#drawable/nav_call"
android:title="#string/title_phone"
/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home"
/>
</menu>
Try using AHBottomNavigation library, I suggest this because is has simple implementation considering your case and reduces your work to create another menu.xml file and handling menu option methods in your code. Just add this line in your gradle file.
compile 'com.aurelhubert:ahbottomnavigation:2.0.6'
It will take care of the layout issue you have moreover, it has lots of customization options. You can read further here, but for your case the simple use to keep titles is..
public class MyActivity extends AppCompatActivity{
#BindView(R.id.bottom_navigationbar)
AHBottomNavigation bottomNavigation;
onCreate(){
bottomNavigation.addItem(new AHBottomNavigationItem("Title1", iconID1);
bottomNavigation.addItem(new AHBottomNavigationItem("Title2", iconID2);
bottomNavigation.addItem(new AHBottomNavigationItem("Title3", iconID3);
bottomNavigation.addItem(new AHBottomNavigationItem("Title4", iconID4);
bottomNavigation.setAccentColor(ContextCompat.getColor(this, themeColor));
//will always show titles
bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
}}
setUpClickListener.
bottomNavigation.setOnTabSelectedListener((position, wasSelected) -> {
if (position == 0 && !wasSelected) {
} else if (position == 1 && !wasSelected) {
} else if (position == 2 && !wasSelected) {
} else if (position == 3 && !wasSelected) {
} else if (position == 4 && !wasSelected) {
}
return true;
}
);
Ok then try to add app:showAsAction in menu item
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_dashboard_black_24dp"
app:showAsAction="ifRoom"
android:title="#string/title_about" />
<item
android:id="#+id/nav_location"
android:icon="#drawable/nav_location"
app:showAsAction="ifRoom"
android:title="#string/title_location"
/>
<item
android:id="#+id/nav_phone"
android:icon="#drawable/nav_call"
app:showAsAction="ifRoom"
android:title="#string/title_phone"
/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
app:showAsAction="ifRoom"
android:title="#string/title_home"
/>
</menu>
If your problem not resolve then replace this one showAsAction="always|withText"
after then you can use this method for disabling shifted menu
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
Updated Menu.java: `package datasite.com.konnex;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.reflect.Field;
public class Menu extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_about:
return true;
case R.id.nav_location:
return true;
case R.id.nav_phone:
return true;
case R.id.nav_home:
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
//Add This
disableShiftMode((BottomNavigationView)findViewById(R.id.navigation))
FrameLayout fml = (FrameLayout) findViewById(R.id.fm_cases);
fml.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Menu.this, Cases.class);
startActivity(i);
}
});
}
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}
Updated Navigation.xml:
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_about"
app:showAsAction="always|withText"/>
<item
android:id="#+id/nav_location"
android:icon="#drawable/nav_location"
android:title="#string/title_location"
app:showAsAction="always|withText" />
<item
android:id="#+id/nav_phone"
android:icon="#drawable/nav_call"
android:title="#string/title_phone"
app:showAsAction="always|withText"/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home"
app:showAsAction="always|withText" />
</menu>
`

How to change imageview shape to round on menu item selected in android?

I am new to android and now I am stuck at one point.
i want to change shape of imageview when user click on menu item Circle.
my circle_shape.xml file in drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomRightRadius="10dp"/>
<solid android:color="#FF44FF"/>
</shape>
my java File Shape.java
package com.example.mixapp;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
public class Shape_with_Spinner extends Activity {
Spinner shapeSpinner;
ImageView imgMaster;
private Drawable drawable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shape_with__spinner);
try {
imgMaster = (ImageView) findViewById(R.id.imgMaster);
Toast.makeText(this, "in try part ", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.i("Magaj jay che have", ""+e.getLocalizedMessage());
Toast.makeText(this, "in catch part : "+e.getMessage(), Toast.LENGTH_LONG).show();
}
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.rect:
drawable = getDrawable(R.drawable.circle_shape);
break;
default:
break;
}
return super.onMenuItemSelected(featureId, item);
}
}
and my Shape.xml file in layout folder.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.mixapp.Shape_with_Spinner" >
<Spinner
android:id="#+id/shapeSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:entries="#array/Spinner_value"/>
<ImageView
android:id="#+id/imgToShape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
any help would be appreciable. :)
I would recommend you to use Picasso or Glide for it. This libs not only allowed you to async load images, but you could also set up the transformations(here is ready to use circle transformations Picasso, Glide) for any image that should be loaded.

java android Multiple addViews on a layout with one xml drawable

I'm trying get multiple copies of a drawable shape on the layout, but change the color each time. I can't tell if the 2nd imageview is just landing on top of the first. I haven't been able to get them placed in distinct locations to troubleshoot further. Does each instance needs it's own params?
#drawable/circle (circle.xml)
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#FF888888"/>
<size
android:width="60dp"
android:height="60dp"/>
</shape>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="#+id/root">
</RelativeLayout>
MainActivity.java
package com.example.thowell09156277.newcircleaddview;
import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Let's create the missing ImageView
ImageView image = new ImageView(this);
ImageView image2 = new ImageView(this);
// Now the layout parameters, these are a little tricky at first
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.RIGHT_OF, RelativeLayout.TRUE);
image.setScaleType(ImageView.ScaleType.MATRIX);
image.setImageResource(R.drawable.circle);
image.setImageTintMode(PorterDuff.Mode.OVERLAY);
image.setImageTintList(ColorStateList.valueOf(Color.YELLOW));
int imageId;
imageId = image.generateViewId();
image.setId(imageId);
int imageId2;
imageId2 = image2.generateViewId();
image2.setId(imageId2);
image2.setScaleType(ImageView.ScaleType.MATRIX);
image2.setImageResource(R.drawable.circle);
image2.setImageTintMode(PorterDuff.Mode.OVERLAY);
image2.setImageTintList(ColorStateList.valueOf(Color.MAGENTA));
image2.setId(imageId2);
//image.setOnTouchListener(this);
// Let's get the root layout and add our ImageView
RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
//params.setMargins(0, 0, 80, 80);
layout.addView(image, 0, params);
//params.setMargins(80, 0, 80, 160);
//params.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
layout.addView(image2, 1, params);
}

Categories