Excuse the title - not sure how to word this question.
I created a file called rectangle.xml inside res/drawable. It is an extremely basic rectangle shape, the I'm using with an ImageView within a custom listview.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FF0000" />
<corners android:radius="4dp"/>
</shape>
Using it like so:
<ImageView
android:id="#+id/row_colorRect"
android:layout_width="24dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/rectangle" />
I want to be able to access that rectangle's solid property so I can change its color on the fly. I can't access that property through the ImageView .. so I imagine I have to somehow dig into the children of the ImageView, find the shape, and change it that way.
How do I go about this?
Thanks!
Just tried it and I managed to do it.
ImageView imageView = (ImageView) findViewById(R.id.row_colorRect);
((GradientDrawable) imageView.getDrawable()).setColor(Color.RED);
Accessing the ImageView GradientDrawable, that represents your rectangle.xml resource, you can change its solid color, among other attributes.
Try this way,hope this will help you to solve your problem.
shape.xml (Drawable)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FF0000" />
<corners android:radius="4dp"/>
</shape>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/shape"/>
</LinearLayout>
MyActivity.java
public class MyActivity extends Activity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GradientDrawable sd = (GradientDrawable) imageView.getBackground().mutate();
sd.setColor(Color.parseColor("#CCC111"));
sd.invalidateSelf();
}
});
}
}
Related
I have two scripts
Activity:
public class HomeActivity extends AppCompatActivity {
public void Init()
{
//Do something
}
}
Fragment:
public class HomeFragment extends Fragment {
private FragmentHomeBinding binding;
#Override
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
HomeActitvity homeActitvity = new HomeActivity()
homeActivity.Init()
return root;
}
}
When I run app I get this errors and app closes, but when I comment homeActivity.Init() app opens
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.limeinc.dabloonsbank/com.package.name.MainActivity}: java.lang.IllegalStateException
Binary XML file line #21 in com.package.name:layout/activity_main: Error inflating class fragment
this is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment //this is 21 line
android:id="#+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
I am using navigation bar template by android studio
I cant find solution in the internet, because they just dont work
I am using a swipe button from com.ebanx:swipe-button library in my application.
The swipe button in the emulator works perfectly, but when I compile the program (app-debug.apk) and install the program and start it, the swipe button doesn't appear for some reason.
What could be the reason. I developed the program from Android 8.
Thank you in advance for your help.
Here is the code: Here the activity_enter.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="#drawable/plejadian"
android:orientation="horizontal"
android:visibility="visible"
tools:context=".EnterActivity"
tools:visibility="visible">
<com.ebanx.swipebtn.SwipeButton
android:id="#+id/swipe_button"
android:layout_width="563dp"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="469dp"
android:layout_marginTop="501dp"
android:layout_marginEnd="248dp"
android:layout_marginBottom="199dp"
app:button_background="#drawable/swipe_button_shape"
app:button_bottom_padding="10dp"
app:button_image_disabled="#drawable/ic_right_arrow_simple"
app:button_image_enabled="#drawable/ic_right_arrow_double"
app:button_left_padding="15dp"
app:button_right_padding="2dp"
app:button_top_padding="10dp"
app:inner_text=">> KÉRLEK LÉPJ BE >>"
app:inner_text_background="#drawable/swipe_inner_text_shape"
app:inner_text_bottom_padding="28dp"
app:inner_text_color="#C9DC17"
app:inner_text_size="25sp"
app:inner_text_top_padding="28dp" />
</RelativeLayout>
Here the java:
package com.example.firsttutorial;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import com.ebanx.swipebtn.SwipeButton;
public class EnterActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter);
SwipeButton swipe_button = findViewById(R.id.swipe_button);
swipe_button.setOnStateChangeListener(active -> startActivity(new Intent(EnterActivity.this,CompanyIntroActivity.class)));
}
}
Here the drawable library:
swipe_button_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00374E"/>
<corners android:radius="50dp"/>
</shape>
swipe_inner_text_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00374E"/>
<corners android:radius="50dp"/>
</shape>
And here the com.ebanx:swipe-button library -> build.gradle(:app)
implementation 'com.ebanx:swipe-button:0.4.0'
So that’s it, I would really like to thank everyone for their help.
Regards
Peter
I was having an issue with the ebanx swipe button dependency not being recognized and fixed it by adding jcenter() under pluginManagement and dependencyResolutionManagement in the settings.gradle file
I want to use a Navigation Drawer within my App by using activities instead of fragments. By using Layout inflater in the different activities I tried to keep the same navigation drawer as in the MainActivity, but to change the inner layout. My problem is that somehow the layout of the MainActivity cant be replaced. When clicking on a item in the navigation drawer the new Activtiy opens and starts the layoutinflater. But the Layout Of the Main Activity remains, while the layout of the other Activity is just added.
public class MainActivity extends AppCompatActivity implements
TabLayout.OnTabSelectedListener{
public static String TAG = "MainActivity";
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "CREATED");
NavigationView navigationView = (NavigationView)
findViewById(R.id.navigation_view);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, ^
toolbar, R.string.drawer_open, R.string.drawer_closed);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
navigationView.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Mensa_in_der_Nähe:
Intent anIntent = new Intent(getApplicationContext(),
LocationActivity.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
}
return false;
}
});
Then the xml file of main activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusableInTouchMode="true">
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/overview_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="enterAlways|scroll"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/menu_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabMode="fixed" />
<android.support.design.widget.TabLayout
android:id="#+id/canteen_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/dish_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="?attr/actionBarSize"
android:background="#android:color/white"
app:menu="#menu/drawer_menu" />
the other activity:
public class LocationActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = (FrameLayout)
findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.activity_location,
contentFrameLayout);
}}
And the xml file of the other activity:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TimePicker
android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
How can I make a radio button group and a TextField, so that whenever I click on TextField the radio button automatically changes from one to another?
Please give me a block of code for this.
If I understood your question correctly, you want the radioButton checked status change once the textView click. You can achieve by this way.
public class MainActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton,male,female;
private TextView btnDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);
btnDisplay=(TextView)findViewById(R.id.textView3);
male = (RadioButton)findViewById(R.id.radioButton);
female=(RadioButton)findViewById(R.id.radioButton2);
btnDisplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId=radioSexGroup.getCheckedRadioButtonId();
radioSexButton=(RadioButton)findViewById(selectedId);
if(radioSexButton.getText().toString().equals("Male"))
{
female.setChecked(true);
}
else
{
male.setChecked(true);
}
}
});
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<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">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_marginTop="58dp"
android:weightSum="1"
android:id="#+id/radioGroup"
android:layout_alignRight="#+id/textView3"
android:layout_alignEnd="#+id/textView3">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="55dp"
android:text="Male"
android:id="#+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:layout_weight="0.13" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me "
android:id="#+id/textView3"
android:textSize="35dp"
android:layout_below="#+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"></TextView>
</RelativeLayout>
https://www.tutorialspoint.com/android/android_radiogroup_control.htm
check this link , to use radio group with textviews
I am trying to get an image to move to certain x and y withing this application.I have tried with layoutParams via searching but it is not working here is my full application code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:screenOrientation="portrait">
<ImageView android:id="#+id/ImageView01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:scaleType="fitXY" android:src="#drawable/myImage1" android:layout_marginTop="5dp"></ImageView>
<FrameLayout android:id="#+id/FrameLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/marker" android:src="#drawable/marker"></ImageView>
</FrameLayout>
</LinearLayout>
myapp Java
ImageView marker;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
marker=(ImageView)findViewById(R.id.marker);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(35, 90, 0, 0);
marker.setLayoutParams(lp);
marker.invalidate();
}
}
My end goal is to move the marker image over myImage1 at different Locations
You can get the size of the phone with this code:
DisplayMetrics metrics = new DisplayMetrics();
((Activity)c).getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = ((int)metrics.xdpi);
screenHeight = ((int)metrics.ydpi);
You can use setPadding and the width and height of the phone can be got by the code above. I had positioning problems too and this was the solution.