Can I have a click event for a complete Linear layout - xamarin

I have a linear layout and some buttons inside the layout.When click on Linear layout it should navigate to new page1, When click on button it should navigate to page2.

Do you want to achieve the result like this GIF?
If so, yes you can add the click event for your LinearLayout.There is my code.
<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:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/my_ll">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:id="#+id/button1"
/>
</LinearLayout>
Here is background code.Add the different click listeners for linearlayout and button.
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
LinearLayout my_ll = FindViewById<LinearLayout>(Resource.Id.my_ll);
Button button1 = FindViewById<Button>(Resource.Id.button1);
my_ll.Click += My_ll_Click;
button1.Click += Button1_Click;
}
private void Button1_Click(object sender, System.EventArgs e)
{
Intent intent = new Intent(this, typeof(Activity2));
StartActivity(intent);
}
private void My_ll_Click(object sender, System.EventArgs e)
{
Intent intent = new Intent(this,typeof(Activity1));
StartActivity(intent);
}
}

Related

xamarin Flyoutpage change text color

enter image description here
Hello friend
How can I change the color of the text in the title in the picture I am using flyoutpage
How can I change the color of the text in the title in the picture I am using flyoutpage
Do you mean the text color of tabs of TabbedPage in your screenshot?
If yes, then in android, you can try to create a Tabbar.xml in folder layout and adding following code:
<?xml version="1.0" encoding="utf-8" ?>
<com.google.android.material.tabs.TabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabTextColor="#color/your_unselected_text_color"
app:tabSelectedTextColor="#color/your_selected_text_color"
app:tabIndicatorColor="#color/your_indicator_color"
/>
Then inflate this layout with code:
FormsAppCompatActivity.TabLayoutResource = Resource.Layout.Tabbar;
The whole code of MainActivity.cs
      
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate (Bundle bundle)
            {
FormsAppCompatActivity.TabLayoutResource = Resource.Layout.Tabbar;
base.OnCreate (bundle);
            global::Xamarin.Forms.Forms.Init (this, bundle);
            LoadApplication (new App ());
            }
      }
On IOS, you can use custom renderer to achieve this.
[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabbedPageRenderer))]
namespace MyApp.iOS
{
public class TabbedPageRenderer : TabbedRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
TabBar.TintColor = UIColor.White;
TabBar.BarTintColor = UIColor.Black;
TabBar.BackgroundColor = UIColor.Gray;
}
}
}

XAMARIN : programatically created layout lost during page navigation between different activities

I have created a Xamarian Form.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<ScrollView
android:id="#+id/configureScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dynamicLinearlayout"
android:orientation="vertical"/>
</ScrollView>
</LinearLayout>
Programatically added a layout to Linearlayout inside the ScrollView.
LinearLayout dynamicL;
protected override void OnCreate(Bundle savedInstanceState)
{
//.....
dynamicL = FindViewById<LinearLayout>(Resource.Id.dynamicLinearLayout);
}
var layout = new LinearLayout(this);
layout.Orientation = Orientation.Horizontal;
layout.Id = 500;
Button btnRemove = new Button(this);
btnRemove.Text = "-";
btnRemove.TextAlignment = TextAlignment.Center;
layout.AddView(btnRemove);
//Add to scroll layout
dynamicL.AddView(layout);
As soon as I navigate to another page and come back it looses the layout. how can i save it?
After dynamically adding views to page.
Once I navigate to home page and comes back.
I create one sample about navigation between different activities, and come back, the layout still appear, please take a look the following code.
Activity_main.xml:
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/configureScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dynamicLinearlayout"
android:orientation="vertical"/>
</ScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="btn"
android:id="#+id/button1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="navigation"
android:id="#+id/button2"/>
</LinearLayout>
MainActivity.cs:
public class MainActivity : AppCompatActivity
{
private Button btn;
private LinearLayout dynamicL;
private Button btn1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
btn = FindViewById<Button>(Resource.Id.button1);
btn.Click += Btn_Click;
btn1 = FindViewById<Button>(Resource.Id.button2);
btn1.Click += Btn1_Click;
dynamicL = FindViewById<LinearLayout>(Resource.Id.dynamicLinearlayout);
}
private void Btn1_Click(object sender, EventArgs e)
{
var intent = new Intent(this, typeof(Activity1));
StartActivity(intent);
}
private void Btn_Click(object sender, EventArgs e)
{
var layout = new LinearLayout(this);
Button btn = new Button(this);
btn.Text = "btn";
btn.TextAlignment = Android.Views.TextAlignment.Center;
layout.AddView(btn);
dynamicL.AddView(layout);
}
}
When click Button to navigate to another activity, come back button in device, layout still appear.

OnTabChanged not called when click on current tab

I implemented a tabbed application with Xamarin.Android using TabHost and MvxTabsFragmentActivity.
I want to refresh the current tab when clicking on it the second time.
Is there any method like OnTabReselected from Android?
That's how I am creating the tabs, using TabHost:
<TabHost android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="7dp"
android:background="#drawable/gradient_border_top"
android:orientation="horizontal" />
<TabWidget android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_weight="0"
android:background="#color/white" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
And MainView:
[Activity(Theme = "#style/MyTheme", ScreenOrientation = ScreenOrientation.Portrait, WindowSoftInputMode = SoftInput.AdjustPan)]
public class MainView : MvxTabsFragmentActivity
{
public MainView() : base(Resource.Layout.main_layout, Resource.Id.actualtabcontent)
{
}
private static TabHost TabHost { get; set; }
public override void OnTabChanged(string tag)
{
var pos = TabHost.CurrentTab;
var tabView = TabHost.TabWidget.GetChildTabViewAt(pos);
tabView.FindViewById<ImageView>(Resource.Id.tabImage)
.SetColorFilter(new Color(ContextCompat.GetColor(Application.Context, Resource.Color.tabColorSelected)));
tabView.FindViewById<TextView>(Resource.Id.tabTitle)
.SetTextColor(new Color(ContextCompat.GetColor(Application.Context, Resource.Color.tabColorSelected)));
for (var i = 0; i < TabHost.TabWidget.ChildCount; i++)
{
if (pos != i)
{
var tabViewUnselected = TabHost.TabWidget.GetChildTabViewAt(i);
tabViewUnselected.FindViewById<ImageView>(Resource.Id.tabImage)
.SetColorFilter(new Color(ContextCompat.GetColor(Application.Context, Resource.Color.tabColor)));
tabViewUnselected.FindViewById<TextView>(Resource.Id.tabTitle)
.SetTextColor(new Color(ContextCompat.GetColor(Application.Context, Resource.Color.tabColor)));
}
}
base.OnTabChanged(tag);
}
}
// This is where I add all 4 tabs
protected override void AddTabs(Bundle args)
{
AddTab<TrackHomeView>(
args,
Mvx.IoCProvider.IoCConstruct<TrackHomeViewModel>(),
CreateTabFor(((int)TabIdentifier.TrackTab).ToString(), Resource.Drawable.ic_track_icon, Strings.Track));
AddTab<SendView>(
args,
Mvx.IoCProvider.IoCConstruct<SendViewModel>(),
CreateTabFor(((int)TabIdentifier.SendTab).ToString(), Resource.Drawable.ic_send_icon, Strings.Send));
if (MainViewModel.IsUserLoggedIn())
{
AddTab<ProfileView>(
args,
Mvx.IoCProvider.IoCConstruct<ProfileViewModel>(),
CreateTabFor(((int)TabIdentifier.ProfileTab).ToString(), Resource.Drawable.ic_profile_icon, Strings.Profile));
}
else
{
AddTab<CreateAccountView>(
args,
Mvx.IoCProvider.IoCConstruct<CreateAccountViewModel>(),
CreateTabFor(((int)TabIdentifier.ProfileTab).ToString(), Resource.Drawable.ic_profile_icon, Strings.Profile));
}
AddTab<MoreView>(
args,
Mvx.IoCProvider.IoCConstruct<MoreViewModel>(),
CreateTabFor(((int)TabIdentifier.MoreTab).ToString(), Resource.Drawable.ic_more_icon, Strings.More));
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
TabHost = FindViewById<TabHost>(global::Android.Resource.Id.TabHost);
TabHost.TabWidget.SetDividerDrawable(null);
TabHost.Setup();
}
I added only relevant code.
The tabs are created using TabHost, and the Activity is inherited from MvxTabsFragmentActivity.
You can inherit the ActionBar.ITabListener interface in your TabHost's MvxTabsFragmentActivity and then you can get the events that you need.
public class MainActivity : MvxTabsFragmentActivity, ActionBar.ITabListener
{
public void OnTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
// Optionally refresh/update the displayed tab.
Log.Debug(Tag, "The tab {0} was re-selected.", tab.Text);
}
public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
// Display the fragment the user should see
Log.Debug(Tag, "The tab {0} has been selected.", tab.Text);
}
public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
{
// Save any state in the displayed fragment.
Log.Debug(Tag, "The tab {0} as been unselected.", tab.Text);
}
...

Popup AlertDialog with a seekBar to change app volume

I am new to Android and I need to create an AlertDialog when I press an specific button. AlertDialog has a Seekbar and the seekBar is used to change the volume for my application. But I cannot make it work. Can you please help me? Thank you.
I get this error :
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setMax(int)' on a null object reference
and my code looks like this:
public class Setting extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public AlertDialog AlarmVolume(View view) {
/*
This part of code creates a dialog which has a seekbar for volume.
*/
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.volume_dialog, (ViewGroup) findViewById(R.id.settingsItem));
builder.setView(v).setTitle("Adjust Alarm Volume").setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//TODO save new volume amount
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
SeekBar seekbarVolume = (SeekBar)findViewById(R.id.volumeSeekBar);
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
seekbarVolume.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM));
seekbarVolume.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_ALARM));
seekbarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, progress, 0);
}
});
return builder.create();
}
}
and this is my volum_dialog.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/volumeSeekBar"
android:layout_marginTop="15sp"
android:layout_weight="1" />
</LinearLayout>
You are trying to find your seekbar in your contextView (R.layout.activity_setting):
SeekBar seekbarVolume = (SeekBar)findViewById(R.id.volumeSeekBar);
It returns null because you created your SeekBar dynamically for your AlertDialog:
View v = inflater.inflate(R.layout.volume_dialog, (ViewGroup) findViewById(R.id.settingsItem));
To solve this issue, change this line (SeekBar)findViewById(R.id.volumeSeekBar); to:
(SeekBar) v.findViewById(R.id.volumeSeekBar);
It tries to find your Seekbar in View v instead of ContextView which should work fine.

Sending sms app inside a Fragment

I'm creating an android using fragments .I have created a an sms fragment for sending sms to a specific number but when it runs well but when i type the message then press send , the app crashes and closes. help me out.
public class SMS extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View v = inflater.inflate(R.layout.fragment, null);
View v = inflater.inflate(R.layout.sms, container, false);
return v;
}
public void sendsms(View v)
{
//refernce the edittext
EditText message=(EditText) v.findViewById(R.id.Messege);
//get the phone the number and the message
String number="0712345678";
String msg=message.getText().toString();
//use the sms manager to send message
SmsManager sm=SmsManager.getDefault();
sm.sendTextMessage(number, null, msg, null, null);
Toast.makeText(getActivity(),"Messege sent",Toast.LENGTH_LONG).show();
}
}
the following is the xml layout that I have used.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#drawable/log"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send your feedback" />
<EditText
android:id="#+id/Messege"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="Enter your messege"
android:layout_marginTop="15dp"
android:ems="10" >
</EditText>
<Button
android:id="#+id/send"
android:onClick="sendsms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>
The onClick() method is run in the UI thread. You don't want to send an SMS message from the UI thread. Try wrapping your sendSMS code in an AsyncTask or create a new Thread to run that code.

Resources